In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to realize the simple memory pool in C++ memory management". In the daily operation, I believe many people have doubts about how to realize the simple memory pool in C++ memory management. I have consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize the simple memory pool in C++ memory management". Next, please follow the editor to study!
What is a memory pool?
Frequent calls to malloc will affect the running efficiency and generate additional cookie, while the idea of the memory pool is to apply for a large block of memory in advance and allocate a piece of memory from the memory pool to the target object when there is a memory request.
Its implementation process is as follows:
Apply for a chunk-sized memory pool in advance and divide the memory pool into multiple memory blocks according to the object size. In the form of a linked list, that is, the memory block is connected by a pointer, and the head pointer points to the first free block. When there is a memory request, first check whether the header pointer points to the free block, if so, allocate the first free block pointed to by the header pointer (removed from the linked list), and point to the next free block; if the header pointer is empty, it means that the current memory pool has been allocated, and you need to reapply for a new memory pool. When there is a need for memory release, the free memory block is re-added to the header of the linked list, and the header pointer is adjusted to point to the newly added free block. This also means that if multiple memory pools are requested, they will slowly merge together during the memory release process.
Preliminary realization
In the above code, set a memory pool to 5 memory blocks. When we print out the first 10 addresses after 100 memory requests, we can see that the first five addresses are contiguous, and the last five addresses are also contiguous. However, due to the reapplication of the memory pool, it is not continuous.
But there is a problem with this approach, which introduces additional pointer memory consumption, which will be improved using embedded pointer.
Improvements using embedded pointers
Embedded pointers are used above. An AirplaneRep object is 8 bytes in size, while an Airplane pointer is 4 or 8 bytes in size. On 32-bit machines, pointers can be used to connect free blocks of memory by borrowing the first four bytes of the 8-byte memory occupied by AirplaneRep objects. When a memory block needs to be allocated to an object, it has been removed from the linked list and no pointer is needed to connect it. At this point, 8 bytes of memory space is occupied by AirplaneRep. The same is true when memory is freed, because Rep and next are not used at the same time, so embedded pointer can reduce memory consumption.
More simplified: static allocator
The previous implementation needs to rewrite operator new and operator delete for each class, and because the content is the same, use another class to do these repetitive operations.
In this way, our class only needs to call allocator to complete the memory request and release work.
Macor for static allocator
In the Foo and Goo above, you have to write a lot of repetitive content at a time, so you can use macros to further simplify:
At this point, the study of "how to implement a simple memory pool in C++ memory management" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
1. There are three basic working modes of vi editor. First, you need to
© 2024 shulou.com SLNews company. All rights reserved.