monotone

monotone Mtn Source Tree

Root/botan/mem_pool.h

1/*************************************************
2* Pooling Allocator Header File *
3* (C) 1999-2005 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 <vector>
13
14namespace Botan {
15
16/*************************************************
17* Pooling Allocator *
18*************************************************/
19class Pooling_Allocator : public Allocator
20 {
21 public:
22 void* allocate(u32bit) const;
23 void deallocate(void*, u32bit) const;
24
25 void init();
26 void destroy();
27
28 Pooling_Allocator(u32bit = 0);
29 ~Pooling_Allocator();
30 private:
31 class Buffer
32 {
33 public:
34 void* buf;
35 u32bit length;
36 bool in_use;
37
38 bool operator<(const Buffer& x) const
39 { return ((const byte*)buf < (const byte*)x.buf); }
40
41 Buffer() { buf = 0; length = 0; in_use = false; }
42 Buffer(void* b, u32bit l, bool used = false)
43 { buf = b; length = l; in_use = used; }
44 };
45
46 void* get_block(u32bit) const;
47 void free_block(void*, u32bit) const;
48
49 virtual void* alloc_block(u32bit) const = 0;
50 virtual void dealloc_block(void*, u32bit) const = 0;
51 virtual u32bit prealloc_bytes() const { return 0; }
52 virtual u32bit keep_free() const { return 64*1024; }
53
54 void* alloc_hook(void*, u32bit) const;
55 void dealloc_hook(void*, u32bit) const;
56 void consistency_check() const;
57
58 void* find_free_block(u32bit) const;
59 void defrag_free_list() const;
60
61 u32bit find_block(void*) const;
62 bool same_buffer(Buffer&, Buffer&) const;
63 void remove_empty_buffers(std::vector<Buffer>&) const;
64
65 static bool is_empty_buffer(const Buffer&);
66 static bool are_contiguous(const Buffer&, const Buffer&);
67
68 const u32bit PREF_SIZE, ALIGN_TO;
69 mutable std::vector<Buffer> real_mem, free_list;
70 mutable Mutex* lock;
71 mutable u32bit defrag_counter;
72 bool initialized, destroyed;
73 };
74
75}
76
77#endif

Archive Download this file

Branches

Tags

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