Giter VIP home page Giter VIP logo

buddy-alloc's Introduction

Buddy Allocator

Copyright 2015, Red Rocket Computing, LLC, [email protected]

Overview

This an implementation Knuth's "buddy" system for dynamic memory allocation (Knuth. The Art of Computer Programming, Volume 1, Fundamental Algorithms, Section 2.5C).

The allocator manages free block using intrusive linked list structures with one list head for every available block size. Block splits and buddy states are tracked using binary bit trees.

The library support both internal and external storage of the allocator metadata and provides both a standard unsized "free" API as well as an accelerated "release" API for use when the size of the block to be free is known.

License

To facilate the use of this library in statically linked open and closed source embedded systems, this library is licensed under the terms of the Mozilla Public License, V2.0.

Acknowledgments

This implementation of Knuth's Buddy Allocator closely follows the most excellent ideas posted by Niklas in his post Allocation Adventures 3: The Buddy Allocator

Niklas primer on memory allocation metadata management is fantastic and has also been included in the doc directory. I took the liberty of converting the blog post to markdown and attaching a copyright attributing the writeup him.

Niklas, if you have a problem with this, let me know and I will remove it markdown version from my project. Just reach out!

Dependencies

The allocator requires a compatible compiler supporting the following built-ins:

__builtin_clzl /* Count lead zeros long */
__builtin_ctzl /* Count trailing zeros long */

and is known to work with recent version of gcc and clang.

Building The Allocator

For the default release version (-O3)

CD=path/to/project
make

To build a debug (-O0 -pg -g) version of the library:

CD=path/to/project
make V=1 BUILD_TYPE=debug

The library can be found at

path/to/project/BUILD_TYPE/libbuddy-alloc.a

The build location can be adjusted with OUTPUT_ROOT which must be an absolute path:

make OUTPUT_ROOT=/some/directory

To cross compile use the CROSS_COMPILE variable:

make CROSS_COMPILE=arm-none-eabi-

Other build options can be adjusted in Makefile.common, Makefile.debug and Makefile.release.

Using The Allocator

To create an 1MB buddy allocator with internal metadata:

#include <stdio.h>
#include <stdlib.h>
#include <buddy-alloc.h>

#define ALLOCATOR_SIZE 1048576

unsigned long int memory_region[ALLOCATOR_SIZE / sizeof(unsigned long int)];

int main(int argc, char **argv)
{
	buddy_allocator_t *allocator;
	void *ptr;
	
	/* The return address is always equal to the provide memory region */
	allocator = buddy_create(memory_region, ALLOCATOR_SIZE);

	/* This will allocate a block 16K in size */
	ptr = buddy_alloc(allocator, 13773);
	if(!ptr) {
		perror("allocation failed");
		exit(1);
	}

	/* This will determine the size of the memory block, by searching the
	 free index at each level, somewhat slower */
	buddy_free(allocator, ptr);

	/* This will allocate a block 16K in size */
	ptr = buddy_alloc(allocator, 13773);
	if(!ptr) {
		perror("allocation failed");
		exit(1);
	}

	/* This frees the memory block, using the provided size, a little
	 faster as the block level can be derived from the size */
	buddy_release(allocator, ptr, 13773);

	return 0;
}

buddy-alloc's People

Contributors

sgstreet avatar

Stargazers

 avatar AlexisM avatar dimage1 avatar MoZhao avatar  avatar Zekai Zhou avatar Bogdan avatar Vladimir Vishnevskii avatar  avatar  avatar Youssef Habri avatar Denis Tereshkin avatar  avatar Albert Tavares de Almeida avatar steven avatar renzhi avatar Simon Lundmark avatar  avatar

Watchers

James Cloos avatar  avatar Zirconium 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.