-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTChandler.cpp
4240 lines (3424 loc) · 88.6 KB
/
BTChandler.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fcntl.h>
#include <io.h>
#include <sys/types.h>
const int max_socket_buffer = 5000;
const int testnet = 1;
unsigned int LAST_BLOCK;
const char Base58str[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
char *ChainFileG=NULL;
unsigned int MWG;
std::atomic_uint totalchain, n_txn, n_vouts, n_vins, at_block;
int nrounds=0;
int CompareBuffers(void *b1, void *b2, int size)
{
int CBa, CBb;
int *b3, *b4, *b5;
CBa = size/4;
b5 = b3 = (int*)b1;
b4 = (int*)b2;
b5+=4;
CBb=0;
while(!CBb & (b3<b5))
{
CBb = *b3 ^ *b4;
b3++; b4++;
}
return CBb;
}
unsigned int B58toI(char in)
{
for(unsigned int i=0; i<58; i++)
if(Base58str[i]==in) return i;
return -1;
}
#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef unsigned __int64 ulong64;
typedef signed __int64 long64;
#else
typedef unsigned long long ulong64;
typedef signed long long long64;
#endif
static ulong64 txfee = 50000;
class MultiFile
{
private:
int na, m, fnameL;
public:
int handle;
unsigned int findex, fcount;
int status;
char *fname;
int ndigits;
char *fext, *dbuffer;
static char *chainfile;
MultiFile::MultiFile()
{
handle = -1;
findex = fcount = 0;
status = 0;
fnameL = 0;
fname = (char*)malloc(MAX_PATH);
ndigits = 0;
dbuffer = (char*)malloc(20);
fext = (char*)malloc(10);
}
MultiFile::~MultiFile()
{
if(handle>-1) _close(handle);
free(fname);
free(dbuffer);
free(fext);
}
static void Init()
{
if(!testnet)
{
strcpy(chainfile, "C:\\Users\\goat\\AppData\\Roaming\\Bitcoin\\blocks\\blk");
}
else
{
strcpy(chainfile, "C:\\Users\\goat\\AppData\\Roaming\\Bitcoin\\testnet3\\blocks\\blk");
}
ChainFileG = chainfile;
}
void SetFile(char *name, char *ext, int ndgt)
{
strcpy(fname, name);
fnameL=strlen(name);
strcpy(fext, ext);
ndigits = ndgt;
}
void Reset()
{
if(handle>-1) _close(handle);
handle = -1;
findex = 0;
status = 0;
}
int OpenFile(int ind)
{
if(!fnameL)
{
strcpy(fname, chainfile);
fnameL = strlen(chainfile);
strcpy(fext, ".dat");
ndigits = 5;
}
if(handle>-1) _close(handle);
else Reset();
if(ind==-1)
{
if(!status) findex=0;
else findex++;
}
else findex=ind;
na = findex; m=1;
while(na/10) m++;
if(m>ndigits) m=ndigits;
na = ndigits - m;
memset(dbuffer, 0x30, ndigits-m);
_itoa(findex, dbuffer +ndigits-m, 10);
fname[fnameL] = 0;
strcat(fname, dbuffer);
strcat(fname, fext);
handle = _open(fname, _O_BINARY | _O_RDONLY);
if(handle==-1 && ind==-1) fcount = findex;
status=1;
return handle;
}
void Close()
{
Reset();
}
MultiFile &operator=(MultiFile &rhs)
{
if(&rhs!=this)
{
this->SetFile(rhs.fname, rhs.fext, rhs.ndigits);
this->fnameL = rhs.fnameL;
this->Reset();
this->fcount = rhs.fcount;
}
return *this;
}
};
char *MultiFile::chainfile = (char*)malloc(MAX_PATH);
#ifdef WIN32
//#include <winsock2.h>
class PlatformSocket
{
private:
SOCKET sock;
sockaddr_in addr;
int a, b, c;
int SetupSocket()
{
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock==INVALID_SOCKET) return 0;
addr.sin_family = AF_INET;
addr.sin_port = htons(8340);
hostent *host;
host = gethostbyname("localhost");
unsigned int zob;
memcpy(&zob, host[0].h_addr_list[0], 4);
addr.sin_addr.s_addr = zob;
return 1;
}
public:
static WSADATA wsaDATA;
static const int ReqVersion = 2;
static int Init()
{
if(WSAStartup(MAKEWORD(ReqVersion, 0), &wsaDATA)!=0) return 0;
if(LOBYTE(wsaDATA.wVersion)<ReqVersion) return 0;
return 1;
}
PlatformSocket::PlatformSocket()
{
SetupSocket();
}
PlatformSocket::~PlatformSocket()
{
closesocket(sock);
}
int Post(char *in)
{
connect(sock, (sockaddr*)&addr, sizeof(sockaddr_in));
if(send(sock, in, strlen(in), 0)<0) return -1;
//what if c<a?
c = 500;
b = a = recv(sock, in, c, 0);
while(a==c && b<max_socket_buffer)
{
if(b+c>max_socket_buffer) c = max_socket_buffer -b;
a = recv(sock, in +b, c, 0);
b+=a;
}
if(b<0) return -1;
if(b>max_socket_buffer) b = max_socket_buffer;
in[b] = 0;
return 0;
}
};
WSADATA PlatformSocket::wsaDATA;
#endif
class BTCsocket
{
private:
PlatformSocket S;
int a, b;
char *clength;
public:
static char *jsonheader;
static int headerlength;
static void Init()
{
strcpy(jsonheader, "POST / HTTP/1.1\r\n");
strcat(jsonheader, "host: localhost:8340\r\n");
//strcat(senddata, "user-agent: ...\r\n");
//strcat(senddata, "accept: application/json-rpc\r\n");
strcat(jsonheader, "content-type: application/json-rpc\r\n");
strcat(jsonheader, "authorization: Basic ZGljazpidXR0\r\n");
strcat(jsonheader, "content-length: ");
headerlength = strlen(jsonheader);
}
BTCsocket::BTCsocket()
{
clength = (char*)malloc(10);
}
BTCsocket::~BTCsocket()
{
free(clength);
}
void Post(char *in)
{
//add id at the end end of the command
strcat(in, ", \"id\":1}\r\n\r\n");
a = strlen(in);
_itoa(a, clength, 10);
b = strlen(clength);
memcpy(in +headerlength +b +4, in, a);
memcpy(in, jsonheader, headerlength);
memcpy(in +headerlength, clength, b);
memcpy(in +headerlength +b, "\r\n\r\n", 4);
in[headerlength +b +4 +a] = 0;
//send it & receive answer in char buffer
S.Post(in);
int m=0;
}
};
char *BTCsocket::jsonheader = (char*)malloc(400);
int BTCsocket::headerlength;
int charcmp(char *i1, char *i2, int lng)
{
int s=0, cc=0;
while(!cc && s<lng) cc |= i1[s] ^ i2[s], s++;
return cc;
}
class HtmlHeader
{
public:
static char **cflags;
static const int ncflags = 2;
int content_length, l, n, i, m, status, t;
float version;
char *body;
char *tmp;
int *flags, nflags;
unsigned int *clrf;
HtmlHeader::HtmlHeader()
{
flags = (int*)malloc(sizeof(int)*20);
}
HtmlHeader::~HtmlHeader()
{
free(flags);
}
static void Init()
{
for(int i=0; i<ncflags; i++)
cflags[i] = (char*)malloc(20);
strcpy(cflags[0], "HTTP/");
strcpy(cflags[1], "Content-Length: ");
}
char *Parse(char *in)
{
l = strlen(in);
if(l>max_socket_buffer) return 0;
//parse header, get end
n = 0;
nflags = 0;
content_length = 0;
while(n<l)
{
clrf = (unsigned int*)(in +n);
if((*clrf & 0x0000FFFF)==0x00000A0D)
{
flags[nflags*2] = content_length;
flags[nflags*2 +1] = n -1 -content_length;
nflags++;
content_length = n+2;
if((*clrf & 0xFFFF0000)==0x0A0D0000) break;
n++;
}
n++;
}
content_length = status = -1; version = -1;
t=0;
for(i=0; i<nflags; i++)
{
tmp = in +flags[i*2];
l = flags[i*2 +1];
if(version==-1)
{
if(!charcmp(tmp, cflags[0], 5))
{
version = (float)atof(tmp+5);
m=5;
while(tmp[m]!=' ' && m<l) m++;
m++;
if(m<l)
{
status = atoi(tmp+m);
}
if(i==nflags-1) break;
i++; t++;
tmp = in +flags[i*2];
l = flags[i*2 +1];
}
}
if(content_length==-1)
{
if(!charcmp(tmp, cflags[1], 16))
{
content_length = atoi(tmp + 16);
if(i==nflags-1) break;
i++; t++;
tmp = in +flags[i*2];
l = flags[i*2 +1];
}
}
if(t==ncflags) break;
}
tmp = in +n +3;
if(*(tmp +content_length +1)) return 0;
return tmp;
}
};
char **HtmlHeader::cflags = (char**)malloc(sizeof(char*)*ncflags);
class JSONhandler
{
public:
static char **cflags;
float version;
char *result, *error, *id, *tmp, C;
byte *intmp;
int l, m, n, g;
static void Init()
{
for(int i=0; i<3; i++)
cflags[i] = (char*)malloc(15);
strcpy(cflags[0], "\"result\":");
strcpy(cflags[1], "\"error\":");
strcpy(cflags[2], "\"id\":");
}
int ParseResult(char *in)
{
l = strlen(in); m=0;
result=error=id=0;
//result
while(m<l)
{
if(in[m]=='"')
{
if(!charcmp(in +m, cflags[0], 9))
{
m+=9;
result = in +m;
if(*result=='[') C = ']', result++;
else C = ',';
while(m<l)
{
if(in[m]==C)
{
in[m] = 0;
break;
}
m++;
}
m++;
break;
}
}
m++;
}
//error
while(m<l)
{
if(in[m]=='"')
{
if(!charcmp(in +m, cflags[1], 8))
{
m+=8;
error = in +m;
while(m<l)
{
if(in[m]==',')
{
in[m] = 0;
break;
}
m++;
}
m++;
break;
}
}
m++;
}
//id
while(m<l)
{
if(in[m]=='"')
{
if(!charcmp(in +m, cflags[2], 5))
{
m+=5;
id = in +m;
while(m<l)
{
if(in[m]=='}')
{
in[m] = 0;
break;
}
m++;
}
m++;
break;
}
}
m++;
}
m = (int)result & (int)error & (int)id;
return m;
}
void GetRawResult(byte *in)
{
//count of data packets is added as first 4 bytes. size of each data packet is written in front of it. as int_32
l = strlen(result);
m=g=0;
C = 34;
intmp = in;
in+=4;
while(m<l)
{
if(result[m]==C)
{
m++;
tmp = result +m;
while(m<l)
{
if(result[m]==C)
{
g++;
n = m -int(tmp -result);
GetByteStream(tmp, in +4, n);
n/=2;
memcpy(in, &n, 4);
in+=n+4;
break;
}
m++;
}
}
m++;
}
memcpy(intmp, &g, 4);
}
};
char **JSONhandler::cflags = (char**)malloc(sizeof(char*)*3);
inline unsigned int varintCompress(ulong64 in, byte *out)
{
if(in<0xFD)
{
out[0] = byte(in);
return 1;
}
else if(in<=0xFFFF)
{
out[0] = 0xFD;
memcpy(out +1, &in, 2);
return 3;
}
else if(in<=0xFFFFFFFF)
{
out[0] = 0xFE;
memcpy(out +1, &in, 4);
return 5;
}
else
{
out[0] = 0xFF;
memcpy(out +1, &in, 8);
return 9;
}
}
struct VINv2
{
int *refOutput;
byte *script;
unsigned int scriptLength;
unsigned int *txnHash;
};
struct VOUTv2
{
ulong64 *value;
byte *script;
unsigned int scriptLength;
byte type;
};
class TXNv2
{
public:
byte *raw;
BufferHeader *bh;
void *holder;
unsigned int nVin, nVout, version, *rawsize;
byte coinbase;
unsigned int *txHash;
unsigned int *locktime;
VINv2 *vin;
VOUTv2 *vout;
int QS; //counts how many branches refer to this txn. If it's 0 or less, txn can be dumped
std::atomic_uint32_t sema;
TXNv2::TXNv2()
{
txHash = 0;
vin=0; vout=0;
sema=1;
holder=0;
QS=0;
}
TXNv2::~TXNv2()
{
}
};
TXNv2 *CopyTXN(BufferHeader *bh, unsigned int size)
{
unsigned int y, z, k, n, Z;
byte usbyte;
TXNv2 *txntmp = (TXNv2*)bh->offset +size;
Z = size + sizeof(TXNv2);
bh->pinuse = (unsigned int*)&txntmp->sema; //LINKING BH->INUSE TO HOLDER'S SEMAPHORE. Needed step for lockless memory defragmentation
txntmp->sema = 1;
byte *raw = bh->offset;
txntmp->raw = raw;
//decode raw tx
//txn version
txntmp->version = (unsigned int)*raw; raw+=4;
//compact size nVin
txntmp->nVin = 0;
usbyte = (byte)raw[0];
if(usbyte<0xFD) txntmp->nVin = usbyte, n=1;
else if(usbyte<0xFE) memcpy(&txntmp->nVin, raw +1, 2), n=3;
else if(usbyte<0xFF) memcpy(&txntmp->nVin, raw +1, 4), n=5;
raw+=n;
VINv2 *vintmp = (VINv2*)(bh->offset + Z);
Z+=sizeof(VINv2)*txntmp->nVin;
txntmp->vin = vintmp;
for(y=0; y<txntmp->nVin; y++)
{
//refered txn hash
vintmp[y].txnHash = (unsigned int*)raw; raw+=32;
k=vintmp[y].txnHash[0];
for(z=1; z<8; z++)
k|=vintmp[y].txnHash[z];
if(k) txntmp->coinbase=0;
else txntmp->coinbase=1;
vintmp[y].refOutput = (int*)raw; raw+=4;
//compact size response script length
vintmp[y].scriptLength = 0;
usbyte = (byte)raw[0];
if(usbyte<0xFD) vintmp[y].scriptLength = usbyte, n=1;
else if(usbyte<0xFE) memcpy(&vintmp[y].scriptLength, raw +1, 2), n=3;
else if(usbyte<0xFF) memcpy(&vintmp[y].scriptLength, raw +1, 4), n=5;
raw+=n;
//script
vintmp[y].script = raw;
raw += vintmp[y].scriptLength;
//sequence
raw+=4;
}
txntmp->nVout = 0;
usbyte = (byte)raw[0];
if(usbyte<0xFD) txntmp->nVout = usbyte, n=1;
else if(usbyte<0xFE) memcpy(&txntmp->nVout, raw +1, 2), n=3;
else if(usbyte<0xFF) memcpy(&txntmp->nVout, raw +1, 4), n=5;
raw+=n;
VOUTv2 *vouttmp = (VOUTv2*)(bh->offset + Z);
Z+=sizeof(VOUTv2)*txntmp->nVout;
txntmp->vout = vouttmp;
for(y=0; y<txntmp->nVout; y++)
{
//value;
vouttmp[y].value = (ulong64*)raw; raw+=8;
//compact size challenge script length
vouttmp[y].scriptLength = 0;
usbyte = (byte)raw[0];
if(usbyte<0xFD) vouttmp[y].scriptLength = usbyte, n=1;
else if(usbyte<0xFE) memcpy(&vouttmp[y].scriptLength, raw +1, 2), n=3;
else if(usbyte<0xFF) memcpy(&vouttmp[y].scriptLength, raw +1, 4), n=5;
raw+=n;
//script
vouttmp[y].script = raw;
raw+=vouttmp[y].scriptLength;
if(vouttmp[y].script[0]==0x41) vouttmp[y].type=1;
else
{
n=0;
while(vouttmp[y].script[n]==0x76) n++; //OP_DUP
if(vouttmp[y].script[n]==0xA9) //OP_HASH160
vouttmp[y].type=0;
}
}
//lock time
txntmp->locktime = (unsigned int*)raw;
raw+=4;
byte* bmp = bh->offset + Z;
txntmp->txHash = (unsigned int*)(bmp +1);
bmp[0]=1;
Z+=33;
bh->Tighten(Z);
return txntmp;
}
void* bh_copy_txn(void *bhin, void *mpin)
{
BufferHeader *BH = (BufferHeader*)bhin, *BHnew;
TXNv2 *txntmp = (TXNv2*)BH->offset;
//build entire txn size
unsigned int m, i, s;
m = BH->size /*- (unsigned int)(txntmp->raw -BH->offset)*/ + *txntmp->rawsize;
//request m from list of unfragged BH
MemPool** MP = (MemPool**)mpin;
s = (unsigned int)*MP;
for(i=1; i<=s; i++)
{
BHnew = MP[i]->GetBuffer(m, 0);
if(BHnew)
{
memcpy(BHnew->offset, txntmp->raw, *txntmp->rawsize);
TXNv2 *TXNnew = CopyTXN(BHnew, *txntmp->rawsize);
//copy hash
memcpy(TXNnew->txHash, txntmp->txHash, 32);
//assign rawsize
TXNnew->rawsize = txntmp->rawsize;
//assign bh
TXNnew->bh = BHnew;
BHnew->move = 0;
//set new txn in holder
TXNnew->holder = txntmp->holder;
void **pass;
pass = (void**)txntmp->holder;
*pass = (void*)TXNnew;
//decrement old sema
txntmp->sema.fetch_add(-1);
/***debug purpose***/
memset(BH->offset, 0, BH->total);
break;
}
}
return 0;
}
void* defrag_thread(void *in)
{
//defrag thread, receives a BufferHandler object as argument
BufferHandler *BH = (BufferHandler*)in;
MemPool **mplf = (MemPool**)malloc(sizeof(MemPool*)*(BH->npools+1));
unsigned int total=0, freemem=0, slack=0, i=0, y=0, reserved=0, a, b, c, e;
int m;
unsigned int *mno=0, mnos=0;
unsigned int *efg = (unsigned int*)malloc(sizeof(int)*(BH->npools +1));
unsigned int *hij = (unsigned int*)malloc(sizeof(int)*(BH->npools +1));
*efg = 0; *hij = 0;
unsigned int *abc = (unsigned int*)malloc(sizeof(int)*BH->npools);
memset(abc, 0, sizeof(int)*BH->npools);
for(i; i<BH->npools; i++)
{
if(BH->MP[i]->total)
{
BH->MP[i]->Stats();
total+=BH->MP[i]->total;
freemem+=BH->MP[i]->fmabs;
slack+=BH->MP[i]->slack;
reserved+=BH->MP[i]->reserved;
}
}
float prt = (float)(freemem+reserved)/float(total);
if(prt>=1.0f)
{
//not enough freemem
//make a list of highly fragmented and low density mempools
for(i=0; i<BH->npools; i++)
{
BH->MP[i]->ComputeMem();
abc[i]=2; //mark as low fragmentation
m = BH->MP[i]->fmabs - BH->MP[i]->freemem;
if(m>0)
{
//mark as highly fragmented
abc[i] = 1;
}
}
for(i=0; i<BH->npools; i++) //build list of fragged and unfragged pools
{
if(abc[i]==1) //fragmented
{
efg[*efg +1] = i;
efg[0]++;
}
else if(abc[i]==2) // clean
{
if((float)BH->MP[i]->freemem>(float)BH->MP[i]->total*0.2f)
{
hij[*hij +1] = i;
hij[0]++;
}
}
}
m=1;
while(m) //order low frag pools by most available space
{
m=0;
for(i=0; i<*hij-1; i++)
{
a = hij[i+1];
b = hij[i+2];
if(BH->MP[a]->freemem<BH->MP[b]->freemem)
{
hij[i+1] = b;
hij[i+2] = a;
m=1;
}
}
}
for(i=1; i<=*hij; i++)
mplf[i] = BH->MP[hij[i]];
*mplf = (MemPool*)*hij;
for(i=0; i<*efg; i++) //run through fragged pools, move end of pools to unfragged pools
{
a = efg[i+1];
//order BHs by offset. there's no guarantee they're tightly packed nor aligned
if(mnos<BH->MP[a]->nBH)
mno = (unsigned int*)realloc(mno, sizeof(int)*BH->MP[a]->nBH), mnos = BH->MP[a]->nBH;
for(y=0; y<BH->MP[a]->nBH; y++)
mno[y] = y;
m=1;
while(m)
{
m=0;
for(y=0; y<BH->MP[a]->nBH-1; y++)
{
b = mno[y]; c = mno[i+y];
if(BH->MP[a]->BH[b]->offset>BH->MP[a]->BH[c]->offset)
{
mno[y] = c;
mno[y+1] = b;
m=1;
}
}
}
b=0;
while(b<BH->MP[a]->nBH)
{
if(BH->MP[a]->BH[mno[b]]->offset) break;
b++;
}
//look for earliest fragmentation
c=-1;
for(b; b<BH->MP[a]->nBH-1; b++)
{
if(!(*BH->MP[a]->BH[mno[b]]->pinuse)) //count is at 0
{
c=b;
break;
}
else
{
e = BH->MP[a]->BH[mno[b+1]]->offset - BH->MP[a]->BH[mno[b]]->offset;
if(e>BH->MP[a]->BH[mno[b]]->total) //unaccounted free space between 2 consecutive BH
{
c=b;
break;
}
}
}
//move everything from c onward
for(b=BH->MP[a]->nBH-1; b>=c; b--)
{
if(*BH->MP[a]->BH[mno[b]]->pinuse && !BH->MP[a]->BH[mno[b]]->move)
{
if(BH->MP[a]->BH[mno[b]]->copy) //if a move function pointer exists for this BH, call it
BH->MP[a]->BH[mno[b]]->copy((void*)BH->MP[a]->BH[mno[b]], (void*)mplf);
}
}
BH->MP[a]->ComputeMem();
/*** go over the mempool again, if the earliest point of fragmentation has changed, there's some more shit to move. Else, go to next one***/
}
}
/*** CLEAN UP ALLOCATED MEM ***/
float slc = (float)slack/float(freemem);
if(slc>0.2)
{
//too much slack
}