Пример #1
0
cache_item_t *beast_cache_create(cache_key_t *key, int size)
{
    cache_item_t *item, *next;
    int i, msize, bsize;
    int pid = (int)getpid();

    msize = sizeof(*item) + size;
    bsize = sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE;

    if ((msize + bsize) > beast_mm_realspace()) {
        beast_write_log(beast_log_error, "Cache item size too big");
        return NULL;
    }

    item = beast_mm_malloc(msize);

    if (!item) {

#if 0
        int index;

        /* clean all caches */

        beast_spinlock(cache_lock, pid);

        for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
            beast_cache_buckets[index] = NULL;
        }

        beast_mm_flush();

        beast_spinunlock(cache_lock, pid);

        item = beast_mm_malloc(msize);
        if (!item) {
            return NULL;
        }
#endif

        beast_write_log(beast_log_notice, "Not enough caches, "
            "please setting <beast.cache_size> bigger in `php.ini' file");
        return NULL;
    }

    item->key.device = key->device;
    item->key.inode = key->inode;
    item->key.fsize = key->fsize;
    item->key.mtime = key->mtime;
    item->next = NULL;

    return item;
}
Пример #2
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;
}
Пример #3
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;
}
Пример #4
0
cache_item_t *beast_cache_create(cache_key_t *key, int size)
{
    cache_item_t *item, *next;
    int i, msize, bsize;

    msize = sizeof(*item) + size;
    bsize = sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE;

    if ((msize + bsize) > beast_mm_realspace()) {
        beast_write_log(beast_log_error, "Cache item size too big");
        return NULL;
    }

    item = beast_mm_malloc(msize);
    if (!item)
    {
        int index;
        
        beast_locker_lock(beast_cache_locker);
        
        for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
            beast_cache_buckets[index] = NULL;
        }

        beast_locker_unlock(beast_cache_locker);

        beast_mm_flush(); /* clean all caches */

        item = beast_mm_malloc(msize);
        if (!item) {
            return NULL;
        }
    }

    item->key.device = key->device;
    item->key.inode = key->inode;
    item->key.fsize = key->fsize;
    item->key.mtime = key->mtime;
    item->next = NULL;

    return item;
}