Browse Source
Set up some helper functions for memory management, including scratchpad memory. Moved the primitive buffer here too.
Set up some helper functions for memory management, including scratchpad memory. Moved the primitive buffer here too.
This will probably have to become inline functions or macros at some point, but for now it's just pure C.tess_experiment
5 changed files with 73 additions and 18 deletions
@ -0,0 +1,46 @@ |
|||
#include "common.h" |
|||
#include "memory.h" |
|||
|
|||
#define PRIMBUFLEN 131072 // TODO: optimize this for a typical scene |
|||
|
|||
static char primbuff[2][PRIMBUFLEN]; // Primitive buffer, just a raw buffer of bytes to use as a pool for primitives |
|||
static char *nextpri; |
|||
static char *primbuff_bounds; |
|||
|
|||
static char* const scratch_start = (char*)0x1F800000; |
|||
static char *scratch = scratch_start; |
|||
|
|||
void mem_scratch_reset() |
|||
{ |
|||
scratch = scratch_start; |
|||
} |
|||
|
|||
char *mem_scratch(unsigned short size) |
|||
{ |
|||
#if _DEBUG |
|||
if (scratch + size > scratch_start + 1024) |
|||
return NULL; |
|||
#endif |
|||
|
|||
char *result = scratch; |
|||
scratch += size; |
|||
return result; |
|||
} |
|||
|
|||
void mem_prim_reset(int db) |
|||
{ |
|||
nextpri = primbuff[db]; |
|||
primbuff_bounds = nextpri + PRIMBUFLEN; |
|||
} |
|||
|
|||
void *mem_prim(size_t size) |
|||
{ |
|||
#if _DEBUG |
|||
if (nextpri + size > primbuff_bounds) |
|||
return NULL; // TODO: report error |
|||
#endif |
|||
|
|||
void *prim = nextpri; |
|||
nextpri += size; |
|||
return prim; |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
#ifndef __MEMORY_H__ |
|||
#define __MEMORY_H__ |
|||
|
|||
/** |
|||
* @brief Allocate memory in the fast scratchpad RAM buffer. |
|||
* |
|||
* @param size The number of bytes to allocate. Be aware that the scratchpad memory only has 1024 bytes available. |
|||
* @return Pointer to the allocated buffer in scratchpad memory. |
|||
*/ |
|||
char *mem_scratch(unsigned short size); |
|||
|
|||
void mem_scratch_reset(); |
|||
|
|||
/** |
|||
* @brief Allocate a single primitive in the primitive buffer. |
|||
* |
|||
* @param size Size of the primitive struct to allocate. |
|||
* @return Pointer to the allocated struct in memory. |
|||
*/ |
|||
void *mem_prim(size_t size); |
|||
|
|||
void mem_prim_reset(int db); |
|||
|
|||
#endif // __MEMORY_H__ |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue