void * suba_realloc(struct allocator *suba, void *ptr, size_t size) { struct cell *c; void *p; if (ptr == NULL) { if ((p = suba_alloc(suba, size, 0)) == NULL) { AMSG(""); } return p; } if (size == 0) { suba_free(suba, ptr); return NULL; } c = P2C(ptr); if (c->size < size || (c->size - ALIGN(size)) > suba->mincell) { p = suba_alloc(suba, size, 0); } else { return ptr; } if (p) { memcpy(p, ptr, size); suba_free(suba, ptr); } return p; }
/*! \brief Allocate Memory for Object Data with Object Data Heap. * For internal use only. * returns Index-Pointer relative to Heap begin. */ kogmo_rtdb_objsize_t kogmo_rtdb_obj_mem_alloc (kogmo_rtdb_handle_t *db_h, kogmo_rtdb_objsize_t size ) { void *ptr = NULL; void *base = db_h->localdata_p->heap; DBGL(DBGL_DB,"mem_alloc: heap pointers: base %p", db_h->localdata_p->heap); DBGL(DBGL_DB,"mem_alloc: %lli bytes (%lli used, %lli free)", (long long int) size, (long long int) db_h->localdata_p->heap_used, (long long int) db_h->localdata_p->heap_free); if ( size <= 0 ) return -1; // speedup: round size to page_size (achieved with tlsf parameters) kogmo_rtdb_heap_lock(db_h); #if defined(RTMALLOC_tlsf) ptr = malloc_ex (size, base); #elif defined(RTMALLOC_suba) ptr = suba_alloc(db_h->heapinfo, size, 0/* dont zero*/); #else if ( size <= db_h->localdata_p->heap_free ) { ptr = base + db_h->localdata_p->heap_used; } #endif DBG("mem_malloc: at %p",ptr); if ( ptr == NULL ) { kogmo_rtdb_heap_unlock(db_h); ERR("object mem_alloc failed for %i continuous bytes, %lli (discontinuous) bytes free", size, (long long int) db_h->localdata_p->heap_free ); return -1; } db_h->localdata_p->heap_free -= size; db_h->localdata_p->heap_used += size; kogmo_rtdb_heap_unlock(db_h); memset (ptr, 0, size); DBG("allocated mem cleared"); return ptr-base; }
void * allocator_alloc(struct allocator *al, size_t size, int zero) { void *p; if (!al) { al = global_allocator ? global_allocator : stdlib_allocator; } if (al->tail) { /* fn ptr in shared mem may be invalid */ p = suba_alloc(al, size, zero); } else { p = al->alloc(al, size, zero); } if (p == NULL) { AMSG(""); } return p; }
static void* chdk_malloc(chdk_heap *h, unsigned size) { if (h && h->heap) return suba_alloc(h->heap,size,0); return 0; }
void *malloc(unsigned size) { if(exmem_heap) return suba_alloc(exmem_heap,size,0); else return _malloc(size); }