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
|
#include "arena_ctx.h"
#define TypeName PG_TypeName
#define SortBy PG_SortBy
#undef SIZEOF_SIZE_T
extern "C" {
#include "postgres.h"
#include "nodes/memnodes.h"
#include "utils/memutils.h"
}
namespace NYql {
void *MyAllocSetAlloc(MemoryContext context, Size size) {
auto fullSize = size + MAXIMUM_ALIGNOF - 1 + sizeof(void*);
auto ptr = TArenaMemoryContext::GetCurrentPool().Allocate(fullSize);
auto aligned = (void*)MAXALIGN(ptr + sizeof(void*));
*(MemoryContext *)(((char *)aligned) - sizeof(void *)) = context;
return aligned;
}
void MyAllocSetFree(MemoryContext context, void* pointer) {
}
void* MyAllocSetRealloc(MemoryContext context, void* pointer, Size size) {
if (!size) {
return nullptr;
}
void* ret = MyAllocSetAlloc(context, size);
if (pointer) {
memmove(ret, pointer, size);
}
return ret;
}
void MyAllocSetReset(MemoryContext context) {
}
void MyAllocSetDelete(MemoryContext context) {
}
Size MyAllocSetGetChunkSpace(MemoryContext context, void* pointer) {
return 0;
}
bool MyAllocSetIsEmpty(MemoryContext context) {
return false;
}
void MyAllocSetStats(MemoryContext context,
MemoryStatsPrintFunc printfunc, void *passthru,
MemoryContextCounters *totals,
bool print_to_stderr) {
}
void MyAllocSetCheck(MemoryContext context) {
}
const MemoryContextMethods MyMethods = {
MyAllocSetAlloc,
MyAllocSetFree,
MyAllocSetRealloc,
MyAllocSetReset,
MyAllocSetDelete,
MyAllocSetGetChunkSpace,
MyAllocSetIsEmpty,
MyAllocSetStats
#ifdef MEMORY_CONTEXT_CHECKING
,MyAllocSetCheck
#endif
};
__thread TArenaMemoryContext* TArenaMemoryContext::Current = nullptr;
TArenaMemoryContext::TArenaMemoryContext() {
Prev = Current;
Current = this;
PrevContext = CurrentMemoryContext;
CurrentMemoryContext = (MemoryContext)malloc(sizeof(MemoryContextData));
MemoryContextCreate(CurrentMemoryContext,
T_AllocSetContext,
&MyMethods,
nullptr,
"arena");
}
TArenaMemoryContext::~TArenaMemoryContext() {
free(CurrentMemoryContext);
CurrentMemoryContext = PrevContext;
Current = Prev;
}
}
|