don't understand the meaning of member currnt_free_index in struct apr_allocator_t

63 Views Asked by At

I'am studying apr libiary source code, and I saw the struct apr_allocator_t:

    struct apr_allocator_t {
    /** largest used index into free[], always < MAX_INDEX */
    apr_size_t        max_index;
    /** Total size (in BOUNDARY_SIZE multiples) of unused memory before
     * blocks are given back. @see apr_allocator_max_free_set().
     * @note Initialized to APR_ALLOCATOR_MAX_FREE_UNLIMITED,
     * which means to never give back blocks.
     */
    apr_size_t        max_free_index;
    /**
     * Memory size (in BOUNDARY_SIZE multiples) that currently must be freed
     * before blocks are given back. Range: 0..max_free_index
     */
    apr_size_t        current_free_index;
 #if APR_HAS_THREADS
    apr_thread_mutex_t *mutex;
 #endif /* APR_HAS_THREADS */
    apr_pool_t         *owner;
    /**
     * Lists of free nodes. Slot 0 is used for oversized nodes,
     * and the slots 1..MAX_INDEX-1 contain nodes of sizes
     * (i+1) * BOUNDARY_SIZE. Example for BOUNDARY_INDEX == 12:
     * slot  0: nodes larger than 81920
     * slot  1: size  8192
     * slot  2: size 12288
     * ...
     * slot 19: size 81920
     */
    apr_memnode_t      *free[MAX_INDEX];
};

I'm confused with the member current_free_index (don't understand the comments) and the usage of it in function

APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator, apr_size_t in_size)
{
    ...
    max_free_index = APR_ALIGN(size, BOUNDARY_SIZE) >> BOUNDARY_INDEX;
    allocator->current_free_index += max_free_index;              // why it need to change here?
    allocator->current_free_index -= allocator->max_free_index;
    allocator->max_free_index = max_free_index;
    if (allocator->current_free_index > max_free_index)
        allocator->current_free_index = max_free_index;
    ...
}

and in function

static APR_INLINE
void allocator_free(apr_allocator_t *allocator, apr_memnode_t *node)
{
    ...
    current_free_index = allocator->current_free_index;
    ...
    index = node->index;
    if (max_free_index != APR_ALLOCATOR_MAX_FREE_UNLIMITED
            && index + 1 > current_free_index) {
            node->next = freelist;
            freelist = node;
        } // what "index + 1 > current_free_index" means?
    ...
}

is also seems to be strange to me

1

There are 1 best solutions below

0
Outcast On

Thinks to @kiner_shah, I fixed the problem. The answer is just like kiner_shah said:

current_free_index is the size of memory which must be freed in order for that memory to be reused again for allocating.

And the max_free_index is the user defined thredshold. In other words, current_free_index indicates how much memory size you need to free to reach the threshold for destorying the apr_memnode_t