monotone

monotone Mtn Source Tree

Root/botan/mem_pool.h

1/*************************************************
2* Pooling Allocator Header File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_POOLING_ALLOCATOR_H__
7#define BOTAN_POOLING_ALLOCATOR_H__
8
9#include <botan/allocate.h>
10#include <botan/exceptn.h>
11#include <botan/mutex.h>
12#include <utility>
13#include <vector>
14
15namespace Botan {
16
17/*************************************************
18* Pooling Allocator *
19*************************************************/
20class Pooling_Allocator : public Allocator
21 {
22 public:
23 void* allocate(u32bit);
24 void deallocate(void*, u32bit);
25
26 void destroy();
27
28 Pooling_Allocator(u32bit, bool);
29 ~Pooling_Allocator();
30 private:
31 void get_more_core(u32bit);
32 byte* allocate_blocks(u32bit);
33
34 virtual void* alloc_block(u32bit) = 0;
35 virtual void dealloc_block(void*, u32bit) = 0;
36
37 class Memory_Block
38 {
39 public:
40 Memory_Block(void*);
41
42 static u32bit bitmap_size() { return BITMAP_SIZE; }
43 static u32bit block_size() { return BLOCK_SIZE; }
44
45 bool contains(void*, u32bit) const throw();
46 byte* alloc(u32bit) throw();
47 void free(void*, u32bit) throw();
48
49 bool operator<(const Memory_Block& other) const
50 {
51 if(buffer < other.buffer && other.buffer < buffer_end)
52 return false;
53 return (buffer < other.buffer);
54 }
55 private:
56 typedef u64bit bitmap_type;
57 static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type);
58 static const u32bit BLOCK_SIZE = 64;
59
60 bitmap_type bitmap;
61 byte* buffer, *buffer_end;
62 };
63
64 const u32bit PREF_SIZE;
65
66 std::vector<Memory_Block> blocks;
67 std::vector<Memory_Block>::iterator last_used;
68 std::vector<std::pair<void*, u32bit> > allocated;
69 Mutex* mutex;
70 };
71
72}
73
74#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status