This project contains two custom memory allocator libraries implemented in C: malloc
and arena
. These libraries provide custom memory management functions and are designed to replace the standard memory allocation routines in C for educational purposes.
smalloc(size_t size)
: Allocates a block of memory of the specified size.scalloc(size_t num, size_t nsize)
: Allocates an array of blocks, initializing all bytes to zero.srealloc(void *block, size_t size)
: Resizes a previously allocated block of memory.sfree(void *block)
: Frees a previously allocated block of memory, making it available for future allocations.
arena_init(size_t capacity)
: Initializes an arena with a given capacity.arena_alloc(size_t size)
: Allocates memory from the arena.arena_realloc(void *old_ptr, size_t old_size, size_t new_size)
: Reallocates memory within the arena.arena_reset()
: Resets the arena, making all memory available for allocation again.arena_free()
: Frees the memory used by the arena.
The initial implementation of this library used the sbrk
function for memory allocation on Unix-like systems. However, sbrk
is considered outdated and not recommended for modern applications, as it is not thread-safe and can lead to fragmentation and other issues.
The new version of this library uses mmap
on Unix-like systems and VirtualAlloc
on Windows for memory allocation. These methods provide better control over memory management and are more suitable for modern multi-threaded environments.
In addition, the arena
library is introduced to provide a simple arena allocator, allowing for fast allocation and deallocation of memory in bulk. It uses the malloc
library internally for its memory management.
To compile and create a shared library (.so
), run:
./build_lib.sh
This will generate libmalloc.so
and libarena.so
.
To compile and create a shared library (.dll), run:
build_lib.cmd
This will generate malloc.dll
and arena.dll
.
- Set the
LD_LIBRARY_PATH
environment variable to include the directory wherelibmalloc.so
andarena.so
are located:
export LD_LIBRARY_PATH=/path/to/directory
- Compile your program that uses the libraries:
gcc -o myprogram myprogram.c -L/path/to/directory -larena -lmalloc
- Run your program:
./myprogram
-
Ensure
malloc.dll
andarena.dll
are in the same directory as your executable or in a directory included in the systemPATH
. -
Compile your program that uses the library:
gcc -o myprogram.exe myprogram.c -L/path/to/directory -larena -lmalloc
- Run your program:
myprogram.exe
- Build the test program using:
./build_test.sh
- Run the test program:
./test
- Build the test.exe program using:
build_test.cmd
- Run the test.exe program:
test.exe
Here is a simple example program (test.c
) that uses both the malloc
and arena
libraries:
#include <stdio.h>
#include "malloc.h"
#include "arena.h"
int main() {
// Example of using malloc
void *ptr1 = smalloc(100);
if (ptr1 == NULL) {
printf("malloc failed\n");
return 1;
}
printf("smalloc: allocated 100 bytes at %p\n", ptr1);
sfree(ptr1);
// Example of using arena
arena_init(1024);
void *arena_ptr = arena_alloc(100);
if (arena_ptr == NULL) {
printf("arena_alloc failed\n");
return 1;
}
printf("arena_alloc: allocated 100 bytes at %p\n", arena_ptr);
arena_free();
return 0;
}
-
Compile the example using the provided build scripts (
build_test.sh
orbuild_test.cmd
). -
Run the compiled program (
test
ortest.exe
).
This project is intended for educational purposes and to demonstrate the basics of implementing a memory allocator. It is not optimized for production use and may not handle all edge cases or provide the same level of safety and efficiency as standard memory allocators.