Botan 1.10.17
mem_pool.h
Go to the documentation of this file.
1/*
2* Pooling Allocator
3* (C) 1999-2007 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#ifndef BOTAN_POOLING_ALLOCATOR_H__
9#define BOTAN_POOLING_ALLOCATOR_H__
10
11#include <botan/allocate.h>
12#include <botan/exceptn.h>
13#include <botan/internal/mutex.h>
14#include <utility>
15#include <vector>
16
17namespace Botan {
18
19/**
20* Pooling Allocator
21*/
23 {
24 public:
25 void* allocate(size_t);
26 void deallocate(void*, size_t);
27
28 void destroy();
29
30 /**
31 * @param mutex used for internal locking
32 */
35 private:
36 void get_more_core(size_t);
37 byte* allocate_blocks(size_t);
38
39 virtual void* alloc_block(size_t) = 0;
40 virtual void dealloc_block(void*, size_t) = 0;
41
42 class Memory_Block
43 {
44 public:
45 Memory_Block(void*);
46
47 static size_t bitmap_size() { return BITMAP_SIZE; }
48 static size_t block_size() { return BLOCK_SIZE; }
49
50 bool contains(void*, size_t) const;
51 byte* alloc(size_t);
52 void free(void*, size_t);
53
54 bool operator<(const Memory_Block& other) const
55 {
56 if(buffer < other.buffer && other.buffer < buffer_end)
57 return false;
58 return (buffer < other.buffer);
59 }
60 private:
61 typedef u64bit bitmap_type;
62 static const size_t BITMAP_SIZE = 8 * sizeof(bitmap_type);
63 static const size_t BLOCK_SIZE = 64;
64
65 bitmap_type bitmap;
66 byte* buffer, *buffer_end;
67 };
68
69 std::vector<Memory_Block> blocks;
70 std::vector<Memory_Block>::iterator last_used;
71 std::vector<std::pair<void*, size_t> > allocated;
72 Mutex* mutex;
73 };
74
75}
76
77#endif
Pooling_Allocator(Mutex *mutex)
Definition mem_pool.cpp:99
void * allocate(size_t)
Definition mem_pool.cpp:133
void deallocate(void *, size_t)
Definition mem_pool.cpp:167
bool BOTAN_DLL operator<(const X509_Time &, const X509_Time &)
Definition asn1_tm.cpp:286
unsigned long long u64bit
Definition types.h:49