Coder Social home page Coder Social logo

comprosoftceo / mymalloc Goto Github PK

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

My own implementation of malloc and free

License: GNU General Public License v3.0

C 68.01% Makefile 2.09% C++ 29.90%
malloc malloc-free malloc-library realloc free calloc heap stdlib-replacement malloc-functions heap-algorithm

mymalloc's Introduction

MyMalloc

My own implementation of malloc and free


Background

I wrote this code for my CS-240 class at IUPUI for the project "Heap of Students". The project was all about using the C++ new and delete operators to store classes on the heap. For the blackbelt assignment, I decided to write my own implementation of malloc and free (which new and delete use internally) to show my understanding of how the heap works.


Features

  • Calls the function sbrk() to extend the data segment to automatically resize the heap
  • Uses a pthread mutex to ensure thread-safe access
  • Creates a checksum for each heap block to ensure data integrity
  • Merges adjacent blocks when freed to prevent fragmentation

Compiling and Running

The code is written for a Linux operating system, but can run on Windows using the Windows Subsystem for Linux (which is actually what I used to write and test the code). The provided makefile is used for building the Testing Utility (see below), which was used to test the function calls during development. To use the code in other projects, merely include the Code Files (listed below) in your project. Due to the way that linking works, these versions of malloc(), calloc(), realloc(), and free() will automatically replace the default versions in libc.

Code Files

  • Heap.h - Contains the definition of a Heap Block structure, internal functions, debug functions, and other useful macros
  • malloc.c, calloc.c, realloc.c, free.c - Function definitions for malloc(), calloc(), realloc(), and free() (as defined in stdlib.h)
  • checksum.c - Algorithm for computing the heap block checksum
  • heap_globals.c - Stores both global variables and functions used by all code files
  • print.c (optional) - Functions for printing out blocks in the heap (useful for debugging)

The Algorithm

Every entry in the heap has a structure at pointer-1 that stores information about the block in the heap. The blocks are a doubly-linked list, and store:

  • The previous block (pointer)
  • The next block (pointer)
  • The size of the block (in bytes)
    • If the size is negative, then the block is inuse. Otherwise, the block is free.
  • A checksum (for detecting heap corrution)

While the algorithm is well-commented in the code, here is the gist of what happens:

  • After a call to malloc, the program searches for the first free block in the heap that has enough memory to store the requested size.
    • If no such block exists, then the heap is resized to fulfill the request
    • If the block is too big, then the program then calculates if it needs to split the block into two smaller blocks
  • When the program frees a block of memory, it looks to see if there are any contiguous free blocks in the front or behind, and merges the blocks together
  • When the realloc method is called, the program looks for possible free blocks in front of the current block. If the free blocks are enough to resize the heap entry, then the blocks are combined together.
    • Otherwise, it allocates a new block using malloc, copies the data into the new block, then frees the old block of data.

Testing Utility

Although the code files can be used in any C or C++ project, the provided makefile is used to compile a testing utility (for debugging the heap). The debug utility (named test.cpp) works like a command-line interface. Commands are entered with the following format:

<Command> [Register] [Bytes]

Here is the list of all commands:

  • h - Show the help screen
  • q - Quit the program
  • m <reg> <bytes> - Malloc size <bytes> into <reg>
  • c <reg> <bytes> - Calloc size <bytes> into <reg>
  • r <reg> <bytes> - Realloc <reg> to size <bytes>
  • f <reg> - Free <reg> from memory
  • a <reg> - Print out the address of <reg>
  • p <reg> - Print out the chunk information of <reg>
  • d - Dump the contents of the heap
  • l - List all registers

Notes

I wrote this implementation to prove that I could create my own version of malloc and free. However, for any practical project, the standard implementation of malloc and free is much safer and better-tested. I cannot guarantee that my version doesn't have any flaws or glitches.

mymalloc's People

Contributors

comprosoftceo avatar

Stargazers

 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.