int beast_cache_init(int size) { int index; if (beast_cache_initialization) { return 0; } if (beast_mm_init(size) == -1) { return -1; } beast_cache_locker = beast_locker_create(); if (beast_cache_locker == -1) { beast_mm_destroy(); return -1; } beast_cache_buckets = beast_mm_malloc(sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE); if (!beast_cache_buckets) { beast_locker_destroy(beast_cache_locker); beast_mm_destroy(); return -1; } for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) { beast_cache_buckets[index] = NULL; } beast_cache_initialization = 1; return 0; }
/* * init memory manager */ int beast_mm_init(int block_size) { beast_header_t *header; beast_block_t *block; void *shmaddr; if (beast_mm_initialized) { return 0; } beast_mm_locker = beast_locker_create(); if (beast_mm_locker == -1) { return -1; } if (block_size < BEAST_SEGMENT_DEFAULT_SIZE) { beast_mm_block_size = BEAST_SEGMENT_DEFAULT_SIZE; } else { beast_mm_block_size = block_size; } shmaddr = beast_mm_block = (void *)mmap(NULL, beast_mm_block_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (!beast_mm_block) { return -1; } header = (beast_header_t *)beast_mm_block; header->segsize = beast_mm_block_size; header->avail = beast_mm_block_size - sizeof(beast_header_t) - sizeof(beast_block_t) - beast_mm_alignmem(sizeof(int)); /* avail size */ /* the free list head block node */ block = _BLOCKAT(sizeof(beast_header_t)); block->size = 0; block->next = sizeof(beast_header_t) + sizeof(beast_block_t); /* the avail block */ block = _BLOCKAT(block->next); block->size = header->avail; block->next = 0; beast_mm_initialized = 1; return 0; }
int beast_cache_init(int size) { int index, bucket_size; char lock_file[512]; if (beast_cache_initialization) { return 0; } if (beast_mm_init(size) == -1) { return -1; } sprintf(lock_file, "%s/beast.clock", beast_lock_path); beast_cache_locker = beast_locker_create(lock_file); if (beast_cache_locker == NULL) { beast_write_log(beast_log_error, "Unable create cache " "locker for beast"); beast_mm_destroy(); return -1; } bucket_size = sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE; beast_cache_buckets = (cache_item_t **)mmap(NULL, bucket_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0); if (!beast_cache_buckets) { beast_write_log(beast_log_error, "Unable alloc memory for beast"); beast_locker_destroy(beast_cache_locker); beast_mm_destroy(); return -1; } for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) { beast_cache_buckets[index] = NULL; } beast_cache_initialization = 1; return 0; }