michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** Thread safe versions of malloc, free, realloc, calloc and cfree. michael@0: */ michael@0: michael@0: #include "primpl.h" michael@0: michael@0: #ifdef _PR_ZONE_ALLOCATOR michael@0: michael@0: /* michael@0: ** The zone allocator code must use native mutexes and cannot michael@0: ** use PRLocks because PR_NewLock calls PR_Calloc, resulting michael@0: ** in cyclic dependency of initialization. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: union memBlkHdrUn; michael@0: michael@0: typedef struct MemoryZoneStr { michael@0: union memBlkHdrUn *head; /* free list */ michael@0: pthread_mutex_t lock; michael@0: size_t blockSize; /* size of blocks on this free list */ michael@0: PRUint32 locked; /* current state of lock */ michael@0: PRUint32 contention; /* counter: had to wait for lock */ michael@0: PRUint32 hits; /* allocated from free list */ michael@0: PRUint32 misses; /* had to call malloc */ michael@0: PRUint32 elements; /* on free list */ michael@0: } MemoryZone; michael@0: michael@0: typedef union memBlkHdrUn { michael@0: unsigned char filler[48]; /* fix the size of this beast */ michael@0: struct memBlkHdrStr { michael@0: union memBlkHdrUn *next; michael@0: MemoryZone *zone; michael@0: size_t blockSize; michael@0: size_t requestedSize; michael@0: PRUint32 magic; michael@0: } s; michael@0: } MemBlockHdr; michael@0: michael@0: #define MEM_ZONES 7 michael@0: #define THREAD_POOLS 11 /* prime number for modulus */ michael@0: #define ZONE_MAGIC 0x0BADC0DE michael@0: michael@0: static MemoryZone zones[MEM_ZONES][THREAD_POOLS]; michael@0: michael@0: static PRBool use_zone_allocator = PR_FALSE; michael@0: michael@0: static void pr_ZoneFree(void *ptr); michael@0: michael@0: void michael@0: _PR_DestroyZones(void) michael@0: { michael@0: int i, j; michael@0: michael@0: if (!use_zone_allocator) michael@0: return; michael@0: michael@0: for (j = 0; j < THREAD_POOLS; j++) { michael@0: for (i = 0; i < MEM_ZONES; i++) { michael@0: MemoryZone *mz = &zones[i][j]; michael@0: pthread_mutex_destroy(&mz->lock); michael@0: while (mz->head) { michael@0: MemBlockHdr *hdr = mz->head; michael@0: mz->head = hdr->s.next; /* unlink it */ michael@0: free(hdr); michael@0: mz->elements--; michael@0: } michael@0: } michael@0: } michael@0: use_zone_allocator = PR_FALSE; michael@0: } michael@0: michael@0: /* michael@0: ** pr_FindSymbolInProg michael@0: ** michael@0: ** Find the specified data symbol in the program and return michael@0: ** its address. michael@0: */ michael@0: michael@0: #ifdef HAVE_DLL michael@0: michael@0: #if defined(USE_DLFCN) && !defined(NO_DLOPEN_NULL) michael@0: michael@0: #include michael@0: michael@0: static void * michael@0: pr_FindSymbolInProg(const char *name) michael@0: { michael@0: void *h; michael@0: void *sym; michael@0: michael@0: h = dlopen(0, RTLD_LAZY); michael@0: if (h == NULL) michael@0: return NULL; michael@0: sym = dlsym(h, name); michael@0: (void)dlclose(h); michael@0: return sym; michael@0: } michael@0: michael@0: #elif defined(USE_HPSHL) michael@0: michael@0: #include michael@0: michael@0: static void * michael@0: pr_FindSymbolInProg(const char *name) michael@0: { michael@0: shl_t h = NULL; michael@0: void *sym; michael@0: michael@0: if (shl_findsym(&h, name, TYPE_DATA, &sym) == -1) michael@0: return NULL; michael@0: return sym; michael@0: } michael@0: michael@0: #elif defined(USE_MACH_DYLD) || defined(NO_DLOPEN_NULL) michael@0: michael@0: static void * michael@0: pr_FindSymbolInProg(const char *name) michael@0: { michael@0: /* FIXME: not implemented */ michael@0: return NULL; michael@0: } michael@0: michael@0: #else michael@0: michael@0: #error "The zone allocator is not supported on this platform" michael@0: michael@0: #endif michael@0: michael@0: #else /* !defined(HAVE_DLL) */ michael@0: michael@0: static void * michael@0: pr_FindSymbolInProg(const char *name) michael@0: { michael@0: /* can't be implemented */ michael@0: return NULL; michael@0: } michael@0: michael@0: #endif /* HAVE_DLL */ michael@0: michael@0: void michael@0: _PR_InitZones(void) michael@0: { michael@0: int i, j; michael@0: char *envp; michael@0: PRBool *sym; michael@0: michael@0: if ((sym = (PRBool *)pr_FindSymbolInProg("nspr_use_zone_allocator")) != NULL) { michael@0: use_zone_allocator = *sym; michael@0: } else if ((envp = getenv("NSPR_USE_ZONE_ALLOCATOR")) != NULL) { michael@0: use_zone_allocator = (atoi(envp) == 1); michael@0: } michael@0: michael@0: if (!use_zone_allocator) michael@0: return; michael@0: michael@0: for (j = 0; j < THREAD_POOLS; j++) { michael@0: for (i = 0; i < MEM_ZONES; i++) { michael@0: MemoryZone *mz = &zones[i][j]; michael@0: int rv = pthread_mutex_init(&mz->lock, NULL); michael@0: PR_ASSERT(0 == rv); michael@0: if (rv != 0) { michael@0: goto loser; michael@0: } michael@0: mz->blockSize = 16 << ( 2 * i); michael@0: } michael@0: } michael@0: return; michael@0: michael@0: loser: michael@0: _PR_DestroyZones(); michael@0: return; michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PR_FPrintZoneStats(PRFileDesc *debug_out) michael@0: { michael@0: int i, j; michael@0: michael@0: for (j = 0; j < THREAD_POOLS; j++) { michael@0: for (i = 0; i < MEM_ZONES; i++) { michael@0: MemoryZone *mz = &zones[i][j]; michael@0: MemoryZone zone = *mz; michael@0: if (zone.elements || zone.misses || zone.hits) { michael@0: PR_fprintf(debug_out, michael@0: "pool: %d, zone: %d, size: %d, free: %d, hit: %d, miss: %d, contend: %d\n", michael@0: j, i, zone.blockSize, zone.elements, michael@0: zone.hits, zone.misses, zone.contention); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void * michael@0: pr_ZoneMalloc(PRUint32 size) michael@0: { michael@0: void *rv; michael@0: unsigned int zone; michael@0: size_t blockSize; michael@0: MemBlockHdr *mb, *mt; michael@0: MemoryZone *mz; michael@0: michael@0: /* Always allocate a non-zero amount of bytes */ michael@0: if (size < 1) { michael@0: size = 1; michael@0: } michael@0: for (zone = 0, blockSize = 16; zone < MEM_ZONES; ++zone, blockSize <<= 2) { michael@0: if (size <= blockSize) { michael@0: break; michael@0: } michael@0: } michael@0: if (zone < MEM_ZONES) { michael@0: pthread_t me = pthread_self(); michael@0: unsigned int pool = (PRUptrdiff)me % THREAD_POOLS; michael@0: PRUint32 wasLocked; michael@0: mz = &zones[zone][pool]; michael@0: wasLocked = mz->locked; michael@0: pthread_mutex_lock(&mz->lock); michael@0: mz->locked = 1; michael@0: if (wasLocked) michael@0: mz->contention++; michael@0: if (mz->head) { michael@0: mb = mz->head; michael@0: PR_ASSERT(mb->s.magic == ZONE_MAGIC); michael@0: PR_ASSERT(mb->s.zone == mz); michael@0: PR_ASSERT(mb->s.blockSize == blockSize); michael@0: PR_ASSERT(mz->blockSize == blockSize); michael@0: michael@0: mt = (MemBlockHdr *)(((char *)(mb + 1)) + blockSize); michael@0: PR_ASSERT(mt->s.magic == ZONE_MAGIC); michael@0: PR_ASSERT(mt->s.zone == mz); michael@0: PR_ASSERT(mt->s.blockSize == blockSize); michael@0: michael@0: mz->hits++; michael@0: mz->elements--; michael@0: mz->head = mb->s.next; /* take off free list */ michael@0: mz->locked = 0; michael@0: pthread_mutex_unlock(&mz->lock); michael@0: michael@0: mt->s.next = mb->s.next = NULL; michael@0: mt->s.requestedSize = mb->s.requestedSize = size; michael@0: michael@0: rv = (void *)(mb + 1); michael@0: return rv; michael@0: } michael@0: michael@0: mz->misses++; michael@0: mz->locked = 0; michael@0: pthread_mutex_unlock(&mz->lock); michael@0: michael@0: mb = (MemBlockHdr *)malloc(blockSize + 2 * (sizeof *mb)); michael@0: if (!mb) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: mb->s.next = NULL; michael@0: mb->s.zone = mz; michael@0: mb->s.magic = ZONE_MAGIC; michael@0: mb->s.blockSize = blockSize; michael@0: mb->s.requestedSize = size; michael@0: michael@0: mt = (MemBlockHdr *)(((char *)(mb + 1)) + blockSize); michael@0: memcpy(mt, mb, sizeof *mb); michael@0: michael@0: rv = (void *)(mb + 1); michael@0: return rv; michael@0: } michael@0: michael@0: /* size was too big. Create a block with no zone */ michael@0: blockSize = (size & 15) ? size + 16 - (size & 15) : size; michael@0: mb = (MemBlockHdr *)malloc(blockSize + 2 * (sizeof *mb)); michael@0: if (!mb) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: mb->s.next = NULL; michael@0: mb->s.zone = NULL; michael@0: mb->s.magic = ZONE_MAGIC; michael@0: mb->s.blockSize = blockSize; michael@0: mb->s.requestedSize = size; michael@0: michael@0: mt = (MemBlockHdr *)(((char *)(mb + 1)) + blockSize); michael@0: memcpy(mt, mb, sizeof *mb); michael@0: michael@0: rv = (void *)(mb + 1); michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: static void * michael@0: pr_ZoneCalloc(PRUint32 nelem, PRUint32 elsize) michael@0: { michael@0: PRUint32 size = nelem * elsize; michael@0: void *p = pr_ZoneMalloc(size); michael@0: if (p) { michael@0: memset(p, 0, size); michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: static void * michael@0: pr_ZoneRealloc(void *oldptr, PRUint32 bytes) michael@0: { michael@0: void *rv; michael@0: MemBlockHdr *mb; michael@0: int ours; michael@0: MemBlockHdr phony; michael@0: michael@0: if (!oldptr) michael@0: return pr_ZoneMalloc(bytes); michael@0: mb = (MemBlockHdr *)((char *)oldptr - (sizeof *mb)); michael@0: if (mb->s.magic != ZONE_MAGIC) { michael@0: /* Maybe this just came from ordinary malloc */ michael@0: #ifdef DEBUG michael@0: fprintf(stderr, michael@0: "Warning: reallocing memory block %p from ordinary malloc\n", michael@0: oldptr); michael@0: #endif michael@0: /* michael@0: * We are going to realloc oldptr. If realloc succeeds, the michael@0: * original value of oldptr will point to freed memory. So this michael@0: * function must not fail after a successfull realloc call. We michael@0: * must perform any operation that may fail before the realloc michael@0: * call. michael@0: */ michael@0: rv = pr_ZoneMalloc(bytes); /* this may fail */ michael@0: if (!rv) { michael@0: return rv; michael@0: } michael@0: michael@0: /* We don't know how big it is. But we can fix that. */ michael@0: oldptr = realloc(oldptr, bytes); michael@0: /* michael@0: * If realloc returns NULL, this function loses the original michael@0: * value of oldptr. This isn't a leak because the caller of michael@0: * this function still has the original value of oldptr. michael@0: */ michael@0: if (!oldptr) { michael@0: if (bytes) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: pr_ZoneFree(rv); michael@0: return oldptr; michael@0: } michael@0: } michael@0: phony.s.requestedSize = bytes; michael@0: mb = &phony; michael@0: ours = 0; michael@0: } else { michael@0: size_t blockSize = mb->s.blockSize; michael@0: MemBlockHdr *mt = (MemBlockHdr *)(((char *)(mb + 1)) + blockSize); michael@0: michael@0: PR_ASSERT(mt->s.magic == ZONE_MAGIC); michael@0: PR_ASSERT(mt->s.zone == mb->s.zone); michael@0: PR_ASSERT(mt->s.blockSize == blockSize); michael@0: michael@0: if (bytes <= blockSize) { michael@0: /* The block is already big enough. */ michael@0: mt->s.requestedSize = mb->s.requestedSize = bytes; michael@0: return oldptr; michael@0: } michael@0: ours = 1; michael@0: rv = pr_ZoneMalloc(bytes); michael@0: if (!rv) { michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: if (oldptr && mb->s.requestedSize) michael@0: memcpy(rv, oldptr, mb->s.requestedSize); michael@0: if (ours) michael@0: pr_ZoneFree(oldptr); michael@0: else if (oldptr) michael@0: free(oldptr); michael@0: return rv; michael@0: } michael@0: michael@0: static void michael@0: pr_ZoneFree(void *ptr) michael@0: { michael@0: MemBlockHdr *mb, *mt; michael@0: MemoryZone *mz; michael@0: size_t blockSize; michael@0: PRUint32 wasLocked; michael@0: michael@0: if (!ptr) michael@0: return; michael@0: michael@0: mb = (MemBlockHdr *)((char *)ptr - (sizeof *mb)); michael@0: michael@0: if (mb->s.magic != ZONE_MAGIC) { michael@0: /* maybe this came from ordinary malloc */ michael@0: #ifdef DEBUG michael@0: fprintf(stderr, michael@0: "Warning: freeing memory block %p from ordinary malloc\n", ptr); michael@0: #endif michael@0: free(ptr); michael@0: return; michael@0: } michael@0: michael@0: blockSize = mb->s.blockSize; michael@0: mz = mb->s.zone; michael@0: mt = (MemBlockHdr *)(((char *)(mb + 1)) + blockSize); michael@0: PR_ASSERT(mt->s.magic == ZONE_MAGIC); michael@0: PR_ASSERT(mt->s.zone == mz); michael@0: PR_ASSERT(mt->s.blockSize == blockSize); michael@0: if (!mz) { michael@0: PR_ASSERT(blockSize > 65536); michael@0: /* This block was not in any zone. Just free it. */ michael@0: free(mb); michael@0: return; michael@0: } michael@0: PR_ASSERT(mz->blockSize == blockSize); michael@0: wasLocked = mz->locked; michael@0: pthread_mutex_lock(&mz->lock); michael@0: mz->locked = 1; michael@0: if (wasLocked) michael@0: mz->contention++; michael@0: mt->s.next = mb->s.next = mz->head; /* put on head of list */ michael@0: mz->head = mb; michael@0: mz->elements++; michael@0: mz->locked = 0; michael@0: pthread_mutex_unlock(&mz->lock); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void *) PR_Malloc(PRUint32 size) michael@0: { michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: return use_zone_allocator ? pr_ZoneMalloc(size) : malloc(size); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize) michael@0: { michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: return use_zone_allocator ? michael@0: pr_ZoneCalloc(nelem, elsize) : calloc(nelem, elsize); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void *) PR_Realloc(void *ptr, PRUint32 size) michael@0: { michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: return use_zone_allocator ? pr_ZoneRealloc(ptr, size) : realloc(ptr, size); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_Free(void *ptr) michael@0: { michael@0: if (use_zone_allocator) michael@0: pr_ZoneFree(ptr); michael@0: else michael@0: free(ptr); michael@0: } michael@0: michael@0: #else /* !defined(_PR_ZONE_ALLOCATOR) */ michael@0: michael@0: /* michael@0: ** The PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free functions simply michael@0: ** call their libc equivalents now. This may seem redundant, but it michael@0: ** ensures that we are calling into the same runtime library. On michael@0: ** Win32, it is possible to have multiple runtime libraries (e.g., michael@0: ** objects compiled with /MD and /MDd) in the same process, and michael@0: ** they maintain separate heaps, which cannot be mixed. michael@0: */ michael@0: PR_IMPLEMENT(void *) PR_Malloc(PRUint32 size) michael@0: { michael@0: #if defined (WIN16) michael@0: return PR_MD_malloc( (size_t) size); michael@0: #else michael@0: return malloc(size); michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize) michael@0: { michael@0: #if defined (WIN16) michael@0: return PR_MD_calloc( (size_t)nelem, (size_t)elsize ); michael@0: michael@0: #else michael@0: return calloc(nelem, elsize); michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(void *) PR_Realloc(void *ptr, PRUint32 size) michael@0: { michael@0: #if defined (WIN16) michael@0: return PR_MD_realloc( ptr, (size_t) size); michael@0: #else michael@0: return realloc(ptr, size); michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_Free(void *ptr) michael@0: { michael@0: #if defined (WIN16) michael@0: PR_MD_free( ptr ); michael@0: #else michael@0: free(ptr); michael@0: #endif michael@0: } michael@0: michael@0: #endif /* _PR_ZONE_ALLOCATOR */ michael@0: michael@0: /* michael@0: ** Complexity alert! michael@0: ** michael@0: ** If malloc/calloc/free (etc.) were implemented to use pr lock's then michael@0: ** the entry points could block when called if some other thread had the michael@0: ** lock. michael@0: ** michael@0: ** Most of the time this isn't a problem. However, in the case that we michael@0: ** are using the thread safe malloc code after PR_Init but before michael@0: ** PR_AttachThread has been called (on a native thread that nspr has yet michael@0: ** to be told about) we could get royally screwed if the lock was busy michael@0: ** and we tried to context switch the thread away. In this scenario michael@0: ** PR_CURRENT_THREAD() == NULL michael@0: ** michael@0: ** To avoid this unfortunate case, we use the low level locking michael@0: ** facilities for malloc protection instead of the slightly higher level michael@0: ** locking. This makes malloc somewhat faster so maybe it's a good thing michael@0: ** anyway. michael@0: */ michael@0: #ifdef _PR_OVERRIDE_MALLOC michael@0: michael@0: /* Imports */ michael@0: extern void *_PR_UnlockedMalloc(size_t size); michael@0: extern void *_PR_UnlockedMemalign(size_t alignment, size_t size); michael@0: extern void _PR_UnlockedFree(void *ptr); michael@0: extern void *_PR_UnlockedRealloc(void *ptr, size_t size); michael@0: extern void *_PR_UnlockedCalloc(size_t n, size_t elsize); michael@0: michael@0: static PRBool _PR_malloc_initialised = PR_FALSE; michael@0: michael@0: #ifdef _PR_PTHREADS michael@0: static pthread_mutex_t _PR_MD_malloc_crustylock; michael@0: michael@0: #define _PR_Lock_Malloc() { \ michael@0: if(PR_TRUE == _PR_malloc_initialised) { \ michael@0: PRStatus rv; \ michael@0: rv = pthread_mutex_lock(&_PR_MD_malloc_crustylock); \ michael@0: PR_ASSERT(0 == rv); \ michael@0: } michael@0: michael@0: #define _PR_Unlock_Malloc() if(PR_TRUE == _PR_malloc_initialised) { \ michael@0: PRStatus rv; \ michael@0: rv = pthread_mutex_unlock(&_PR_MD_malloc_crustylock); \ michael@0: PR_ASSERT(0 == rv); \ michael@0: } \ michael@0: } michael@0: #else /* _PR_PTHREADS */ michael@0: static _MDLock _PR_MD_malloc_crustylock; michael@0: michael@0: #ifdef IRIX michael@0: #define _PR_Lock_Malloc() { \ michael@0: PRIntn _is; \ michael@0: if(PR_TRUE == _PR_malloc_initialised) { \ michael@0: if (_PR_MD_GET_ATTACHED_THREAD() && \ michael@0: !_PR_IS_NATIVE_THREAD( \ michael@0: _PR_MD_GET_ATTACHED_THREAD())) \ michael@0: _PR_INTSOFF(_is); \ michael@0: _PR_MD_LOCK(&_PR_MD_malloc_crustylock); \ michael@0: } michael@0: michael@0: #define _PR_Unlock_Malloc() if(PR_TRUE == _PR_malloc_initialised) { \ michael@0: _PR_MD_UNLOCK(&_PR_MD_malloc_crustylock); \ michael@0: if (_PR_MD_GET_ATTACHED_THREAD() && \ michael@0: !_PR_IS_NATIVE_THREAD( \ michael@0: _PR_MD_GET_ATTACHED_THREAD())) \ michael@0: _PR_INTSON(_is); \ michael@0: } \ michael@0: } michael@0: #else /* IRIX */ michael@0: #define _PR_Lock_Malloc() { \ michael@0: PRIntn _is; \ michael@0: if(PR_TRUE == _PR_malloc_initialised) { \ michael@0: if (_PR_MD_CURRENT_THREAD() && \ michael@0: !_PR_IS_NATIVE_THREAD( \ michael@0: _PR_MD_CURRENT_THREAD())) \ michael@0: _PR_INTSOFF(_is); \ michael@0: _PR_MD_LOCK(&_PR_MD_malloc_crustylock); \ michael@0: } michael@0: michael@0: #define _PR_Unlock_Malloc() if(PR_TRUE == _PR_malloc_initialised) { \ michael@0: _PR_MD_UNLOCK(&_PR_MD_malloc_crustylock); \ michael@0: if (_PR_MD_CURRENT_THREAD() && \ michael@0: !_PR_IS_NATIVE_THREAD( \ michael@0: _PR_MD_CURRENT_THREAD())) \ michael@0: _PR_INTSON(_is); \ michael@0: } \ michael@0: } michael@0: #endif /* IRIX */ michael@0: #endif /* _PR_PTHREADS */ michael@0: michael@0: PR_IMPLEMENT(PRStatus) _PR_MallocInit(void) michael@0: { michael@0: PRStatus rv = PR_SUCCESS; michael@0: michael@0: if( PR_TRUE == _PR_malloc_initialised ) return PR_SUCCESS; michael@0: michael@0: #ifdef _PR_PTHREADS michael@0: { michael@0: int status; michael@0: pthread_mutexattr_t mattr; michael@0: michael@0: status = _PT_PTHREAD_MUTEXATTR_INIT(&mattr); michael@0: PR_ASSERT(0 == status); michael@0: status = _PT_PTHREAD_MUTEX_INIT(_PR_MD_malloc_crustylock, mattr); michael@0: PR_ASSERT(0 == status); michael@0: status = _PT_PTHREAD_MUTEXATTR_DESTROY(&mattr); michael@0: PR_ASSERT(0 == status); michael@0: } michael@0: #else /* _PR_PTHREADS */ michael@0: _MD_NEW_LOCK(&_PR_MD_malloc_crustylock); michael@0: #endif /* _PR_PTHREADS */ michael@0: michael@0: if( PR_SUCCESS == rv ) michael@0: { michael@0: _PR_malloc_initialised = PR_TRUE; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: void *malloc(size_t size) michael@0: { michael@0: void *p; michael@0: _PR_Lock_Malloc(); michael@0: p = _PR_UnlockedMalloc(size); michael@0: _PR_Unlock_Malloc(); michael@0: return p; michael@0: } michael@0: michael@0: #if defined(IRIX) michael@0: void *memalign(size_t alignment, size_t size) michael@0: { michael@0: void *p; michael@0: _PR_Lock_Malloc(); michael@0: p = _PR_UnlockedMemalign(alignment, size); michael@0: _PR_Unlock_Malloc(); michael@0: return p; michael@0: } michael@0: michael@0: void *valloc(size_t size) michael@0: { michael@0: return(memalign(sysconf(_SC_PAGESIZE),size)); michael@0: } michael@0: #endif /* IRIX */ michael@0: michael@0: void free(void *ptr) michael@0: { michael@0: _PR_Lock_Malloc(); michael@0: _PR_UnlockedFree(ptr); michael@0: _PR_Unlock_Malloc(); michael@0: } michael@0: michael@0: void *realloc(void *ptr, size_t size) michael@0: { michael@0: void *p; michael@0: _PR_Lock_Malloc(); michael@0: p = _PR_UnlockedRealloc(ptr, size); michael@0: _PR_Unlock_Malloc(); michael@0: return p; michael@0: } michael@0: michael@0: void *calloc(size_t n, size_t elsize) michael@0: { michael@0: void *p; michael@0: _PR_Lock_Malloc(); michael@0: p = _PR_UnlockedCalloc(n, elsize); michael@0: _PR_Unlock_Malloc(); michael@0: return p; michael@0: } michael@0: michael@0: void cfree(void *p) michael@0: { michael@0: _PR_Lock_Malloc(); michael@0: _PR_UnlockedFree(p); michael@0: _PR_Unlock_Malloc(); michael@0: } michael@0: michael@0: void _PR_InitMem(void) michael@0: { michael@0: PRStatus rv; michael@0: rv = _PR_MallocInit(); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: } michael@0: michael@0: #endif /* _PR_OVERRIDE_MALLOC */