-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcxt.h
193 lines (168 loc) · 7.61 KB
/
mcxt.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef __MCXT_H__
#define __MCXT_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
typedef size_t Size;
typedef unsigned long int uintptr_t;
typedef void *AllocPointer;
typedef void (*MemoryContextCallbackFunction)(void *arg);
typedef struct MemoryContextCallback
{
MemoryContextCallbackFunction func; /* function to call */
void *arg; /* argument to pass it */
struct MemoryContextCallback *next; /* next in list of callbacks */
} MemoryContextCallback;
typedef struct MemoryContextData *MemoryContext;
typedef struct MemoryContextMethods
{
/** MemoyMethods:
* AllocSetAlloc : setAlloc
* AllocSetDelete : setDelete
* AllocSetRealloc : setRealloc
* AllocSetFree : setFree
* AllocSetReset : setReset
*/
void *(*setAlloc)(MemoryContext context, Size size);
void (*setDelete)(MemoryContext context);
void *(*setRealloc)(MemoryContext context, void *pointer, Size size);
void (*setFree)(MemoryContext context, void *pointer);
void (*setReset)(MemoryContext context);
} MemoryContextMethods;
typedef struct MemoryContextData
{
const MemoryContextMethods *methods; /* vitual functions */
MemoryContext parent; /* NULL if toplevel context */
MemoryContext first_child; /* head of linked list of children */
MemoryContext next_child; /* next child of same parent */
MemoryContext prev_child; /* prev child of same parent */
char *name; /* context name */
char isReset; /* T = nospace alloced since last reset */
MemoryContextCallback *reset_cbs; /* list of reset/delete callbacks */
} MemoryContextData;
extern MemoryContext TopMemoryContext;
extern MemoryContext ErrorMemoryContext;
extern MemoryContext CurrentMemoryContext;
extern MemoryContext AllocSetContextCreateInternal(MemoryContext parent,
const char *name,
Size minContextSize,
Size initBlockSize,
Size maxBlockSize);
extern void MemoryContextReset(MemoryContext context);
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
extern void MemoryContextDelete(MemoryContext context);
extern void MemoryContextInit(void);
#define Max(_x, _y) ((_x) > (_y) ? (_x) : (_y))
#define TYPEALIGN(ALIGNVAL, LEN) \
(((uintptr_t)(LEN) + ((ALIGNVAL)-1)) & ~((uintptr_t)((ALIGNVAL)-1)))
#define MAXALIGN(LEN) TYPEALIGN(8, (LEN))
#define ALLOC_BLOCKHDRSZ MAXALIGN(sizeof(AllocBlockData))
#define ALLOC_CHUNKHDRSZ sizeof(struct AllocChunkData)
#define AllocPointerGetChunk(ptr) \
((AllocChunk)(((char *)(ptr)) - ALLOC_CHUNKHDRSZ))
#define AllocChunkGetPointer(chk) \
((AllocPointer)(((char *)(chk)) + ALLOC_CHUNKHDRSZ))
#define AllocSetContextCreate AllocSetContextCreateInternal
#define MemoryContextResetAndDeleteChildren(ctx) MemoryContextReset(ctx)
#define ALLOC_MINBITS 3
#define ALLOC_CHUNK_FRACTION 4
#define ALLOCSET_NUM_FREELISTS 11
#define MEMSET_LOOP_LIMIT 1024
#define MAX_FREE_CONTEXTS 100
#define ALLOC_CHUNK_LIMIT (1 << (ALLOCSET_NUM_FREELISTS - 1 + ALLOC_MINBITS))
#define MemSetAligned(start, val, len) \
do \
{ \
long *_start = (long *)(start); \
int _val = (val); \
Size _len = (len); \
\
if ((_len & (sizeof(long) - 1)) == 0 && \
_val == 0 && \
_len <= MEMSET_LOOP_LIMIT && \
MEMSET_LOOP_LIMIT != 0) \
{ \
long *_stop = (long *)((char *)_start + _len); \
while (_start < _stop) \
*_start++ = 0; \
} \
else \
memset(_start, _val, _len); \
} while (0)
static const unsigned char LogTable256[256] =
{
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
};
typedef struct AllocBlockData *AllocBlock;
typedef struct AllocChunkData *AllocChunk;
typedef struct AllocSetContext
{
MemoryContextData header; /* 内存上下文的结构体,主要用于将不同的内存结构形成上下文 */
AllocBlock blocks; /* block 组成的链表,这里指向头部 */
AllocChunk freelist[ALLOCSET_NUM_FREELISTS]; /* free chunk list */
Size initBlockSize; /* init size of block */
Size maxBlockSize; /* max size of block */
Size nextBlockSize; /* next size of block */
Size allocChunkLimit; /* */
AllocBlock keeper; /* keep this block over resets */
} AllocSetContext;
typedef AllocSetContext *AllocSet;
typedef struct AllocBlockData
{
AllocSet aset; /* AllocSet that owns this block */
AllocBlock prev; /* prev block in this list */
AllocBlock next; /* next block in this list */
char *freeptr; /* first address of free space in this block */
char *endptr; /* end address of free space in this block */
} AllocBlockData;
typedef struct AllocChunkData
{
Size size; /* size */
void *aset; /* 空间复用指针,如果为空则指向下一个也为空的chunk
* 如果不为空,则指向其所属的AllocSet
*/
} AllocChunkData;
typedef struct StringInfoData
{
char *data;
int len;
int maxlen;
int cursor;
} StringInfoData;
typedef StringInfoData *StringInfo;
extern StringInfo makeStringInfo(void);
extern void initStringInfo(StringInfo str);
extern void resetStringInfo(StringInfo str);
extern void appendStringInfo(StringInfo str, const char *fmt, ...);
#define ALLOCSET_DEFAULT_MINSIZE 0
#define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
#define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
#define ALLOCSET_DEFAULT_SIZES \
ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
#define ALLOCSET_SMALL_MINSIZE 0
#define ALLOCSET_SMALL_INITSIZE (1 * 1024)
#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
#define ALLOCSET_SMALL_SIZES \
ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE
extern void *palloc(Size size);
extern void *palloc0(Size size);
extern void pfree(void *pointer);
extern void *repalloc(void *pointer, Size size);
#endif