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 initChunks(void) { int i; used_chunks = 0; initChunksCommon(); pagesize = getpagesize(); if((CHUNK_SIZE * ARENA_CHUNKS) % pagesize != 0) { do_log(L_ERROR, "The arena size %d (%d x %d) " "is not a multiple of the page size %d.\n", ARENA_CHUNKS * CHUNK_SIZE, ARENA_CHUNKS, CHUNK_SIZE, pagesize); abort(); } numArenas = (CHUNKS(chunkHighMark) + (ARENA_CHUNKS - 1)) / ARENA_CHUNKS; chunkArenas = malloc(numArenas * sizeof(ChunkArenaRec)); if(chunkArenas == NULL) { do_log(L_ERROR, "Couldn't allocate chunk arenas.\n"); polipoExit(); } for(i = 0; i < numArenas; i++) { chunkArenas[i].bitmap = EMPTY_BITMAP; chunkArenas[i].chunks = NULL; } currentArena = NULL; }
static void maybe_free_chunks(int arenas, int force) { if (force || used_chunks >= CHUNKS(chunkHighMark)) { discardObjects(force, force); } if (arenas) free_chunk_arenas(); if (used_chunks >= CHUNKS(chunkLowMark) && !objectExpiryScheduled) { TimeEventHandlerPtr event; event = scheduleTimeEvent(1, discardObjectsHandler, 0, NULL); if (event) objectExpiryScheduled = 1; } }
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; }
void *maybe_get_chunk() { void *chunk; if (used_chunks > CHUNKS(chunkHighMark)) return NULL; chunk = malloc(CHUNK_SIZE); if (chunk) used_chunks++; return chunk; }