Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   C++ Memory Pool (http://www.go4expert.com/articles/cpp-memory-pool-t3026/)

Sanskruti 15Feb2007 19:44

C++ Memory Pool
Memory pools allow dynamic memory allocation comparable to malloc or the Operator new in C++. As those implementations suffer from fragmentation because of variable block sizes, it can be impossible to use them in a real time system due to performance. A more efficient solution is preallocating a number of memory blocks with the same size called the memory pool. The application can allocate, access and free blocks represented by handles at runtime.

Memory pool implementation

A simple memory pool module can allocate 3 pools at compile time with block sizes optimized for the application, which deploys the module.

The application can allocate, access and free memory with the following interface:

MemPoolHandle allocateMemory(size_t bytes);

To allocate memory from the pools. The function will determine the pool, where the required block fits in. If all blocks of that pool are already reserved, the function tries to find one in the next bigger pool.

An allocated memory block is represented with a handle.

void * getPointer(MemPoolHandle h);

To get an access pointer to the allocated memory .

void freeMemory(MemPoolHandle h);

To free the formerly allocated memory block.

typedef signed int MemPoolHandle;

The handle can for example be implemented with an unsigned int.

The module can interpret the handle internally by dividing it into pool index, memory block index and a version. The pool and memory block index allow fast access to the corresponding block with the handle, while the version, which is incremented at each new allocation, allows detection of handles whose memory block is already freed .

Dynamic Memory Allocation

When you design programs, you cannot always determine how much memory you need before your program runs. The length of an array or the number of structures, for example, may be unknown until your executing program determines what these values should be. An area of memory called the free store is available in the C++ run-time environment to handle dynamic memory allocation at run time. Programs use operator new to allocate memory from free store and operator delete to release free store memory.

Operator new

The predeclared formats for dynamic allocation with operator new are

Code: CPP

new Type                      // one object
new Type(initial_value)       // one object, initial value
new Type[size1]               // array of objects
new Type[size1][size2][sizeN] // multidimensional array of objects

For now, let's work with objects as C++ built-in types. Operator new allocates one object or an array of objects from free store. There are no default initial values for built-in types. In an expression, operator new returns a nonzero pointer to free store memory if it's available and throws a bad_alloc exception , The second format allocates one object and initializes it to the value you supply inside the parentheses. The [] notation allocates one-dimensional or multidimensional arrays of objects, where size1 is any integer expression (0 or greater).

For multidimensional arrays, size2 through sizeN must be constant integer expressions (1 or greater). With the [] format, operator new returns a pointer to the first element of the array.

There is no format to initialize array elements. The C++ standard library also provides versions of operator new that do not throw exceptions.

These "nothrow" versions are not predeclared; therefore, you must specify include file <new> in order to use them.

Here are the formats.

Code: CPP

new(nothrow) Type                      // one object
 new(nothrow) Type(initial_value)       // one object , initial value
 new(nothrow) Type[size1]               // array of objects
 new(nothrow) Type[size1][size2][sizeN] // multidim array of objects

The nothrow versions of operator new return a nonzero pointer to free store memory if it's available and 0 if it is not.

Difference between Memory pool and malloc

Memory pools allow memory allocation with constant execution time (no fragmentation). The memory release for thousands of objects in a pool is just one operation, not one by one if one uses malloc to allocate memory for each object. Third, memory pools can be grouped in hierarchical tree structures, which is suitable for special programming structures like loops and recursions. On the other hand, they need to be tuned for the application which deploys them.

Allocating enough memory to use, keeping track of those allocations, freeing the memory when you no longer need it—these tasks can be quite complex. And of course, failure to do those things properly can result in a program that crashes itself, or worse, crashes the computer. A memory pool is an abstract representation of a chunk of memory allocated for use by a program. Rather than requesting memory directly from the OS using the standard malloc() and friends, programs that link against APR can simply request that a pool of memory be created (using the apr_pool_create() function).

APR will allocate a moderately sized chunk of memory from the OS, and that memory will be instantly available for use by the program. Any time the program needs some of the pool memory, it uses one of the APR pool API functions, like apr_palloc(), which returns a generic memory location from the pool. The program can keep requesting bits and pieces of memory from the pool, and APR will keep granting the requests.

Pools will automatically grow in size to accommodate programs that request more memory than the original pool contained, until of course there is no more memory available on the system.

Now, if this were the end of the pool story, it would hardly have merited special attention. Fortunately, that's not the case. Pools can not only be created; they can also be cleared and destroyed, using apr_pool_clear() and apr_pool_destroy() respectively. This gives developers the flexibility to allocate several—or several thousand—things from the pool, and then clean up all of that memory with a single function call! Further, pools have hierarchy. You can make “subpools” of any previously created pool. When you clear a pool, all of its subpools are destroyed; if you destroy a pool, it and its subpools are destroyed.

Before we go further, developers should be aware that they probably will not find many calls to the APR pool functions we just mentioned in the Subversion source code. APR pools offer some extensibility mechanisms, like the ability to have custom “user data” attached to the pool, and mechanisms for registering cleanup functions that get called when the pool is destroyed. Subversion makes use of these extensions in a somewhat non-trivial way. So, Subversion supplies (and most of its code uses) the wrapper functions svn_pool_create(), svn_pool_clear(), and svn_pool_destroy().

While pools are helpful for basic memory management, the pool construct really shines in looping and recursive scenarios. Since loops are often unbounded in their iterations, and recursions in their depth, memory consumption in these areas of the code can become unpredictable.

Pools might not be ideal for every application, but they are extremely useful in Subversion. As a Subversion developer, you'll need to grow comfortable with pools and how to wield them correctly. Memory usage bugs and bloating can be difficult to diagnose and fix regardless of the API, but the pool construct provided by APR has proven a tremendously convenient, time-saving bit of functionality

asadullah.ansari 23Jan2008 11:24

Re: C++ Memory Pool
Nice Info!!!

asadullah.ansari 23Jan2008 11:25

Re: C++ Memory Pool
Nice Info!!!

But You know Creating Pool is itself a overhead. By using POOL we can get excellent features like No Fregmentation, No collapse with other application, Safe etc.
But management of these things iteself a time consuming.
Offcourse If you increase facility then you have to compromise with efficiency.

debleena_doll2002 20Feb2008 11:27

Re: C++ Memory Pool
By any means Pool concepts is very strong. You can save your application from protection..

lead.smart34 26Feb2008 18:20

Re: C++ Memory Pool
good info

crazytolearn57 26Feb2008 18:37

Re: C++ Memory Pool
good one

aisha.ansari84 5Mar2008 18:39

Re: C++ Memory Pool
nice concept

All times are GMT +5.5. The time now is 12:28.