How does boost.pool achieve reuse of allocated memory?

Background

My previous question about boost.poolmade me study boost.pool in detail, and now I have an additional question to complete my understanding.

Prelude

This link states the following about the object pool template:

An object pool template is a software creation template that uses a set of initialized objects ready for use, rather than distributing and destroying them on demand.

From what I can say, boost.pool(simplified) implements an object pool template by allocating and managing memory mainly based on size element_typeand returns a simple pointer to the selected object:

element_type * malloc();
void free(element_type * p);

A simple boost example also shows that there is no need for an explicitly freereceived element:

X * const t = p.malloc();
... // Do something with t; don't take the time to free() it.

Question

I understand that the allocated memory will be safely freed when the pool object is destroyed, but as the pool knows, when the memory block received by the client was released back to the pool and can be reused if its interface goes back a direct pointer to element_type, but the call free()is still not required? Those. how does the backup pool reuse this memory if you cannot be sure that the memory is not yet in use? And if he doesn’t reuse this memory, is that even considered the same model as the wiki link?

+5
source share
2 answers

, , ?

. . , .

, , wiki?

, : ,

Boost pool: , .

, . , (, opengl ..). , , .

, :

  • , malloc()/free(), . , .
  • .

: , , node . . , , :

std::map<node*,node*> old_ptr_to_new_ptr;

, ( , - std): ( ), .

+7

STL, ( std::allocator, new delete). , Stroustrup Alexandrescu .

, : , , . , ( ). , allocate(1), , construct( ptr, value ) ptr, value ( ). , . , STL --- .

, ( ), , -. , , , , , , .

, wiki ( ), , . wiki, , (,

, , , , element_type, free() - ?

, . , . , , , - , , . , " ", , - , .

, (, , ), , (, - -, ).

, , ?

, , . , ​​ , ", , ", , , , undefined, , -.

, , wiki?

, , . , . , " ", " ". , , . (freestore) , , . , - - . , , , . ( ) (, std::vector). , . , , , . , - :

- , () .

+6

All Articles