-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_pool.h
52 lines (43 loc) · 915 Bytes
/
memory_pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef HZ_SERVER_MEMORY_POOL_H
#define HZ_SERVER_MEMORY_POOL_H
#include <cstdlib>
#include <pthread.h>
struct pool;
#define HZ_ALIGNMENT sizeof(unsigned long)
#define hz_align_ptr(p, a) \
(u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
struct large_mem
{
large_mem *next;
void *data;
};
struct pool_data
{
u_char *start;
u_char *end;
pool *next;
unsigned int failed = 0;
};
struct pool
{
pool_data d;
size_t max;
pool *current;
large_mem *large;
};
class allocator
{
public:
allocator(size_t size = 4096);
~allocator();
void *mem_palloc(size_t size);
void mem_free(void* p);
void reset();
pthread_mutex_t *lock;
private:
pool *memory_pool = NULL;
void *mem_alloc(size_t size);
void *mem_palloc_block(size_t size);
void *mem_palloc_large(size_t size);
};
#endif //HZ_SERVER_MEMORY_POOL_H