a firstworks project
Rudiments
About Documentation Download Licensing News

Using the memorypool class

The memorypool class provides methods for creating and using a memory pool.

If you have an iterative process that requires variable amounts of memory for each iteration, using a memory pool can perform better than allocating memory on-demand and consume less memory than allocating static buffers that are large enough to accommodate the maximum amount of data you may have to store.

A memory pool is configured with an initial size, an increment size, and a resize interval. Memory is allocated from the pool using the allocate() method. When the pool is exhausted, it grows by the increment size. The clear() method returns the pool to its initial size.

The following example creates a memory pool, allocates several blocks of different sizes from it, verifies the data in each block, and then clears the pool.

#include <rudiments/memorypool.h>
#include <rudiments/bytestring.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

	// create a memory pool with initial size 1024, growing by
	// 256 bytes, and resizing every 10 allocations
	memorypool	pool(1024,256,10);

	stdoutput.printf("initial size:     %d\n",pool.getInitialSize());
	stdoutput.printf("increment size:   %d\n",pool.getIncrementSize());
	stdoutput.printf("resize interval:  %d\n\n",pool.getResizeInterval());


	// allocate some blocks from the pool
	byte_t	*block1=pool.allocate(100);
	bytestring::set(block1,'A',100);
	stdoutput.write("allocated 100 bytes\n");

	byte_t	*block2=pool.allocate(200);
	bytestring::set(block2,'B',200);
	stdoutput.write("allocated 200 bytes\n");

	byte_t	*block3=pool.allocate(50);
	bytestring::set(block3,'C',50);
	stdoutput.write("allocated 50 bytes\n\n");


	// verify the data
	stdoutput.printf("block1[0]: %c\n",block1[0]);
	stdoutput.printf("block2[0]: %c\n",block2[0]);
	stdoutput.printf("block3[0]: %c\n\n",block3[0]);


	// clear the pool, shrinking it back to its initial size
	pool.clear();
	stdoutput.write("pool cleared\n");
}
Copyright 2017 - David Muse - Contact