forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shmmc.c
347 lines (283 loc) · 6.85 KB
/
shmmc.c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
*
* Shared memory control - based on alocating chunks aligned on
* asize array (fibonachi), and dividing free bigger block.
*
*/
#include "postgres.h"
#include "shmmc.h"
#include "stdlib.h"
#include "string.h"
#include "orafce.h"
#include "stdint.h"
#define LIST_ITEMS 512
int context;
typedef struct {
size_t size;
void* first_byte_ptr;
bool dispossible;
/* int16 context; */
} list_item;
typedef struct {
int list_c;
size_t max_size;
vardata data[1]; /* flexible array member */
} mem_desc;
#define MAX_SIZE 82688
static size_t asize[] = {
32,
64, 96, 160, 256,
416, 672, 1088, 1760,
2848, 4608, 7456, 12064,
19520, 31584, 51104, 82688};
int *list_c = NULL;
list_item *list = NULL;
size_t max_size;
int cycle = 0;
/* align requested size */
static int
ptr_comp(const void* a, const void* b)
{
ptrdiff_t d;
list_item *_a = (list_item*) a;
list_item *_b = (list_item*) b;
d = (uintptr_t)_a->first_byte_ptr - (uintptr_t)_b->first_byte_ptr;
return d > 0 ? 1 : (d < 0 ? -1 : 0);
}
char *
ora_sstrcpy(char *str)
{
size_t len;
char *result;
len = strlen(str);
if (NULL != (result = ora_salloc(len+1)))
memcpy(result, str, len + 1);
else
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed while allocation block %d bytes in shared memory.", (int) len+1),
errhint("Increase SHMEMMSGSZ and recompile package.")));
return result;
}
char *
ora_scstring(text *str)
{
int len;
char *result;
len = VARSIZE_ANY_EXHDR(str);
if (NULL != (result = ora_salloc(len+1)))
{
memcpy(result, VARDATA_ANY(str), len);
result[len] = '\0';
}
else
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed while allocation block %d bytes in shared memory.", (int) len+1),
errhint("Increase SHMEMMSGSZ and recompile package.")));
return result;
}
/*
* Compact the list of slots, by merging adjacent unused slots into larger
* slots.
*/
static void
defragmentation()
{
int src, target;
/* Sort the array to pointer order */
qsort(list, *list_c, sizeof(list_item), ptr_comp);
/* Merge adjacent dispossible slots, and move up other slots */
target = 0;
for (src = 0; src < *list_c; src++)
{
if (target > 0 &&
list[src].dispossible &&
list[target - 1].dispossible)
{
list[target - 1].size += list[src].size;
}
else
{
if (src != target)
memcpy(&list[target], &list[src], sizeof(list_item));
target++;
}
}
*list_c = target;
}
static size_t
align_size(size_t size)
{
int i;
/* default, we can allocate max MAX_SIZE memory block */
for (i = 0; i < 17; i++)
if (asize[i] >= size)
return asize[i];
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("too much large memory block request"),
errdetail("Failed while allocation block %lu bytes in shared memory.", (unsigned long) size),
errhint("Increase MAX_SIZE constant, fill table a_size and recompile package.")));
return 0;
}
/*
initialize shared memory. It works in two modes, create and no create.
No create is used for mounting shared memory buffer. Top of memory is
used for list_item array.
*/
void
ora_sinit(void *ptr, size_t size, bool create)
{
if (list == NULL)
{
mem_desc *m = (mem_desc*)ptr;
list = (list_item*)m->data;
list_c = &m->list_c;
max_size = m->max_size = size;
if (create)
{
list[0].size = size - sizeof(list_item)*LIST_ITEMS - sizeof(mem_desc);
list[0].first_byte_ptr = ((char *) &m->data) + sizeof(list_item)*LIST_ITEMS;
list[0].dispossible = true;
*list_c = 1;
}
}
}
void*
ora_salloc(size_t size)
{
size_t aligned_size;
int repeat_c;
void *ptr = NULL;
aligned_size = align_size(size);
for (repeat_c = 0; repeat_c < 2; repeat_c++)
{
size_t max_min = max_size;
int select = -1;
int i;
/* find first good free block */
for (i = 0; i < *list_c; i++)
{
if (list[i].dispossible)
{
/* If this block is just the right size, return it */
if (list[i].size == aligned_size)
{
list[i].dispossible = false;
ptr = list[i].first_byte_ptr;
/* list[i].context = context; */
return ptr;
}
if (list[i].size > aligned_size && list[i].size < max_min)
{
max_min = list[i].size;
select = i;
}
}
}
/* If no suitable free slot found, defragment and try again. */
if (select == -1 || *list_c == LIST_ITEMS)
{
defragmentation();
continue;
}
/*
* A slot larger than required was found. Divide it to avoid wasting
* space, and return the slot of the right size.
*/
list[*list_c].size = list[select].size - aligned_size;
list[*list_c].first_byte_ptr = (char*)list[select].first_byte_ptr + aligned_size;
list[*list_c].dispossible = true;
list[select].size = aligned_size;
list[select].dispossible = false;
/* list[select].context = context; */
ptr = list[select].first_byte_ptr;
*list_c += 1;
break;
}
return ptr;
}
void
ora_sfree(void* ptr)
{
int i;
/*
if (cycle++ % 100 == 0)
{
size_t suma = 0;
for (i = 0; i < *list_c; i++)
if (list[i].dispossible)
suma += list[i].size;
elog(NOTICE, "=============== FREE MEM REPORT === %10d ================", suma);
}
*/
for (i = 0; i < *list_c; i++)
if (list[i].first_byte_ptr == ptr)
{
list[i].dispossible = true;
/* list[i].context = -1; */
memset(list[i].first_byte_ptr, '#', list[i].size);
return;
}
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("corrupted pointer"),
errdetail("Failed while reallocating memory block in shared memory."),
errhint("Report this bug to autors.")));
}
void*
ora_srealloc(void *ptr, size_t size)
{
void *result;
size_t aux_s = 0;
int i;
for (i = 0; i < *list_c; i++)
if (list[i].first_byte_ptr == ptr)
{
if (align_size(size) <= list[i].size)
return ptr;
aux_s = list[i].size;
}
if (aux_s == 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("corrupted pointer"),
errdetail("Failed while reallocating memory block in shared memory."),
errhint("Report this bug to autors.")));
if (NULL != (result = ora_salloc(size)))
{
memcpy(result, ptr, aux_s);
ora_sfree(ptr);
}
return result;
}
/*
* alloc shared memory, raise exception if not
*/
void*
salloc(size_t size)
{
void* result;
if (NULL == (result = ora_salloc(size)))
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed while allocation block %lu bytes in shared memory.", (unsigned long) size),
errhint("Increase SHMEMMSGSZ and recompile package.")));
return result;
}
void*
srealloc(void *ptr, size_t size)
{
void* result;
if (NULL == (result = ora_srealloc(ptr, size)))
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed while reallocation block %lu bytes in shared memory.", (unsigned long) size),
errhint("Increase SHMEMMSGSZ and recompile package.")));
return result;
}