A simple and preforment memory allocator. More...
Data Structures | |
struct | HeapItem |
Macros | |
#define | HEAP_SIZE 1024 |
The size of the calculator variable the heap is stored as. You can set this to a larger (or smaller) value if you want. | |
#define | CUSTOM_HEAP_NAME "ConHeap" |
allows setting a custom heap variable name | |
#define | purge_heap() delete(HEAP_VAR_NAME) |
Cleans up the heap for when the program is closing. | |
#define | __hs # |
#define | auto_purge_heap() |
Automaticlly runs purge_heap() for you. | |
Heap memory locations | |
#define | HEAP_LOCATION 0x83E5 |
Memory location of heap related information. | |
#define | HEAP_CONSTANT HEAP_LOCATION |
#define | HEAP_CONSTANT2 ( HEAP_CONSTANT+1) |
#define | HEAP_LOCATION_PTR (HEAP_CONSTANT2+1) |
#define | HEAP_TEMP (HEAP_LOCATION_PTR+2) |
Functions | |
void | initHeap () |
Create the heap for you. | |
void | _malloc () __naked |
Assembly malloc implementation. | |
void * | malloc (int size) __naked |
Request to allocate an amount of memory from the heap (filled undefined random data) | |
void | _free () __naked |
Assembly free() implementation. | |
void | free (void *ptr) __naked |
Frees an allocated memory address from the heap. | |
void | _calloc () __naked |
Assembly function for calloc() | |
void * | calloc (int size) __naked |
Request to allocate an amount of memory from the heap (filled zeros) | |
void | copy (void *dst, void *source, int size) __naked |
Copy a region of memory. | |
Variables | |
const char | HEAP_VAR_NAME [] = addAppVarObj(CUSTOM_HEAP_NAME) |
Name of heap variable in calculator. | |
A simple and preforment memory allocator.
This file allows for a psuedo-heap, it is a lot like how the heap works in the typical c std. This works by creating a calculator variable typically called "ConHeap" where the data is stored. It should be noted that initHeap() uses getOrCreateVar() so if somebody forgets to purge_heap() and you are using a custom HEAP_SIZE you might run into some error. So always call purge_heap() or make use of auto_purge_heap() !
It should be noted that auto_purge_heap() seems not to run correctly in programs
#define auto_purge_heap | ( | ) |
Automaticlly runs purge_heap() for you.
This must be called in main, this is because it will automaticlly run purge_heap() when the current function returns. This also must be called before ANY variables are set, it will destroy the hl register. If you call auto_purge_heap() in main before any variables are set, it will automaticlly call purge_heap() for you as main returns.
Also doesn't seem to run currectly programs.
#define purge_heap | ( | ) | delete(HEAP_VAR_NAME) |
Cleans up the heap for when the program is closing.
Call this before you exit the program if you initialized the heap
void _calloc | ( | ) |
Assembly function for calloc()
[bc] | Size of memory to be requested |
void _free | ( | ) |
void _malloc | ( | ) |
Assembly malloc implementation.
[bc] | Size of memory to be requested |
See malloc() and all error conditions apply.
void * calloc | ( | int | size | ) |
Request to allocate an amount of memory from the heap (filled zeros)
[size] | Amount of memory to be allocated |
Same as malloc() except fills data buffer with zeros
void copy | ( | void * | dst, |
void * | source, | ||
int | size ) |
Copy a region of memory.
[dst] | Location to copy to |
[source] | Location to copy from |
[size] | Amount of ram to copy. |
This is not strncpy and will disregard null termination.
void free | ( | void * | ptr | ) |
Frees an allocated memory address from the heap.
[ptr] | Memory to be freed |
void initHeap | ( | ) |
Create the heap for you.
Call this to create/reset the heap. You can call this a secound time to reset the heap for you. But don't use any pointers you got from malloc/calloc or you might get undefined outputs.
void * malloc | ( | int | size | ) |
Request to allocate an amount of memory from the heap (filled undefined random data)
[size] | Amount of memory to be allocated |
This should be identical to the normal malloc function. But be warned that if you write to bytes past what you have allocated you can cause undefined behavior!
If NO_MEMMORY_ERRORS is defined instead of raising an error, malloc will return NULL. Malloc can error for two reasons, if you pass in zero, or if you allocate too much memory for the heap variable.