-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomAlloc.cpp
569 lines (474 loc) · 11.1 KB
/
CustomAlloc.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include <CustomAlloc.h>
struct BufferHeader
{
unsigned int *pinuse, linuse;
byte *offset, move;
unsigned int size, total;
void* (*copy)(void*, void*);
/***
move is a flag for the defrag thread
-1: don't move this buffer
0: ready for move
1: being moved
***/
void Tighten(unsigned int Z)
{
size = total = Z;
}
};
class MemPool
{
/***
rule: the BH linking the furthest into *pool has to be the last one in the BH array
make sure to respect this rule when defragging;
***/
private:
static const int BHstep = 50;
unsigned int totalBH;
byte *pool;
BufferHeader* GetBH(unsigned int size)
{
//check to see if a bh isn't already available
if(size)
{
for(i=0; i<nBH; i++)
{
if(!*BH[i]->pinuse)
{
if(BH[i]->total>=size)
{
BH[i]->copy=0;
return BH[i];
}
}
}
}
if(nBH==totalBH)
{
totalBH+=BHstep;
BufferHeader **bht;
while(!(bht = (BufferHeader**)malloc(sizeof(BufferHeader*)*totalBH))) {}
memcpy(bht, BH, sizeof(BufferHeader*)*nBH);
free(BH);
BH = bht;
}
while(!(bhtmp = (BufferHeader*)malloc(sizeof(BufferHeader)))) {}
ResetBH(bhtmp);
BH[nBH] = bhtmp;
bhtmp->offset = 0;
bhtmp->pinuse = &bhtmp->linuse;
bhtmp->linuse = 1;
nBH++;
return bhtmp;
}
public:
BufferHeader **BH;
static const int memsize = 1024*1024; //1 mb per page
unsigned int freemem, total, reserved, slack, fmabs, nBH;
std::atomic_bool ab;
byte defrag;
unsigned int i;
BufferHeader *bhtmp; //global bhtmp could result in unexpected behavior,
//should use local bhtmp instead. However, private pools are locked and
//thus function sequentially. Random buffer query locks the pool as well.
/***reserved: marks how deep the pool buffer has been assigned for.
GetBuffer will always assign new BH starting pool + reserved, and increment reserved by size
PopBH will ignore it***/
void ResetBH(BufferHeader *bhtmp)
{
memset(bhtmp, 0, sizeof(BufferHeader));
bhtmp->pinuse = &bhtmp->linuse;
}
void ComputeMem()
{
//reset all bh that aren't inuse anymore, compute reserved based on the deepest bh in pool
if(nBH)
{
unsigned int sm=0, g=0;
for(g; g<nBH; g++)
{
if(*BH[g]->pinuse)
{
if(BH[g]->offset>BH[sm]->offset) sm=g;
}
else ResetBH(BH[g]);
}
if(BH[sm]->offset) reserved = (unsigned int)(BH[sm]->offset -pool) +BH[sm]->total;
else reserved = 0;
freemem = total - reserved;
}
}
byte *GetPrivatePool(unsigned int size)
{
if(ab.exchange(1)) return 0; //lock the pool on private gets
if(!pool)
{
if(size<memsize) Alloc(memsize);
else Alloc(size);
}
freemem-=size;
reserved+=size;
return pool +reserved -size;
}
void ReleasePool()
{
ab.store(0); //unlock pool from private gets
}
void Alloc(unsigned int size)
{
while(!pool)
{
pool = (byte*)malloc(size);
}
mlock(pool, size);
total = size;
freemem = size;
}
MemPool::MemPool()
{
BH=0;
nBH=0; totalBH=0;
defrag=0;
pool=0;
freemem=0;
total=0;
ab=0;
reserved=0;
}
MemPool::~MemPool()
{
if(pool)
{
munlock(pool, total);
free(pool);
for(i=0; i<nBH; i++)
free(BH[i]);
if(BH) free(BH);
}
}
BufferHeader* GetBuffer(unsigned int size, unsigned int *sema) //needs fixing
{
if(ab.exchange(1)==TRUE) return 0; //pools are meant to function as single threaded
if(!total)
{
if(size<memsize) Alloc(memsize);
else Alloc(size);
}
else if(size>freemem)
{
ab=0;
return 0;
}
GetBH(size);
if(!bhtmp->offset) //if this bh is new (offset is null), prep it
{
bhtmp->offset = pool +reserved;
bhtmp->total = size;
reserved += size;
}
if(sema) bhtmp->pinuse=sema;
bhtmp->size = size;
bhtmp->move = 0;
freemem-=size;
ab.store(0);
return bhtmp;
}
BufferHeader *PopBH(byte *start, unsigned int size, unsigned int *sema)
{
//this method is used to assign manually alligned bufferheader pointers for reserved mempools
//it doesn't provide any garanty on buffer integrity, caller should pay attention to overrides.
bhtmp = GetBH(size);
bhtmp->pinuse = sema;
bhtmp->offset = start;
bhtmp->size = size;
bhtmp->total = size;
bhtmp->move = 0;
/*** Do no update freemem in popbh. We assume this function is used to assign memeory that has already
been reserved through getprivatepool(). Use GetBuffer otherwise. The general rule would imply PopBH
doesn't manage any pool level information, only BH data***/
return bhtmp;
}
BufferHeader *PushBH(BufferHeader *bh, unsigned int size, unsigned int *sema)
{
/***This function provides a bh at the end of the reserved memory. Used on private pools to get a variable length buffer
if *bh is null, a new bh is returned, otherwise, the one linked is used. the code doesn't verify that the bh belongs to the pool, so don't be a dick.
each call will add size to the bh, and reduce it from the pool
***/
if(!bh)
{
bh = GetBH(0);
bh->offset = pool + reserved;
bh->size = 0;
bh->total = 0;
bh->pinuse = sema;
bh->move = 0;
}
bh->size+=size;
bh->total+=size;
freemem-=size;
reserved+=size;
return bh;
}
byte *GetPool()
{
return pool;
}
void Stats()
{
unsigned int i;
fmabs=total;
slack=0;
for(i=0; i<nBH; i++)
{
if(BH[i])
{
if(*BH[i]->pinuse)
{
fmabs -= BH[i]->total;
slack+=BH[i]->total - BH[i]->size;
}
}
}
}
};
class BufferHandler
{
/*** allocates and recycles memory used to hold transactions' raw data
***/
private:
unsigned int *order, *order2, *otmp, S, I, otmpS;
static const int poolstep = 100;
static const int max_fetch = 10;
std::atomic_bool ab, ordering, abu;
std::atomic_uint orderlvl, bufferfetch, bufferfetchu;
std::atomic_int getpoolflag, getpoolflagu;
public:
MemPool **MP, **MPu;
unsigned int npools, npoolsu;
BufferHandler::BufferHandler()
{
MP=0;
otmp=0; otmpS=0;
npools=0;
order=order2=0;
ab=ordering=0;
orderlvl=0;
getpoolflag=0;
bufferfetch=0;
MPu=0;
npoolsu=0;
getpoolflagu=0;
bufferfetchu=0;
abu=0;
}
BufferHandler::~BufferHandler()
{
for(unsigned int i=0; i<npools; i+=poolstep)
delete[] MP[i];
for(unsigned int i=0; i<npoolsu; i++)
delete[] MPu[i];
free(MP);
free(MPu);
free(otmp);
free(order);
free(order2);
}
BufferHeader *GetBuffer(unsigned int size, unsigned int *sema)
{
/*** set new lock system here ***/
unsigned int i;
MemPool *mp;
BufferHeader *bh;
fetchloop:
getpoolflag.fetch_add(1);
while((i=bufferfetch.fetch_add(1))<npools)
{
mp = MP[order[i]];
if(mp->freemem>size || !mp->total) //look for available size
{
bh = mp->GetBuffer(size, sema); //maybe have broken this by passing sema instead of 0
if(bh)
{
getpoolflag.fetch_add(-1);
bufferfetch.store(0);
UpdateOrder(i);
return bh;
}
else UpdateOrder(i);
}
}
getpoolflag.fetch_add(-1);
//either all pools are locked or they're full, add a new batch of pools
if(ab.exchange(1)==FALSE)
{
ExtendPool();
ab.store(0);
}
while(ab) {}
goto fetchloop;
}
void UpdateOrder(unsigned int in)
{
if(orderlvl.fetch_add(1)==poolstep*2)
{
in++;
while(ab) {} //extendpool lock
ordering.store(1);
unsigned int *ordtmp;
memcpy(order2, order+in, sizeof(int)*(npools-in));
memcpy(order2 +npools -in, order, sizeof(int)*in);
ordtmp = order;
order = order2;
order2 = ordtmp;
orderlvl.store(0);
ordering.store(0);
}
}
MemPool *GetPool(unsigned int size, byte* &out)
{
unsigned int i;
MemPool *mp;
fetchloop:
getpoolflag.fetch_add(1);
while((i=bufferfetch.fetch_add(1))<npools)
{
mp = MP[order[i]];
if(mp->freemem>size || !mp->total) //look for available size
{
out = mp->GetPrivatePool(size);
if(out)
{
getpoolflag.fetch_add(-1);
bufferfetch.store(0);
UpdateOrder(i);
return mp;
}
else UpdateOrder(i);
}
}
getpoolflag.fetch_add(-1);
//getting here means there were no virgin pools available, extend the pools
if(ab.exchange(1)==FALSE)
{
bufferfetch.store(npools);
ExtendPool();
bufferfetch.store(0);
ab.store(0);
}
while(ab) {}
goto fetchloop;
}
MemPool *GetPoolU(unsigned int size)
{
unsigned int i;
MemPool *mp;
byte *out;
fetchloop:
getpoolflagu.fetch_add(1);
while((i=bufferfetchu.fetch_add(1))<npoolsu)
{
mp = MPu[i];
if(mp->freemem>size || !mp->total) //look for available size
{
out = mp->GetPrivatePool(size);
if(out)
{
getpoolflagu.fetch_add(-1);
bufferfetchu.store(0);
//UpdateOrder(i);
return mp;
}
//else UpdateOrder(i);
}
}
getpoolflagu.fetch_add(-1);
//getting here means there were no virgin pools available, extend the pools
if(abu.exchange(1)==FALSE)
{
bufferfetchu.store(npoolsu);
ExtendPoolU();
bufferfetchu.store(0);
abu.store(0);
}
while(abu) {}
goto fetchloop;
}
void ExtendPool()
{
while(ordering) {} //updateorder lock
unsigned int F;
S = npools +poolstep;
MemPool **mptmp = (MemPool**)malloc(sizeof(MemPool*)*S);
memcpy(mptmp, MP, sizeof(MemPool*)*npools);
unsigned int *ordtmp = order2;
order2 = (unsigned int*)malloc(sizeof(int)*S);
memcpy(order2 +poolstep, order, sizeof(int)*npools);
MemPool **mptmp2 = MP;
MemPool *mptmp3 = new MemPool[poolstep];
for(I=npools; I<S; I++)
{
F = I-npools;
mptmp[I] = &mptmp3[F];
order2[F] = I;
}
orderlvl.store(0);
MP = mptmp;
free(ordtmp);
ordtmp = order;
order = order2;
while(getpoolflag) {} //lock this thread until all other threads reach the main lock.
order2 = (unsigned int*)malloc(sizeof(int)*S);
free(ordtmp);
free(mptmp2);
npools = S;
}
void ExtendPoolU()
{
unsigned int F, S;
S = npoolsu +poolstep;
MemPool **mptmp = (MemPool**)malloc(sizeof(MemPool*)*S);
memcpy(mptmp, MPu, sizeof(MemPool*)*npoolsu);
MemPool **mptmp2 = MPu;
MemPool *mptmp3 = new MemPool[poolstep];
for(I=npoolsu; I<S; I++)
{
F = I-npoolsu;
mptmp[I] = &mptmp3[F];
}
MPu = mptmp;
while(getpoolflagu) {} //lock this thread until all other threads reach the main lock.
free(mptmp2);
npoolsu = S;
}
unsigned int GetUnusedMem()
{
unsigned int i, m=0;
for(i=0; i<npools; i++)
m+=MP[i]->freemem;
return m;
}
unsigned int GetTotalMem()
{
unsigned int i, m=0;
for(i=0; i<npools; i++)
{
m+=MP[i]->total;
}
return m;
}
void FillRate()
{
unsigned int i, hd=0, ld=0;
float c;
for(i=0; i<npools; i++)
{
c = (float)MP[i]->freemem/(float)MP[i]->total;
if(c<=0.2f) hd++;
else if(c>=0.8f) ld++;
}
float fhd = (float)hd/(float)npools;
float fld = (float)ld/(float)npools;
int abc=0;
}
};
BufferHandler hBuffer;