Update of /cvsroot/xsb/XSB/emu In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1969 Modified Files: biassert.c bineg_xsb_i.h builtin.c cinterf.c dynamic_stack.c error_xsb.c error_xsb.h gc_mark.h hash_xsb.c hashtable_xsb.c heap_xsb.c init_xsb.c interprolog_callback.c io_builtins_xsb.c io_builtins_xsb_i.h loader_xsb.c loader_xsb.h main_xsb.c memory_xsb.c memory_xsb.h random_xsb.c slgdelay.c slgdelay.h socket_xsb.c std_pred_xsb_i.h sub_delete.c thread_xsb.c tr_utils.c trie_internals.h tries.c tst_insert.c Log Message: Changed many mallocs,callocs,reallocs,frees to mem_allocs, mem_callocs, mem_reallocs, and mem_deallocs, to keep better track of XSB memory usage. The goal is eventually to add a new argument to these to indicate a "category" of memory usage. But that's not done here. And this is only a little more than half-way done of changing the system calls to our mem_* versions. Most of these changes are very local, but a couple require extending interfaces to keep track of block sizes (since mem_dealloc, and mem_realloc require a length parameter that the corresponding free and realloc don't.) Index: biassert.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/biassert.c,v retrieving revision 1.98 retrieving revision 1.99 diff -u -r1.98 -r1.99 --- biassert.c 20 Oct 2005 13:49:07 -0000 1.98 +++ biassert.c 12 Nov 2005 15:48:49 -0000 1.99 @@ -625,8 +625,8 @@ { /* xsb_dbgmsg((LOG_DEBUG,"Enter buff_realloc(%d) %X", asrtBuff->Buff_size,asrtBuff->Buff)); */ asrtBuff->Buff_size = asrtBuff->Buff_size + asrtBuff->Buff_size; - if (asrtBuff->Buff == NULL) asrtBuff->Buff = (char *)malloc(asrtBuff->Buff_size); - else asrtBuff->Buff = (char *)realloc(asrtBuff->Buff,asrtBuff->Buff_size); + if (asrtBuff->Buff == NULL) asrtBuff->Buff = (char *)mem_alloc(asrtBuff->Buff_size); + else asrtBuff->Buff = (char *)mem_realloc(asrtBuff->Buff,(asrtBuff->Buff_size / 2),asrtBuff->Buff_size); asrtBuff->BLim = asrtBuff->Buff_size-16; /* xsb_dbgmsg((LOG_DEBUG,"Leave buff_realloc(%d) %X", asrtBuff->Buff_size,asrtBuff->Buff)); */ return(asrtBuff->Buff); @@ -2468,8 +2468,8 @@ struct DispBlk_t *dispblk; if (*(pb)get_ep(psc) != switchonthread) { /* create new switchonthread instruction and dispblock */ - pb disp_instr_addr = calloc(sizeof(Cell),2); - dispblk = (struct DispBlk_t *)calloc(sizeof(struct DispBlk_t)+MAXTHREAD*sizeof(Cell),1); + pb disp_instr_addr = mem_calloc(sizeof(Cell),2); + dispblk = (struct DispBlk_t *)mem_calloc(sizeof(struct DispBlk_t)+MAXTHREAD*sizeof(Cell),1); if (DispBlkHdr.firstDB) DispBlkHdr.firstDB->PrevDB = dispblk; dispblk->NextDB = DispBlkHdr.firstDB; Index: bineg_xsb_i.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/bineg_xsb_i.h,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- bineg_xsb_i.h 30 Sep 2005 20:04:26 -0000 1.24 +++ bineg_xsb_i.h 12 Nov 2005 15:48:49 -0000 1.25 @@ -239,7 +239,7 @@ * answer for the head predicate. */ int i; - copy_of_var_addr = (CPtr *)calloc(var_addr_arraysz, sizeof(CPtr)); + copy_of_var_addr = (CPtr *)mem_calloc(var_addr_arraysz, sizeof(CPtr)); if(copy_of_var_addr == NULL){ xsb_exit("No enough memory to calloc copy_of_var_addr!\nBye"); } @@ -275,7 +275,7 @@ } } bind_nil(dls_tail); - free(copy_of_var_addr); + mem_dealloc(copy_of_var_addr,var_addr_arraysz*sizeof(CPtr)); } else { bind_nil((CPtr)delay_lists); } Index: builtin.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/builtin.c,v retrieving revision 1.230 retrieving revision 1.231 diff -u -r1.230 -r1.231 --- builtin.c 10 Nov 2005 23:05:54 -0000 1.230 +++ builtin.c 12 Nov 2005 15:48:49 -0000 1.231 @@ -2741,11 +2741,13 @@ if (psc_profile_count_table == NULL) { psc_profile_count_max = initial_psc_profile_count_size; psc_profile_count_table = (psc_profile_count *) - malloc(psc_profile_count_max*sizeof(psc_profile_count)); + mem_alloc(psc_profile_count_max*sizeof(psc_profile_count)); } else { psc_profile_count_max = 2*psc_profile_count_max; psc_profile_count_table = (psc_profile_count *) - realloc(psc_profile_count_table,psc_profile_count_max*sizeof(psc_profile_count)); + mem_realloc(psc_profile_count_table, + (psc_profile_count_max/2)*sizeof(psc_profile_count), + psc_profile_count_max*sizeof(psc_profile_count)); } } for (i=0; i<psc_profile_count_num; i++) Index: cinterf.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/cinterf.c,v retrieving revision 1.60 retrieving revision 1.61 diff -u -r1.60 -r1.61 --- cinterf.c 10 Nov 2005 23:05:55 -0000 1.60 +++ cinterf.c 12 Nov 2005 15:48:50 -0000 1.61 @@ -896,7 +896,7 @@ if (*(ptr++) != '(') cppc_error(15); fields = count_fields(ptr, ')'); size = count_csize(ptr, ')'); - cdptr2 = (char *)malloc(size); + cdptr2 = (char *)mem_alloc(size); /* leak */ *((char **)c_dataptr) = cdptr2; argno = 0; for (i = 1; i <= fields; i++) { @@ -918,7 +918,7 @@ if (*(ptr++)!='(') cppc_error(16); fields = count_fields(ptr, ')'); size = count_csize(ptr, ')'); - cdptr2 = (char *)malloc(size); + cdptr2 = (char *)mem_alloc(size); /* leak */ *((char **)c_dataptr) = cdptr2; argno = 0; for (i = 1; i <= fields; i++) { @@ -1244,7 +1244,7 @@ exit(1); } strncpy(cmdline, cmdline_param, 2*MAXPATHLEN - 1); - argv = (char **) malloc(20*sizeof(char *)); + argv = (char **) mem_alloc(20*sizeof(char *)); /* count space even if never released */ while (cmdline[i] == ' ') i++; while (cmdline[i] != '\0') { Index: dynamic_stack.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/dynamic_stack.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- dynamic_stack.c 14 Jan 2005 18:30:55 -0000 1.5 +++ dynamic_stack.c 12 Nov 2005 15:48:50 -0000 1.6 @@ -37,6 +37,7 @@ #include "error_xsb.h" #include "debug_xsb.h" #include "flags_xsb.h" +#include "memory_xsb.h" /*-------------------------------------------------------------------------*/ @@ -72,7 +73,7 @@ xsb_dbgmsg((LOG_TRIE_STACK, "Initializing %s", name)); total_bytes = stack_size * frame_size; - DynStk_Base(*ds) = malloc(total_bytes); + DynStk_Base(*ds) = mem_alloc(total_bytes); if ( IsNULL(DynStk_Base(*ds)) ) xsb_abort("Ran out of memory in allocation of %s", DynStk_Name(*ds)); DynStk_Top(*ds) = DynStk_Base(*ds); @@ -108,7 +109,7 @@ dbg_dsPrint(LOG_TRIE_STACK, *ds, "Before expansion"); total_bytes = new_size * DynStk_FrameSize(*ds); - new_base = realloc(DynStk_Base(*ds),total_bytes); + new_base = mem_realloc(DynStk_Base(*ds),DynStk_CurSize(*ds)*DynStk_FrameSize(*ds),total_bytes); if ( IsNULL(new_base) ) xsb_abort("Ran out of memory during expansion of %s", DynStk_Name(*ds)); DynStk_Top(*ds) = @@ -135,7 +136,7 @@ if ( DynStk_CurSize(*ds) <= DynStk_InitSize(*ds) ) return; total_bytes = DynStk_InitSize(*ds) * DynStk_FrameSize(*ds); - new_base = realloc(DynStk_Base(*ds),total_bytes); + new_base = mem_realloc(DynStk_Base(*ds),DynStk_CurSize(*ds)*DynStk_FrameSize(*ds),total_bytes); xsb_dbgmsg((LOG_TRIE_STACK, "Shrinking %s: %d -> %d", DynStk_Name(*ds), DynStk_CurSize(*ds), DynStk_InitSize(*ds))); Index: error_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/error_xsb.c,v retrieving revision 1.41 retrieving revision 1.42 diff -u -r1.41 -r1.42 --- error_xsb.c 11 Nov 2005 21:24:30 -0000 1.41 +++ error_xsb.c 12 Nov 2005 15:48:50 -0000 1.42 @@ -81,7 +81,7 @@ #endif /* TLS: now frees Ball, which was assumed to be malloced */ -DllExport void call_conv xsb_throw(CTXTdeclc prolog_term Ball) +DllExport void call_conv xsb_throw(CTXTdeclc prolog_term Ball, unsigned long Ball_len) { Psc exceptballpsc; PrRef Prref; @@ -90,8 +90,9 @@ Cell *tptr; prolog_term term_to_assert; Cell *space_for_ball_assert; + unsigned long space_for_ball_assert_len = 3*sizeof(Cell); - space_for_ball_assert = (Cell *) malloc(3*sizeof(Cell)); + space_for_ball_assert = (Cell *) mem_alloc(space_for_ball_assert_len); if (!space_for_ball_assert) xsb_exit("out of memory in xsb_throw!"); exceptballpsc = pair_psc((Pair)insert("$$exception_ball", (byte)2, @@ -107,8 +108,8 @@ /* need arity of 3, for extra cut_to arg */ Prref = get_prref(CTXTc exceptballpsc); assert_buff_to_clref_p(CTXTc term_to_assert,3,Prref,0,makeint(0),0,&clause); - free(cs_val(Ball)); - free(space_for_ball_assert); + mem_dealloc(cs_val(Ball),Ball_len); + mem_dealloc(space_for_ball_assert,space_for_ball_assert_len); /* reset WAM emulator state to Prolog catcher */ if (unwind_stack(CTXT)) xsb_exit("Unwind_stack failed in xsb_throw!"); /* Resume main emulator instruction loop */ @@ -123,10 +124,11 @@ prolog_term ball_to_throw; int isnew; Cell *tptr; char message[255]; + unsigned long ball_len = 10*sizeof(Cell); sprintf(message,"in arg %d of predicate %s/%d)",arg,predicate,arity); - tptr = (Cell *) malloc(10*sizeof(Cell)); + tptr = (Cell *) mem_alloc(ball_len); ball_to_throw = makecs(tptr); bld_functor(tptr, pair_psc(insert("error",3, @@ -146,7 +148,7 @@ if (culprit == (Cell)NULL) bld_int(tptr,0); else bld_ref(tptr,culprit); - xsb_throw(CTXTc ball_to_throw); + xsb_throw(CTXTc ball_to_throw, ball_len); } @@ -157,10 +159,11 @@ prolog_term ball_to_throw; int isnew; Cell *tptr; char message[255]; + unsigned long ball_len = 10*sizeof(Cell); sprintf(message,"in arg %d of predicate %s/%d)",arg,predicate,arity); - tptr = (Cell *) malloc(10*sizeof(Cell)); + tptr = (Cell *) mem_alloc(ball_len); ball_to_throw = makecs(tptr); bld_functor(tptr, pair_psc(insert("error",3, @@ -180,7 +183,7 @@ if (culprit == (Cell)NULL) bld_int(tptr,0); else bld_ref(tptr,culprit); - xsb_throw(CTXTc ball_to_throw); + xsb_throw(CTXTc ball_to_throw,ball_len); } @@ -192,10 +195,11 @@ prolog_term ball_to_throw; int isnew; Cell *tptr; char message[255]; + unsigned long ball_len = 10*sizeof(Cell); sprintf(message,"(return %d) in predicate %s/%d)",rtrn,predicate,arity); - tptr = (Cell *) malloc(10*sizeof(Cell)); + tptr = (Cell *) mem_alloc(ball_len); ball_to_throw = makecs(tptr); bld_functor(tptr, pair_psc(insert("error",3, @@ -214,7 +218,7 @@ tptr++; bld_string(tptr,string_find(object,1)); - xsb_throw(CTXTc ball_to_throw); + xsb_throw(CTXTc ball_to_throw,ball_len); } @@ -225,6 +229,7 @@ prolog_term ball_to_throw; int isnew; Cell *tptr; char message[255]; + unsigned long ball_len = 10*sizeof(Cell); if (! IsNULL(state)) { sprintf(message," in arg %d of predicate %s/%d must be %s",arg,predicate,arity, @@ -233,7 +238,7 @@ sprintf(message," in arg %d of predicate %s/%d",arg,predicate,arity); } - tptr = (Cell *) malloc(10*sizeof(Cell)); + tptr = (Cell *) mem_alloc(ball_len); ball_to_throw = makecs(tptr); bld_functor(tptr, pair_psc(insert("error",3, @@ -245,7 +250,7 @@ tptr++; bld_copy(tptr,build_xsb_backtrace(CTXT)); - xsb_throw(CTXTc ball_to_throw); + xsb_throw(CTXTc ball_to_throw,ball_len); } @@ -256,6 +261,7 @@ prolog_term ball_to_throw; int isnew; Cell *tptr; + unsigned long ball_len = 10*sizeof(Cell); #ifdef MULTI_THREAD char mtmessage[MAXBUFSIZE]; int tid = xsb_thread_self(); @@ -263,7 +269,7 @@ th = find_context(xsb_thread_self()); #endif - tptr = (Cell *) malloc(10*sizeof(Cell)); + tptr = (Cell *) mem_alloc(ball_len); ball_to_throw = makecs(tptr); bld_functor(tptr, pair_psc(insert("error",3, (Psc)flags[CURRENT_MODULE],&isnew))); @@ -279,7 +285,7 @@ #endif tptr++; bld_copy(tptr,build_xsb_backtrace(CTXT)); - xsb_throw(CTXTc ball_to_throw); + xsb_throw(CTXTc ball_to_throw,ball_len); } DllExport void call_conv xsb_abort(char *description, ...) Index: error_xsb.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/error_xsb.h,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- error_xsb.h 14 Oct 2005 13:24:37 -0000 1.18 +++ error_xsb.h 12 Nov 2005 15:48:50 -0000 1.19 @@ -128,6 +128,6 @@ int assert_code_to_buff_p(CTXTdeclc prolog_term); -DllExport void call_conv xsb_throw(CTXTdeclc prolog_term); +DllExport void call_conv xsb_throw(CTXTdeclc prolog_term, unsigned long); extern prolog_term build_xsb_backtrace(CTXTdecl); Index: gc_mark.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/gc_mark.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- gc_mark.h 19 Aug 2005 16:24:10 -0000 1.9 +++ gc_mark.h 12 Nov 2005 15:48:50 -0000 1.10 @@ -702,7 +702,7 @@ #ifdef INDIRECTION_SLIDE /* space for keeping pointers to live data */ slide_buf_size = (unsigned long) ((hreg+1-(CPtr)glstack.low)*0.2); - slide_buf = (CPtr *) calloc(slide_buf_size+1, sizeof(CPtr)); + slide_buf = (CPtr *) mem_calloc(slide_buf_size+1, sizeof(CPtr)); if (!slide_buf) xsb_exit("Not enough space to allocate slide_buf"); slide_top=0; @@ -719,13 +719,14 @@ #endif #ifdef SLG_GC - cp_marks = (char *)calloc(cp_bot - cp_top + 1,1); - tr_marks = (char *)calloc(tr_top - tr_bot + 1,1); + cp_marks = (char *)mem_calloc(cp_bot - cp_top + 1,1); + tr_marks = (char *)mem_calloc(tr_top - tr_bot + 1,1); if ((! cp_marks) || (! tr_marks)) xsb_exit("Not enough core to perform garbage collection chaining phase"); #endif - heap_marks = (char * )calloc(heap_top - heap_bot + 2 + avail_dreg_marks,1); - ls_marks = (char * )calloc(ls_bot - ls_top + 1,1); + heap_marks_size = heap_top - heap_bot + 2 + avail_dreg_marks; + heap_marks = (char * )mem_calloc(heap_marks_size,1); + ls_marks = (char * )mem_calloc(ls_bot - ls_top + 1,1); if ((! heap_marks) || (! ls_marks)) xsb_exit("Not enough core to perform garbage collection marking phase"); Index: hash_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/hash_xsb.c,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- hash_xsb.c 10 Dec 2004 22:03:49 -0000 1.8 +++ hash_xsb.c 12 Nov 2005 15:48:50 -0000 1.9 @@ -34,6 +34,7 @@ #include "hash_xsb.h" #include "psc_xsb.h" #include "flags_xsb.h" +#include "memory_xsb.h" /* @@ -144,7 +145,7 @@ unsigned long index, new_size, new_index; new_size = next_prime(symbol_table.size); - new_table = (Pair *)calloc(new_size, sizeof(void *)); + new_table = (Pair *)mem_calloc(new_size, sizeof(void *)); for (bucket_ptr = (Pair *)symbol_table.table, index = 0; index < symbol_table.size; bucket_ptr++, index++) @@ -157,7 +158,7 @@ new_table[new_index] = cur_pair; } - free((void *)symbol_table.table); + mem_dealloc((void *)symbol_table.table,symbol_table.size); symbol_table.size = new_size; symbol_table.table = (void **)new_table; } @@ -179,7 +180,7 @@ unsigned long index, new_size, new_index; new_size = next_prime(string_table.size); - new_table = (void **)calloc(new_size, sizeof(void *)); + new_table = (void **)mem_calloc(new_size, sizeof(void *)); for (bucket_ptr = string_table.table, index = 0; index < string_table.size; @@ -194,7 +195,7 @@ new_table[new_index] = (void *)cur_entry; } - free((void *)string_table.table); + mem_dealloc((void *)string_table.table,string_table.size); string_table.size = new_size; string_table.table = new_table; } Index: hashtable_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/hashtable_xsb.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- hashtable_xsb.c 16 Sep 2005 00:56:41 -0000 1.7 +++ hashtable_xsb.c 12 Nov 2005 15:48:50 -0000 1.8 @@ -41,6 +41,7 @@ #include "tr_utils.h" #include "debug_xsb.h" #include "flags_xsb.h" +#include "memory_xsb.h" /* A simple hashtable. @@ -84,7 +85,8 @@ if (bucket) { /* use memcpy() because client bucket might have extra fields */ memcpy(prev, bucket, table->bucket_size); - free(bucket); + mem_dealloc(bucket,table->bucket_size); + //printf("dealloc bucket size: %d\n",table->bucket_size); } else { mark_bucket_free(prev,table->bucket_size); xsb_dbgmsg((LOG_HASHTABLE, @@ -97,7 +99,8 @@ } else { /* Not top bucket: rearrange pointers & free space */ prev->next = bucket->next; - free(bucket); + mem_dealloc(bucket,table->bucket_size); + //printf("dealloc bucket size: %d\n",table->bucket_size); } return NULL; } else @@ -111,7 +114,8 @@ /* else create new bucket */ /* calloc nullifies the allocated space; CLIENTS RELY ON THIS */ if (!bucket) { /* i.e., it is not a top bucket */ - bucket = (xsbBucket *)calloc(1,table->bucket_size); + bucket = (xsbBucket *)mem_calloc(1,table->bucket_size); + //printf("calloc bucket, size: %d\n",table->bucket_size); if (!bucket) xsb_exit("Out of Memory: Can't allocate hash bucket"); prev->next = bucket; @@ -125,7 +129,8 @@ static void init_hashtable(xsbHashTable *table) { /* calloc zeroes the allocated space; clients rely on this */ - table->table = (byte *)calloc(table->length,table->bucket_size); + table->table = (byte *)mem_calloc(table->length,table->bucket_size); + //printf("calloc table, size: %d\n",table->length*table->bucket_size); if (!table->table) xsb_exit("Out of Memory: Can't create hash table"); table->initted = TRUE; @@ -142,11 +147,14 @@ bucket=get_top_bucket(table,i)->next; while (bucket != NULL) { next = bucket->next; - free(bucket); + mem_dealloc(bucket,table->bucket_size); + //printf("dealloc bucket size: %d\n",table->bucket_size); bucket = next; } } - free(table->table); + mem_dealloc(table->table,table->length*table->bucket_size); + //printf("dealloc table size: %d\n",table->length*table->bucket_size); + } Index: heap_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/heap_xsb.c,v retrieving revision 1.45 retrieving revision 1.46 diff -u -r1.45 -r1.46 --- heap_xsb.c 19 Aug 2005 16:24:10 -0000 1.45 +++ heap_xsb.c 12 Nov 2005 15:48:50 -0000 1.46 @@ -211,6 +211,8 @@ tr_bot,tr_top, cp_bot,cp_top, compl_top,compl_bot; +static unsigned long heap_marks_size; + #define stack_boundaries \ heap_top = hreg; \ @@ -554,11 +556,11 @@ #endif /* get rid of the marking areas - if they exist */ - if (heap_marks) { free((heap_marks-1)); heap_marks = NULL; } - if (tr_marks) { free(tr_marks); tr_marks = NULL; } - if (ls_marks) { free(ls_marks); ls_marks = NULL; } - if (cp_marks) { free(cp_marks); cp_marks = NULL; } - if (slide_buf) { free(slide_buf); slide_buf = NULL; } + if (heap_marks) { mem_dealloc((heap_marks-1),heap_marks_size); heap_marks = NULL; } + if (tr_marks) { mem_dealloc(tr_marks,tr_top-tr_bot+1); tr_marks = NULL; } + if (ls_marks) { mem_dealloc(ls_marks,ls_bot - ls_top + 1); ls_marks = NULL; } + if (cp_marks) { mem_dealloc(cp_marks,cp_bot - cp_top + 1); cp_marks = NULL; } + if (slide_buf) { mem_dealloc(slide_buf,(slide_buf_size+1)*sizeof(CPtr)); slide_buf = NULL; } goto end; } @@ -618,14 +620,14 @@ GC_PROFILE_COPY_START_TIME; - begin_new_heap = (CPtr)malloc(marked*sizeof(Cell)); + begin_new_heap = (CPtr)mem_alloc(marked*sizeof(Cell)); if (begin_new_heap == NULL) xsb_exit("copying garbage collection could not allocate new heap"); end_new_heap = begin_new_heap+marked; hreg = copy_heap(CTXTc marked,begin_new_heap,end_new_heap,arity); - free(begin_new_heap); + mem_dealloc(begin_new_heap,marked*sizeof(Cell)); adapt_hfreg_from_choicepoints(CTXTc hreg); hbreg = cp_hreg(breg); @@ -645,26 +647,26 @@ /* get rid of the marking areas - if they exist */ if (heap_marks) { check_zero(heap_marks,(heap_top - heap_bot),"heap") ; - free((heap_marks-1)) ; /* see its calloc */ + mem_dealloc((heap_marks-1),heap_marks_size) ; /* see its calloc */ heap_marks = NULL ; } if (tr_marks) { check_zero(tr_marks,(tr_top - tr_bot + 1),"tr") ; - free(tr_marks) ; + mem_dealloc(tr_marks,tr_top-tr_bot+1) ; tr_marks = NULL ; } if (ls_marks) { check_zero(ls_marks,(ls_bot - ls_top + 1),"ls") ; - free(ls_marks) ; + mem_dealloc(ls_marks,ls_bot - ls_top + 1) ; ls_marks = NULL ; } if (cp_marks) { check_zero(cp_marks,(cp_bot - cp_top + 1),"cp") ; - free(cp_marks) ; + mem_dealloc(cp_marks,cp_bot - cp_top + 1) ; cp_marks = NULL ; } if (slide_buf) { - free(slide_buf); + mem_dealloc(slide_buf,(slide_buf_size+1)*sizeof(CPtr)); slide_buf = NULL; } #ifdef SAFE_GC Index: init_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/init_xsb.c,v retrieving revision 1.67 retrieving revision 1.68 diff -u -r1.67 -r1.68 --- init_xsb.c 30 Sep 2005 20:04:26 -0000 1.67 +++ init_xsb.c 12 Nov 2005 15:48:50 -0000 1.68 @@ -755,17 +755,17 @@ findall_solutions = NULL; #define MAXSBUFFS 30 - LSBuff = (VarString **)calloc(sizeof(VarString *),MAXSBUFFS); + LSBuff = (VarString **)mem_calloc(sizeof(VarString *),MAXSBUFFS); /* vars for io_builtins_XXX */ opstk_size = 0; funstk_size = 0; funstk = NULL; opstk = NULL; - rc_vars = (struct vartype *)malloc(MAXVAR*sizeof(struct vartype)); + rc_vars = (struct vartype *)mem_alloc(MAXVAR*sizeof(struct vartype)); /* vars for token_xsb_XXX */ - token = (struct token_t *)malloc(sizeof(struct token_t)); + token = (struct token_t *)mem_alloc(sizeof(struct token_t)); strbuff = NULL; lastc = ' '; strbuff_len = InitStrLen; @@ -773,11 +773,11 @@ random_seeds = 0; /* used in trie_lookup */ - a_tstCCPStack = (struct tstCCPStack_t *)malloc(sizeof(struct tstCCPStack_t)); - a_variant_cont = (struct VariantContinuation *)malloc(sizeof(struct VariantContinuation)); - a_tstCPStack = (struct tstCPStack_t *)malloc(sizeof(struct tstCPStack_t)); + a_tstCCPStack = (struct tstCCPStack_t *)mem_alloc(sizeof(struct tstCCPStack_t)); + a_variant_cont = (struct VariantContinuation *)mem_alloc(sizeof(struct VariantContinuation)); + a_tstCPStack = (struct tstCPStack_t *)mem_alloc(sizeof(struct tstCPStack_t)); - asrtBuff = (struct asrtBuff_t *)malloc(sizeof(struct asrtBuff_t)); + asrtBuff = (struct asrtBuff_t *)mem_alloc(sizeof(struct asrtBuff_t)); asrtBuff->Buff = NULL; asrtBuff->Buff_size = 512; asrtBuff->Loc = NULL; @@ -785,7 +785,7 @@ asrtBuff->Size = 0; i_have_dyn_mutex = 0; - last_answer = (VarString *)malloc(sizeof(VarString)); + last_answer = (VarString *)mem_alloc(sizeof(VarString)); XSB_StrInit(last_answer); OldestCl = retracted_buffer; NewestCl = retracted_buffer; @@ -805,13 +805,13 @@ th->last_ans = 1; #endif - tsgLBuff1 = (VarString *)malloc(sizeof(VarString)); + tsgLBuff1 = (VarString *)mem_alloc(sizeof(VarString)); XSB_StrInit(tsgLBuff1); - tsgLBuff2 = (VarString *)malloc(sizeof(VarString)); + tsgLBuff2 = (VarString *)mem_alloc(sizeof(VarString)); XSB_StrInit(tsgLBuff2); - tsgSBuff1 = (VarString *)malloc(sizeof(VarString)); + tsgSBuff1 = (VarString *)mem_alloc(sizeof(VarString)); XSB_StrInit(tsgSBuff1); - tsgSBuff2 = (VarString *)malloc(sizeof(VarString)); + tsgSBuff2 = (VarString *)mem_alloc(sizeof(VarString)); XSB_StrInit(tsgSBuff2); /* Allocate Stack Spaces and set Boundary Parameters @@ -961,8 +961,8 @@ int i, new_indicator; inst_begin = 0; - symbol_table.table = (void **)calloc(symbol_table.size, sizeof(Pair)); - string_table.table = (void **)calloc(string_table.size, sizeof(char *)); + symbol_table.table = (void **)mem_calloc(symbol_table.size, sizeof(Pair)); + string_table.table = (void **)mem_calloc(string_table.size, sizeof(char *)); /* insert mod name global */ /*tp = insert_module(T_MODU, "global"); / loaded */ Index: interprolog_callback.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/interprolog_callback.c,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- interprolog_callback.c 20 Jul 2005 20:52:16 -0000 1.11 +++ interprolog_callback.c 12 Nov 2005 15:48:50 -0000 1.12 @@ -60,7 +60,7 @@ xsb_close_query(CTXT); return NULL; } - b = (jbyte *) malloc(size); + b = (jbyte *) mem_alloc(size); newHead = p2p_car(p2p_arg(reg_term(CTXTc 1), 2)); newTail = p2p_cdr(p2p_arg(reg_term(CTXTc 1), 2)); b[i-1] = p2c_int(newHead); @@ -73,7 +73,7 @@ xsb_close_query(CTXT); bytes = (*env)->NewByteArray(env, size); (*env)->SetByteArrayRegion(env, bytes, 0, size, b); - free(b); + mem_dealloc(b,size); return bytes; } @@ -169,7 +169,7 @@ char *XSBPath = (*env)->GetStringUTFChars(env, jXSBPath, 0); #ifdef MULTI_THREAD - th = malloc( sizeof( th_context ) ) ; + th = mem_alloc( sizeof( th_context ) ) ; #endif if (debug==JNI_TRUE) printf("Entering Java_com_xsb_interprolog_NativeEngine_xsb_1init_1internal\n"); @@ -193,20 +193,20 @@ char *XSBPath; myargc = (*env)->GetArrayLength(env, jXSBParameters) + 2; - myargv = (char **) malloc(myargc * sizeof(char *)); + myargv = (char **) mem_alloc(myargc * sizeof(char *)); XSBPath = (*env)->GetStringUTFChars(env, jXSBPath, 0); if (debug==JNI_TRUE) printf("Entering Java_com_xsb_interprolog_NativeEngine_xsb_1init_1internal\n"); myargv[0] = XSBPath; myargv[1] = "-n"; - parameters = (jstring *) malloc((myargc - 2) * sizeof(jstring *)); + parameters = (jstring *) mem_alloc((myargc - 2) * sizeof(jstring *)); for (i=0; i < myargc-2; i++) { parameters[i] = (jstring)(*env)->GetObjectArrayElement(env, jXSBParameters, i); myargv[i + 2] = (*env)->GetStringUTFChars(env, parameters[i], 0); } #ifdef MULTI_THREAD - th = malloc( sizeof( th_context ) ) ; + th = mem_alloc( sizeof( th_context ) ) ; #endif rcode=xsb_init(CTXTc myargc,myargv); @@ -217,8 +217,8 @@ (*env)->ReleaseStringUTFChars(env, parameters[i], myargv[i + 2]); // may be needed ?(*env)->DeleteLocalRef(env, parameters[i]); } - free(parameters); - free(myargv); + mem_dealloc(parameters,(myargc-2)*sizeof(jstring *)); + mem_dealloc(myargv,myargc*sizeof(char *)); if (debug==JNI_TRUE) printf("Exiting Java_com_xsb_interprolog_NativeEngine_xsb_1init_1internal\n"); return rcode; @@ -264,7 +264,7 @@ size = p2c_int(reg_term(CTXTc 1)); - b = (jbyte *) malloc(size); + b = (jbyte *) mem_alloc(size); newHead = p2p_car(reg_term(CTXTc 2)); newTail = p2p_cdr(reg_term(CTXTc 2)); b[i-1] = p2c_int(newHead); @@ -301,6 +301,7 @@ c2p_int(CTXTc b[i], newHead); i++; } + mem_dealloc(b,size); c2p_nil(CTXTc newTail); return 1; } Index: io_builtins_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/io_builtins_xsb.c,v retrieving revision 1.58 retrieving revision 1.59 diff -u -r1.58 -r1.59 --- io_builtins_xsb.c 16 Aug 2005 19:19:09 -0000 1.58 +++ io_builtins_xsb.c 12 Nov 2005 15:48:50 -0000 1.59 @@ -202,7 +202,7 @@ int i, Arity=0; long int_arg; /* holder for int args */ float float_arg; /* holder for float args */ - struct fmt_spec *current_fmt_spec = (struct fmt_spec *)malloc(sizeof(struct fmt_spec)); + struct fmt_spec *current_fmt_spec = (struct fmt_spec *)mem_alloc(sizeof(struct fmt_spec)); int width=0, precision=0; /* these are used in conjunction with the *.* format */ XSB_StrSet(&FmtBuf,""); @@ -313,7 +313,7 @@ xsb_segfault_message = xsb_default_segfault_msg; signal(SIGSEGV, xsb_default_segfault_handler); - free(current_fmt_spec); + mem_dealloc(current_fmt_spec,sizeof(struct fmt_spec)); return TRUE; } @@ -352,7 +352,7 @@ int i, Arity; long int_arg; /* holder for int args */ float float_arg; /* holder for float args */ - struct fmt_spec *current_fmt_spec = (struct fmt_spec *)malloc(sizeof(struct fmt_spec)); + struct fmt_spec *current_fmt_spec = (struct fmt_spec *)mem_alloc(sizeof(struct fmt_spec)); int width=0, precision=0; /* these are used in conjunction with the *.* format */ int bytes_formatted=0; /* the number of bytes formatted as @@ -473,7 +473,7 @@ xsb_segfault_message = xsb_default_segfault_msg; signal(SIGSEGV, xsb_default_segfault_handler); - free(current_fmt_spec); + mem_dealloc(current_fmt_spec,sizeof(struct fmt_spec)); /* fmt_write_string is used in places where interning of the string is needed (such as constructing library search paths) Therefore, must use string_find(..., 1). */ @@ -508,7 +508,7 @@ Integer i ; long int_arg; /* holder for int args */ float float_arg; /* holder for float args */ - struct fmt_spec *current_fmt_spec = (struct fmt_spec *)malloc(sizeof(struct fmt_spec)); + struct fmt_spec *current_fmt_spec = (struct fmt_spec *)mem_alloc(sizeof(struct fmt_spec)); int Arity=0; int number_of_successes=0, curr_assignment=0; int cont; /* continuation indicator */ @@ -667,12 +667,12 @@ number_of_successes = -1; EXIT_READ: - free(current_fmt_spec); + mem_dealloc(current_fmt_spec,sizeof(struct fmt_spec)); ctop_int(CTXTc 5, number_of_successes); return TRUE; EXIT_READ_FALSE: - free(current_fmt_spec); + mem_dealloc(current_fmt_spec,sizeof(struct fmt_spec)); return FALSE; } #undef FmtBuf @@ -777,13 +777,15 @@ #define expand_opstk {\ opstk_size = opstk_size+opstk_size;\ - opstk = (struct opstktype *)realloc(opstk,opstk_size*sizeof(struct opstktype));\ + opstk = (struct opstktype *)mem_realloc(opstk,(opstk_size/2)*sizeof(struct opstktype),\ + opstk_size*sizeof(struct opstktype));\ if (!opstk) xsb_abort("[READ_CANONICAL] Out of space for operand stack");\ /*printf("RC opstk expanded to %d\n",opstk_size);*/ \ } #define expand_funstk {\ funstk_size = funstk_size+funstk_size;\ - funstk = (struct funstktype *)realloc(funstk,funstk_size*sizeof(struct funstktype));\ + funstk = (struct funstktype *)mem_realloc(funstk,(funstk_size/2)*sizeof(struct funstktype),\ + funstk_size*sizeof(struct funstktype));\ if (!funstk) xsb_abort("[READ CANONICAL] Out of space for function stack");\ /*printf("RC funstk expanded to %d\n",funstk_size);*/ \ } @@ -843,10 +845,10 @@ if (opstk_size == 0) { opstk = - (struct opstktype *)malloc(INIT_STK_SIZE*sizeof(struct opstktype)); + (struct opstktype *)mem_alloc(INIT_STK_SIZE*sizeof(struct opstktype)); opstk_size = INIT_STK_SIZE; funstk = - (struct funstktype *)malloc(INIT_STK_SIZE*sizeof(struct funstktype)); + (struct funstktype *)mem_alloc(INIT_STK_SIZE*sizeof(struct funstktype)); funstk_size = INIT_STK_SIZE; } @@ -1191,8 +1193,8 @@ } if (opstk_size > MAX_INIT_STK_SIZE) { - free(opstk); opstk = NULL; - free(funstk); funstk = NULL; + mem_dealloc(opstk,opstk_size); opstk = NULL; + mem_dealloc(funstk,funstk_size); funstk = NULL; opstk_size = 0; funstk_size = 0; } return retpscptr; @@ -1606,10 +1608,10 @@ fprintf(file,"\'%s\'",lnew_string); } else { char* new_string; - new_string = (char *)malloc(neededlen); + new_string = (char *)mem_alloc(neededlen); double_quotes(string,new_string); fprintf(file,"\'%s\'",new_string); - free(new_string); + mem_dealloc(new_string,neededlen); } } } Index: io_builtins_xsb_i.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/io_builtins_xsb_i.h,v retrieving revision 1.38 retrieving revision 1.39 diff -u -r1.38 -r1.39 --- io_builtins_xsb_i.h 9 Nov 2005 17:52:27 -0000 1.38 +++ io_builtins_xsb_i.h 12 Nov 2005 15:48:50 -0000 1.39 @@ -384,8 +384,9 @@ line_buff_disp = 0; do { if (line_buff_disp >= line_buff_len) { + int old_len = line_buff_len; line_buff_len = line_buff_disp+MAX_IO_BUFSIZE; - if(!(line_buff = realloc(line_buff,line_buff_len))) + if(!(line_buff = mem_realloc(line_buff,old_len,line_buff_len))) xsb_exit("No space for line buffer"); } *(line_buff+line_buff_disp) = c = getc(fptr); @@ -411,7 +412,7 @@ ctop_tag(CTXTc 3, new_list); - if (line_buff) free(line_buff); + if (line_buff) mem_dealloc(line_buff,line_buff_len); /* this complex cond takes care of incomplete lines: lines that end with end of file and not with end-of-line. */ Index: loader_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/loader_xsb.c,v retrieving revision 1.44 retrieving revision 1.45 diff -u -r1.44 -r1.45 --- loader_xsb.c 13 Sep 2005 13:02:04 -0000 1.44 +++ loader_xsb.c 12 Nov 2005 15:48:50 -0000 1.45 @@ -135,6 +135,7 @@ /* === working variables ============================================== */ static pw *reloc_table = NULL; +static unsigned long reloc_table_size = 0; static pseg last_text = NULL; /* permanent var, chain of text seg */ static pseg current_seg; /* starting address -- used for relocation */ static CPtr *index_reloc; /* index relocation table */ @@ -217,7 +218,7 @@ size = hsize(clause_no); - indextab = (struct hrec *)malloc(size*sizeof(struct hrec)); + indextab = (struct hrec *)mem_alloc(size*sizeof(struct hrec)); for (j = 0; j < size; j++) { indextab[j].l = 0; @@ -454,15 +455,15 @@ temp_ptr = hptr = hreg; else #endif - temp_ptr = hptr = (CPtr)malloc(temp_space*sizeof(CPtr)); + temp_ptr = hptr = (CPtr)mem_alloc(temp_space*sizeof(CPtr)); t_len = get_index_tab(fd, clause_no); gen_index((xsbBool)(table_num > 0), clause_no, sob_arg_p, arity); - free(indextab); + mem_dealloc(indextab,hsize(clause_no)*sizeof(struct hrec)); #ifndef MULTI_THREAD - if (temp_ptr != hreg) free(temp_ptr); + if (temp_ptr != hreg) mem_dealloc(temp_ptr,temp_space*sizeof(CPtr)); #else - free(temp_ptr); + mem_dealloc(temp_ptr,temp_space*sizeof(CPtr)); #endif count += 10 + t_len; } @@ -477,7 +478,7 @@ current_seg = (pseg) mem_alloc(ZOOM_FACTOR*text_bytes+SIZE_SEG_HDR); /* Allocate first chunk of index_reloc */ - index_reloc = (CPtr *)malloc(NUM_INDEX_BLKS*sizeof(CPtr)); + index_reloc = (CPtr *)mem_alloc(NUM_INDEX_BLKS*sizeof(CPtr)); if (!index_reloc) { xsb_error("Couldn't allocate index relocation space"); return NULL; @@ -497,7 +498,7 @@ } index_block_chain = &seg_index(current_seg); load_index(fd, index_bytes, current_tab); - free(index_reloc); + mem_dealloc(index_reloc,NUM_INDEX_BLKS*sizeof(CPtr)); /* set text-index segment chain */ if (last_text) seg_next(last_text) = current_seg; @@ -669,11 +670,6 @@ } if (!temp_pair) return FALSE; - /* if (count >= REL_TAB_SIZE) { - xsb_dbgmsg(("Reloc_table overflow")); - return FALSE; - } */ - reloc_table[count] = (pw)temp_pair; return TRUE; } /* load_one_sym */ @@ -702,7 +698,8 @@ { int i; - reloc_table = (pw *) calloc((psc_count), sizeof(pw)); + reloc_table = (pw *) mem_calloc((psc_count), sizeof(pw)); + reloc_table_size = psc_count*sizeof(pw); /* xsb_dbgmsg(("reloc_table %x,psc_count %d",reloc_table,psc_count)); */ for (i = count; i < psc_count; i++) { @@ -923,9 +920,9 @@ fclose(fd); if (reloc_table) { - free(reloc_table); + mem_dealloc(reloc_table,reloc_table_size); + reloc_table = 0; } - reloc_table = 0; return first_inst; } /* loader */ Index: loader_xsb.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/loader_xsb.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- loader_xsb.h 20 Aug 2005 06:50:27 -0000 1.6 +++ loader_xsb.h 12 Nov 2005 15:48:50 -0000 1.7 @@ -25,7 +25,6 @@ #include "context.h" -#define REL_TAB_SIZE 32768 #define FOREIGN_NAMELEN 128 #define SHORT_LDOPTIONLEN 254 Index: main_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/main_xsb.c,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- main_xsb.c 18 Jul 2005 21:54:11 -0000 1.8 +++ main_xsb.c 12 Nov 2005 15:48:50 -0000 1.9 @@ -78,7 +78,7 @@ #endif #ifdef MULTI_THREAD - th = malloc( sizeof( th_context ) ) ; + th = malloc( sizeof( th_context ) ) ; /* don't use mem_alloc */ #endif xsb(CTXTc 0, argc, argv); /* init xsb */ Index: memory_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/memory_xsb.c,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- memory_xsb.c 19 Aug 2005 16:24:10 -0000 1.18 +++ memory_xsb.c 12 Nov 2005 15:48:50 -0000 1.19 @@ -99,6 +99,42 @@ } +/* === calloc permanent memory ============================================= */ + +byte *mem_calloc(unsigned long size, unsigned long occs) +{ + byte * ptr; + unsigned long length = (size*occs+7) & ~0x7; + + SYS_MUTEX_LOCK(MUTEX_MEM); + pspacesize += length; + ptr = (byte *) calloc(size,occs); +#if defined(GENERAL_TAGGING) + // printf("mem_calloc %x %x\n",ptr,ptr+length); + extend_enc_dec_as_nec(ptr,ptr+length); +#endif + SYS_MUTEX_UNLOCK(MUTEX_MEM); + return ptr; +} + + +/* === realloc permanent memory ============================================ */ + +byte *mem_realloc(void *addr, unsigned long oldsize, unsigned long newsize) +{ + newsize = (newsize+7) & ~0x7 ; /* round to 8 */ + oldsize = (oldsize+7) & ~0x7 ; /* round to 8 */ + SYS_MUTEX_LOCK(MUTEX_MEM); + pspacesize = pspacesize - oldsize + newsize; + addr = (byte *) realloc(addr,newsize); +#if defined(GENERAL_TAGGING) + extend_enc_dec_as_nec(addr,addr+newsize); +#endif + SYS_MUTEX_UNLOCK(MUTEX_MEM); + return addr; +} + + /* === dealloc permanent memory ============================================ */ void mem_dealloc(void *addr, unsigned long size) Index: memory_xsb.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/memory_xsb.h,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- memory_xsb.h 3 Oct 2005 13:26:43 -0000 1.19 +++ memory_xsb.h 12 Nov 2005 15:48:50 -0000 1.20 @@ -123,6 +123,8 @@ /* Memory Function Prototypes -------------------------- */ extern byte *mem_alloc(unsigned long); +extern byte *mem_calloc(unsigned long, unsigned long); +extern byte *mem_realloc(void *, unsigned long, unsigned long); extern void mem_dealloc(void *, unsigned long); #ifndef MULTI_THREAD extern void tcpstack_realloc(long); Index: random_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/random_xsb.c,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- random_xsb.c 11 Jul 2005 19:47:20 -0000 1.8 +++ random_xsb.c 12 Nov 2005 15:48:51 -0000 1.9 @@ -48,6 +48,7 @@ #include "ptoc_tag_xsb_i.h" #include "error_xsb.h" #include "context.h" +#include "memory_xsb.h" #ifndef MULTI_THREAD struct random_seeds_t *random_seeds = 0; @@ -56,7 +57,7 @@ struct random_seeds_t *init_random_seeds() { struct random_seeds_t *seeds; - seeds = (struct random_seeds_t *)malloc(sizeof(struct random_seeds_t)); + seeds = (struct random_seeds_t *)mem_alloc(sizeof(struct random_seeds_t)); if (!seeds) xsb_abort("No space for random seeds!!"); seeds->IX = 6293; seeds->IY = 21877; Index: slgdelay.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/slgdelay.c,v retrieving revision 1.40 retrieving revision 1.41 diff -u -r1.40 -r1.41 --- slgdelay.c 28 Oct 2005 21:50:29 -0000 1.40 +++ slgdelay.c 12 Nov 2005 15:48:51 -0000 1.41 @@ -108,7 +108,7 @@ NEW_ENTRY = NEXT_FREE++; \ else { \ char *new_block; \ - if ((new_block = (char *) malloc(BLOCK_SIZE + sizeof(Cell))) == NULL)\ + if ((new_block = (char *) mem_alloc(BLOCK_SIZE + sizeof(Cell))) == NULL)\ xsb_abort(ABORT_MESG); \ *(char **) new_block = CURRENT_BLOCK; \ CURRENT_BLOCK = new_block; \ @@ -623,7 +623,7 @@ simplify_neg_fails(CTXTc unsup_subgoal); } } - free(unsup_asi); + mem_dealloc(unsup_asi,sizeof(struct AS_info)); } /* @@ -690,7 +690,7 @@ * free it, and really mark `as_leaf' as an unconditional answer. */ Child(as_leaf) = NULL; - free(asi); + mem_dealloc(asi,sizeof(struct AS_info)); } /* @@ -837,7 +837,7 @@ while (current_de_block) { last_block = *(char **) current_de_block; - free(current_de_block); + mem_dealloc(current_de_block,de_block_size + sizeof(Cell)); current_de_block = last_block; } @@ -845,7 +845,7 @@ while (current_dl_block) { last_block = *(char **) current_dl_block; - free(current_dl_block); + mem_dealloc(current_dl_block,dl_block_size + sizeof(Cell)); current_dl_block = last_block; } @@ -853,7 +853,7 @@ while (current_pnde_block) { last_block = *(char **) current_pnde_block; - free(current_pnde_block); + mem_dealloc(current_pnde_block,pnde_block_size + sizeof(Cell)); current_pnde_block = last_block; } Index: slgdelay.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/slgdelay.h,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- slgdelay.h 14 Jan 2005 18:31:30 -0000 1.19 +++ slgdelay.h 12 Nov 2005 15:48:51 -0000 1.20 @@ -97,7 +97,7 @@ #define asi_dl_list(X) (X) -> dl_list #define create_as_info(ANS, SUBG) \ - asi = (ASI) malloc(sizeof(struct AS_info)); \ + asi = (ASI) mem_alloc(sizeof(struct AS_info)); \ Child(ANS) = (NODEptr) asi; \ asi_pdes(asi) = NULL; \ asi_subgoal(asi) = SUBG; \ Index: socket_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/socket_xsb.c,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- socket_xsb.c 20 Sep 2005 22:51:38 -0000 1.32 +++ socket_xsb.c 12 Nov 2005 15:48:51 -0000 1.33 @@ -75,6 +75,7 @@ #include "socket_xsb.h" #include "psc_xsb.h" #include "register.h" +#include "memory_xsb.h" #ifdef WIN_NT typedef int socklen_t; @@ -221,7 +222,7 @@ error: SOCK_READMSG_FAILED Read message header, then read the message itself. */ -static int readmsg(SOCKET sock_handle, char **msg_buff) +static int readmsg(SOCKET sock_handle, char **msg_buff, unsigned long *msg_len) { int actual_len; /* 4-char buf that keeps the length of the subsequent msg */ @@ -239,8 +240,9 @@ memcpy((void *) &net_encoded_len, (void *) lenbuf, XSB_MSG_HEADER_LENGTH); msglen = ntohl(net_encoded_len); + *msg_len = (msglen+1)*sizeof(char); - if ((*msg_buff = (char *)calloc(msglen+1, sizeof(char))) == NULL) { + if ((*msg_buff = (char *)mem_calloc(msglen+1, sizeof(char))) == NULL) { xsb_abort("[SOCKET_RECV] Can't allocate memory for the message buffer"); } @@ -346,10 +348,10 @@ } } -static int socket_recv(CTXTdeclc int *rc, char** buffer, int timeout) { +static int socket_recv(CTXTdeclc int *rc, char** buffer, unsigned long *buffer_len, int timeout) { SOCKET sock_handle = (SOCKET) ptoc_int(CTXTc 2); if (read_select(sock_handle, timeout)) { - *rc = readmsg(sock_handle, buffer); + *rc = readmsg(sock_handle, buffer, buffer_len); return NORMAL_TERMINATION; } else { return TIMED_OUT; @@ -369,7 +371,7 @@ msg_body_len = strlen(send_msg_aux); /* We use the first XSB_MSG_HEADER_LENGTH bytes for the message size.*/ - message_buffer = calloc(msg_body_len + XSB_MSG_HEADER_LENGTH + 1, sizeof(char)); + message_buffer = mem_calloc(msg_body_len + XSB_MSG_HEADER_LENGTH + 1, sizeof(char)); if (message_buffer == NULL) { xsb_abort("[SOCKET_SEND] Can't allocate memory for the message buffer"); } @@ -379,7 +381,7 @@ strcpy(message_buffer + XSB_MSG_HEADER_LENGTH, send_msg_aux); *rc = sendto(sock_handle, message_buffer, msg_body_len+XSB_MSG_HEADER_LENGTH, 0, NULL, 0); - free(message_buffer); + mem_dealloc(message_buffer,(msg_body_len + XSB_MSG_HEADER_LENGTH + 1)*sizeof(char)); return NORMAL_TERMINATION; } @@ -422,6 +424,7 @@ struct linger sock_linger_opt; int rc; char *message_buffer; + unsigned long msg_len; char char_read; switch (ptoc_int(CTXTc 1)) { @@ -552,7 +555,7 @@ case SOCKET_RECV: /* socket_request(SOCKET_RECV,+Sockfd, -Msg, -Error,_,_,_) */ - timeout_flag = socket_recv(CTXTc &rc, &message_buffer, (int)pflags[SYS_TIMER]); + timeout_flag = socket_recv(CTXTc &rc, &message_buffer, &msg_len, (int)pflags[SYS_TIMER]); if (timeout_flag == TIMED_OUT) { return set_error_code(CTXTc TIMEOUT_ERR, 4, "SOCKET_SEND"); @@ -575,7 +578,7 @@ if (message_buffer != NULL) { ctop_string(CTXTc 3, (char*)string_find(message_buffer, 1)); - free(message_buffer); + mem_dealloc(message_buffer,msg_len); } else { /* this happens at end of file */ ctop_string(CTXTc 3, (char*)string_find("", 1)); } @@ -750,7 +753,7 @@ if (isinteger(timeout_term)|isboxedinteger(timeout_term)) { timeout = oint_val(timeout_term); /* initialize tv */ - tv = (struct timeval *)malloc(sizeof(struct timeval)); + tv = (struct timeval *)mem_alloc(sizeof(struct timeval)); tv->tv_sec = timeout; tv->tv_usec = 0; } else @@ -819,7 +822,7 @@ connections[count].exception_fds,connections[count].sizee); } - if (tv) free((struct timeval *)tv); + if (tv) mem_dealloc((struct timeval *)tv,sizeof(struct timeval)); return set_error_code(CTXTc ecode, 7, "SOCKET_SELECT"); } @@ -923,7 +926,7 @@ prolog_term head; *size = getsize(local); - *fds = (int*)malloc(sizeof(int)*(*size)); + *fds = (int*)mem_alloc(sizeof(int)*(*size)); while (!isnil(list)) { head = p2p_car(list); @@ -979,10 +982,10 @@ connections[i].connection_name = NULL; connections[i].maximum_fd = 0; - /* free the fds obtained by malloc() */ - free(connections[i].read_fds); - free(connections[i].write_fds); - free(connections[i].exception_fds); + /* free the fds obtained by mem_alloc() */ + mem_dealloc(connections[i].read_fds,connections[i].sizer); + mem_dealloc(connections[i].write_fds,connections[i].sizew); + mem_dealloc(connections[i].exception_fds,connections[i].sizee); connections[i].sizer = 0; connections[i].sizew = 0 ; Index: std_pred_xsb_i.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/std_pred_xsb_i.h,v retrieving revision 1.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- std_pred_xsb_i.h 13 Oct 2005 14:25:28 -0000 1.27 +++ std_pred_xsb_i.h 12 Nov 2005 15:48:51 -0000 1.28 @@ -309,11 +309,9 @@ term = ptoc_tag(CTXTc 1); list = ptoc_tag(CTXTc 2); if (!isnonvar(term)) { /* use is: CODES/CHARS --> ATOM */ - if (atomnameaddr == NULL) { - atomnameaddr = (char *)malloc(INITIAL_NAMELEN); - atomnamelen = INITIAL_NAMELEN; - /* printf("Allocated namebuf: %p, %d\n",atomnameaddr,atomnamelen);*/ - } + atomnameaddr = (char *)mem_alloc(INITIAL_NAMELEN); + atomnamelen = INITIAL_NAMELEN; + // printf("Allocated namebuf: %p, %d\n",atomnameaddr,atomnamelen); atomname = atomnameaddr; atomnamelast = atomnameaddr + (atomnamelen - 1); term2 = list; /* DON'T use heap for temp storage */ @@ -345,7 +343,7 @@ return FALSE; /* fail */ } if (atomname >= atomnamelast) { - atomnameaddr = (char *)realloc(atomnameaddr, (atomnamelen << 1)); + atomnameaddr = (char *)mem_realloc(atomnameaddr,atomnamelen,(atomnamelen << 1)); atomname = atomnameaddr + (atomnamelen - 1); atomnamelen = atomnamelen << 1; atomnamelast = atomnameaddr + (atomnamelen - 1); @@ -361,6 +359,7 @@ } } while (1); bind_string((CPtr)(term), (char *)string_find((char *)atomnameaddr, 1)); + mem_dealloc(atomnameaddr,atomnamelen); SYS_MUTEX_UNLOCK(MUTEX_ATOM_BUF); return TRUE; } else { /* use is: ATOM --> CODES/CHARS */ @@ -640,7 +639,7 @@ list = ptoc_tag(CTXTc 1); /* reset in case moved */ if (len > 0) { term2 = list; - cell_tbl = (Cell *)malloc((len * sizeof(Cell))); + cell_tbl = (Cell *)mem_alloc((len * sizeof(Cell))); if (!cell_tbl) xsb_abort("Cannot allocate temporary memory for sort/2"); for (i=0 ; i < len ; ++i) { @@ -664,7 +663,7 @@ follow(top) = makelist(hreg); } } follow(top) = makenil; - free(cell_tbl); + mem_dealloc(cell_tbl,len * sizeof(Cell)); term = ptoc_tag(CTXTc 2); return unify(CTXTc new_list, term); } @@ -707,7 +706,7 @@ term = ptoc_tag(CTXTc 2); if (len > 0) { term2 = list; - cell_tbl = (Cell *)malloc(len * sizeof(Cell)); + cell_tbl = (Cell *)mem_alloc(len * sizeof(Cell)); if (!cell_tbl) xsb_abort("Cannot allocate temporary memory for keysort/2"); for (i=0 ; i < len ; ++i) { @@ -727,7 +726,7 @@ top = hreg++; follow(top) = makelist(hreg); } follow(top) = makenil; - free(cell_tbl); + mem_dealloc(cell_tbl,len * sizeof(Cell)); return unify(CTXTc new_list, term); } return unify(CTXTc list, term); @@ -841,7 +840,7 @@ term = ptoc_tag(CTXTc 4); if (len > 0) { term2 = list; - cell_tbl = (Cell *)malloc(len * sizeof(Cell)); + cell_tbl = (Cell *)mem_alloc(len * sizeof(Cell)); if (!cell_tbl) xsb_abort("Cannot allocate temporary memory for parsort/4"); for (i=0 ; i < len ; ++i) { @@ -874,7 +873,7 @@ } } follow(top) = makenil; - free(cell_tbl); + mem_dealloc(cell_tbl,len * sizeof(Cell)); return unify(CTXTc new_list, term); } return unify(CTXTc list, term); Index: sub_delete.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/sub_delete.c,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- sub_delete.c 14 Jan 2005 18:31:33 -0000 1.8 +++ sub_delete.c 12 Nov 2005 15:48:51 -0000 1.9 @@ -36,6 +36,7 @@ #include "macro_xsb.h" #include "error_xsb.h" #include "thread_xsb.h" +#include "memory_xsb.h" extern BTHTptr hhadded; @@ -89,12 +90,12 @@ -------------------------------------- */ static void delete_btht(BTHTptr btht) { - free(BTHT_BucketArray(btht)); + mem_dealloc(BTHT_BucketArray(btht),BTHT_NumBuckets(btht)*sizeof(void *)); free_btht(btht); } static void delete_tstht(TSTHTptr tstht) { - free(BTHT_BucketArray(tstht)); + mem_dealloc(BTHT_BucketArray(tstht),BTHT_NumBuckets(tstht)*sizeof(void *)); free_tsi(tstht); free_tstht(tstht); } Index: thread_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/thread_xsb.c,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- thread_xsb.c 20 Aug 2005 06:50:28 -0000 1.22 +++ thread_xsb.c 12 Nov 2005 15:48:51 -0000 1.23 @@ -24,6 +24,7 @@ #include "ptoc_tag_xsb_i.h" #include "thread_xsb.h" #include "rw_lock.h" +#include "memory_xsb.h" #ifdef MULTI_THREAD #include <errno.h> @@ -257,7 +258,7 @@ cleanup_machine(CTXT) ; thread_free_dyn_blks(CTXT); thread_free_tab_blks(CTXT); - free( th ) ; + mem_dealloc(th,sizeof(th_context)) ; flags[NUM_THREADS]-- ; pthread_mutex_lock( &th_mutex ); tid2 = pthread_self(); Index: tr_utils.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/tr_utils.c,v retrieving revision 1.91 retrieving revision 1.92 diff -u -r1.91 -r1.92 --- tr_utils.c 7 Nov 2005 20:55:41 -0000 1.91 +++ tr_utils.c 12 Nov 2005 15:48:51 -0000 1.92 @@ -327,8 +327,10 @@ #define push_node(node) {\ if (node_stk_top >= freeing_stack_size) {\ + unsigned long old_freeing_stack_size = freeing_stack_size; \ freeing_stack_size = freeing_stack_size + freeing_stack_increment;\ - freeing_stack = (BTNptr *)realloc(freeing_stack,freeing_stack_size*sizeof(BTNptr));\ + freeing_stack = (BTNptr *)mem_realloc(freeing_stack,old_freeing_stack_size*sizeof(BTNptr),\ + freeing_stack_size*sizeof(BTNptr));\ }\ freeing_stack[node_stk_top] = node;\ node_stk_top++;\ @@ -343,7 +345,7 @@ static void free_trie_ht(CTXTdeclc BTHTptr ht) { TrieHT_RemoveFromAllocList(*smBTHT,ht); - free(BTHT_BucketArray(ht)); + mem_dealloc(BTHT_BucketArray(ht),BTHT_NumBuckets(ht)*sizeof(void *)); SM_DeallocateStruct(*smBTHT,ht); } @@ -385,7 +387,7 @@ } /* free answer trie */ free_answer_list(pSF); FreeProducerSF(pSF); - free(freeing_stack); + mem_dealloc(freeing_stack,freeing_stack_size); } static void delete_variant_table(CTXTdeclc BTNptr x) { @@ -456,7 +458,7 @@ } TRIE_W_UNLOCK(); - free(freeing_stack); + mem_dealloc(freeing_stack,freeing_stack_size); } @@ -680,7 +682,7 @@ trie_op_top++;\ if (trie_op_top >= trie_op_size) {\ trie_op_size = 2*trie_op_size;\ - delete_trie_op = (char *)realloc(delete_trie_op,trie_op_size*sizeof(char));\ + delete_trie_op = (char *)mem_realloc(delete_trie_op,(trie_op_size/2)*sizeof(char),trie_op_size*sizeof(char));\ if (!delete_trie_op) xsb_exit("out of space for deleting trie");\ /*xsb_dbgmsg((LOG_DEBUG,"realloc delete_trie_op to %d",trie_op_size));*/\ }\ @@ -688,7 +690,7 @@ trie_node_top++;\ if (trie_node_top >= trie_node_size) {\ trie_node_size = 2*trie_node_size;\ - delete_trie_node = (BTNptr *)realloc(delete_trie_node,trie_node_size*sizeof(BTNptr));\ + delete_trie_node = (BTNptr *)mem_realloc(delete_trie_node,(trie_node_size/2)*sizeof(BTNptr),trie_node_size*sizeof(BTNptr));\ if (!delete_trie_node) xsb_exit("out of space for deleting trie");\ /*xsb_dbgmsg((LOG_DEBUG,"realloc delete_trie_node to %d",trie_node_size));*/\ }\ @@ -698,7 +700,7 @@ trie_op_top++;\ if (trie_op_top >= trie_op_size) {\ trie_op_size = 2*trie_op_size;\ - delete_trie_op = (char *)realloc(delete_trie_op,trie_op_size*sizeof(char));\ + delete_trie_op = (char *)mem_realloc(delete_trie_op,(trie_op_size/2)*sizeof(char),trie_op_size*sizeof(char));\ if (!delete_trie_op) xsb_exit("out of space for deleting trie");\ /*xsb_dbgmsg((LOG_DEBUG,"realloc delete_trie_op to %d",trie_op_size));*/\ }\ @@ -706,7 +708,7 @@ trie_hh_top++;\ if (trie_hh_top >= trie_hh_size) {\ trie_hh_size = 2*trie_hh_size;\ - delete_trie_hh = (BTHTptr *)realloc(delete_trie_hh,trie_hh_size*sizeof(BTHTptr));\ + delete_trie_hh = (BTHTptr *)mem_realloc(delete_trie_hh,(trie_hh_size/2)*sizeof(BTHTptr),trie_hh_size*sizeof(BTHTptr));\ if (!delete_trie_hh) xsb_exit("out of space for deleting trie");\ /*xsb_dbgmsg((LOG_DEBUG,"realloc delete_trie_hh to %d",trie_hh_size));*/\ }\ @@ -727,9 +729,9 @@ int trie_op_size, trie_node_size, trie_hh_size; if (!delete_trie_op) { - delete_trie_op = (char *)malloc(DELETE_TRIE_STACK_INIT*sizeof(char)); - delete_trie_node = (BTNptr *)malloc(DELETE_TRIE_STACK_INIT*sizeof(BTNptr)); - delete_trie_hh = (BTHTptr *)malloc(DELETE_TRIE_STACK_INIT*sizeof(BTHTptr)); + delete_trie_op = (char *)mem_alloc(DELETE_TRIE_STACK_INIT*sizeof(char)); + delete_trie_node = (BTNptr *)mem_alloc(DELETE_TRIE_STACK_INIT*sizeof(BTNptr)); + delete_trie_hh = (BTHTptr *)mem_alloc(DELETE_TRIE_STACK_INIT*sizeof(BTHTptr)); trie_op_size = trie_node_size = trie_hh_size = DELETE_TRIE_STACK_INIT; } @@ -788,9 +790,9 @@ break; } } - free(delete_trie_op); delete_trie_op = NULL; - free(delete_trie_node); delete_trie_node = NULL; - free(delete_trie_hh); delete_trie_hh = NULL; + mem_dealloc(delete_trie_op,trie_op_size); delete_trie_op = NULL; + mem_dealloc(delete_trie_node,trie_node_size); delete_trie_node = NULL; + mem_dealloc(delete_trie_hh,trie_hh_size); delete_trie_hh = NULL; trie_op_size = 0; } @@ -988,7 +990,7 @@ first_free_set = 0; Set_ArraySz = 100; num_sets = 1; - Set_ArrayPtr = (BTNptr *) calloc(Set_ArraySz,sizeof(BTNptr)); + Set_ArrayPtr = (BTNptr *) mem_calloc(Set_ArraySz,sizeof(BTNptr)); bt_storage_hash_table.length = STORAGE_TBL_SIZE; bt_storage_hash_table.bucket_size = sizeof(STORAGE_HANDLE); @@ -1014,15 +1016,17 @@ else { if (num_sets == Set_ArraySz) { /* run out of elements */ BTNptr *temp_arrayptr; + unsigned long temp_arraysz; temp_arrayptr = Set_ArrayPtr; + temp_arraysz = Set_ArraySz; Set_ArraySz += ADJUST_SIZE; /* adjust the array size */ - Set_ArrayPtr = (BTNptr *) calloc(Set_ArraySz ,sizeof(BTNptr)); + Set_ArrayPtr = (BTNptr *) mem_calloc(Set_ArraySz ,sizeof(BTNptr)); if (Set_ArrayPtr == NULL) xsb_exit("Out of memory in new_trie/1"); for (i = 0; i < num_sets; i++) Set_ArrayPtr[i] = temp_arrayptr[i]; - free(temp_arrayptr); + mem_dealloc(temp_arrayptr,temp_arraysz); } result = (Integer)num_sets; num_sets++; @@ -1190,7 +1194,7 @@ { IGRptr igr; - igr = malloc(sizeof(InternGarbageRoot)); + igr = (IGRptr) mem_alloc(sizeof(InternGarbageRoot)); igr -> root = root; igr -> leaves = NULL; igr -> next = NULL; @@ -1201,7 +1205,7 @@ { IGLptr igl; - igl = malloc(sizeof(InternGarbageLeaf)); + igl = (IGLptr) mem_alloc(sizeof(InternGarbageLeaf)); igl -> leaf = leafn; igl -> next = NULL; return igl; @@ -1318,13 +1322,13 @@ else return; - free(r); + mem_dealloc(r,sizeof(InternGarbageRoot)); while(l != NULL){ /* printf("Loop b %p\n", l); */ leaf = l -> leaf; p = l -> next; - free(l); + mem_dealloc(l,sizeof(InternGarbageLeaf)); switch_to_trie_assert; if(IsDeletedNode(leaf)) { delete_branch(CTXTc leaf, &(Set_ArrayPtr[rootidx])); @@ -1356,7 +1360,7 @@ } else{ if(p -> leaf == leafn){ r -> leaves = p -> next; - free(p); + mem_dealloc(p,sizeof(InternGarbageLeaf)); if(r -> leaves == NULL){ /* Do not want roots with no leaves hanging around */ getAndRemoveIGRnode(rootIdx); Index: trie_internals.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/trie_internals.h,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- trie_internals.h 21 Oct 2005 17:47:52 -0000 1.24 +++ trie_internals.h 12 Nov 2005 15:48:51 -0000 1.25 @@ -646,7 +646,7 @@ BTHT_NumContents(((BTHTptr)btht)) = MAX_SIBLING_LEN + 1; \ BTHT_NumBuckets(((BTHTptr)btht)) = TrieHT_INIT_SIZE; \ BTHT_BucketArray(((BTHTptr)btht)) = \ - (BTNptr *)calloc(TrieHT_INIT_SIZE, sizeof(void *)); \ + (BTNptr *)mem_calloc(TrieHT_INIT_SIZE, sizeof(void *)); \ if ( IsNonNULL(BTHT_BucketArray(((BTHTptr)btht))) ) \ TrieHT_AddNewToAllocList(SM,((BTHTptr)btht)) \ else { \ @@ -675,7 +675,7 @@ \ for ( pBTHT = (BTHTptr)SM_AllocList(SM); IsNonNULL(pBTHT); \ pBTHT = (BTHTptr)BTHT_NextBTHT(pBTHT) ) \ - free(BTHT_BucketArray(pBTHT)); \ + mem_dealloc(BTHT_BucketArray(pBTHT),BTHT_NumBuckets(pBTHT)*sizeof(void *)); \ } /* Allocating Headers Index: tries.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/tries.c,v retrieving revision 1.76 retrieving revision 1.77 diff -u -r1.76 -r1.77 --- tries.c 8 Nov 2005 01:05:16 -0000 1.76 +++ tries.c 12 Nov 2005 15:48:51 -0000 1.77 @@ -449,7 +449,7 @@ new_size = TrieHT_NewSize(pHT); - bucket_array = (BTNptr *)realloc( BTHT_BucketArray(pHT), + bucket_array = (BTNptr *)mem_realloc( BTHT_BucketArray(pHT), BTHT_NumBuckets(pHT)*sizeof(void*), new_size * sizeof(BTNptr) ); if ( IsNULL(bucket_array) ) re... [truncated message content] |