From f8070bdd592a21d00b7fb8d3a3f236a79e921a6c Mon Sep 17 00:00:00 2001 From: vsc Date: Wed, 3 Feb 2010 22:54:47 +0000 Subject: [PATCH] YAPOR: lock memory allocation under YAPOR/dlmalloc --- C/alloc.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ H/dhstruct.h | 3 +++ H/hstruct.h | 3 +++ H/ihstruct.h | 3 +++ H/rhstruct.h | 3 +++ misc/HEAPFIELDS | 3 +++ 6 files changed, 60 insertions(+) diff --git a/C/alloc.c b/C/alloc.c index cb6533f47..86111ad1e 100644 --- a/C/alloc.c +++ b/C/alloc.c @@ -136,6 +136,9 @@ static inline char * call_malloc(unsigned long int size) { char *out; +#if USE_DL_MALLOC + LOCK(DLMallocLock); +#endif #if INSTRUMENT_MALLOC if (mallocs % 1024*4 == 0) minfo('A'); @@ -145,6 +148,9 @@ call_malloc(unsigned long int size) Yap_PrologMode |= MallocMode; out = (char *) my_malloc(size); Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif return out; } @@ -158,6 +164,9 @@ static inline char * call_realloc(char *p, unsigned long int size) { char *out; +#if USE_DL_MALLOC + LOCK(DLMallocLock); +#endif #if INSTRUMENT_MALLOC if (mallocs % 1024*4 == 0) minfo('A'); @@ -167,6 +176,9 @@ call_realloc(char *p, unsigned long int size) Yap_PrologMode |= MallocMode; out = (char *) my_realloc0(p, size); Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif return out; } @@ -179,6 +191,9 @@ Yap_ReallocCodeSpace(char *p, unsigned long int size) void Yap_FreeCodeSpace(char *p) { +#if USE_DL_MALLOC + LOCK(DLMallocLock); +#endif Yap_PrologMode |= MallocMode; #if INSTRUMENT_MALLOC @@ -188,6 +203,9 @@ Yap_FreeCodeSpace(char *p) #endif my_free (p); Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif } char * @@ -199,6 +217,9 @@ Yap_AllocAtomSpace(unsigned long int size) void Yap_FreeAtomSpace(char *p) { +#if USE_DL_MALLOC + LOCK(DLMallocLock); +#endif Yap_PrologMode |= MallocMode; #if INSTRUMENT_MALLOC if (frees % 1024*4 == 0) @@ -207,6 +228,9 @@ Yap_FreeAtomSpace(char *p) #endif my_free (p); Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif } #endif @@ -219,16 +243,28 @@ Yap_InitPreAllocCodeSpace(void) char *ptr; UInt sz = ScratchPad.msz; if (ScratchPad.ptr == NULL) { +#if USE_DL_MALLOC + LOCK(DLMallocLock); +#endif Yap_PrologMode |= MallocMode; while (!(ptr = my_malloc(sz))) { Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif if (!Yap_growheap(FALSE, Yap_Error_Size, NULL)) { Yap_Error(OUT_OF_HEAP_ERROR, TermNil, Yap_ErrorMessage); return(NULL); } +#if USE_DL_MALLOC + LOCK(DLMallocLock); +#endif Yap_PrologMode |= MallocMode; } Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif ScratchPad.ptr = ptr; } else { ptr = ScratchPad.ptr; @@ -251,6 +287,9 @@ Yap_ExpandPreAllocCodeSpace(UInt sz0, void *cip, int safe) sz = sz0; sz = AdjustLargePageSize(sz+sz/4); +#if USE_DL_MALLOC + LOCK(DLMallocLock); +#endif #if INSTRUMENT_MALLOC if (reallocs % 1024*4 == 0) minfo('R'); @@ -259,9 +298,15 @@ Yap_ExpandPreAllocCodeSpace(UInt sz0, void *cip, int safe) Yap_PrologMode |= MallocMode; if (!(ptr = my_realloc(ScratchPad.ptr, sz, ScratchPad.sz, safe))) { Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif return NULL; } Yap_PrologMode &= ~MallocMode; +#if USE_DL_MALLOC + UNLOCK(DLMallocLock); +#endif ScratchPad.sz = ScratchPad.msz = sz; ScratchPad.ptr = ptr; AuxBase = ptr; diff --git a/H/dhstruct.h b/H/dhstruct.h index 8494e58a2..6090007f8 100644 --- a/H/dhstruct.h +++ b/H/dhstruct.h @@ -51,6 +51,9 @@ #if USE_DL_MALLOC #define Yap_MemoryHoles Yap_heap_regs->memory_holes #define Yap_NOfMemoryHoles Yap_heap_regs->nof_memory_holes +#if defined(YAPOR) || defined(THREADS) +#define DLMallocLock Yap_heap_regs->dlmalloc_lock +#endif #endif #if USE_DL_MALLOC || (USE_SYSTEM_MALLOC && HAVE_MALLINFO) #ifndef HeapUsed diff --git a/H/hstruct.h b/H/hstruct.h index f1e81b579..2cc039914 100644 --- a/H/hstruct.h +++ b/H/hstruct.h @@ -51,6 +51,9 @@ #if USE_DL_MALLOC struct memory_hole memory_holes[MAX_DLMALLOC_HOLES]; UInt nof_memory_holes; +#if defined(YAPOR) || defined(THREADS) + lockvar dlmalloc_lock; +#endif #endif #if USE_DL_MALLOC || (USE_SYSTEM_MALLOC && HAVE_MALLINFO) #ifndef HeapUsed diff --git a/H/ihstruct.h b/H/ihstruct.h index dbf67fc45..d78c5c4ce 100644 --- a/H/ihstruct.h +++ b/H/ihstruct.h @@ -51,6 +51,9 @@ #if USE_DL_MALLOC +#if defined(YAPOR) || defined(THREADS) + INIT_LOCK(Yap_heap_regs->dlmalloc_lock); +#endif #endif #if USE_DL_MALLOC || (USE_SYSTEM_MALLOC && HAVE_MALLINFO) #ifndef HeapUsed diff --git a/H/rhstruct.h b/H/rhstruct.h index 4db075cf2..8f3d6947c 100644 --- a/H/rhstruct.h +++ b/H/rhstruct.h @@ -51,6 +51,9 @@ #if USE_DL_MALLOC +#if defined(YAPOR) || defined(THREADS) + REINIT_LOCK(Yap_heap_regs->dlmalloc_lock); +#endif #endif #if USE_DL_MALLOC || (USE_SYSTEM_MALLOC && HAVE_MALLINFO) #ifndef HeapUsed diff --git a/misc/HEAPFIELDS b/misc/HEAPFIELDS index 9148b7ed4..5e902781e 100644 --- a/misc/HEAPFIELDS +++ b/misc/HEAPFIELDS @@ -53,6 +53,9 @@ struct malloc_state *av_ Yap_av void void #if USE_DL_MALLOC struct memory_hole memory_holes[MAX_DLMALLOC_HOLES] Yap_MemoryHoles void void UInt nof_memory_holes Yap_NOfMemoryHoles void void +#if defined(YAPOR) || defined(THREADS) +lockvar dlmalloc_lock DLMallocLock MkLock +#endif #endif #if USE_DL_MALLOC || (USE_SYSTEM_MALLOC && HAVE_MALLINFO) #ifndef HeapUsed