Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory pools and buffers C++

Tags:

People also ask

What is a memory pool in C?

Memory pools, also called fixed-size blocks allocation, is the use of pools for memory management that allows dynamic memory allocation comparable to malloc or C++'s operator new.

What is a memory pool and why do we need it?

Memory pools. A memory pool is a logical division of main memory or storage that is reserved for processing a job or group of jobs. On your system, all main storage can be divided into logical allocations called memory pools. By default, the system manages the transfer of data and programs into memory pools.

What is buffer pool?

A buffer pool is an area of main memory that has been allocated by the database manager for the purpose of caching table and index data as it is read from disk. Every Db2® database must have a buffer pool. Each new database has a default buffer pool defined, called IBMDEFAULTBP.


I have a few questions about buffers and memory pools i would like to have answered.

Say i have a server, sending and receiving ~50-100+ msg / second. All msgs come in various sizes. How would you go about it to make the best of the memory management here ? My original plan was using fixed sized buffer nodes, and pool them, something like:

struct buffer{
    uint8_t  data[512];
    uint32_t end;
    buffer*  next;
}
buffer* b = pool_get_new_buffer();

So when a msg is sent, i create one or more buffers depending on the size and link them together. This way i dont have be afraid of fragmentation in the pool it self. (or thats atleast what i think). But on small msg, its a waste of space.

But reading more and more and checking out code on the internet, it looks like nobody uses this approach at all. So what would be a better approach ? Allocating memory from the pool depending on msg size ?

EDIT: So what im after here is maybe a more indept comparison of the different approaches.

If i use the chained buffer approach im guessing i will keep fragmentation at its lowest but on the other hand i would guess that doing memcpy for every buffer in the chain comes at a cost as well. But then again, allocating a large enough buffer and doing a single memcpy must have its downsides as well, even though most people choose this approach anyway.