HAllocator
A Simple C++ Memory Allocator
Loading...
Searching...
No Matches
Namespaces | Functions | Variables
basic_alloc.cpp File Reference

Implementation of basic linked-list memory allocator. More...

#include "basic_alloc.hpp"
#include <unistd.h>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdexcept>
Include dependency graph for basic_alloc.cpp:

Namespaces

namespace  hh
 
namespace  hh::basic_alloc
 

Functions

bool hh::basic_alloc::is_free (MemSizeT &size)
 Check if block is free using bit 63.
 
void hh::basic_alloc::make_free (MemSizeT &size)
 Mark block as free by setting bit 63.
 
void hh::basic_alloc::make_used (MemSizeT &size)
 Mark block as used by clearing bit 63.
 
MemSizeT hh::basic_alloc::get_size (MemSizeT &size)
 Extract actual size by masking off bit 63.
 
MemSizeT hh::basic_alloc::add (MemSizeT a, MemSizeT b)
 Add two sizes (clearing free bits first).
 
MemSizeT hh::basic_alloc::sub (MemSizeT a, MemSizeT b)
 Subtract two sizes (clearing free bits first).
 
void * hh::basic_alloc::sbrk_then_alloc (MemSizeT size)
 Request memory from OS using sbrk and allocate.
 
void hh::basic_alloc::coalesce_nodes (MemNode *nd)
 Merge a free node with adjacent free nodes.
 
void * hh::basic_alloc::free (void *ptr)
 Free a memory block and merge with adjacent free blocks.
 
void hh::basic_alloc::shrink_then_align (MemNode *nd, MemSizeT size)
 Shrink a block and create a new free block from remainder.
 
void * hh::basic_alloc::try_alloc (MemSizeT size)
 Allocate memory using first-fit strategy.
 
void hh::basic_alloc::mem_copy (void *dest, const void *src, size_t n)
 Copy n bytes from source to destination.
 
void * hh::basic_alloc::try_realloc (void *ptr, MemSizeT size)
 Reallocate a memory block to a new size.
 
void hh::basic_alloc::mem_set (void *ptr, int value, size_t num)
 Set num bytes to specified value.
 
void * hh::basic_alloc::try_calloc (size_t num, size_t size)
 Allocate and zero-initialize an array.
 
void hh::basic_alloc::alloc_print ()
 Print allocation status table for debugging.
 

Variables

MemNodehh::basic_alloc::__head = nullptr
 Pointer to the head of the memory block linked list.
 
MemNodehh::basic_alloc::__tail = nullptr
 

Detailed Description

Implementation of basic linked-list memory allocator.

This file implements a simple educational memory allocator using:

Warning
NOT for production use - educational purposes only