当前位置:网站首页>I Space distributor

I Space distributor

2022-06-25 20:02:00 Xiaohong_ coding

One . Space allocator

Allocate memory :

  • When a container needs space to hold elements , Space configurator required ( That is, the distributor ) Allocate memory , When the allocated memory is greater than 128 Bytes , Call the first level configurator , call malloc Allocate memory for it , When the allocated memory is less than 128 Bytes , Call the second level configurator , Check the corresponding free-list Is there a block available on , If any , Use directly , If not, call rfill.

  • rfill Workflow :

  • If I found free list After there are no available blocks in , From the memory pool ( adopt chunk_alloc) To get new blocks .

  • chunk_alloc Workflow :

  1. With end_free - start_free To determine whether there is memory , If there's enough memory , Then get 20 New nodes ( New area block ), When memory pool space is insufficient 20 individual , But satisfy one , Get less 20 Number of nodes , When memory pool No node can be assigned , Call the first level configurator from malloc Recapture a chunk of memory .

  2. At the end of time ,system heap There's not enough space ( Now the computer memory is very large , This rarely happens ), He has out-of-memory Processing mechanism ( similar new_handler Mechanism ), Free up memory elsewhere , Assign in turn , Call again malloc, If you can, you will succeed , If it fails, send out bad_alloc abnormal .

Reclaim memory :

  • First, determine the memory size , If memory is greater than 128 Bytes , Call free Reclaiming memory , If memory is less than 128 Bytes , Find out the corresponding free-list Reclaim blocks .

Be careful :malloc and free from os From there

First level configurator :

  1. allocate() Use it directly malloc(),deallocate() Use it directly free().
  2. simulation c ++ Of set_new_handler() To handle out of memory conditions .

Second level configurator :

  1. maintain 16 A free list (free list).

    • be responsible for 16 Configuration capability of small blocks .
    • Memory pool (memory) With malloc( ) From configuration . If there is not enough memory , Call the first level configurator ( There is memory processing ).
  2. If the demand block is greater than 128bytes, Then call the first level configurator .

  3. If the demand block is greater than 128bytes, Then call the first level configurator .

原网站

版权声明
本文为[Xiaohong_ coding]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190509390254.html