Click
Loading...
Searching...
No Matches
bighashmap_arena.hh
1// -*- c-basic-offset: 4; related-file-name: "../../lib/bighashmap_arena.cc" -*-
2#ifndef CLICK_BIGHASHMAP_ARENA_HH
3#define CLICK_BIGHASHMAP_ARENA_HH
4CLICK_DECLS
5
6class HashMap_Arena { public:
7
8 HashMap_Arena(uint32_t element_size);
9
10 void use() { _refcount++; }
11 void unuse();
12
13 bool detached() const { return _detached; }
14 void detach() { _detached = true; }
15
16 void *alloc();
17 void free(void *);
18
19 private:
20
21 struct Link {
22 Link *next;
23 };
24 Link *_free;
25
26 enum { NELEMENTS = 127 }; // not a power of 2 so we don't fall into a
27 // too-large bucket
28 char *_cur_buffer;
29 int _buffer_pos;
30
31 uint32_t _element_size;
32
33 char **_buffers;
34 int _nbuffers;
35 int _buffers_cap;
36
37 uint32_t _refcount;
38 bool _detached;
39
40 HashMap_Arena(const HashMap_Arena &);
41 ~HashMap_Arena();
42 HashMap_Arena &operator=(const HashMap_Arena &);
43
44 void *hard_alloc();
45
46 friend struct Link; // shut up, compiler
47
48};
49
50class HashMap_ArenaFactory { public:
51
52 HashMap_ArenaFactory();
53 virtual ~HashMap_ArenaFactory();
54
55 static void static_initialize();
56 static void static_cleanup();
57
58 static HashMap_Arena *get_arena(uint32_t, HashMap_ArenaFactory * =0);
59 virtual HashMap_Arena *get_arena_func(uint32_t);
60
61 private:
62
63 HashMap_Arena **_arenas[2];
64 int _narenas[2];
65
66 static HashMap_ArenaFactory *the_factory;
67
68};
69
70inline void
71HashMap_Arena::unuse()
72{
73 _refcount--;
74 if (_refcount <= 0)
75 delete this;
76}
77
78inline void *
79HashMap_Arena::alloc()
80{
81 if (_free) {
82 void *ret = _free;
83 _free = _free->next;
84 return ret;
85 } else if (_buffer_pos > 0) {
86 _buffer_pos -= _element_size;
87 return _cur_buffer + _buffer_pos;
88 } else
89 return hard_alloc();
90}
91
92inline void
93HashMap_Arena::free(void *v)
94{
95 Link *link = reinterpret_cast<Link *>(v);
96 link->next = _free;
97 _free = link;
98}
99
100CLICK_ENDDECLS
101#endif