static ChunkArenaPtr findArena() { ChunkArenaPtr arena = NULL; int i; for(i = 0; i < numArenas; i++) { arena = &(chunkArenas[i]); if(arena->bitmap != 0) break; else arena = NULL; } assert(arena != NULL); if(!arena->chunks) { void *p; p = alloc_arena(CHUNK_SIZE * ARENA_CHUNKS); if(p == MAP_FAILED) { do_log_error(L_ERROR, errno, "Couldn't allocate chunk"); maybe_free_chunks(1, 1); return NULL; } arena->chunks = p; } return arena; }
void * get_chunk() { int i; ChunkArenaPtr arena = NULL; if(currentArena && currentArena->bitmap != 0) { arena = currentArena; } else { if(used_chunks >= CHUNKS(chunkHighMark)) maybe_free_chunks(0, 0); if(used_chunks >= CHUNKS(chunkHighMark)) return NULL; arena = findArena(); if(!arena) return NULL; currentArena = arena; } i = BITMAP_FFS(arena->bitmap) - 1; arena->bitmap &= ~BITMAP_BIT(i); used_chunks++; return arena->chunks + CHUNK_SIZE * i; }
void *get_chunk() { void *chunk; if (used_chunks > CHUNKS(chunkHighMark)) maybe_free_chunks(0, 0); if (used_chunks > CHUNKS(chunkHighMark)) return NULL; chunk = malloc(CHUNK_SIZE); if (!chunk) { maybe_free_chunks(1, 1); chunk = malloc(CHUNK_SIZE); if (!chunk) return NULL; } used_chunks++; return chunk; }