Skip to content

About

malloc and free replacement in C — first, best, and worst fit, chunk splitting, coalescing (CST 334)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memory Allocator

A malloc and free replacement written in C. Manages a fixed arena, tracks free chunks in a linked list, and hands out and reclaims memory without the standard library.

Core

  • mem_alloc(size) finds a chunk, splits it, and returns a pointer past the header
  • mem_free(ptr) walks back to the header, returns the chunk, and coalesces neighbors
  • init(size) / destroy() set up and tear down the arena

Allocation strategies

set_allocation_strategy switches how find_free_chunk picks a chunk, which changes the fragmentation profile of the same workload:

  • First fit takes the first chunk large enough, fastest to search
  • Best fit takes the smallest sufficient chunk, minimizes wasted bytes per allocation
  • Worst fit takes the largest, leaving bigger remainders behind

set_free_list_order independently controls whether the free list is kept in address order or size order.

Fragmentation handling

split_node carves a request out of a larger chunk and leaves the remainder on the list, but only when the remainder is big enough to hold a header. coalesce_nodes merges adjacent free chunks on release, which is what keeps the arena from degrading into unusable slivers over a long run of allocations and frees.

get_header does the pointer arithmetic to recover a chunk's metadata from the user pointer, which is the same trick real allocators use.

Written in C for CST 334 Operating Systems at CSU Monterey Bay.

Build and test

make

This repository contains the implementation only. The course's unit test harness and autograder configuration are instructor materials and are not included, so the build here will not run the graded test suite.

About

malloc and free replacement in C — first, best, and worst fit, chunk splitting, coalescing (CST 334)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages