Skip to content
/ dmm Public

A memory management library.

License

Notifications You must be signed in to change notification settings

awooos/dmm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6340098 · Nov 9, 2023
Sep 9, 2020
Nov 9, 2023
Nov 9, 2023
Nov 9, 2023
Nov 9, 2023
Jan 14, 2018
Feb 16, 2019
Jan 3, 2018
Nov 9, 2023
Aug 26, 2020
Sep 9, 2020
May 14, 2018
Jan 10, 2018

Repository files navigation

DMM

A memory management library, made for awooOS.

DMM supports multiple simultaneous instances, allowing you to manage different sections of memory independently. It also provides a simpler API, if you wish to use a single instance.

The multi-instance version isn't documented yet. (Sorry about that.)

Requirements

  1. A valid stddef.h and stdint.h (usually provided by libc).
  2. A "panic" function (see below).

The panic function must have the following signature:

void _panic(const char *message, const char *function,
            const char *filename, size_t line);

Basic Usage (global instance)

If you want just a single DMM instance, you can do the following:

#include <dmm.h>    // pulls in <stddef.h>
#include <stdlib.h> // for exit()

void _panic(const char *message, const char *function,
            const char *filename, size_t line) {
    // Loosely based on Rust's panic message format.
    fprintf(stderr, "function '%s' panicked at '%s', %s:%zu\n", function, message, filename, line);
    exit(1);
}

int main(void) {
    char *region1 = malloc(10);
    char *region2 = malloc(512);
    char *region3 = malloc(1024);

    dmm_init(&_panic);

    dmm_add_memory_region(region1, 10);
    dmm_add_memory_region(region2, 512);
    dmm_add_memory_region(region3, 1024);

    dmm_malloc(9);   // can be from any region
    dmm_malloc(100); // can be from region2 or region3
    dmm_malloc(600); // can only be from region3.

    // Clean up after ourselves.
    free(region1);
    free(region2);

    return 0;
}

Advanced Usage (multiple instances)

TODO.

License

The code is available as open source under the MIT License.