Skip to content

Commit a1a5d98

Browse files
committed
Add the thread initialization so that all the threads call mem_sbrk with 64bytes in init. Merge with the remove pointer while allocated.
1 parent faa6c64 commit a1a5d98

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

allocator.cpp

+38-18
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ namespace my {
4848

4949
struct MemoryBlock {
5050
void * threadInfo;
51+
uint32_t size; // size is the entire block size, including the header and the footer
52+
bool isFree;
5153
MemoryBlock * nextFreeBlock; // pointer to the next free block in the binned free list that this belongs to.
5254
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;;
5555
};
5656

5757
struct ThreadSharedInfo {
@@ -61,15 +61,18 @@ struct ThreadSharedInfo {
6161

6262
typedef uint32_t MemoryBlockFooter;
6363

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+
6568
#define BIN_INDEX_THRESHOLD 1024
6669
#define NUM_OF_BINS 150
6770
#define FREE_BLOCK_SPLIT_THRESHOLD 8
6871
#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
7073
#define MB_ADDRESS_TO_OWN_FOOTER_ADDRESS(mbptr) (MemoryBlockFooter *) ((char *) (mbptr) + (mbptr)->size - sizeof(MemoryBlockFooter))
7174
#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))
7376

7477
void * memoryStart; //is always mem_heap_lo
7578
void * endOfHeap;
@@ -175,9 +178,17 @@ int allocator::check() {
175178
MemoryBlockFooter * footer;
176179
for (locMB = (MemoryBlock *) memoryStart; locMB && locMB !=endOfHeap; locMB = (MemoryBlock *) ((char *) locMB + locMB->size))
177180
{
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+
*/
178188
footer = MB_ADDRESS_TO_OWN_FOOTER_ADDRESS(locMB);
179189
if (locMB->size != *footer) {
180190
printf("Memory space contains a block at %p that does not have a correctly assigned footer\n", locMB);
191+
//printStateOfMemory();
181192
return -1;
182193
}
183194
/*
@@ -187,7 +198,6 @@ int allocator::check() {
187198
}
188199
*/
189200
}
190-
191201
return 0;
192202
}
193203

@@ -203,6 +213,9 @@ int allocator::init() {
203213
memoryStart = endOfHeap;
204214
isInitialized = false;
205215
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;
206219
return 0;
207220
}
208221

@@ -244,13 +257,20 @@ static inline void printStateOfBins() {
244257

245258
static inline void printStateOfMemory() {
246259
MemoryBlock * mb = (MemoryBlock *) memoryStart;
260+
//MemoryBlockFooter * footer;
247261
std::cout<<"\n";
248262
while (mb && mb != endOfHeap) {
249263
if (mb->isFree) {
250264
std::cout<<"{"<<mb->size<<"}";
251265
} else {
252266
std::cout<<"["<<mb->size<<"]";
253267
}
268+
//footer = MB_ADDRESS_TO_OWN_FOOTER_ADDRESS(mb);
269+
/*
270+
if (mb->size != *footer) {
271+
std::cout<<"*"<<(*footer)<<"*";
272+
}
273+
*/
254274
mb = (MemoryBlock *) ((char *) mb + mb->size);
255275
}
256276
}
@@ -303,7 +323,7 @@ static inline void assignBlockFooter (MemoryBlock * mb) {
303323

304324
static inline void truncateMemoryBlock (MemoryBlock * mb, size_t new_size) {
305325
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) {
307327
assert(mb->threadInfo);
308328
MemoryBlock * nextBlock = (MemoryBlock *)((char *)mb + new_size);
309329
nextBlock->size = mb->size - new_size;
@@ -360,7 +380,7 @@ static inline void binAllUnbinnedBlocks() {
360380

361381
// Coalesce with free blocks on the left
362382
if ((void *) mb > memoryStart) {
363-
assert((void *) mb >= (void *)((char *) memoryStart + TOTAL_BLOCK_OVERHEAD));
383+
assert((void *) mb >= (void *)((char *) memoryStart + ALLOCATED_BLOCK_OVERHEAD));
364384
MemoryBlockFooter * footer = MB_ADDRESS_TO_PREVIOUS_FOOTER_ADDRESS(mb);
365385
prevMB = (MemoryBlock *) ((char *) mb - *footer);
366386
totalFree = mb->size;
@@ -425,8 +445,8 @@ void * allocator::malloc(size_t size) {
425445
threadInit();
426446
}
427447
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, &currentThreadInfo);
448+
size_t alignedSize = ALIGN(size + ALLOCATED_BLOCK_OVERHEAD);
449+
alignedSize = (alignedSize > MINIMUM_ALLOCATED_BLOCK_SIZE)? alignedSize : MINIMUM_ALLOCATED_BLOCK_SIZE;
430450
MemoryBlock * currentLocMB;
431451
int i = getBinIndex(alignedSize);
432452
binAllUnbinnedBlocks();
@@ -440,8 +460,8 @@ void * allocator::malloc(size_t size) {
440460
if (currentLocMB->size >= alignedSize) {
441461
truncateMemoryBlock(currentLocMB, alignedSize);
442462
removeBlockFromLinkedList(currentLocMB, bins[i]);
443-
currentLocMB->nextFreeBlock = 0;
444-
currentLocMB->previousFreeBlock = 0;
463+
// currentLocMB->nextFreeBlock = 0;
464+
// currentLocMB->previousFreeBlock = 0;
445465
currentLocMB->isFree = false;
446466
assert(currentLocMB->threadInfo == (void *) &currentThreadInfo);
447467
return MB_ADDRESS_TO_INTERNAL_SPACE_ADDRESS(currentLoc);
@@ -463,8 +483,8 @@ void * allocator::malloc(size_t size) {
463483
endOfHeap += alignedSize; // increase
464484
GLOBAL_UNLOCK;
465485
currentLocMB = (MemoryBlock *) currentLoc;
466-
currentLocMB->nextFreeBlock = 0;
467-
currentLocMB->previousFreeBlock = 0;
486+
// currentLocMB->nextFreeBlock = 0;
487+
// currentLocMB->previousFreeBlock = 0;
468488
currentLocMB->size = alignedSize;
469489
currentLocMB->threadInfo = (void *) &currentThreadInfo;
470490
currentLocMB->isFree = false;
@@ -477,7 +497,7 @@ void allocator::free(void *ptr) {
477497
MemoryBlock * mb;
478498
mb = INTERNAL_SPACE_ADDRESS_TO_MB_ADDRESS(ptr);
479499
assert(!mb->isFree);
480-
assert(mb->nextFreeBlock == 0 && mb->previousFreeBlock == 0);
500+
// assert(mb->nextFreeBlock == 0 && mb->previousFreeBlock == 0);
481501
mb->isFree = true;
482502
if (mb->threadInfo == &currentThreadInfo) {
483503
assignBlockToBinnedList(mb);
@@ -504,8 +524,8 @@ void * allocator::realloc(void *ptr, size_t size) {
504524
// std::cout<<"\n\nAsked to reallocate block at "<<ptr;
505525
MemoryBlock * mb = INTERNAL_SPACE_ADDRESS_TO_MB_ADDRESS(ptr);
506526
// 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, &currentThreadInfo);
527+
size_t alignedSize = ALIGN(size + ALLOCATED_BLOCK_OVERHEAD);
528+
alignedSize = (alignedSize > MINIMUM_ALLOCATED_BLOCK_SIZE)? alignedSize : MINIMUM_ALLOCATED_BLOCK_SIZE;
509529
// std::cout<<"\nOriginal state of memory: ";
510530
// printStateOfMemory();
511531

@@ -564,7 +584,7 @@ void * allocator::realloc(void *ptr, size_t size) {
564584
if (!newptr) {
565585
return NULL;
566586
}
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
568588
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
569589
std::memcpy(newptr, ptr, copy_size);
570590
free(ptr);

perf-test.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ echo "
3535
Best run-time: 0.073"
3636
./linux-scalability 999 10000000 12
3737

38-
: '
38+
3939
echo "-----------------------VALIDATION------------------------"
4040
echo "
4141
./validate.py ./cache-scratch-validate 1 1000 8 1000000"
@@ -56,12 +56,12 @@ echo "
5656
./validate.py ./larson-validate 10 7 8 1000 10000 6172 12"
5757
./validate.py ./larson-validate 10 7 8 1000 10000 6172 12
5858
echo "
59-
./validate.py ./linux-scalability-validate 8 1000000 1"
60-
./validate.py ./linux-scalability-validate 8 1000000 1
59+
./validate.py ./linux-scalability-validate 8 10000000 1"
60+
./validate.py ./linux-scalability-validate 8 10000000 1
6161
echo "
62-
./validate.py ./linux-scalability-validate 8 1000000 12"
63-
./validate.py ./linux-scalability-validate 8 1000000 12
62+
./validate.py ./linux-scalability-validate 8 10000000 12"
63+
./validate.py ./linux-scalability-validate 8 10000000 12
6464
echo "
65-
./validate.py ./linux-scalability-validate 999 1000000 12"
66-
./validate.py ./linux-scalability-validate 999 1000000 12
67-
'
65+
./validate.py ./linux-scalability-validate 999 10000000 12"
66+
./validate.py ./linux-scalability-validate 999 10000000 12
67+

0 commit comments

Comments
 (0)