-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearAlloc.h
executable file
·80 lines (73 loc) · 1.51 KB
/
LinearAlloc.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
/*
* LinearAlloc.h
*
* Created on: 1 Nov 2013
* Author: lester
*/
#ifndef LINEARALLOC_H_
#define LINEARALLOC_H_
#include "Page.h"
/*
this class is not a object is a wrapper for linear allocation of memory over a page
*/
class LinearAlloc: private Page
{
public:
// create a page to store some content
static LinearAlloc* create(unsigned int size = 0)
{
LinearAlloc* p = (LinearAlloc*) Page::create(size + sizeof(LinearAlloc) - sizeof(Page));
p->init();
return p;
}
inline LinearAlloc** getnext()
{
return (LinearAlloc**) &next;
}
inline void init()
{
pos = 0;
}
// return null if there is not memory
inline void* get(unsigned int size)
{
unsigned short idx = pos;
pos += ((size + sizeof(*dt) -1)/sizeof(*dt));
return &dt[idx];
}
// get free space
inline unsigned int getFree()
{
return (pos == 0xFFFF) ? (0) : (size - sizeof(*this) - pos * sizeof(*dt) + sizeof(dt));
}
// store a string
void AddString(const char *str, unsigned int len, char * &mem);
// release memory
void release()
{
Page::release();
}
/*
* get remained free space on memory
* return pointer index and size
* this memory can be reduce, but it can't be grow
*/
void* lock(unsigned short &size, unsigned int &idx)
{
size = getFree();
idx = pos;
pos = 0xFFFF;
return &dt[idx];
}
/*
* Change size of this block on memory
*/
void unlock(unsigned int idx,unsigned int size)
{
}
private:
unsigned int pos; // current free pos
// data go here
int dt[1];
};
#endif /* LINEARALLOC_H_ */