Code Monkey home page Code Monkey logo

heap-memory-management's Introduction

Heap-Memory-Management

The goal is to implement the malloc and free C functions, on a heap, using the first-fit allocation strategy. This project was done as part of the "Concepts in Programming Languages" course at the Computer Science and Engineering Department of VNIT, Nagpur, India.

Background

The corresponding function signatures in C are:

  • void* malloc(size_t size)
  • void free (void* ptr)

void*: a generic pointer to any data type (it's represented in memory as a char pointer)

size_t: memory size in bytes

ptr: was allocated by malloc

Implementation

We allocate a character array as the contigous heap memory.

To keep track of the memory 'fragments' in the heap, we keep a doubly linked list that contains the fragment's information ('mi' struct in the code). This gives us a overhead memory cost that we must account for within the heap (sizeof(mi), which is 32 bytes in the code). Hence, sizeof(mi) are always used up within the heap, even prior to allocating any memory.

We use pointer arithmetic and typecasting to switch between the actual memory pointer and list node (mi).

Additionally, the main() code also checks whether the heap is freed completely or not, before the program exits.

Allocation (malloc)

First-fit allocation strategy allocates the requested memory into the first free memory fragment that fits the needed size ('alloc_size' in the code) (done by the mem_alloc function, which is the emulated malloc function). If the free fragment is larger than needed, it is broken into the requested fragment and the remaining as the reduced free fragment (done by the frag function). Remember that this also incurs the memory cost for each fragment created.

Freeing (free)

The fragment is set free (done by the free_mem function, akin to the C free function). Then, heap is checked for all adjacent free fragments to merge (in the merge_free_mem function), where we are refunded the cost of the struct size for each fragment merged.

Example shown in the code

Heap: 1 KB (1000 bytes)

So available memory: 968B, as sizeof(mi) = 32 here is always present for initial MI

When allocating new memory, sizeof(mi) + alloc_size is used up; while the same size is freed up when requested so.

As of now, the code doesn't take user input, but a full illustration is present in the main driver code. The 'free' and 'size' variables for each fragment are printed after each step (by the print_heap_info function).

heap-memory-management's People

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.