@@ -48,10 +48,10 @@ namespace my {
48
48
49
49
struct MemoryBlock {
50
50
void * threadInfo;
51
+ uint32_t size; // size is the entire block size, including the header and the footer
52
+ bool isFree;
51
53
MemoryBlock * nextFreeBlock; // pointer to the next free block in the binned free list that this belongs to.
52
54
MemoryBlock * previousFreeBlock; // pointer to the previous free block in the binned free list that this belongs to.
53
- uint32_t size; // size is the entire block size, including the header and the footer
54
- bool isFree;;
55
55
};
56
56
57
57
struct ThreadSharedInfo {
@@ -61,15 +61,18 @@ struct ThreadSharedInfo {
61
61
62
62
typedef uint32_t MemoryBlockFooter;
63
63
64
- #define TOTAL_BLOCK_OVERHEAD (sizeof (MemoryBlock) + sizeof (MemoryBlockFooter))
64
+ #define FREE_BLOCK_OVERHEAD (sizeof (MemoryBlock) + sizeof (MemoryBlockFooter))
65
+ #define ALLOCATED_BLOCK_OVERHEAD (FREE_BLOCK_OVERHEAD - 2 * sizeof (MemoryBlock *))
66
+ #define MINIMUM_ALLOCATED_BLOCK_SIZE ALIGN (FREE_BLOCK_OVERHEAD)
67
+
65
68
#define BIN_INDEX_THRESHOLD 1024
66
69
#define NUM_OF_BINS 150
67
70
#define FREE_BLOCK_SPLIT_THRESHOLD 8
68
71
#define MEM_SBRK_REQUEST_FACTOR 64
69
- #define MB_ADDRESS_TO_INTERNAL_SPACE_ADDRESS (mbptr ) ((void *) ((char *)(mbptr) + sizeof (MemoryBlock))) // given a MemoryBlock pointer mbptr, returns the internal space address that should be visible to the user
72
+ #define MB_ADDRESS_TO_INTERNAL_SPACE_ADDRESS (mbptr ) ((void *) ((char *)(mbptr) + sizeof (MemoryBlock) - 2 * sizeof (MemoryBlock *) )) // given a MemoryBlock pointer mbptr, returns the internal space address that should be visible to the user
70
73
#define MB_ADDRESS_TO_OWN_FOOTER_ADDRESS (mbptr ) (MemoryBlockFooter *) ((char *) (mbptr) + (mbptr)->size - sizeof (MemoryBlockFooter))
71
74
#define MB_ADDRESS_TO_PREVIOUS_FOOTER_ADDRESS (mbptr ) (MemoryBlockFooter *)((char *) (mbptr) - sizeof (MemoryBlockFooter))
72
- #define INTERNAL_SPACE_ADDRESS_TO_MB_ADDRESS (ptr ) (MemoryBlock *) ((char *) (ptr) - sizeof (MemoryBlock))
75
+ #define INTERNAL_SPACE_ADDRESS_TO_MB_ADDRESS (ptr ) (MemoryBlock *) ((char *) (ptr) + 2 * sizeof (MemoryBlock *) - sizeof (MemoryBlock))
73
76
74
77
void * memoryStart; // is always mem_heap_lo
75
78
void * endOfHeap;
@@ -175,9 +178,17 @@ int allocator::check() {
175
178
MemoryBlockFooter * footer;
176
179
for (locMB = (MemoryBlock *) memoryStart; locMB && locMB !=endOfHeap; locMB = (MemoryBlock *) ((char *) locMB + locMB->size ))
177
180
{
181
+ /*
182
+ std::cout<<"\n\nMemoryBlock address: "<<((void *) locMB);
183
+ std::cout<<"\nthreadInfo address: "<<((void *) &(locMB->threadInfo));
184
+ std::cout<<"\nsize address: "<<((void *) &(locMB->size));
185
+ std::cout<<"\nisFree address: "<<((void *) &(locMB->isFree));
186
+ std::cout<<"\nnextFreeAddress: "<<((void *) &(locMB->nextFreeBlock));
187
+ */
178
188
footer = MB_ADDRESS_TO_OWN_FOOTER_ADDRESS (locMB);
179
189
if (locMB->size != *footer) {
180
190
printf (" Memory space contains a block at %p that does not have a correctly assigned footer\n " , locMB);
191
+ // printStateOfMemory();
181
192
return -1 ;
182
193
}
183
194
/*
@@ -187,7 +198,6 @@ int allocator::check() {
187
198
}
188
199
*/
189
200
}
190
-
191
201
return 0 ;
192
202
}
193
203
@@ -203,6 +213,9 @@ int allocator::init() {
203
213
memoryStart = endOfHeap;
204
214
isInitialized = false ;
205
215
GLOBAL_UNLOCK;
216
+ // std::cout<<"\nMemory block size = "<<sizeof(MemoryBlock);
217
+ // std::cout<<"\nFREE-BLOCK-OVERHEAD = "<<FREE_BLOCK_OVERHEAD;
218
+ // std::cout<<"\nALLOCATED-BLOCK-OVERHEAD = "<<ALLOCATED_BLOCK_OVERHEAD;
206
219
return 0 ;
207
220
}
208
221
@@ -244,13 +257,20 @@ static inline void printStateOfBins() {
244
257
245
258
static inline void printStateOfMemory () {
246
259
MemoryBlock * mb = (MemoryBlock *) memoryStart;
260
+ // MemoryBlockFooter * footer;
247
261
std::cout<<" \n " ;
248
262
while (mb && mb != endOfHeap) {
249
263
if (mb->isFree ) {
250
264
std::cout<<" {" <<mb->size <<" }" ;
251
265
} else {
252
266
std::cout<<" [" <<mb->size <<" ]" ;
253
267
}
268
+ // footer = MB_ADDRESS_TO_OWN_FOOTER_ADDRESS(mb);
269
+ /*
270
+ if (mb->size != *footer) {
271
+ std::cout<<"*"<<(*footer)<<"*";
272
+ }
273
+ */
254
274
mb = (MemoryBlock *) ((char *) mb + mb->size );
255
275
}
256
276
}
@@ -303,7 +323,7 @@ static inline void assignBlockFooter (MemoryBlock * mb) {
303
323
304
324
static inline void truncateMemoryBlock (MemoryBlock * mb, size_t new_size) {
305
325
assert (mb);
306
- if (mb->size > new_size + TOTAL_BLOCK_OVERHEAD + FREE_BLOCK_SPLIT_THRESHOLD) {
326
+ if (mb->size > new_size + FREE_BLOCK_OVERHEAD + FREE_BLOCK_SPLIT_THRESHOLD) {
307
327
assert (mb->threadInfo );
308
328
MemoryBlock * nextBlock = (MemoryBlock *)((char *)mb + new_size);
309
329
nextBlock->size = mb->size - new_size;
@@ -360,7 +380,7 @@ static inline void binAllUnbinnedBlocks() {
360
380
361
381
// Coalesce with free blocks on the left
362
382
if ((void *) mb > memoryStart) {
363
- assert ((void *) mb >= (void *)((char *) memoryStart + TOTAL_BLOCK_OVERHEAD ));
383
+ assert ((void *) mb >= (void *)((char *) memoryStart + ALLOCATED_BLOCK_OVERHEAD ));
364
384
MemoryBlockFooter * footer = MB_ADDRESS_TO_PREVIOUS_FOOTER_ADDRESS (mb);
365
385
prevMB = (MemoryBlock *) ((char *) mb - *footer);
366
386
totalFree = mb->size ;
@@ -425,8 +445,8 @@ void * allocator::malloc(size_t size) {
425
445
threadInit ();
426
446
}
427
447
void * currentLoc;
428
- size_t alignedSize = ALIGN (size + TOTAL_BLOCK_OVERHEAD );
429
- // printf("Request size: %zu\t aligned size: %zu\t thread: %p\n", size, alignedSize, ¤tThreadInfo) ;
448
+ size_t alignedSize = ALIGN (size + ALLOCATED_BLOCK_OVERHEAD );
449
+ alignedSize = (alignedSize > MINIMUM_ALLOCATED_BLOCK_SIZE)? alignedSize : MINIMUM_ALLOCATED_BLOCK_SIZE ;
430
450
MemoryBlock * currentLocMB;
431
451
int i = getBinIndex (alignedSize);
432
452
binAllUnbinnedBlocks ();
@@ -440,8 +460,8 @@ void * allocator::malloc(size_t size) {
440
460
if (currentLocMB->size >= alignedSize) {
441
461
truncateMemoryBlock (currentLocMB, alignedSize);
442
462
removeBlockFromLinkedList (currentLocMB, bins[i]);
443
- currentLocMB->nextFreeBlock = 0 ;
444
- currentLocMB->previousFreeBlock = 0 ;
463
+ // currentLocMB->nextFreeBlock = 0;
464
+ // currentLocMB->previousFreeBlock = 0;
445
465
currentLocMB->isFree = false ;
446
466
assert (currentLocMB->threadInfo == (void *) ¤tThreadInfo);
447
467
return MB_ADDRESS_TO_INTERNAL_SPACE_ADDRESS (currentLoc);
@@ -463,8 +483,8 @@ void * allocator::malloc(size_t size) {
463
483
endOfHeap += alignedSize; // increase
464
484
GLOBAL_UNLOCK;
465
485
currentLocMB = (MemoryBlock *) currentLoc;
466
- currentLocMB->nextFreeBlock = 0 ;
467
- currentLocMB->previousFreeBlock = 0 ;
486
+ // currentLocMB->nextFreeBlock = 0;
487
+ // currentLocMB->previousFreeBlock = 0;
468
488
currentLocMB->size = alignedSize;
469
489
currentLocMB->threadInfo = (void *) ¤tThreadInfo;
470
490
currentLocMB->isFree = false ;
@@ -477,7 +497,7 @@ void allocator::free(void *ptr) {
477
497
MemoryBlock * mb;
478
498
mb = INTERNAL_SPACE_ADDRESS_TO_MB_ADDRESS (ptr);
479
499
assert (!mb->isFree );
480
- assert (mb->nextFreeBlock == 0 && mb->previousFreeBlock == 0 );
500
+ // assert(mb->nextFreeBlock == 0 && mb->previousFreeBlock == 0);
481
501
mb->isFree = true ;
482
502
if (mb->threadInfo == ¤tThreadInfo) {
483
503
assignBlockToBinnedList (mb);
@@ -504,8 +524,8 @@ void * allocator::realloc(void *ptr, size_t size) {
504
524
// std::cout<<"\n\nAsked to reallocate block at "<<ptr;
505
525
MemoryBlock * mb = INTERNAL_SPACE_ADDRESS_TO_MB_ADDRESS (ptr);
506
526
// std::cout<<" that had an internal size of "<<mb->size - TOTAL_BLOCK_OVERHEAD<<", an aligned size (incl. overhead) of "<<mb->size<<", and a new requested size of "<<size;
507
- size_t alignedSize = ALIGN (size + TOTAL_BLOCK_OVERHEAD );
508
- // printf("Realloc: request size: %zu\t aligned size: %zu\t thread: %p\n", size, alignedSize, ¤tThreadInfo) ;
527
+ size_t alignedSize = ALIGN (size + ALLOCATED_BLOCK_OVERHEAD );
528
+ alignedSize = (alignedSize > MINIMUM_ALLOCATED_BLOCK_SIZE)? alignedSize : MINIMUM_ALLOCATED_BLOCK_SIZE ;
509
529
// std::cout<<"\nOriginal state of memory: ";
510
530
// printStateOfMemory();
511
531
@@ -564,7 +584,7 @@ void * allocator::realloc(void *ptr, size_t size) {
564
584
if (!newptr) {
565
585
return NULL ;
566
586
}
567
- size_t copy_size = mb->size - TOTAL_BLOCK_OVERHEAD ; // internal size of the original memory block
587
+ size_t copy_size = mb->size - ALLOCATED_BLOCK_OVERHEAD ; // internal size of the original memory block
568
588
copy_size = (size < copy_size)? size : copy_size; // if the new size is less that the original internal size, we MUST NOT copy more than new size bytes to the new block
569
589
std::memcpy (newptr, ptr, copy_size);
570
590
free (ptr);
0 commit comments