static void calloc_func() { size_t pool_size = 0x40000; size_t malloc_size; /* element size 1 */ for(size_t i = 1; i < pool_size - 0x3e001; i++) { malloc_size = i; void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); char* mem = __calloc(malloc_size, 1, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); for(size_t j = 0; j < malloc_size; j++) assert_int_equal(0, (int)mem[j]); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } /* element size 4 */ for(size_t i = 1; i < (pool_size - 0x3e001) / 4; i++) { malloc_size = i; void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); int* mem = __calloc(malloc_size, 4, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); for(size_t j = 0; j < malloc_size; j++) assert_int_equal(0, (int)mem[j]); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } /* element size 8 */ for(size_t i = 1; i < (pool_size - 0x3e001) / 8; i++) { malloc_size = i; void* malloc_pool = malloc(pool_size); init_memory_pool(pool_size, malloc_pool, 0); uint64_t* mem = __calloc(malloc_size, 8, malloc_pool); assert_in_range(mem, malloc_pool, malloc_pool + pool_size); for(size_t j = 0; j < malloc_size; j++) assert_int_equal(0, mem[j]); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; } }
inline void* calloc(size_t nmemb, size_t size) { void* ptr = __calloc(nmemb, size, __malloc_pool); #if DEBUG malloced(((void**)read_rbp())[1], nmemb * size, ptr); #endif /* DEBUG */ return ptr; }
void *zcalloc(size_t count, size_t size) { size_t nSize = count * size; void *ptr = __calloc(count, size); if (!ptr) zmalloc_oom_handler(nSize); update_zmalloc_stat_alloc(__malloc_size(ptr)); return ptr; }