Git Product home page Git Product logo

mymalloc's Introduction

mymalloc

The Ancient Rite of Passage: Writing your own memory manager in plain C ;).

I wrote this following this excellent online tutorial: https://danluu.com/malloc-tutorial/.

I've slightly refactored the code, annotated it extensively, and also improved it a bit (at least, I think I did...).

Changes to the original:

  • Added a best-fit search as an alternative to the current first-fit search in find_free_block().
  • Introduced pointer TAIL to last element of the global linked list of memory blocks, so that we don't have to search for it anew every time. In my opinion, this makes the code slightly easier to understand, as we avoid the double pointer in the parameters of find_free_block() that gets changed during search.
  • Removed sbrk(0) before sbrk(META_SIZE + size) in request_space(), because as far as I can tell this is not required.
  • Switched to a double-linked list instead of a single-linked one to make finding the left neighbour happen in O(1) instead of O(n).
  • Added a convenience function to print the current state of the global linked list of memory blocks.
  • Added block splitting when a free block is recycled which is too large (has been suggested as additional exercise in the tutorial).
  • Added block merging upon freeing of blocks (has been suggested as additional exercise in the tutorial).
  • Added overflow check in calloc() (has been suggested as additional exercise in the tutorial).

mymalloc's People

Contributors

ma-fi-94 avatar

Watchers

 avatar  avatar

mymalloc's Issues

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.