-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMemoryPool.h
More file actions
72 lines (66 loc) · 1.14 KB
/
MemoryPool.h
File metadata and controls
72 lines (66 loc) · 1.14 KB
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
// MemoryPool.h
// Copyright 2018/11/7 Robin.Rowe@CinePaint.org
// License open source MIT
#ifndef MemoryPool_h
#define MemoryPool_h
#include <string>
#include <stdio.h>
namespace IPC {
class MemoryPool
{protected:
void* p;
bool isAllocator;
std::string name;
std::string info;
void Reset()
{ p = 0;
isAllocator = false;
name.clear();
info.clear();
}
void Init(const char* name,bool isAllocator)
{ Reset();
this->name = name;
this->isAllocator = isAllocator;
}
void SetInfo(const char* msg,size_t size)
{ info = "Memory pool ";
info += name;
info += ": ";
info += msg;
info += " ";
info += std::to_string(size);
info += " bytes";
}
virtual void PrintError(const char* msg)
{ perror(msg);
}
virtual bool Close()
{ return false;
}
virtual void* Create(size_t size)
{ return 0;
}
virtual void* Open(size_t size)
{ return 0;
}
public:
MemoryPool()
: p(0)
{}
virtual ~MemoryPool()
{ Close();
}
void* Allocate(size_t size)
{ return isAllocator? Create(size):Open(size);
}
void Deallocate(void* p)
{ if(this->p == p)
{ Close();
} }
virtual const char* to_string() const
{ return info.c_str();
}
};
}
#endif