SGDK
A free and open development kit for the Sega Mega Drive
Loading...
Searching...
No Matches
memory.h File Reference

Memory handling methods. More...

#include "memory_base.h"
Include dependency graph for memory.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GET_DWORDFROMPBYTE(src)
 Get u32 from u8 array (BigEndian order).
 
#define GET_DWORDFROMPBYTE_LI(src)
 Get u32 from u8 array (LittleEndian order).
 
#define GET_WORDFROMPBYTE(src)
 Get u16 from u8 array (BigEndian order).
 
#define GET_WORDFROMPBYTE_LI(src)
 Get u16 from u8 array (LittleEndian order).
 
#define GET_DWORDFROMPWORD(src)
 Get u32 from u16 array (BigEndian order).
 
#define GET_DWORDFROMPWORD_LI(src)
 Get u32 from u16 array (LittleEndian order).
 
#define SWAP_u8(x, y)
 Exchange value of specified u8 variables.
 
#define SWAP_s8(x, y)
 Exchange value of specified s8 variables.
 
#define SWAP_u16(x, y)
 Exchange value of specified u16 variables.
 
#define SWAP_s16(x, y)
 Exchange value of specified s16 variables.
 
#define SWAP_u32(x, y)
 Exchange value of specified u32 variables.
 
#define SWAP_s32(x, y)
 Exchange value of specified s32 variables.
 
#define malloc(x)
 
#define free(x)
 
#define memcpyU16(to, from, len)
 
#define memcpyU32(to, from, len)
 
#define fastMemset(to, value, len)
 
#define fastMemsetU16(to, value, len)
 
#define fastMemsetU32(to, value, len)
 
#define astMemcpy(to, from, len)
 
#define fastMemcpyU16(to, from, len)
 
#define fastMemcpyU32(to, from, len)
 

Functions

u16 MEM_getFree (void)
 Return available memory in bytes.
 
u16 MEM_getAllocated (void)
 Return allocated memory in bytes.
 
u16 MEM_getLargestFreeBlock (void)
 Return largest free memory block in bytes.
 
void MEM_free (void *ptr)
 Deallocate space in memory.
 
void * MEM_alloc (u16 size)
 Allocate memory block.
 
void * MEM_allocAt (u32 addr, u16 size)
 Allocate memory block at a specific address (useful for short addressing or fixed low level working address)
 
void MEM_pack (void)
 Pack all free blocks and reset allocation search from start of heap.
You can call this method before trying to allocate small block of memory to reduce memory fragmentation.
 
void MEM_dump (void)
 Show memory dump.
 
void memset (void *to, u8 value, u16 len)
 Fill block of memory.
 
void memsetU16 (u16 *to, u16 value, u16 len)
 Fill block of memory (optimized for u16)
 
void memsetU32 (u32 *to, u32 value, u16 len)
 Fill block of memory (optimized for u32)
 
void memcpy (void *to, const void *from, u16 len)
 Copy block of memory.
 

Detailed Description

Memory handling methods.

Author
Stephane Dallongeville
Date
08/2011

This unit provides memory copy/set operation and dynamic memory allocation.

Memory organization :

Memory is composed of bloc, the first 2 bytes of a bloc define its size and its state:
b15-b1 = size in number of word (2 bytes)
b0 = used state (1=used, 0=free)
To reach the next bloc you just need to do:
next_bloc_address = bloc_addres + bloc_size The end of memory is defined with a 0 sized bloc.

Macro Definition Documentation

◆ astMemcpy

#define astMemcpy ( to,
from,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memcpy(..) instead.\"")

◆ fastMemcpyU16

#define fastMemcpyU16 ( to,
from,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memcpy(..) instead.\"")

◆ fastMemcpyU32

#define fastMemcpyU32 ( to,
from,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memcpy(..) instead.\"")

◆ fastMemset

#define fastMemset ( to,
value,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memset(..) instead.\"")

◆ fastMemsetU16

#define fastMemsetU16 ( to,
value,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memsetU16(..) instead.\"")

◆ fastMemsetU32

#define fastMemsetU32 ( to,
value,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memsetU32(..) instead.\"")

◆ free

#define free ( x)
Value:
MEM_free(x)

◆ GET_DWORDFROMPBYTE

#define GET_DWORDFROMPBYTE ( src)
Value:
((src[0] << 24) | (src[1] << 16) | (src[2] << 8) | (src[3] << 0))

Get u32 from u8 array (BigEndian order).

◆ GET_DWORDFROMPBYTE_LI

#define GET_DWORDFROMPBYTE_LI ( src)
Value:
((src[0] << 0) | (src[1] << 8) | (src[2] << 16) | (src[3] << 24))

Get u32 from u8 array (LittleEndian order).

◆ GET_DWORDFROMPWORD

#define GET_DWORDFROMPWORD ( src)
Value:
((src[0] << 16) | (src[1] << 0))

Get u32 from u16 array (BigEndian order).

◆ GET_DWORDFROMPWORD_LI

#define GET_DWORDFROMPWORD_LI ( src)
Value:
((src[0] << 0) | (src[1] << 16))

