File: memorypool.h

package info (click to toggle)
librudiments0 0.27-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,528 kB
  • ctags: 2,284
  • sloc: cpp: 14,657; sh: 7,547; ansic: 2,664; makefile: 945; xml: 15
file content (59 lines) | stat: -rw-r--r-- 2,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) 2002 David Muse
// See the COPYING file for more information.

#ifndef RUDIMENTS_MEMORYPOOL_H
#define RUDIMENTS_MEMORYPOOL_H

#include <rudiments/private/memorypoolincludes.h>

typedef	linkedlist<memorypoolnode *>		memorypoollist;
typedef	linkedlistnode<memorypoolnode *>	memorypoollistnode;

// The memorypool class provides methods for creating and using a memory pool.
//
// If you have an iterative process that requires variable amounts of ram
// for each iteration, using a memory pool can be faster than allocating
// and deallocating memory on-demand and less resource-intensive than
// allocating static buffers that are large enough to accommodate the maximum
// amount of data you may have to store.

class memorypool {
	public:
			memorypool(size_t initialsize,
					size_t increment,
					size_t resizeinterval);
			// Creates a memory pool of initial size "initialsize".
			//
			// When the pool needs to grow, it will grow by at least
			// "increment" bytes.  If more than increment bytes
			// are requested, it will grow by that amount instead.
			//
			// When free() has been called "resizeinterval" times,
			// it will evaluate the average amount of memory
			// malloc'ed (since the last time it did this) and
			// resize the initial buffer size to this size.  This
			// will ideally minimize the 
			~memorypool();
			// Destroys the memory pool.

		unsigned char	*malloc(size_t size);
			// Returns a pointer to a contiguous block of "size"
			// bytes in the pool.  The pool will grow as necessary
			// to accomodate allocations.
		unsigned char	*calloc(size_t size);
			// Returns a pointer to a contiguous block of "size"
			// bytes in the pool.  The pool will grow as necessary
			// to accomodate allocations.  Each byte of data in
			// the block of data is set to NULL.

		void	free();
			// Shrinks the pool back down to it's initial size
			// and frees all previously allocated blocks.

		void	print();
			// Prints a visual representation of the pool.

	#include <rudiments/private/memorypool.h>
};

#endif