EXPORT void* Smalloc( size_t size ) { void *p; MEMLOCK( return NULL ) p = _mem_malloc(size, &_Smacb); MEMUNLOCK() return p; }
/* * Memory allocate and clear */ static void *_mem_calloc( RAW_U32 nmemb, RAW_U32 size, MACB *macb ) { RAW_U32 sz = nmemb * size; void *p; /* Allocate memory */ p = _mem_malloc(sz, macb); if ( p == 0 ) { return 0; } /* Memory clear */ return raw_memset(p, 0, sz); }
RAW_VOID *raw_malloc(RAW_U32 size) { RAW_VOID *addr; MACB *macb = &malloc_macb; if (raw_int_nesting) { return 0; } raw_mutex_get(&macb->mem_lock, RAW_WAIT_FOREVER); addr = _mem_malloc(size, macb); raw_mutex_put(&macb->mem_lock); return addr; }
/* * Memory allocation size change */ static void *_mem_realloc( void *ptr, RAW_U32 size, MACB *macb ) { LIST *aq; RAW_U32 oldsz, sz; if ( macb->testmode > 0 ) { if ( !chkalloc(ptr, 0, macb) ) { return 0; } } /* If smaller than minimum fragment size, allocate minimum fragment size */ if ( size > 0 && size < MIN_FRAGMENT ) { size = MIN_FRAGMENT; } size = ROUND(size); aq = (LIST *)ptr - 1; if ( ptr != 0 ) { /* Current allocation size */ oldsz = (RAW_U32)AreaSize(aq); /* Merge if next space is free space */ if ( !chkAreaFlag(aq->next, AREA_END|AREA_USE) ) { removeFreeQue(aq->next + 1); removeAreaQue(aq->next); } sz = (RAW_U32)AreaSize(aq); } else { sz = oldsz = 0; } if ( size <= sz ) { if ( size > 0 ) { /* Fragment current area and allocate */ allocate(aq, size, macb); } else { /* Release area */ _mem_free(ptr, macb); ptr = 0; } } else { /* Allocate new area */ void *newptr = _mem_malloc(size, macb); if ( newptr == 0 ) { /* Reallocate original area at original size */ if ( ptr != 0 ) { allocate(aq, oldsz, macb); } return 0; } if ( ptr != 0 ) { /* Copy contents */ raw_memcpy(newptr, ptr, oldsz); /* Release old area */ _mem_free(ptr, macb); } ptr = newptr; } return ptr; }