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

Basic memory allocator interface - educational implementation. More...

#include <unistd.h>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdexcept>
Include dependency graph for basic_alloc.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  hh::basic_alloc::MemNode
 Metadata structure for each memory block. More...
 

Namespaces

namespace  hh
 
namespace  hh::basic_alloc
 

Typedefs

using hh::basic_alloc::MemSizeT = unsigned long long
 Type definition for memory size.
 

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

constexpr MemSizeT hh::basic_alloc::MIN_FRAGMENT_SIZE = 32
 Minimum fragment size to consider splitting a block.
 
constexpr MemSizeT hh::basic_alloc::BLOCK_SIZE = 4096
 Size of each memory block requested from OS via sbrk.
 
constexpr MemSizeT hh::basic_alloc::MEM_NODE_SIZE = sizeof(MemNode)
 Size of the MemNode structure.
 

Detailed Description

Basic memory allocator interface - educational implementation.

This file provides a simple linked-list-based memory allocator using sbrk() to request memory from the operating system. It uses first-fit allocation strategy and maintains a singly-linked list of free/used blocks.

Warning
NOT THREAD SAFE - do not use in multi-threaded environments
VERY SLOW AND INEFFICIENT - for educational purposes only
Production code should use halloc or standard allocators

Implementation details: