apapche内存池疑问

    技术2022-05-13  26

    apache的apr_allocator_t结构体有一个变量叫做current_free_index,这个东东似乎表明的是超过这个值的内存,都得给释放到系统,在这个值以内的内存可以一直保存。

    不过在allocator_alloc函数有这么一段 if ((*ref = node->next) == NULL && i >= max_index) { do { ref--; max_index--; } while (*ref == NULL && max_index > 0); allocator->max_index = max_index; } allocator->current_free_index += node->index; if (allocator->current_free_index > allocator->max_free_index) allocator->current_free_index = allocator->max_free_index;  

     

    就是分配内存的时候先从apache分配子里面寻找是否有可用的内存,找到之后呢,为什么要出现

    allocator->current_free_index += node->index;

    这么一句?还不明白,node是apr_memnode_t类型,这个index又是什么呢? 只能等后面再看看源码的了。。。 现在看来,current_free_index 是事实上的最大的不会释放的内存,如果某个index超过这个数字,那么就会返给系统了 node的index指的是node大小的那个index,但是为什么是current_free_index 加上这个还是不明白!!! 还有一个问题,allocator_alloc的时候有个寻找可用的分配子的过程,就是 while (*ref == NULL && i < max_index) { ref++; i++; } 这个地方用这些代码寻找的时候,为什么都不判断对应的分配子里面是否有内存被占用了呢?是否这个里面的分配子都是完全没有使用过的? 似乎之前的方向搞错了,allocator_alloc不是真正的分配内存,只是寻找分配子,如果当前内存池里面还有的话就用当前内存池里面的,如果当前内存池里面没有了就去找系统要这么大的一个分配子了。 真正对外分配内存的是allocator_palloc函数。

    最新回复(0)