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