Skip to content

papadonut9/memory-allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

memalloc - A simple memory allocator in C

This memory allocator tries to implement the following functions:

  • malloc()
  • calloc()
  • realloc()
  • free()
  • print_mem_heap(): A debug function to print the full linked list of the memory heap.

This memory allocator uses sbrk(0) for program break. I know that I could've used proc(5) for efficient memory querying, but I insisted on using the legacy option.

Areas of improvement

This memory allocator can be further improved by implementing reallocarray() which can dynamically allocate and free memory. Also there are no error codes present in this memory allocator. So error codes like ERRMEM won't show up in case of invalid memory size which might lead into a memory leak.

Usage

This allocator can compiled and used in a utility funtion like ls.

First this needs to be compiled as a library file

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

the -fPIC flag makes sure that compiler compiled a positin-independent code.

the -shared flag tells the linker to make it a shared object which is suitable for dynamic linking.

Now, in order to use the allocator in linux, we have to set the LD_PRELOAD environment variable to the compiled shared object file. This makes sure that our library gets loaded before any other library. It's a neat little trick to make sure the shell commands use our memory allocation functions viz. malloc(), free(), calloc() and realloc().

If you want to make sure this works, go ahead and print out a debug message.

References:

These are a list of memory allocators I referred.

About

A simple memory allocator in C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages