sgdk
memory.h File Reference

Memory handling methods. More...

Go to the source code of this file.

Defines

#define ROM   0x00000000
 Define start of ROM region.
#define RAM   0xE0FF0000
 Define start of RAM region.
#define STACK_SIZE   0x0A00
 Define memory allocated for stack (default = 0xA00)
#define MEMORY_HIGH   (0xE1000000 - STACK_SIZE)
 Define the memory high address limit for dynamic allocation.
#define GET_DWORDFROMPBYTE(src)   ((src[0] << 24) | (src[1] << 16) | (src[2] << 8) | (src[3] << 0))
 Get u32 from u8 array (BigEndian order).
#define GET_DWORDFROMPBYTE_LI(src)   ((src[0] << 0) | (src[1] << 8) | (src[2] << 16) | (src[3] << 24))
 Get u32 from u8 array (LittleEndian order).
#define GET_WORDFROMPBYTE(src)   ((src[0] << 8) | (src[1] << 0))
 Get u16 from u8 array (BigEndian order).
#define GET_WORDFROMPBYTE_LI(src)   ((src[0] << 0) | (src[1] << 8))
 Get u16 from u8 array (LittleEndian order).
#define GET_DWORDFROMPWORD(src)   ((src[0] << 16) | (src[1] << 0))
 Get u32 from u16 array (BigEndian order).
#define GET_DWORDFROMPWORD_LI(src)   ((src[0] << 0) | (src[1] << 16))
 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)   MEM_alloc(x)
#define free(x)   MEM_free(x)

Functions

u16 MEM_getFree ()
 Return available memory in bytes.
u16 MEM_getAllocated ()
 Return allocated memory in bytes.
u16 MEM_getLargestFreeBlock ()
 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_pack ()
 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 ()
 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.
void memcpyU16 (u16 *to, const u16 *from, u16 len)
void memcpyU32 (u32 *to, const u32 *from, u16 len)
void fastMemset (void *to, u8 value, u16 len)
void fastMemsetU16 (u16 *to, u16 value, u16 len)
void fastMemsetU32 (u32 *to, u32 value, u16 len)
void fastMemcpy (void *to, const void *from, u16 len)
void fastMemcpyU16 (u16 *to, const u16 *from, u16 len)
void fastMemcpyU32 (u32 *to, const u32 *from, u16 len)

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.


Define Documentation

#define SWAP_s16 (   x,
 
)
Value:
{                           \
    s16 swp;                \
                            \
    swp = x;                \
    x = y;                  \
    y = swp;                \
}

Exchange value of specified s16 variables.

#define SWAP_s32 (   x,
 
)
Value:
{                           \
    s32 swp;                \
                            \
    swp = x;                \
    x = y;                  \
    y = swp;                \
}

Exchange value of specified s32 variables.

#define SWAP_s8 (   x,
 
)
Value:
{                           \
    s8 swp;                 \
                            \
    swp = x;                \
    x = y;                  \
    y = swp;                \
}

Exchange value of specified s8 variables.

#define SWAP_u16 (   x,
 
)
Value:
{                           \
    u16 swp;                \
                            \
    swp = x;                \
    x = y;                  \
    y = swp;                \
}

Exchange value of specified u16 variables.

#define SWAP_u32 (   x,
 
)
Value:
{                           \
    u32 swp;                \
                            \
    swp = x;                \
    x = y;                  \
    y = swp;                \
}

Exchange value of specified u32 variables.

#define SWAP_u8 (   x,
 
)
Value:
{                           \
    u8 swp;                 \
                            \
    swp = x;                \
    x = y;                  \
    y = swp;                \
}

Exchange value of specified u8 variables.


Function Documentation

void fastMemcpy ( void *  to,
const void *  from,
u16  len 
)
void fastMemcpyU16 ( u16 to,
const u16 from,
u16  len 
)
void fastMemcpyU32 ( u32 to,
const u32 from,
u16  len 
)
void fastMemset ( void *  to,
u8  value,
u16  len 
)
void fastMemsetU16 ( u16 to,
u16  value,
u16  len 
)
Deprecated:
Uses memsetU16(void *to, u16 value, u16 len) instead.
void fastMemsetU32 ( u32 to,
u32  value,
u16  len 
)
Deprecated:
Uses memsetU32(void *to, u32 value, u16 len) instead.
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.

void MEM_dump ( )

Show memory dump.

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

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.

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.

void memcpyU16 ( u16 to,
const u16 from,
u16  len 
)
void memcpyU32 ( u32 to,
const u32 from,
u16  len 
)
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.

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.

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.

 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines