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; }
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, bucket_size; if (beast_cache_initialization) { return 0; } if (beast_mm_init(size) == -1) { return -1; } /* init cache lock */ cache_lock = (int *)mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0); if (!cache_lock) { beast_write_log(beast_log_error, "Unable alloc share memory for cache lock"); beast_mm_destroy(); return -1; } *cache_lock = 0; /* init cache buckets's memory */ 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 share memory for cache buckets"); munmap(cache_lock, sizeof(int)); beast_mm_destroy(); return -1; } for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) { beast_cache_buckets[index] = NULL; } beast_cache_initialization = 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; }
int beast_cache_destroy() { int index; cache_item_t *item, *next; int pid = (int)getpid(); if (!beast_cache_initialization) { return 0; } beast_mm_destroy(); /* destroy memory manager */ /* free cache buckets's mmap memory */ munmap(beast_cache_buckets, sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE); munmap(cache_lock, sizeof(int)); beast_cache_initialization = 0; return 0; }