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 void*) const;
51 bool operator<(const Memory_Block& other) const
52 { return (buffer < other.buffer); }
53 bool operator>(const Memory_Block& other) const
54 { return (buffer > other.buffer); }
55 private:
56 typedef u64bit bitmap_type;
57 static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type);
58 bitmap_type bitmap;
59 byte* buffer, *buffer_end;
60 u32bit block_size;
61 };
62
63template <typename _first, typename _second>
64struct diff_less : public std::binary_function<_first,_second,bool>
65{
66 bool operator()(const _first& __x, const _second& __y) const { return __x < __y; }
67#if defined(_MSC_VER) && defined(_DEBUG)
68 bool operator()(const _second& __y, const _first& __x) const { return __x > __y; }
69 bool operator()(const _first& __x, const _first& __y) const { return __x < __y; }
70#endif
71};
72
73 const u32bit PREF_SIZE, BLOCK_SIZE;
74
75 std::vector<Memory_Block> blocks;
76 std::vector<Memory_Block>::iterator last_used;
77 std::vector<std::pair<void*, u32bit> > allocated;
78 Mutex* mutex;
79 };
80
81}
82
83#endif

Archive Download this file

Branches

Tags

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