File: MemoryPool.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (44 lines) | stat: -rw-r--r-- 1,219 bytes parent folder | download | duplicates (4)
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
#pragma once

#include "Object.h"

namespace util {

	//A specific memory pool used by the PoolAllocator. The pool
	//is allocated in a linear fashion until it is full. Further allocations
	//will then fail until the pool is entirely empty.
	class MemoryPool : NoCopy {
	public:
		//Create the pool with a specific size. This malloc's the memory.
		MemoryPool(nat size);

		//Destroy the pool. This will ASSERT if the pool is not empty.
		~MemoryPool();

		//Allocate memory here. Returns null if the pool is full.
		void *alloc(nat size);

		//Returns true if this pool is full. It is considered full after the first failing alloc
		inline bool filled() const { return firstFree == dataSize; }

		//Returns true if no memory is allocated in this pool.
		inline bool empty() const { return firstFree == 0; }

		//Get the pool which allocated the memory.
		static MemoryPool *getPoolFromAlloc(void *mem);

		//Free memory. Sets "filled" to false if the pool is entirely freed.
		void free(void *memory);
	private:
		//The allocated data and its size.
		byte *data;
		nat dataSize;

		//The first free memory address, relative "data".
		nat firstFree;

		//The number of allocations in this pool.
		nat allocCount;
	};

}