Chapter 2: Space configurator Allocator
SGI Special Space configurator, STD: alloc
SGI configures and releases memory based on malloc () and free.
SGI has designed a dual-layer configurator:
The first-level configurator directly uses malloc () and free (); _ malloc_alloc_template
The second-level provisioner uses the unused policy as needed: _ default_alloc_template
When the size of the configuration block exceeds bytes, the first-level Configurator is called;
When the configuration block is smaller than bytes, it is regarded as "too small". To reduce the extra burden, you can use
Use a complex memory pool arrangement instead of the first-level configurator.
Interface encapsulated by SGI: simple_alloc
1 template<class T, class Alloc> 2 class simp_alloc { 3 public: 4 static T *allocate(size_t n) { 5 return 0 ==n ? 0 : (T*)Alloc:: allocate(n * sizeof(T)); 6 } 7 static T *allocate(void) { 8 return (T*)Alloc::allocate(n * sizeof(T)); 9 }10 static void deallocate(T *p, size_t n) {11 if(0 !=n) Alloc::deallocate(p, n* sizeof(T));12 }13 static T *deallocate(T *p) {14 Alloc::deallocate(p * sizeof(T));15 } 16 }17 18 template<class T, class Alloc = alloc) 19 class vector {20 typedef simp_alloc<value_type, Alloc> data_allocator; 21 void deallocate() {22 if(...)23 data_allocator::deallocate(start,end_of_storage - start);24 }25 ...26 }
The second-level SGI configurator will take the initiative to increase the memory demand for any small block to a multiple of 8 (for example, the client requires 30 bytes,
Automatically adjusted to 32 bytes), and maintained 16 free-lists, each of which is 8, 16, 24, 32, 40, 48, 56, respectively,
A small block of 64, 72, 80, 88, and 96,104,112,120,128.
The node Structure of freelist is as follows:
union obj { union obj* free_list_link; char client_data[1];};
Refill free lists. When no available blocks are found in the Free List, call refill () to re-fill the space for the Free List.
The new space will be taken from the memory pool (via chunk_alloc ). 20 new nodes (new blocks) are obtained by default, but in case of Memory Pool
Insufficient space. The number of nodes (number of blocks) obtained may be less than 20.
Allocation Policy:
Assume that the client calls chunk_alloc () at the beginning, and malloc () configures 40 32bytes blocks.
One hand (for use, because there is a request), the other 19 are handed over to the Free List [3] for maintenance, and the remaining 20 are left to the memory pool. Next, the customer
At this time, free_list [7] is empty and must be supported by the memory pool. The memory pool is sufficient.
(32*20)/64 = 10 64bytes blocks, the 10 blocks are returned, the first is handed over to the client, and the remaining 9 are returned by free_list [11]
Maintenance. The memory pool is empty. Next, call chunk_alloc (). In this case, free_list [11] is empty and must be directed to the memory.
The pool must be supported, while the memory pool is empty. In malloc (), 40 + N (additional volume) 96 bytes blocks are configured, the first of which is
Hand over the other 19 to free_list [11] for maintenance, and the remaining 20 + blocks (additional volume) are left to the memory pool.
If the system heap space is insufficient, malloc () will fail, and chunk_alloc () will be searched
Free lists, which has unused blocks and the blocks are large enough, will be handed over when it is found. If it cannot be found, the first-level configurator will be called. The first-level Configurator is also used
But it has an out-of-memory processing mechanism. You may have a chance to release the memory that is already in use. Otherwise
An error occurred while listing bad_alloc.
Review the second chapter of STL source code analysis