Get u32 from u16 array (LittleEndian order).

◆ GET_WORDFROMPBYTE

#define GET_WORDFROMPBYTE ( src)
Value:
((src[0] << 8) | (src[1] << 0))

Get u16 from u8 array (BigEndian order).

◆ GET_WORDFROMPBYTE_LI

#define GET_WORDFROMPBYTE_LI ( src)
Value:
((src[0] << 0) | (src[1] << 8))

Get u16 from u8 array (LittleEndian order).

◆ malloc

#define malloc ( x)
Value:
MEM_alloc(x)

◆ memcpyU16

#define memcpyU16 ( to,
from,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memcpy(..) instead.\"")

◆ memcpyU32

#define memcpyU32 ( to,
from,
len )
Value:
_Pragma("GCC error \"This method is deprecated, use memcpy(..) instead.\"")

◆ SWAP_s16

#define SWAP_s16 ( x,
y )
Value:
{ \
s16 swp; \
\
swp = x; \
x = y; \
y = swp; \
}
short s16
Definition types.h:84

Exchange value of specified s16 variables.

◆ SWAP_s32

#define SWAP_s32 ( x,
y )
Value:
{ \
s32 swp; \
\
swp = x; \
x = y; \
y = swp; \
}
long s32
Definition types.h:89

Exchange value of specified s32 variables.

◆ SWAP_s8

#define SWAP_s8 ( x,
y )
Value:
{ \
s8 swp; \
\
swp = x; \
x = y; \
y = swp; \
}
char s8
Definition types.h:79

Exchange value of specified s8 variables.

◆ SWAP_u16

#define SWAP_u16 ( x,
y )
Value:
{ \
u16 swp; \
\
swp = x; \
x = y; \
y = swp; \
}
unsigned short u16
Definition types.h:100

Exchange value of specified u16 variables.

◆ SWAP_u32

#define SWAP_u32 ( x,
y )
Value:
{ \
u32 swp; \
\
swp = x; \
x = y; \
y = swp; \
}
unsigned long u32
Definition types.h:105

Exchange value of specified u32 variables.

◆ SWAP_u8

#define SWAP_u8 ( x,
y )
Value:
{ \
u8 swp; \
\
swp = x; \
x = y; \
y = swp; \
}
unsigned char u8
Definition types.h:95

Exchange value of specified u8 variables.

Function Documentation

◆ MEM_alloc()

void * MEM_alloc ( u16 size)

Allocate memory block.

Parameters
sizeNumber of bytes to allocate
Returns
On success, a pointer to the memory block allocated by the function. The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable. If the function failed to allocate the requested block of memory (or if specified size = 0), a NULL pointer is returned.

Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

◆ MEM_allocAt()

void * MEM_allocAt ( u32 addr,
u16 size )

Allocate memory block at a specific address (useful for short addressing or fixed low level working address)

Parameters
addrAddress where we want to allocate memory
sizeNumber of bytes to allocate
Returns
On success, a pointer to the memory block allocated by the function (should match the addr parameter). The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable. If the function failed to allocate the requested block of memory (or if specified size = 0), a NULL pointer is returned.

Allocates a block of size bytes of memory at the given address, returning a pointer to the beginning of the block (equal to the address here). The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

◆ MEM_dump()

void MEM_dump ( void )

Show memory dump.

Do a memory dump in GensKMod console (useful for debugging).

◆ MEM_free()

void MEM_free ( void * ptr)

Deallocate space in memory.

Parameters
ptrPointer to a memory block previously allocated with Mem_alloc to be deallocated.
If a null pointer is passed as argument, no action occurs.

A block of memory previously allocated using a call to Mem_alloc is deallocated, making it available again for further allocations. Notice that this function leaves the value of ptr unchanged, hence it still points to the same (now invalid) location, and not to the null pointer.

◆ memcpy()

void memcpy ( void * to,
const void * from,
u16 len )

Copy block of memory.

Parameters
toPointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
fromPointer to the source of data to be copied, type-casted to a pointer of type void*.
lenNumber of bytes to copy.

Copies the values of len long from the location pointed by from directly to the memory block pointed by to. The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.

◆ memset()

void memset ( void * to,
u8 value,
u16 len )

Fill block of memory.

Parameters
toPointer to the block of memory to fill.
valueValue to be set.
lenNumber of u8 (byte) to be set to the value.

Sets the first num bytes of the block of memory pointed by to with the specified value.

◆ memsetU16()

void memsetU16 ( u16 * to,
u16 value,
u16 len )

Fill block of memory (optimized for u16)

Parameters
toPointer to the block of memory to fill.
valueValue to be set.
lenNumber of (u16) short to be set to the value.

Sets the first num shorts of the block of memory pointed by to with the specified value.

◆ memsetU32()

void memsetU32 ( u32 * to,
u32 value,
u16 len )

Fill block of memory (optimized for u32)

Parameters
toPointer to the block of memory to fill.
valueValue to be set.
lenNumber of u32 (long) to be set to the value.

Sets the first num longs of the block of memory pointed by to with the specified value.