fix memory leak in scanner

instrument memory allocation with default_malloc


git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1445 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2005-11-08 13:51:15 +00:00
parent 5b4773d923
commit c13d5d2655
3 changed files with 55 additions and 44 deletions

View File

@ -12,7 +12,7 @@
* Last rev: *
* mods: *
* comments: allocating space *
* version:$Id: alloc.c,v 1.72 2005-07-06 15:10:02 vsc Exp $ *
* version:$Id: alloc.c,v 1.73 2005-11-08 13:51:15 vsc Exp $ *
*************************************************************************/
#ifdef SCCS
static char SccsId[] = "%W% %G%";
@ -65,27 +65,64 @@ static char SccsId[] = "%W% %G%";
#if USE_SYSTEM_MALLOC||USE_DL_MALLOC
long long unsigned int mallocs, reallocs, frees;
long long unsigned int tmalloc;
#include <malloc.h>
#if INSTRUMENT_MALLOC
static void
minfo(char mtype)
{
struct mallinfo minfo = mallinfo();
fprintf(stderr,"%c %lld (%lld), %lld, %lld %d/%d/%d\n", mtype, mallocs, tmalloc, reallocs, frees,minfo.arena,minfo.ordblks,minfo.fordblks);
}
#endif
char *
Yap_AllocCodeSpace(unsigned int size)
{
#if INSTRUMENT_MALLOC
if (mallocs % 1024*4 == 0)
minfo('A');
mallocs++;
tmalloc += size;
#endif
return malloc(size);
}
void
Yap_FreeCodeSpace(char *p)
{
#if INSTRUMENT_MALLOC
if (frees % 1024*4 == 0)
minfo('F');
frees++;
#endif
free (p);
}
char *
Yap_AllocAtomSpace(unsigned int size)
{
#if INSTRUMENT_MALLOC
if (mallocs % 1024*4 == 0)
minfo('A');
mallocs++;
tmalloc += size;
#endif
return malloc(size);
}
void
Yap_FreeAtomSpace(char *p)
{
#if INSTRUMENT_MALLOC
if (frees % 1024*4 == 0)
minfo('F');
frees++;
#endif
free (p);
}
@ -121,6 +158,11 @@ Yap_ExpandPreAllocCodeSpace(UInt sz0, void *cip)
ScratchPad.sz =
sz = sz + sz0;
#if INSTRUMENT_MALLOC
if (reallocs % 1024*4 == 0)
minfo('R');
reallocs++;
#endif
while (!(ptr = realloc(ScratchPad.ptr, sz))) {
#if USE_DL_MALLOC
if (!Yap_growheap((cip!=NULL), sz, cip)) {
@ -1136,7 +1178,7 @@ free(MALLOC_T ptr)
}
MALLOC_T
realloc(MALLOC_T ptr, size_t size)
XX realloc(MALLOC_T ptr, size_t size)
{
MALLOC_T new = malloc(size);

View File

@ -128,12 +128,15 @@ typedef struct scanner_extra_alloc {
void *filler;
} ScannerExtraBlock;
#if USE_SYSTEM_MALLOC
#define EXPAND_TRAIL TRUE
#else
#define EXPAND_TRAIL FALSE
#endif
static char *
AllocScannerMemory(unsigned int size)
{
#if USE_SYSTEM_MALLOC
return malloc(AdjustSize(size));
#else
char *AuxSpScan;
AuxSpScan = ScannerStack;
@ -152,7 +155,7 @@ AllocScannerMemory(unsigned int size)
if (size > alloc_size)
alloc_size = size;
if(!Yap_growtrail (alloc_size, TRUE)) {
if(!EXPAND_TRAIL || !Yap_growtrail (alloc_size, TRUE)) {
struct scanner_extra_alloc *ptr;
if (!(ptr = (struct scanner_extra_alloc *)malloc(size+sizeof(ScannerExtraBlock)))) {
@ -165,15 +168,11 @@ AllocScannerMemory(unsigned int size)
}
ScannerStack = AuxSpScan+size;
return AuxSpScan;
#endif
}
static void
PopScannerMemory(char *block, unsigned int size)
{
#if USE_SYSTEM_MALLOC
return free(block);
#else
if (block == ScannerStack-size) {
ScannerStack -= size;
} else if (block == (char *)(ScannerExtraBlocks+1)) {
@ -182,7 +181,6 @@ PopScannerMemory(char *block, unsigned int size)
ScannerExtraBlocks = ptr->next;
free(ptr);
}
#endif
}
char *
@ -656,7 +654,8 @@ Yap_scan_num(int (*Nxtch) (int))
ch = Nxtch(-1);
}
if (chtype[ch] != NU) {
return(TermNil);
Yap_clean_tokenizer(NULL, NULL, NULL);
return TermNil;
}
cherr = 0;
out = get_num(&ch, &cherr, -1, Nxtch, Nxtch, ptr, 4096);
@ -667,6 +666,7 @@ Yap_scan_num(int (*Nxtch) (int))
else if (IsFloatTerm(out))
out = MkFloatTerm(-FloatOfTerm(out));
}
Yap_clean_tokenizer(NULL, NULL, NULL);
if (Yap_ErrorMessage != NULL || ch != -1 || cherr)
return TermNil;
return(out);
@ -1040,30 +1040,6 @@ Yap_tokenizer(int inp_stream)
return (l);
}
#if USE_SYSTEM_MALLOC
static
void clean_vtable(VarEntry *vt)
{
if (vt == NULL)
return;
clean_vtable(vt->VarLeft);
clean_vtable(vt->VarRight);
free(vt);
}
static
void clean_tokens(TokEntry *tk)
{
while (tk != NULL) {
TokEntry *ntk = tk->TokNext;
if (tk->Tok == Ord(String_tok)) {
free((void *)(tk->TokInfo));
}
free(tk);
tk = ntk;
}
}
void
Yap_clean_tokenizer(TokEntry *tokstart, VarEntry *vartable, VarEntry *anonvartable)
{
@ -1073,8 +1049,5 @@ Yap_clean_tokenizer(TokEntry *tokstart, VarEntry *vartable, VarEntry *anonvartab
free(ptr);
ptr = next;
}
clean_vtable(vartable);
clean_vtable(anonvartable);
clean_tokens(tokstart);
}
#endif

View File

@ -255,11 +255,7 @@ Term STD_PROTO(Yap_VarNames,(VarEntry *,Term));
/* routines in scanner.c */
TokEntry STD_PROTO(*Yap_tokenizer,(int));
#if USE_SYSTEM_MALLOC
void STD_PROTO(Yap_clean_tokenizer,(TokEntry *, VarEntry *, VarEntry *));
#else
#define Yap_clean_tokenizer(T,V,A)
#endif
Term STD_PROTO(Yap_scan_num,(int (*)(int)));
char STD_PROTO(*Yap_AllocScannerMemory,(unsigned int));