APR Pool內存管理策略是以memnode爲單位整存零取
先看看memnode的定義:
struct apr_memnode_t {
apr_memnode_t *next;
apr_memnode_t **ref;
apr_uint32_t index;
apr_uint32_t free_index;
char *first_avail;
char *endp;
};
memnode分爲兩種:正在使用的和已釋放的
正在使用的memnode利用next和ref兩個字段組織爲一個雙向的環:
最有趣的在於ref字段
從定義上可以看到這是一個指向指針的指針
實際上它指向環中上一個memnode節點的next字段
上一個節點的next字段指向哪兒呢?
就是ref所在memnode的地址
所以註釋說是"reference to self"
知道了數據結構,操作鏈表的算法也就不言自明瞭:
#define list_insert(node, point) do { \
node->ref = point->ref; \
*node->ref = node; \
node->next = point; \
point->ref = &node->next; \
} while (0)
#define list_remove(node) do { \
*node->ref = node->next; \
node->next->ref = node->ref; \
} while (0)
已被應用程序釋放的memnode被回收在allocator的free數組中
先看allocator的定義
#define MIN_ALLOC 8192
#define MAX_INDEX 20
struct apr_allocator_t {
apr_uint32_t max_index;
apr_uint32_t max_free_index;
apr_uint32_t current_free_index;
apr_thread_mutex_t *mutex;
apr_pool_t *owner;
apr_memnode_t *free[MAX_INDEX];
};
每個free數組元素是一個用memnode的next字段串起來的鏈表
(鏈尾節點的next字段被設置爲NULL)
memnode根據自身的大小被放入free數組中相應位置的鏈表中
free[1]下掛的memnode大小爲212(即上面定義的MIN_ALLOC)
以後以2爲冪遞增
超過數組允許最大值的(230)則放入free[0]中
這裏有趣的地方在於賦予了數組的位置以尋址以外的含義
就像阿拉伯數字所做的那樣,不過是以2爲進制而已