-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmem_pool.h
111 lines (88 loc) · 1.85 KB
/
mem_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// mem_pool.h
// A memory buffer pool based on ObjPool
//
//
// 2014-1-15 shaneyu Created
//
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <map>
#include <string>
#include "obj_pool.h"
#ifndef __UC_MEM_POOL__
#define __UC_MEM_POOL__
using namespace std;
class UcMemPool;
class UcMemManager;
class UcMem : public ObjBase
{
public:
UcMem() : ObjBase(), mem(NULL), pool(NULL)
{
}
UcMem(uint64_t sz) : ObjBase(), pool(NULL)
{
mem = malloc(sz);
}
virtual ~UcMem()
{
if(mem) free(mem);
}
virtual void ReleaseObject()
{
// nothing to be released
}
operator void *() { return (void *)mem; }
void *ptr() { return (void *)mem; }
uint64_t GetAllocSize();
private:
void *mem;
friend class UcMemPool;
friend class UcMemManager;
UcMemPool *pool;
};
//
// the mem pool
//
class UcMemPool
{
public:
UcMemPool() : sz_each(0), sz_total(0), sz_free(0), sz_max(0), pool(){ }
~UcMemPool() {}
UcMem *Alloc(bool &exceed_limit);
void Free(UcMem *m);
uint64_t Shrink(); // shrink memory for other pool, return the shrinked size
uint64_t GetMemSize() { return sz_each; }
private:
uint64_t sz_each; // size of each mem
uint64_t sz_total; // current total size
uint64_t sz_free; // current free size
uint64_t sz_max; // max total size
ObjPool<UcMem> pool;
friend class UcMemManager;
};
inline uint64_t UcMem::GetAllocSize()
{
return pool? pool->GetMemSize() : 0;
}
class UcMemManager
{
public:
static UcMem *Alloc(uint64_t sz);
static void Free(UcMem *m);
static void SetMaxSize(uint64_t sz) { GetInstance()->sz_max = sz; }
private:
static UcMemManager *GetInstance();
UcMemManager(uint64_t max_sz):sz_max(max_sz) {}
UcMemPool *GetPool(int magic);
uint64_t Shrink(int magic);
uint64_t sz_max;
};
#endif