📄 memory.c
字号:
} } return r;}BitOperator *new_BitOperator( char *name ){ BitOperator *tmp = ( BitOperator * ) calloc( 1, sizeof( BitOperator ) ); CHECK_PTR( tmp );#ifdef MEMORY_INFO gmemo_memory += sizeof( BitOperator );#endif if ( name ) { tmp->name = copy_string( name ); } else { tmp->name = NULL; } tmp->p_preconds = new_FactInfo(); tmp->n_preconds = new_FactInfo(); tmp->unconditional = NULL; tmp->conditionals = NULL; tmp->next = NULL; return tmp;}Effect *new_Effect( void ){ Effect *tmp = ( Effect * ) calloc( 1, sizeof( Effect ) ); CHECK_PTR( tmp );#ifdef MEMORY_INFO gmemory += sizeof( Effect );#endif tmp->p_conds = new_FactInfo(); tmp->n_conds = new_FactInfo(); tmp->p_effects = new_FactInfo(); tmp->n_effects = new_FactInfo(); tmp->next = NULL; return tmp;}FactInfo *new_FactInfo( void ){ FactInfo *tmp = ( FactInfo * ) calloc( 1, sizeof( FactInfo ) ); CHECK_PTR( tmp );#ifdef MEMORY_INFO gmemory += sizeof( FactInfo );#endif tmp->vector = new_bit_vector( gft_vector_length ); tmp->indices = NULL; return tmp;} /********************************************************************** * Creates a new bit vector with all bits unset. * * int length: The length of the vector in integer. * * RETURNS a new bit vector with all bits set to 0. *********************************************************************/BitVector *new_bit_vector(int length){ BitVector * result = (BitVector *) calloc(length, sizeof(int)); CHECK_PTR(result);#ifdef MEMORY_INFO gmemory += length;#endif memset(result, 0, length); return result;}BitVector *new_excl_bit_vector(int length){ BitVector * result = (BitVector *) calloc(length, sizeof(int)); CHECK_PTR(result);#ifdef MEMORY_INFO gexcl_memory += length;#endif memset(result, 0, length); return result;}/********************************************************************** * Creates a new instantiated effect, and creates all necessary * bit vectors with the global bit vector length "gft_vector_length". * * NOTE: gft_vector_length is global! * The conditions are NOT allocated! * * RETURNS a new instantiated effect. *********************************************************************/Effect * new_effect(){ Effect * result = (Effect *) malloc(sizeof(Effect)); CHECK_PTR(result);#ifdef MEMORY_INFO gmemory += sizeof(Effect);#endif result->p_conds = NULL; result->n_conds = NULL; result->p_effects = new_fact_info(); result->n_effects = new_fact_info(); result->next = NULL; return result;}/********************************************************************* * Creates a new structure that holds one integer !! * * NOTE: The index is initialized to -1. * * int i: The value for index. * * RETURNS a pointer to the new structure. *********************************************************************/Integers * new_integers(int i){ Integers * result = (Integers *) malloc(sizeof(Integers)); CHECK_PTR(result);#ifdef MEMORY_INFO gmemory += sizeof(Integers);#endif result->index = i; result->next = NULL; return result;}/********************************************************************* * Creates a new fact info structure with a bit vector with the * correct length. A fact info is a tuple of a bit vector and a list * of integers. The integers are the indices of the set bits in the * vector. * * NOTE: indices is initialized to NULL; * * RETURNS a pointer to the new structure. *********************************************************************/FactInfo * new_fact_info(){ FactInfo * result = (FactInfo *) malloc(sizeof(FactInfo)); CHECK_PTR(result);#ifdef MEMORY_INFO gmemory += sizeof(FactInfo);#endif result->vector = new_bit_vector(gft_vector_length); result->indices = NULL; return result;}/********************************************************************* * Creates a new pair of two fact info structures. * * FactInfo * pos: This represents the positive part. * FactInfo * neg: This represents the negaitve part. * * RETURNS a pointer to the new structure. *********************************************************************/FactInfoPair * new_fact_info_pair(FactInfo * pos, FactInfo * neg){ FactInfoPair * result = (FactInfoPair *) malloc(sizeof(FactInfoPair)); CHECK_PTR(result);#ifdef MEMORY_INFO gmemory += sizeof(FactInfoPair);#endif result->positive = pos; result->negative = neg; return result;}/********************************************************************** ********************** DELETION FUNCTIONS **************************** *********************************************************************//********************************************************************* * Free a complete TokenList, but do not delete predicates and * constants. It is therefore important that all variables start * with a unique identifier, we use '?'. * * TokenList * tl: The beginning of the TokenList to free. *********************************************************************/voidfree_token_list(TokenList * tl){ if (NULL != tl) { free_token_list(tl->next); /* Now free the token, but only if it is a variable. */ if (NULL != tl->item && '?' == *(tl->item)) { free(tl->item); } free(tl); }}/********************************************************************** * Free a complete FactList, but do not free any predicates or * constants, i.e. only delete strings beginning with a '?'. * * FactList * list: the list that should be deleted. *********************************************************************/voidfree_fact_list(FactList * list){ if (NULL != list) { free_fact_list(list->next); /* This function takes care of predicates and constants. */ free_token_list(list->item); free(list); }}/********************************************************************* * Free a complete tree, but leave predicate names in memory. * * PlNode * node: The root of the tree to free. *********************************************************************/void free_tree(PlNode * node){ if (NULL != node) { free_tree(node->sons); free_tree(node->next); /* now free the node itself */ free_token_list(node->atom); free(node); }}void free_CodeNode( CodeNode *node ){ if ( node ) { free_CodeNode( node->sons ); free_CodeNode( node->next );#ifdef MEMORY_INFO gmemory -= sizeof( CodeNode );#endif free( node ); }}void free_CodeOperator( CodeOperator *arme_sau ){ if ( arme_sau ) { if ( arme_sau->name ) { free( arme_sau->name ); } free_CodeNode( arme_sau->preconds ); free_CodeNode( arme_sau->conditionals ); /* resources are still missing. (I'll be missing you...) */#ifdef MEMORY_INFO gmemory -= sizeof( CodeOperator );#endif free( arme_sau ); }}/********************************************************************* * Free a pl_node including its atom tokenlist, but leave predicate * names in memory. * * PlNode * node: The node to free. *********************************************************************/void free_pl_node(PlNode * node){ if (NULL != node) { free_token_list(node->atom); free(node); }}/********************************************************************** * *********************************************************************/void free_complete_token_list(TokenList * source ){ if ( source ) { free_complete_token_list( source->next ); if ( source->item ) { free( source->item ); source->item = NULL; } free( source ); }}/********************************************************************** * *********************************************************************/void free_complete_fact_list(FactList * source ){ if ( source ) { free_complete_fact_list( source->next ); free_complete_token_list( source->item ); free( source ); }}/********************************************************************** * Delete a list of operators and take care of predicates and * constants. * * PlOperator * op: The start of the list that is deleted. **********************************************************************/void free_ops(PlOperator * op){ if (NULL != op) { free_ops(op->next); free_complete_token_list(op->name); free_complete_fact_list(op->params); free_tree(op->preconds); free_tree(op->effects); free(op); /* Resources are still missing. */ }}/********************************************************************** * Delete a PlOperator and take care of predicates and * constants. * * PlOperator * op: The operator that is deleted. **********************************************************************/void free_pl_op(PlOperator * op){ if (NULL != op) { free_token_list(op->name); free_fact_list(op->params); free_tree(op->preconds); free_tree(op->effects); free(op); /* Resources are still missing. */ }}/********************************************************************** * *********************************************************************/void free_integers( Integers *l ){ if ( l != NULL ) { free_integers( l->next ); free( l ); }}void free_partial_effect( Effect *ef ){ if ( ef != NULL ) { free_fact_info( ef->p_conds ); free_fact_info( ef->n_conds ); free_integers( ef->p_effects->indices ); free( ef->p_effects ); free_integers( ef->n_effects->indices ); free( ef->n_effects ); free( ef ); }}void free_partial_operator( BitOperator *op ){ if ( op != NULL ) { if ( op->name ) { free( op->name ); } /* preconds: we need the vectors in search; only free indices and * structure holding parts together */ free_integers( op->p_preconds->indices ); free( op->p_preconds ); free_integers( op->n_preconds->indices ); free( op->n_preconds ); if ( op->unconditional ) { /* unconditional is thorwn away, except for the strings * describing the pos and neg effects; * keep those for exclusions (interfere) check */ free_integers( op->unconditional->p_effects->indices ); free( op->unconditional->p_effects ); free_integers( op->unconditional->n_effects->indices ); free( op->unconditional->n_effects ); free_fact_info( op->unconditional->p_conds ); free_fact_info( op->unconditional->n_conds ); free( op->unconditional ); } /* do not touch the conditionals at all, those can be activated in * later time steps, so we have to keep them still * (get pointed to by op_node->unactivated_effects) */ free( op ); }} void free_fact_info_pair( FactInfoPair *p ){ if ( p ) { free_fact_info( p->positive ); free_fact_info( p->negative ); free( p ); }} /********************************************************************** * *********************************************************************/void free_bit_vector(BitVector * bvec){ if ( bvec ) { free(bvec); }}/********************************************************************** * *********************************************************************/void free_effect(Effect * effect){ if (NULL != effect) { free_effect(effect->next); free_fact_info(effect->p_conds); free_fact_info(effect->n_conds); free_fact_info(effect->p_effects); free_fact_info(effect->n_effects); free(effect); }}/********************************************************************** * *********************************************************************/void free_fact_info(FactInfo * info){ if ( info != NULL ) { free_bit_vector(info->vector); free_integers(info->indices); free( info ); }}void free_BitOperator( BitOperator *op ){ if ( op != NULL ) { if ( op->name ) { free( op->name ); } free_fact_info( op->p_preconds ); free_fact_info( op->n_preconds ); free_effect( op->unconditional ); free_effect( op->conditionals ); free( op ); }}void free_complete_FactList( FactList *source ){ if ( source ) { free_complete_FactList( source->next ); free_complete_TokenList( source->item ); free( source ); }}void free_complete_TokenList( TokenList *source ){ if ( source ) { free_complete_TokenList( source->next ); if ( source->item ) { free( source->item ); } free( source ); }}void free_TypedList( TypedList *t ){ if ( t ) { if ( t->name ) { free( t->name ); t->name = NULL; } if ( t->type ) { free_token_list( t->type ); t->type = NULL; } free_TypedList( t->next ); free( t ); }}void free_TypedListList( TypedListList *t ){ if ( t ) { if ( t->predicate ) { free( t->predicate ); t->predicate = NULL; } if ( t->args ) { free_TypedList( t->args ); t->args = NULL; } free_TypedListList( t->next ); free( t ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -