-
Notifications
You must be signed in to change notification settings - Fork 1
/
alloc_types.cpp
executable file
·1308 lines (1168 loc) · 48.1 KB
/
alloc_types.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
// See alloc_types.h for thorough documentation on classes and structure.
#include <algorithm>
#include <stdio.h> // The most useful debugger
#include <stdlib.h>
#include "alloc_types.h"
#include "assert.h"
#include "memlib.h" // Useful debugger
// ** NB - foo::check() internal validation routines are
// all defined at the bottom of the file, since they're verbose and
// don't belong with the memory management per se
/*********************
* Utility Functions *
*********************/
// Get small size class of an allocation request
// Large and HUGE have different handling that
// comes in before this, so checking for those sizes here
// is not handy.
size_t get_small_size_class(size_t real_size) {
assert(real_size <= MAX_SMALL_SIZE);
// We've determined that performing linear search on the SMALL_SIZE_CLASSES
// performs much better than binary search, since SMALL_SIZE_CLASSES fits into L1 cache
// and all branch are predicted at 100%
int i;
// In ascending size order, look for smallest fit
for(i=0; i<NUM_SMALL_CLASSES; i++) {
if (real_size <= SMALL_CLASS_SIZES[i]) {
return (i);
// We don't want the size - we want the bin!
}
}
// PANIC! This doesn't actually fit in a small container!
assert(0); // Nothing to check here - we *did* screw up.
return MAX_SIZE_T;
}
// Take a set of chunks that you're about to free. Write internal linked-list pointers
// so that the first_index-th chunk points to the (first_index+1)th chunk....
// ...(last_index-1)th chunk points to the last_index-th chunk
void internally_link_chunks(size_t* first_chunk, size_t first_index, size_t last_index) {
size_t ii;
for (ii = first_index ; ii < (last_index - 1) ; ii++ ) {
*(size_t**)(((byte*)first_chunk) + FINAL_CHUNK_SIZE * ii) = (size_t*)((byte*)(first_chunk) + FINAL_CHUNK_SIZE * (ii+1));
}
}
// Get how many chunks are necessary for a HUGE allocation request
size_t get_num_chunks(size_t huge_allocation) {
int num_chunks = 1;
// There's a clear max size for a single data chunk, but we need
// to account for the size of the header, too.
if (huge_allocation > MAX_SINGLE_CHUNK) {
num_chunks++;
huge_allocation -= MAX_SINGLE_CHUNK;
}
// ...but after the first time there's no header
num_chunks += (huge_allocation-1) / FINAL_CHUNK_SIZE;
return num_chunks;
}
// Get how many chunks are necessary for a Large allocation request
size_t get_num_pages(size_t large_allocation) {
int num_pages = 1;
if (large_allocation > MAX_SINGLE_PAGE) {
num_pages++;
large_allocation -= MAX_SINGLE_PAGE;
}
// ...but after the first time there's no header
num_pages += (large_allocation-1) / PAGE_SIZE;
return num_pages;
}
/*********
* Arena *
*********/
arena_hdr::arena_hdr() {
free_list = NULL; // No free list initially
// We really can't do anything else until we're on the heap.
// It's hard to give children a pointer to us otherwise.
// That chunk has normal metadata for small/large assignments,
// so we should put it in our tree. Again, node_t <-> any header type
/// ...but we can't do this while we're still on the stack!
}
// Called the moment we're allowed to work with the heap!
void arena_hdr::finalize() {
// Now we have our final address, so we can initialize bins
int ii;
for (ii = 0 ; ii < NUM_SMALL_CLASSES ; ii++) {
bin_headers[ii] = arena_bin(this, (size_t)SMALL_CLASS_SIZES[ii],
(size_t)SMALL_CLASS_RUN_PAGE_USAGE[ii]);
bin_headers[ii].finalize();
}
// Also create and initialize a chunk. We can find its address.
assert((mem_heap_lo() <= this) && (this <= mem_heap_hi()));
arena_chunk_hdr* new_address = (arena_chunk_hdr*)((byte*)this + ARENA_HDR_SIZE);
arena_chunk_hdr foo = arena_chunk_hdr(this);
assert((mem_heap_lo() <= new_address) && (new_address <= mem_heap_hi()));
*new_address = foo;
new_address->finalize();
// Take note that this, our first chunk, is the deepest chunk assigned.
deepest = (size_t*)new_address;
// Initialize trees and locks
lock_init();
tree_new(&normal_chunks);
}
// Delegated malloc. Malloc if it's your responsibility, or delegate further.
void* arena_hdr::malloc(size_t size) {
// Two cases we care about: HUGE allocations and Small allocations.
// For the former, we do this ourselves.
// For the latter, we delegate to our bins.
PRINT_TRACE("Entering malloc at the arena level for (%zu).\n", size);
if (size > MAX_LARGE_SIZE) {
// ** Use Huge Mode **
void * new_address = NULL;
void * new_heap = NULL;
// If you're doing a huge allocation, you require the arena-level
// mutex, period.
lock(); // Use own lock method
PRINT_TRACE(" Using a HUGE allocation.\n");
size_t num_chunks = get_num_chunks(size);
PRINT_TRACE(" Number of chunks is %lu\n", num_chunks);
if (free_list != NULL) {
// Try to pull something from the free list
PRINT_TRACE(" The free list has some space; try to pull something from the free list\n");
// Try to find space to place this allocation
// Traverse the free list to find if there's a contiguous block of chunks
size_t num_contiguous_chunks = 1;
size_t * prev = (size_t*)free_list;
size_t * curr = *(size_t**)free_list;
// The pointer to the beginnning of contiguous free chunks
size_t * beg_cont_free_chunks = (size_t*)free_list;
PRINT_TRACE(" Traversing the list to find contiguous free chunks\n");
// Iterate over the free_list until
// we either run off the end of the list OR
// we find the necessary amount of contiguous free chunks
// to place our huge allocation in
while ((curr != NULL) && (num_contiguous_chunks < num_chunks)) {
if ((byte *)curr - (byte *)prev == FINAL_CHUNK_SIZE) {
PRINT_TRACE(" We found two contiguous chunks at addresses %p and %p\n", prev, curr);
// curr and prev pointer are spaced exactly one chunk size apart
// so they are contiguous
num_contiguous_chunks += 1;
} else {
// We either didn't find any contiguous free chunks yet OR
// the number of contiguous chunks wasn't big enough
num_contiguous_chunks = 0; //reset the accumulator
beg_cont_free_chunks = curr; // move the beginning of contiguous chunks
}
prev = (size_t *) curr;
curr = (size_t *) *curr;
}
// After the while loop exits, prev is a pointer
// to a chunk after the current allocation
if (num_contiguous_chunks == num_chunks) {
// We found the perfect spot in the free_list to place our huge allocation in
new_heap = beg_cont_free_chunks;
PRINT_TRACE(" We found the perfect spot in the free list to place our huge allocation in at %p\n", new_heap);
// Write a new huge_run_hdr into the new space.
*(huge_run_hdr*)new_heap = huge_run_hdr(size, num_chunks);
// Take note of the deepst object assigned
deepest = (size_t*)new_heap;
// OK, header in place - let's give them back the pointer, skipping the header
new_address = ((byte*) new_heap + HUGE_RUN_HDR_SIZE);
PRINT_TRACE(" The actual allocation is at the address %p\n", new_address);
// Now we need to update the free list
// Start again from the head of the free_list
// and iterate until you find the address
PRINT_TRACE(" Now cleaning up the free list\n");
size_t * pred = (size_t*)free_list;
size_t * succ = *(size_t**)free_list;
// If the new address is the head of the free_list,
// prev pointer becomes the head of the free_list
if (pred == new_heap) {
PRINT_TRACE(" Our new heap is the head of the free_list\n");
pred = prev;
if (*((size_t*)pred) == NULL) {
PRINT_TRACE(" The free list should be empty\n");
free_list = NULL;
} else {
PRINT_TRACE(" The free list has more elements\n");
// the head of the linked free list becomes the chunk following the newly allocated chunk
free_list = *(size_t **)prev;
PRINT_TRACE(" The new head of the free_list is now at %p\n", free_list);
}
} else {
PRINT_TRACE(" Our new heap is in the middle of the free_list\n");
while ((succ != NULL) && (succ != new_heap)) {
pred = succ;
succ = (size_t *) *succ;
}
if (succ == new_heap) {
// The free list now connects chunks that come before the allocation and after
*(size_t **)(pred) = (size_t *)prev;
PRINT_TRACE(" Updated the free list\n");
} else {
// This case should never happen:
// IF the new heap for huge allocations is not at the beginning of the free list
// AND we couldn't find it in the free list
// THEN something really bad happened
PRINT_TRACE("!!!Something really terrible happened; you should never get here!!!\n");
}
}
} else {
// There were no contiguous free chunks that would fit our huge allocation
// Handle the same we would handle it if the free_list was empty
// Uh-oh. The free list couldn't help us. This needs a *new chunk*.
// Arena is going to demand new space on the heap! Single thread, everything fine.
PRINT_TRACE(" We couldn't find space in the free list. Creating a new chunk for this allocation.\n");
// A point of care: It may be the case that we have an ungrown arena chunk
// on top right now. We need to align the new chunk on top of that.
if ((mem_heapsize() - ARENA_HDR_SIZE) % FINAL_CHUNK_SIZE) {
// Allocate heap up to the next chunk boundary. If we got here, this is a small run.
grow_max((arena_chunk_hdr*)deepest);
}
void* new_heap = mem_sbrk(num_chunks * FINAL_CHUNK_SIZE);
PRINT_TRACE(" Increased the heap by %lu\n", num_chunks * FINAL_CHUNK_SIZE);
assert(new_heap != NULL);
// Write a new huge_run_hdr into the new space.
*(huge_run_hdr*)new_heap = huge_run_hdr(size, num_chunks);
// Take note of the deepst object assigned
deepest = (size_t*)new_heap;
// OK, header in place - let's give them back the pointer, skipping the header
new_address = ((byte*) new_heap + HUGE_RUN_HDR_SIZE);
}
} else {
// Uh-oh. The free list couldn't help us. This needs a *new chunk*.
// Arena is going to demand new space on the heap! Single thread, everything fine.
PRINT_TRACE(" There's no free list. Creating a new chunk for this allocation.\n");
// A point of care: It may be the case that we have an ungrown arena chunk
// on top right now. We need to align the new chunk on top of that.
if ((mem_heapsize() - ARENA_HDR_SIZE) % FINAL_CHUNK_SIZE) {
// Allocate heap up to the next chunk boundary. If we got here, this is a small run.
grow_max((arena_chunk_hdr*)deepest);
}
void* new_heap = mem_sbrk(num_chunks * FINAL_CHUNK_SIZE);
PRINT_TRACE(" Increased the heap by %lu\n", num_chunks * FINAL_CHUNK_SIZE);
assert(new_heap != NULL);
// Write a new huge_run_hdr into the new space.
*(huge_run_hdr*)new_heap = huge_run_hdr(size, num_chunks);
// Take note of the deepst object assigned
deepest = (size_t*)new_heap;
// OK, header in place - let's give them back the pointer, skipping the header
new_address = ((byte*) new_heap + HUGE_RUN_HDR_SIZE);
}
PRINT_TRACE(" ...succeeded, at %p.\n", new_address);
unlock(); // Unlock arena
return (void*) (new_address);
} else if (size <= MAX_SMALL_SIZE) {
// ** Use Small Mode **
PRINT_TRACE(" Using a small allocation.\n");
// Make sure our sizer is working properly.
assert (get_small_size_class(size) != MAX_SIZE_T);
// Now make a bin do the work
// Note - the bin no longer cares about the size.
PRINT_TRACE(" ...delegating to bin %zu (%d).\n", get_small_size_class(size), SMALL_CLASS_SIZES[get_small_size_class(size)]);
size_t bin_index = get_small_size_class(size);
return bin_headers[bin_index].malloc();
} else {
// ** Use Large Mode **
PRINT_TRACE(" Using a Large allocation.\n");
// We need to ask chunks to try to fit this thing.
// Fortunately, that is *probably* just a tree lookup.
lock();
node_t* lowest_normal_chunk = tree_first(&normal_chunks);
size_t consec_pages = get_num_pages(size);
byte* new_address;
while (lowest_normal_chunk != NULL) {
PRINT_TRACE(" ...asking chunk %p to fit %zu pages...\n", lowest_normal_chunk, consec_pages);
new_address = (byte*)((arena_chunk_hdr*)(lowest_normal_chunk))->fit_large_run(consec_pages);
if (new_address != NULL) {
// Got one! Bookkeeping has been done already
PRINT_TRACE(" ...succeeded, at %p.\n", new_address);
unlock();
return new_address;
}
lowest_normal_chunk = tree_next(&normal_chunks, lowest_normal_chunk);
}
PRINT_TRACE(" ...couldn't find any space, so we need a new chunk.\n");
// Allocate and prepare a new chunk.
// To do that, you must own the arena lock!
arena_chunk_hdr* new_chunk = add_normal_chunk();
unlock();
// A new chunk *will* have space for a Large allocation
return new_chunk->fit_large_run(consec_pages);
}
}
// Delegated free
void arena_hdr::free(void* ptr) {
// Determine if this is a HUGE allocation. We know if this is a HUGE allocation if it's
// on a chunk boundary.
// First, get the distance from the end of the header (ptr - this)
// Then, subtract the arena metadata offset (ARENA_HDR_SIZE)
// If that falls on the first page of a multiple of FINAL_CHUNK_SIZE we are in business.
size_t header_offset = ((byte*)ptr - (byte*)this - ARENA_HDR_SIZE);
if (header_offset % FINAL_CHUNK_SIZE <= PAGE_SIZE ) {
// If you're doing a huge deallocation, you require the arena mutex, period
lock();
PRINT_TRACE("Deallocating a HUGE chunk at %p.\n", ptr);
// We know by computation this lies on a HUGE chunk boundary and
// is a HUGE allocation
// Find HUGE allocation header
huge_run_hdr* header = (huge_run_hdr*)(((byte*)(ptr)) - HUGE_RUN_HDR_SIZE);
PRINT_TRACE(" Header data located at %p.\n", header);
// OK, now we can get freeing! Iteratively add freed chunks to the free list
// We keep the free list sorted for contiguity checks.
// Assemble the free list links
PRINT_TRACE(" Writing %lu internal headers.\n", (header->num_chunks -1));
internally_link_chunks((size_t*)header, 0, header->num_chunks);
// Also, save the first link target and last link site to help us
// The first chunk link address is just the header
size_t** last_chunk_link_site = (size_t**)(((byte*)(header)) + FINAL_CHUNK_SIZE * (header->num_chunks-1));
if (free_list == NULL) {
PRINT_TRACE(" Creating a chunk free list.\n");
// Attach out segment to the free list
free_list = (size_t*)header;
*last_chunk_link_site = NULL;
} else {
PRINT_TRACE(" Hey, we already have a free list!\n");
size_t * curr = *(size_t**)free_list;
size_t * prev = (size_t*)free_list;
PRINT_TRACE(" Recursing free list...\n");
while ((curr != NULL) && (curr < (size_t*)header)) {
prev = curr;
curr = (size_t *) *curr;
}
*(size_t **)(prev) = (size_t *)header;
*(size_t **)last_chunk_link_site = (size_t *)(curr);
}
unlock();
} else {
PRINT_TRACE("I saw a header_offset of %zu, so I'm asking a chunk to free.\n", header_offset);
// Which chunk is this in? Try using header_offset / FINAL_CHUNK_SIZE to index
arena_chunk_hdr* owning_chunk = (arena_chunk_hdr*)((byte*)this + ARENA_HDR_SIZE + (header_offset/FINAL_CHUNK_SIZE)*FINAL_CHUNK_SIZE);
PRINT_TRACE("...I think chunk %p (%zu chunks forward) will do.\n", owning_chunk, header_offset/FINAL_CHUNK_SIZE);
// Delegate!
owning_chunk->free(ptr);
}
}
void* arena_hdr::realloc(void* ptr, size_t size, size_t old_size) {
size_t header_offset = ((byte*)ptr - (byte*)this - ARENA_HDR_SIZE);
if (header_offset % FINAL_CHUNK_SIZE <= PAGE_SIZE ) {
// If the new size isn't huge, no cleverness possible.
if (size < MAX_LARGE_SIZE)
return NULL;
// If you're going to to a huge realloc, you need the arena mutex to manipulate chunks
PRINT_TRACE("Trying clever realloc on a HUGE allocation.\n");
// This is a huge allocation, and at this level we can decide what
// to do with the chunks
huge_run_hdr* header = (huge_run_hdr*)(((byte*)(ptr)) - HUGE_RUN_HDR_SIZE);
size_t new_size_chunks = get_num_chunks(size);
size_t old_size_chunks = get_num_chunks(old_size);
if (new_size_chunks == old_size_chunks) {
PRINT_TRACE("Size is close enough; aborting.\n");
// Awesome! Nothing to be done. It goes in the same place.
return ptr;
} else if (new_size_chunks < old_size_chunks) {
lock();
PRINT_TRACE("Realloc is much smaller; shrinking.\n");
// Update header of this allocation
header->num_chunks = new_size_chunks;
// Internally link chunks for insertion onto free list
PRINT_TRACE(" Writing %zu internal headers.\n", (old_size_chunks - new_size_chunks - 1));
internally_link_chunks((size_t*)header, new_size_chunks, old_size_chunks);
// Put chunks on the free list
size_t** last_chunk_link_site = (size_t**)(((byte*)(header)) + FINAL_CHUNK_SIZE * (old_size_chunks-1));
if (free_list == NULL) {
PRINT_TRACE(" Creating a chunk free list.\n");
// Attach out segment to the free list
free_list = (size_t*)header;
*last_chunk_link_site = NULL;
} else {
PRINT_TRACE(" Hey, we already have a free list!\n");
size_t * curr = *(size_t**)free_list;
size_t * prev = (size_t*)free_list;
PRINT_TRACE(" Recursing free list...\n");
while ((curr != NULL) && (curr < (size_t*)header)) {
prev = curr;
curr = (size_t *) *curr;
}
*(size_t **)(prev) = (size_t *)header;
*(size_t **)last_chunk_link_site = (size_t *)(curr);
}
// We resized down, but you can keep the pointe
unlock();
return ptr;
} else { // new_size_chunks > old_size_chunks
lock();
PRINT_TRACE(" Realloc is much bigger; growing?\n");
// We need to make this chunk bigger, or give up and
// alloc and free
if ((size_t*)header == deepest) {
PRINT_TRACE(" We are on top. Growing.\n");
// Best case! We can just grow the heap; we're on top.
header->num_chunks = new_size_chunks;
// The arena header is allowed to grow the heap
mem_sbrk((new_size_chunks-old_size_chunks) * FINAL_CHUNK_SIZE);
// In the end, nothing moves
unlock();
return ptr;
} else {
PRINT_TRACE(" Not on top, cannot grow.\n");
// We're not on top; we can't necessarily grow;
// Guess we're stuck; do this the hard way.
// TODO: OPT: This can check the linked list for adjacency
// to determine that we may be able to consume adjacent
// freed space, even if we're not on top.
unlock();
return NULL;
}
}
} else {
// Delegate to a chunk
arena_chunk_hdr* owning_chunk = (arena_chunk_hdr*)((byte*)this + ARENA_HDR_SIZE + (header_offset/FINAL_CHUNK_SIZE)*FINAL_CHUNK_SIZE);
// We already calculated the old alloc size; we might as well pass it
return owning_chunk->realloc(ptr, size, old_size);
}
}
// realloc needs to know how big something is. This will tell you.
size_t arena_hdr::size_of_alloc(void* ptr) {
size_t header_offset = ((byte*)ptr - (byte*)this - ARENA_HDR_SIZE);
if (header_offset % FINAL_CHUNK_SIZE <= PAGE_SIZE) {
// This allocation is HUGE
huge_run_hdr* header = (huge_run_hdr*)(((byte*)ptr) - HUGE_RUN_HDR_SIZE);
// The size is the number of pages spanned, minus header size
return (header->num_chunks * FINAL_CHUNK_SIZE - HUGE_RUN_HDR_SIZE);
// Note: Copying bricks of memory is ~fast, so we have no need to store
// and track the actual size.
} else {
// Large or small
arena_chunk_hdr* owning_chunk = (arena_chunk_hdr*)((byte*)this + ARENA_HDR_SIZE + (header_offset/FINAL_CHUNK_SIZE)*FINAL_CHUNK_SIZE);
// Delegation is a beautiful thing
return owning_chunk->size_of_alloc(ptr);
}
}
// We need more space. We've got no chunks to expand. Let's try this.
// This can only be called if you ALREADY OWN the arena lock
arena_chunk_hdr* arena_hdr::add_normal_chunk() {
// We know we've currently got heap up to a chunk limit - if we didn't,
// we would have grown a small chunk.
// We might have something on the free list. That would be good.
arena_chunk_hdr* new_chunk;
if (free_list != NULL) {
new_chunk = (arena_chunk_hdr*)free_list;
// Bind the free list head to its next element
free_list = *(size_t**)free_list;
*new_chunk = arena_chunk_hdr(this);
new_chunk->finalize();
insert_chunk((node_t*)new_chunk);
return new_chunk;
} else {
new_chunk = (arena_chunk_hdr*)mem_sbrk(INITIAL_CHUNK_SIZE);
*new_chunk = arena_chunk_hdr(this);
new_chunk->finalize();
deepest = (size_t*)new_chunk; // Take note that this is now the deepst chunk
insert_chunk((node_t*)new_chunk); // Also, it's new and has space in it
return new_chunk;
}
}
// This can only be called if you ALREADY OWN the arena lock
void arena_hdr::insert_chunk(node_t* chunk) {
assert((mem_heap_lo() <= chunk) && (chunk <= mem_heap_hi()));
assert((mem_heap_lo() <= &normal_chunks) && (&normal_chunks <= mem_heap_hi()));
PRINT_TRACE("Inserting a chunk into a tree; did you know that?\n");
assert(tree_search(&normal_chunks, chunk) == NULL);
tree_insert(&(normal_chunks), chunk);
}
// A chunk is full. Drop it.
// This can only be called if you ALREADY OWN the arena lock
void arena_hdr::filled_chunk(node_t* filled) {
// Removing something not in the tree is bad!
assert(tree_search(&normal_chunks, filled) != NULL);
tree_remove(&normal_chunks, filled);
}
// Tell a chunk how many pages it is allowed to be, knowing that it has
// requested more pages.
// This can only be called if you ALREADY OWN the arena lock
size_t arena_hdr::grow(arena_chunk_hdr* chunk) {
if ((size_t*)chunk == deepest) {
PRINT_TRACE("Growing the deepest chunk.\n");
assert(chunk->num_pages_allocated * 2 <= FINAL_CHUNK_PAGES);
// We have open VM ahead of us
mem_sbrk(chunk->num_pages_allocated * PAGE_SIZE);
return chunk->num_pages_allocated * 2;
} else {
PRINT_TRACE("Fully inflating a chunk that's not the deepest.\n");
// Something's already ahead of you! Grow, grow!
return FINAL_CHUNK_PAGES;
}
}
// You know, for a fact, that you want a chunk grown to max size
// This can only be called if you ALREADY OWN the arena lock
size_t arena_hdr::grow_max(arena_chunk_hdr* chunk) {
if ((size_t*)chunk == deepest) {
PRINT_TRACE("Maxing out a chunk!\n");
mem_sbrk((FINAL_CHUNK_PAGES - chunk->num_pages_allocated) * PAGE_SIZE);
return FINAL_CHUNK_PAGES;
} else {
// grow(chunk) will inflate this anyway
return grow(chunk);
}
}
/**********************
* Arena Chunk Header *
**********************/
arena_chunk_hdr::arena_chunk_hdr(arena_hdr* _parent) {
parent = _parent;
num_pages_allocated = INITIAL_CHUNK_PAGES;
num_pages_available = num_pages_allocated-1; // The header consumes one page
// Initialize the page map
memset(&page_map, FREE, (sizeof(uint8_t) * (FINAL_CHUNK_PAGES)));
page_map[0] = HEADER;
}
void arena_chunk_hdr::finalize() {
lock_init();
//tree_new(&clean_page_runs);
}
// An arena has told us this memory belongs to us. Free it.
void arena_chunk_hdr::free(void* ptr) {
size_t bin = get_page_index((byte*)ptr);
PRINT_TRACE(" arena_chunk_hdr has located a pointer into page %zu (%d).\n", bin, page_map[bin]);
assert((page_map[bin] == SMALL_RUN_HEADER) ||
(page_map[bin] == SMALL_RUN_FRAGMENT) ||
(page_map[bin] == LARGE_RUN_HEADER));
if (page_map[bin] == LARGE_RUN_HEADER) {
lock();
size_t num_pages = ((large_run_hdr*)get_page_location(bin))->num_pages;
int ii;
for(ii = 0 ; ii < num_pages ; ii++) {
page_map[bin + ii] = FREE;
// TODO: OPT: Treed page run management
}
// Note that cells have been returned for rapid bookkeeping
if ((num_pages_available == 0) && (num_pages_allocated == FINAL_CHUNK_PAGES)) {
// We're about to stop being full
parent->lock();
PRINT_TRACE("Inserting a chunk into normal chunks.\n");
assert(tree_search(&(parent->normal_chunks), (node_t*)this) == NULL);
tree_insert(&(parent->normal_chunks), (node_t*)this);
parent->unlock();
}
num_pages_available += num_pages;
unlock();
} else {
// You'll need to find the appropriate control structure.
while (page_map[bin] == SMALL_RUN_FRAGMENT) {
bin--;
}
assert(page_map[bin] == SMALL_RUN_HEADER);
// Now we're looking at a small header
small_run_hdr* this_run = ((small_run_hdr*)get_page_location(bin));
this_run->free(ptr);
}
}
void* arena_chunk_hdr::realloc(void* ptr, size_t size, size_t old_size) {
size_t bin = get_page_index((byte*)ptr);
PRINT_TRACE(" arena_chunk_hdr has located a pointer into page %zu (%d).\n", bin, page_map[bin]);
assert((page_map[bin] == SMALL_RUN_HEADER) ||
(page_map[bin] == SMALL_RUN_FRAGMENT) ||
(page_map[bin] == LARGE_RUN_HEADER));
if (page_map[bin] == LARGE_RUN_HEADER) {
PRINT_TRACE(" Looks like we're reallocating a large run.\n");
// Possible early termination - we want to go from Large to small or HUGE
if ((size < MAX_SMALL_SIZE) || (size > MAX_LARGE_SIZE))
return NULL;
// OK, we're dealing with a Large run. Three cases, same as HUGE
size_t old_size_pages = ((large_run_hdr*)get_page_location(bin))->num_pages;
size_t new_size_pages = get_num_pages(size);
PRINT_TRACE(" ...from %zu pages to %zu pages.\n", old_size_pages, new_size_pages);
if (new_size_pages == old_size_pages) {
PRINT_TRACE(" ...so we're keeping it in place.\n");
// Perfect! It stays in place
return ptr;
} else if (new_size_pages < old_size_pages) {
lock();
// We can free some pages off the end
PRINT_TRACE(" ...so we're freeing some off the end.\n");
((large_run_hdr*)get_page_location(bin))->num_pages = new_size_pages;
int ii;
for (ii = new_size_pages ; ii < old_size_pages ; ii++) {
page_map[ii] = FREE;
}
// Make sure to note these pages are actually available
num_pages_available += (old_size_pages - new_size_pages);
// Leave original poitner untouched
unlock();
return ptr;
} else {
PRINT_TRACE(" ...so we're trying to extend.\n");
// We need to try to extend in place. It's possible there are free pages
// on top of us to allow us to do so.
// TODO: NEXT: Chunk may be growable
// It's possible we're asking to extend off the end and should abort
lock();
if (bin + new_size_pages > num_pages_allocated) {
PRINT_TRACE(" ...but we can't.\n");
unlock();
return NULL;
}
int ii;
for (ii = old_size_pages ; ii < new_size_pages ; ii++) {
if (page_map[bin + ii] != FREE) {
PRINT_TRACE(" ..but we ran into something.\n");
unlock();
return NULL;
}
}
//...if we're down here, we can actually extend
((large_run_hdr*)get_page_location(bin))->num_pages = new_size_pages;
for (ii = old_size_pages ; ii < new_size_pages ; ii++) {
page_map[bin + ii] = LARGE_RUN_FRAGMENT;
}
unlock();
return ptr;
}
} else {
// This is a small run fragment or header
// Locate the control structure and delegate
while (page_map[bin] == SMALL_RUN_FRAGMENT) {
bin--;
}
assert(page_map[bin] == SMALL_RUN_HEADER);
return ((small_run_hdr*)get_page_location(bin))->realloc(ptr, size, old_size);
}
}
// Given a pointer, determine its allocation size.
// e.g. used by realloc to find size of pointer being freed
size_t arena_chunk_hdr::size_of_alloc(void* ptr) {
size_t bin = get_page_index((byte*)ptr);
// Mostly the same rules as free
assert((page_map[bin] == SMALL_RUN_HEADER) ||
(page_map[bin] == SMALL_RUN_FRAGMENT) ||
(page_map[bin] == LARGE_RUN_HEADER));
if (page_map[bin] == LARGE_RUN_HEADER) {
/// Allocation size calculable from large_run_hdr
size_t num_pages = ((large_run_hdr*)get_page_location(bin))->num_pages;
return (num_pages * PAGE_SIZE);
} else if (page_map[bin] == SMALL_RUN_HEADER) {
// Allocation size readable from header
return ((small_run_hdr*)get_page_location(bin))->parent->object_size;
} else { // fragment, then
// *Find* the header and read the size
while (page_map[bin] == SMALL_RUN_FRAGMENT) {
bin--;
}
assert(page_map[bin] == SMALL_RUN_HEADER);
return ((small_run_hdr*)get_page_location(bin))->parent->object_size;
}
}
// TODO: OPT: Replace all this with tree management in clean_page_runs
// Can coalesce there. Or really, make this anything less painful than linear search
// You have free pages. Do you have consec_pages in a row? Make a large run there.
void* arena_chunk_hdr::fit_large_run(size_t consec_pages) {
PRINT_TRACE(" Trying to fit into chunk %p, which has %zu free pages (%zu total).\n", this, num_pages_available, num_pages_allocated);
// Three segments -
// 1. If you have enough free pages, try to fit
// 2. Try to grow
// 3. If you're *sure* you grew enough to fit, fit.
// ** 1. If you have enough free pages, the attempt can be made **
lock();
if (consec_pages <= num_pages_available) {
PRINT_TRACE(" Making fit attempt, at least.\n");
int consec = 0;
int ii;
for (ii = 1 ; ii < num_pages_allocated ; ii++) {
if (page_map[ii] == FREE) {
consec++;
if (consec == consec_pages) {
// We've found space for a large run. Make it so.
int start_point = ii + 1 - consec;
PRINT_TRACE(" We've found consecutive slots for the large allocation.\n");
PRINT_TRACE(" %zu of them, starting at %d (%p)\n", consec_pages, start_point, get_page_location(start_point));
page_map[start_point] = LARGE_RUN_HEADER;
int jj;
for (jj = 1 ; jj < consec_pages ; jj++) {
page_map[start_point + jj] = LARGE_RUN_FRAGMENT;
}
byte* new_address = get_page_location(start_point);
*(large_run_hdr*)new_address = large_run_hdr(consec_pages);
num_pages_available -= consec_pages;
if ((num_pages_available == 0) && (num_pages_allocated == FINAL_CHUNK_PAGES)) {
// Actually, this already owns the parent lock. It's inefficient but it's
// not buggy. TODO: Resolve.
// parent->lock();
PRINT_TRACE("--Chunk is definitely full--\n");
assert(tree_search(&(parent->normal_chunks), (node_t*)this) != NULL);
tree_remove(&(parent->normal_chunks), (node_t*)this);
// See above
// parent->unlock();
}
// This returns the *address* for use.
unlock();
return (new_address + LARGE_RUN_HDR_SIZE);
}
} else {
consec = 0;
}
}
}
// ** 2. Try to grow **
if (( FINAL_CHUNK_PAGES - num_pages_allocated) > consec_pages) {
// We've determined growing can work. Note: If there are no small runs, growing MAY work,
// but you are on dangerous ground there.
PRINT_TRACE(" Growing chunk for large run.\n");
PRINT_TRACE(" We need %zu pages, and are currently %zu big.\n", consec_pages, num_pages_allocated);
size_t old_allocation = num_pages_allocated;
// Grow generously
while ((num_pages_allocated - old_allocation) < consec_pages) {
parent->lock();
num_pages_allocated = parent->grow(this);
parent->unlock();
PRINT_TRACE(" ...%zu big...\n", num_pages_allocated);
}
num_pages_available += (num_pages_allocated - old_allocation);
// ** 3. Definitely fit **
// At this point, we know perfectly well the *first* N new pages are open
// ...but maybe a few more, too.
int ii, jj;
size_t start_point = 1; // 0 contains header data, so you're not using that!
for (ii = old_allocation ; ii > 0 ; ii--) {
PRINT_TRACE(" Backwalking page %d looking for end-of-free...\n", ii);
if (page_map[ii] != FREE) {
PRINT_TRACE(" ...but it's safe to start at page %d.\n", ii+1);
start_point = ii+1;
break;
}
}
page_map[start_point] = LARGE_RUN_HEADER;
for (jj = 1 ; jj < consec_pages ; jj++) {
page_map[start_point + jj] = LARGE_RUN_FRAGMENT;
}
byte* new_address = get_page_location(start_point);
*(large_run_hdr*)new_address = large_run_hdr(consec_pages);
num_pages_available -= consec_pages;
if ((num_pages_available == 0) && (num_pages_allocated == FINAL_CHUNK_PAGES)) {
parent->lock();
PRINT_TRACE("--Chunk is definitely full--\n");
assert(tree_search(&(parent->normal_chunks), (node_t*)this) != NULL);
tree_remove(&(parent->normal_chunks), (node_t*)this);
parent->unlock();
}
unlock();
return (new_address + LARGE_RUN_HDR_SIZE);
} else {
PRINT_TRACE(" ...but this chunk can't fit it even by growing (currently %zu pages).\n", this->num_pages_allocated);
unlock();
return NULL;
}
}
// You have free pages. Someone needs a small run. Go for it.
small_run_hdr* arena_chunk_hdr::carve_small_run(arena_bin* owner) {
PRINT_TRACE(" Entering small run carver to allocate %zu consecutive pages.\n", owner->run_length / PAGE_SIZE);
PRINT_TRACE(" Before allocating, we have %zu pages left.\n", num_pages_available);
size_t consec_pages = owner->run_length / PAGE_SIZE;
lock();
if (consec_pages < num_pages_available) {
// We can at least try to fit
// Crawl the page map, looking for a place to fit
// TODO: Use a tree implementation instead
int consec = 0;
int ii;
for (ii = num_pages_allocated-1 ; ii >= 0 ; ii--) {
if (page_map[ii] == FREE) {
consec++;
if (consec == consec_pages) {
PRINT_TRACE(" We've found consecutive slots for the small allocation.\n");
PRINT_TRACE(" %zu of them, starting at %d (%p)\n", consec_pages, ii, get_page_location(ii));
small_run_hdr* new_page = (small_run_hdr*)get_page_location(ii);
PRINT_TRACE(" Installing new small run at %p.\n", new_page);
*new_page = small_run_hdr(owner);
new_page->finalize();
page_map[ii] = SMALL_RUN_HEADER;
int jj;
for (jj = 1 ; jj < consec_pages ; jj++) {
page_map[ii+jj] = SMALL_RUN_FRAGMENT;
}
// Let's finish the construction properly by making it available
// to the owner of bins of that size
owner->run_available((node_t*) new_page);
num_pages_available -= consec_pages;
// This returns the *run* for use.
unlock();
return new_page;
}
} else {
consec = 0;
}
}
}
// Well, that didn't help. How about growing? Does that help?
if ((FINAL_CHUNK_PAGES - num_pages_allocated) > consec_pages) {
PRINT_TRACE(" Growing chunk for small run.\n");
size_t old_allocation = num_pages_allocated;
while ((num_pages_allocated - old_allocation) < consec_pages) {
parent->lock();
num_pages_allocated = parent->grow(this);
parent->unlock();
PRINT_TRACE(" ...%zu big...\n", num_pages_allocated);
}
num_pages_available = (num_pages_allocated - old_allocation);
// At this point, we know perfectly well the last N pages are fair game
int ii = num_pages_allocated - consec_pages, jj; // Imagine a +1, -1 there
page_map[ii] = SMALL_RUN_HEADER;
for (jj = 1 ; jj < consec_pages ; jj++) {
page_map[ii+jj] = SMALL_RUN_FRAGMENT;
}
small_run_hdr* new_page = (small_run_hdr*)get_page_location(ii);
*new_page = small_run_hdr(owner);
new_page->finalize();
owner->run_available((node_t*) new_page);
num_pages_available -= consec_pages;
unlock();
return new_page;
} else {
PRINT_TRACE(" Even a grown chunk won't fit this; try somewhere else.\n");
unlock();
return NULL;
}
// Sorry, friend. You'll have to go somewhere else.
return NULL;
}
// Conversion routines
inline byte* arena_chunk_hdr::get_page_location(size_t page_no) {
return ((byte*)this + (page_no * PAGE_SIZE));
}
inline size_t arena_chunk_hdr::get_page_index(byte* page_addr) {
return (page_addr - (byte*)this) / PAGE_SIZE;
}
/*************
* Arena Bin *
*************/
// Decoy constructor used to help with list initialization
arena_bin::arena_bin() {
current_run = NULL; // ...we're at least going to be safe about pointers.
};
// Proper constructor
arena_bin::arena_bin(arena_hdr* _parent, size_t _object_size, size_t num_pages) {
parent = _parent;
current_run = NULL;
object_size = _object_size;
run_length = PAGE_SIZE * num_pages; // TODO: Assign multiple pages to runs of larger objects
available_registrations = (run_length - SMALL_RUN_HDR_SIZE) / object_size;
}
void arena_bin::finalize() {
// Finalize trees and locks once heaped
lock_init();
tree_new(&available_runs);
}
// Delegated malloc. Sorry, you're it - you're going to have to figure it out.
void* arena_bin::malloc() {
PRINT_TRACE(" Entering malloc at the arena_bin level.\n");
lock(); // Lock this bin
// If we have a current run, we can ask it to malloc. But otherwise...
if (current_run == NULL) {
PRINT_TRACE(" No current run; choosing from tree.\n");
// All right, let's get a chunk from the tree then!
node_t* new_run = tree_first(&available_runs);
if (new_run != NULL) {
PRINT_TRACE(" ...got a run from the tree (%p).\n", new_run);
current_run = (small_run_hdr*)new_run;
} else {
// Get a chunk from our parent
PRINT_TRACE(" No good, we need a chunk from a parent.\n");
node_t* new_chunk;
byte* new_address;
// Care: Do we need to lock on this read?
new_chunk = tree_first(&parent->normal_chunks);
while (new_chunk != NULL) {
PRINT_TRACE(" ...asking chunk %p to fit %zu pages...\n", new_chunk, run_length/PAGE_SIZE);
new_address = (byte*)(((arena_chunk_hdr*)(new_chunk))->carve_small_run(this));
if (new_address != NULL) {
// A new small run has been allocated for us. Move along.
PRINT_TRACE(" ...succeeded, at %p.\n", new_address);
current_run = (small_run_hdr*)new_address;
break;
}
new_chunk = tree_next(&parent->normal_chunks, new_chunk);
}
if (new_address == NULL) {
PRINT_TRACE(" Argh! There's not a single chunk we can work with.\n");
// More space! Parent, take care of it.
parent->lock();
new_chunk = (node_t*)parent->add_normal_chunk();
parent->unlock();
current_run = ((arena_chunk_hdr*)new_chunk)->carve_small_run(this);
}
}
} else {
PRINT_TRACE(" We're just going to use the current run at %p.\n", current_run);
}
assert(current_run != NULL);
PRINT_TRACE(" Assigning this allocation to the run at %p.\n", current_run);
PRINT_TRACE(" ...which is serving objects of size %zu.\n", current_run->parent->object_size);
// We're set up either way, so now we can just have the run malloc
void* ret = current_run->malloc();
unlock();
return ret;
}
void arena_bin::run_available(node_t* avail_run) {
// Make sure we don't somehow have a duplicate entry
assert(tree_search(&available_runs, avail_run) == NULL);
tree_insert(&available_runs, avail_run);
}
// Note that a run is full and should not be considered for runs.
void arena_bin::filled_run(node_t* full_run) {
// You can only remove a run from a tree if it exists
assert(tree_search(&available_runs, full_run) != NULL);
tree_remove(&available_runs, full_run);
assert(tree_search(&available_runs, full_run) == NULL);
if (full_run == (node_t*) current_run) { // Not anymore!
PRINT_TRACE(" ...and it was the current run.\n");
current_run = NULL;