26

GitHub - arjun024/memalloc: A simple memory allocator - Memory allocation 101

 5 years ago
source link: https://github.com/arjun024/memalloc
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

memalloc

memalloc is a simple memory allocator.

It implements malloc(), calloc(), realloc() and free().

####Article####

Write a simple memory allocator

(http://arjunsreedharan.org/post/148675821737/write-a-simple-memory-allocator)

####Compile and Run####

gcc -o memalloc.so -fPIC -shared memalloc.c

The -fPIC and -shared options makes sure the compiled output has position-independent code and tells the linker to produce a shared object suitable for dynamic linking.

On Linux, if you set the enivornment variable LD_PRELOAD to the path of a shared object, that file will be loaded before any other library. We could use this trick to load our compiled library file first, so that the later commands run in the shell will use our malloc(), free(), calloc() and realloc().

export LD_PRELOAD=$PWD/memalloc.so

Now, if you run something like ls, it will use our memory allocator.

ls
memalloc.c		memalloc.so

You can also run your own programs with this memory allocator.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK