GithubHelp home page GithubHelp logo

miguelperes / custom-malloc Goto Github PK

View Code? Open in Web Editor NEW
52.0 3.0 5.0 8 KB

A custom malloc implementation in C explained

C 95.69% Makefile 4.31%
memory-management memory-allocation algorithm sbrk malloc c tutorial learn optimization

custom-malloc's Introduction

Custom Memory Management implementation

A custom malloc implementation in C, made for CSC369H (Operating Systems) - Fall 2014 - University of Toronto

References

Useful readings to better understand how memory management works:

How this implementation works

1. Data Structure

The data structure is located before each chunk of memory, to keep track of its status (metadata). Through the linked list of this data structure, it is possible to access the address of any memory block in the list, its size, availability, the next and previous chunks of adjacent memory and the end of the block. With this attributes, it’s possible to manage and manipulate the memory using the algorithm quickly described below.

2. Algorithm

It uses the “first fit” strategy. The algorithm search the list from the beginning to end, until it finds the first big enough (greater or equal the size requested) chunk of memory (splitting it in two, in case it’s bigger than needed), and finally return its adress to the user. If there is no more space in the heap, the algorithm then extends it, using the sbrk system call. To free memory, the algorithm changes the “available” state of the chunk’s metadata (struct), releasing it to further allocation. After that, the freed chunk is merged with the previous block (if this one is also freed), this is useful to avoid memory fragmentation in small sized blocks (it can happens after some splittings).


Optimization

Space optimization: now myfree function will merge the two adjacent chunks (adjacents to the chunk being freed at the moment), if they are available (not allocated). By doing this, I'm preventing memory fragmentation, and these blocks will be easily reallocated in the next mymalloc.

Sbrk calls: to avoid calling sbrk() all the time, when the user first calls mymalloc (or when the heap need extension), mymalloc will allocate more memory than the amount asked. This way, it will take longer before the heap need to be extended again, and less times sbrk() will be invoked.

custom-malloc's People

Contributors

dolev146 avatar miguelperes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

custom-malloc's Issues

pthread_mutex_unlock

What alternatives could be used to the pthreads mutex, and how exactly is this pthread_mutex_unlock achieves its end goal.

I'm aware of the Unix method of Mutex, another solution could be to have a heap allocated to each CPU Core, which would be ideal if each core was assigned to a different task.

Would hyper-threading cause any unforeseen issues in such an implementation though I wonder?

Shouldn't the first chunk start with available == 1?

I'm using your code for a project and it seems like the first chunk is marked as not available, which causes the first allocation request to fail.

Should head->available = 0; on line 189 be head->available = 1; ?

Thanks!

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.