Hey guys! Long time no see, eeeeeh..... not!
Another assignment submitted, and if you have been following
this blog for a long time (seriously?) you will know that I have “been there,
done that” with this assignment. This one is Assignment #3 (PA3) – Memory System.
The whole idea behind this assignment was to build my own Memory System to replace the standard
one that comes with C++, in other
words, I had to create my own version of malloc()
and free() functions. In order to achieve
this I allocated one big block of memory (50KB
+ alignment padding), which we call the
heap, then moved the initial block
address so that it was 16-byte aligned,
placed the heap header on this
address and created a free block
with the remaining space of the original block. The 16-byte alignment is not essential to the functionality of this project,
but it will be useful later (SIMD).
The heap header
is the Memory System part that holds
all the pieces together. It has pointers to the first used and free blocks,
and has an internal structure that keeps updated stats about the system usage.
All blocks in the system are connected by pointers (next and previous) to
form doubly-linked lists for easier
insertion and removal of new blocks. Now, let me talk briefly about the two
functions implemented:
- malloc(): this function first rounds up the requested size to the next multiple of 16 (to preserve the 16-byte alignment), then checks if there is enough space available in the heap, if that’s the case it proceeds to find the first free block that can hold the required amount. When the block is found, if its size is bigger than the requested one it is split into a used block and a free block, then both blocks are added to their respective lists. Finally, the stats are updated and the address of the used block is returned to the user.
Beside this, PA3 also had another part related to using the Placement New operator. The big
difference between this operator and the regular New is that this one takes a pointer to a previously allocated memory
location as a parameter, and then uses it to create the new object there. On
the other hand, the traditional new first allocates the blocks (malloc) and then creates the object
there.
I was going to add some diagrams to this post, but I am kind
of lazy today. Anyway, I hope this information was useful. See you later!
Adios!
No comments:
Post a Comment