background
Original link: http://blog.csdn.net/ordeder/article/details/31768749
Looking at the design of several memory pools, such as python,stl, there are basically two kinds of structures for memory management:
1.block, which is the memory block, is generally related to the size of the memory page (pagesize).
2.chunk, which is the memory shard, which allocates the memory space to be used on that block of memory.
For example, the implementation of the buffer pool used in Python's pyintobject: http://blog.csdn.net/ordeder/article/details/25343633
For the management of the memory pool, two kinds of linked lists are involved.
1. Memory block linked list of the pool
2. For the memory shards that are recycled after being used, the linked list will be replayed with the memory shards.
Memory pool for a memory request (k size), the user is given a memory space that is slightly greater than k in multiples of 2. For memory shards, a 2^ (i+2) (0<= i <=11) byte size is generally provided for different memory shards. Request for memory greater than 2^13b bytes, the memory pool is not provided service, but directly through the system's alloc allocation, of course, the release of this memory will not be recycled into the memory fragmented list (freelist).
memory context for PostgreSQL
Each memory pool is organized into a memory context by him, and multiple memory pools in the system are organized and maintained uniformly by a tree structure for these memory contexts. Here are some data structure diagrams (sketches) of his memory context:
Memorycontentdata: As a Memory context node, multiple nodes form the system's memory context tree.
Allocsetcontext: As the basic data structure of the memory pool, mainly includes the memory block list: *block, and the array of memory Shard list freelist[]
Allocblockdata: the memory block structure, as the header information of a memory block, where two char* pointers point to the memory address of the beginning and end of the memory block. Aset the token from which the memory block belongs to which Allocsetcontext.
Allocchunkdata: The Memory Shard structure (header), as the header information of the Shard, is immediately followed by the memory space of his specific shard. There is something interesting in the header information to belong to the void * aset pointer, if the Shard is idle (unassigned), then the Aset pointer will be used as the freelist, and as the next pointer points to the next free shard. Otherwise, the memory shard is allocated (not idle) then the Shard must not be in the freelist[] list, thus Aset records which memory block The Shard belongs to (Allocblockdata)