int beast_cache_destroy() { int index; cache_item_t *item, *next; if (!beast_cache_initialization) { return 0; } beast_locker_lock(beast_cache_locker); for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) { item = beast_cache_buckets[index]; while (item) { next = item->next; beast_mm_free(item); item = next; } } beast_mm_free(beast_cache_buckets); beast_mm_destroy(); beast_locker_unlock(beast_cache_locker); beast_locker_destroy(beast_cache_locker); beast_cache_initialization = 0; return 0; }
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; }
void beast_mm_destroy() { if (beast_mm_initialized) { munmap(beast_mm_block, beast_mm_block_size); beast_locker_destroy(beast_mm_locker); beast_mm_initialized = 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; }