-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathalloc.h
More file actions
81 lines (70 loc) · 1.81 KB
/
alloc.h
File metadata and controls
81 lines (70 loc) · 1.81 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
73
74
75
76
77
78
79
80
81
#pragma once
#include <stdint.h>
#include <cassert>
template <class Alloc, class SizeT>
concept Alloc_Trait = requires(Alloc alloc,SizeT t) {
alloc.alloc(t);
};
struct RAlloc{
uint64_t raddr;
uint64_t roffset;
uint64_t rsize;
uint64_t r_start; // end of the whole buf
uint64_t r_total; // size of the whole buf
void SetRemote(uint64_t _raddr, uint64_t _rsize, uint64_t _start, uint64_t _total)
{
raddr = _raddr;
rsize = _rsize;
roffset = 0;
r_start = _start;
r_total = _total;
}
// 从尾部开始分配
uint64_t alloc(uint64_t size, bool align = false)
{
roffset += size;
uint64_t res = raddr - roffset;
if(align){
res = (res>>3)<<3; //按八字节对齐
roffset = raddr - res;
}
assert(roffset < rsize);
return res;
}
inline uint64_t offset(uint64_t ptr){return r_start + r_total - ptr;}
inline uint64_t ptr(uint64_t off){return r_start + r_total - off;}
};
struct Alloc{
char *start;
uint64_t offset;
uint64_t buf_size;
void Set(char *_start, uint64_t _size)
{
start = _start;
buf_size = _size;
offset = 0;
// memset(_start, 0, _size);
}
void ReSet(uint64_t _start){
offset=_start;
}
// 从前往后分配
char * alloc(uint64_t size, bool align = false)
{
char *res = start + offset;
if(align){
uint64_t tmp = (uint64_t)res;
tmp = ((tmp+7)>>3)<<3; //按八字节对齐
res = (char*)tmp;
offset = res - start;
}else{
offset += size;
}
assert(offset < buf_size);
return res;
}
void free(uint64_t size, bool align = false)
{
offset -= size;
}
};