Ti Constructor
 
Loading...
Searching...
No Matches
memory.c File Reference

A simple and preforment memory allocator. More...

#include "variables.c"
#include "errors.c"

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.
 

Detailed Description

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

Optional #defines

  • HEAP_SIZE - A number that is the size of varible to create for the heap.
  • CUSTOM_HEAP_NAME - Sets the heap variable to a custom name

Macro Definition Documentation

◆ auto_purge_heap

#define auto_purge_heap ( )
Value:
__asm \
ld hl, __hs AUTO_CLOSE_HEAP_LOCATION \
push hl \
jp AFTER_AUTO_CLOSE_HEAP \
AUTO_CLOSE_HEAP_LOCATION: \
__endasm; purge_heap(); __asm \
ret \
AFTER_AUTO_CLOSE_HEAP: __endasm
#define purge_heap()
Cleans up the heap for when the program is closing.
Definition memory.c:68

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.

◆ purge_heap

#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

Function Documentation

◆ _calloc()

void _calloc ( )

Assembly function for calloc()

Parameters
[bc]Size of memory to be requested
Returns
hl = memory location

◆ _free()

void _free ( )

Assembly free() implementation.

Parameters
[hl]Location of memory to be freed

See free()

◆ _malloc()

void _malloc ( )

Assembly malloc implementation.

Parameters
[bc]Size of memory to be requested
Returns
hl = memory location

See malloc() and all error conditions apply.

◆ calloc()

void * calloc ( int size)

Request to allocate an amount of memory from the heap (filled zeros)

Parameters
[size]Amount of memory to be allocated
Returns
A pointer to free memory

Same as malloc() except fills data buffer with zeros

◆ copy()

void copy ( void * dst,
void * source,
int size )

Copy a region of memory.

Parameters
[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.

◆ free()

void free ( void * ptr)

Frees an allocated memory address from the heap.

Parameters
[ptr]Memory to be freed

◆ initHeap()

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.

◆ malloc()

void * malloc ( int size)

Request to allocate an amount of memory from the heap (filled undefined random data)

Parameters
[size]Amount of memory to be allocated
Returns
A pointer to free memory

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.