Jdesk / memcached

Automatically exported from code.google.com/p/memcached
0 stars 0 forks source link

question of cache_create #143

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi guys:

1. function cache_create in cache.c:

cache_t* cache_create(const char *name, size_t bufsize, size_t align,
                      cache_constructor_t* constructor,
                      cache_destructor_t* destructor) {
    cache_t* ret = calloc(1, sizeof(cache_t));
    char* nm = strdup(name);
    void** ptr = calloc(initial_pool_size, bufsize); ====>question?
    if (ret == NULL || nm == NULL || ptr == NULL ||
        pthread_mutex_init(&ret->mutex, NULL) == -1) {
        free(ret);
        free(nm);
        free(ptr);
        return NULL;
    }

    ret->name = nm;
    ret->ptr = ptr;
    ret->freetotal = initial_pool_size;
    ret->constructor = constructor;
    ret->destructor = destructor;

#ifndef NDEBUG
    ret->bufsize = bufsize + 2 * sizeof(redzone_pattern);
#else
    ret->bufsize = bufsize;
#endif

    return ret;
} 

   the line should be:  void** ptr = calloc(initial_pool_size,
sizeof(void*));             ???

2.function cache_free:
void cache_free(cache_t *cache, void *ptr) {
    pthread_mutex_lock(&cache->mutex);

#ifndef NDEBUG
 ...
#endif
    if (cache->freecurr < cache->freetotal) {
        cache->ptr[cache->freecurr++] = ptr;
    } else {
        /* try to enlarge free connections array */                      --|
        size_t newtotal = cache->freetotal * 2;                          ? |
        void **new_free = realloc(cache->ptr, sizeof(char *) * newtotal);--|
        if (new_free) {
            cache->freetotal = newtotal;
            cache->ptr = new_free;
            cache->ptr[cache->freecurr++] = ptr;
        } else {
            if (cache->destructor) {
                cache->destructor(ptr, NULL);
            }
            free(ptr);

        }
    }
    pthread_mutex_unlock(&cache->mutex); 
}
  why not add a constraint with the total free objects?

Original issue reported on code.google.com by weare.th...@gmail.com on 3 Jun 2010 at 3:57

GoogleCodeExporter commented 9 years ago
"question of cache_create" is not an actionable bug.  It's unclear whether you 
actually have some bad behavior 
you're trying to fix or just want to know more about the intent of the code.  
You also seem to be asking two 
questions.  Have you found two bugs?

Please rephrase this in the form of an action that must be taken to improve the 
code.

Original comment by dsalli...@gmail.com on 3 Jun 2010 at 4:56