monotone

monotone Mtn Source Tree

Root/botan/mem_pool.h

1/*************************************************
2* Pooling Allocator Header File *
3* (C) 1999-2006 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#include <functional>
15
16namespace Botan {
17
18/*************************************************
19* Pooling Allocator *
20*************************************************/
21class Pooling_Allocator : public Allocator
22 {
23 public:
24 void* allocate(u32bit);
25 void deallocate(void*, u32bit);
26
27 void destroy();
28
29 Pooling_Allocator(u32bit, bool);
30 ~Pooling_Allocator();
31 private:
32 void get_more_core(u32bit);
33 byte* allocate_blocks(u32bit);
34
35 virtual void* alloc_block(u32bit) = 0;
36 virtual void dealloc_block(void*, u32bit) = 0;
37
38 class Memory_Block
39 {
40 public:
41 Memory_Block(void*, u32bit, u32bit);
42
43 static u32bit bitmap_size() { return BITMAP_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 void*) const;
50 bool operator<(const Memory_Block& other) const
51 { return (buffer < other.buffer); }
52 private:
53 typedef u64bit bitmap_type;
54 static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type);
55 bitmap_type bitmap;
56 byte* buffer, *buffer_end;
57 u32bit block_size;
58 };
59
60template <typename _first, typename _second>
61struct diff_less : public std::binary_function<_first,_second,bool>
62{
63 bool operator()(const _first& __x, const _second& __y) const { return __x < __y; }
64#if defined(_MSC_VER) && defined(_DEBUG)
65 bool operator()(const _second& __y, const _first& __x) const { return __x < __y; }
66 bool operator()(const _first& __x, const _first& __y) const { return __x < __y; }
67#endif
68};
69
70 const u32bit PREF_SIZE, BLOCK_SIZE;
71
72 std::vector<Memory_Block> blocks;
73 std::vector<Memory_Block>::iterator last_used;
74 std::vector<std::pair<void*, u32bit> > allocated;
75 Mutex* mutex;
76 };
77
78}
79
80#endif

Archive Download this file

Branches

Tags

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