GithubHelp home page GithubHelp logo

houssemlahmar / memory_allocator Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 2 KB

This project is a simple memory allocator implemented in C, providing dynamic memory allocation and deallocation functionalities such as `malloc()`, `free()`, `calloc()`, and `realloc()`. It utilizes a linked list data structure and the `sbrk()` system call for memory management, ensuring thread safety through basic locking mechanisms.

C 100.00%

memory_allocator's Introduction


Simple Memory Allocator

This is a simple memory allocator implementation in C.

Overview

This memory allocator provides basic implementations of malloc(), free(), calloc(), and realloc() functions. It manages memory allocation using a linked list structure and the sbrk() system call to manipulate the program break.

Features

  • Dynamic Memory Allocation: Allocate and deallocate memory dynamically at runtime.
  • Thread Safety: Basic locking mechanism ensures thread safety during memory allocation and deallocation.
  • Header Management: Each allocated memory block is preceded by a header containing metadata such as size and free status.
  • Alignment: Memory blocks are aligned to 16-byte boundaries for better performance.

Usage

To use the memory allocator in your project, include the memallocator.h header file and link against the memallocator.c source file.

Example usage:

#include "memallocator.h"
#include <stdio.h>

int main() {
    // Allocate memory
    int *ptr = (int *)malloc(sizeof(int));
    if (ptr == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }

    // Use allocated memory
    *ptr = 42;
    printf("Value: %d\n", *ptr);

    // Free allocated memory
    free(ptr);

    return 0;
}

Building

To compile the memory allocator as a shared library, you can use the following command:

gcc -o libmemalloc.so -fPIC -shared memallocator.c -pthread

memory_allocator's People

Contributors

houssemlahmar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.