0

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

Outcast
  • 49
  • 4
  • `current_free_index` is the size of memory which must be freed in order for that memory to be reused again for allocating. – kiner_shah Jan 18 '22 at 06:40
  • I think I might understand the question in second funcion. The condition **index + 1 > current_free_index** is true means the actual reuesd memory have been bigger than **max_free_index**. But I still don't know the why the **current_free_index** changed when I setting **max_free_index** (question in the first function) @kiner_shah – Outcast Jan 18 '22 at 08:54
  • This code is unclear to me. I have no idea what this code is about. Just guessing, but seems like some kind of memory pool. Without looking at the whole code and understanding this allocator data structure, I cannot comment what this code is doing. – kiner_shah Jan 18 '22 at 09:49

1 Answers1

0

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

Outcast
  • 49
  • 4