QuakeForge  0.7.2.210-815cf
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Resource Management

Builtin module private data management. More...

Functions

void PR_Resources_Clear (progs_t *pr)
 Clear all resources before loading a new progs. More...
 
void * PR_Resources_Find (progs_t *pr, const char *name)
 Retrieve a resource registered with the VM. More...
 
void PR_Resources_Init (progs_t *pr)
 Initialize the resource management fields. More...
 
void PR_Resources_Register (progs_t *pr, const char *name, void *data, void(*clear)(progs_t *, void *))
 Register a resource with a VM. More...
 

Resource Map support

These macros can be used to create functions for mapping C resources to QuakeC integer handles.

Valid handles are always negative.

Note
map is the resource map itself, not a pointer to the resource map.
#define PR_RESMAP(type)   struct { type *_free; type **_map; unsigned _size; }
 Type delcaration for the resource map. More...
 
#define PR_RESNEW(type, map)
 Allocate a new resource from the resource map. More...
 
#define PR_RESFREE(type, map, t)
 Free a resource returning it to the resource map. More...
 
#define PR_RESRESET(type, map)
 Free all resources in the resource map. More...
 
#define PR_RESGET(map, col)
 Retrieve a resource from the resource map using a handle. More...
 
#define PR_RESINDEX(map, ptr)
 Convert a resource pointer to a handle. More...
 

Detailed Description

Builtin module private data management.

Macro Definition Documentation

#define PR_RESFREE (   type,
  map,
 
)
Value:
memset (t, 0, sizeof (type)); \
*(type **) t = map._free; \
map._free = t
int type
Definition: net_wins.c:85

Free a resource returning it to the resource map.

Parameters
typeThe type of the resource. Must match the type parameter used for PR_RESMAP.
mapThe resource map.
tPointer to the resource to be freed.
#define PR_RESGET (   map,
  col 
)
Value:
unsigned row = ~col / 1024; \
col = ~col % 1024; \
if (row >= map._size) \
return 0; \
return &map._map[row][col]
if(!(yy_init))
Definition: qp-lex.c:893

Retrieve a resource from the resource map using a handle.

Parameters
mapThe resource map.
colThe handle.
Returns
A pointer to the resource, or NULL if the handle is invalid.
#define PR_RESINDEX (   map,
  ptr 
)
Value:
unsigned i; \
for (i = 0; i < map._size; i++) { \
long d = ptr - map._map[i]; \
if (d >= 0 && d < 1024) \
return ~(i * 1024 + d); \
} \
return 0
if(!(yy_init))
Definition: qp-lex.c:893

Convert a resource pointer to a handle.

Parameters
mapThe resource map.
ptrThe resource pointer.
Returns
The handle or 0 if the pointer is invalid.
#define PR_RESMAP (   type)    struct { type *_free; type **_map; unsigned _size; }

Type delcaration for the resource map.

Parameters
typeThe type of the resource. The size must be at least as large as sizeof(type *).
#define PR_RESNEW (   type,
  map 
)
Value:
type *t; \
if (!map._free) { \
int i, size; \
map._size++; \
size = map._size * sizeof (type *); \
map._map = realloc (map._map, size); \
if (!map._map) \
return 0; \
map._free = calloc (1024, sizeof (type)); \
if (!map._free) \
return 0; \
map._map[map._size - 1] = map._free; \
for (i = 0; i < 1023; i++) \
*(type **) &map._free[i] = &map._free[i + 1]; \
*(type **) &map._free[i] = 0; \
} \
t = map._free; \
map._free = *(type **) t; \
memset (t, 0, sizeof (type)); \
return t
int(PASCAL FAR *pWSAStartup)(WORD wVersionRequired
if(!(yy_init))
Definition: qp-lex.c:893
char * realloc()
int type
Definition: net_wins.c:85

Allocate a new resource from the resource map.

Parameters
typeThe type of the resource. Must match the type parameter used for PR_RESMAP.
mapThe resource map.
Returns
A pointer to the new resource, or null if no more could be allocated.
#define PR_RESRESET (   type,
  map 
)
Value:
unsigned i, j; \
if (!map._size) \
return; \
for (i = 0; i < map._size; i++) { \
map._free = map._map[i]; \
for (j = 0; j < 1023; j++) \
*(type **) &map._free[j] = &map._free[j + 1]; \
if (i < map._size - 1) \
*(type **) &map._free[j] = &map._map[i + 1][0]; \
} \
map._free = map._map[0];
if(!(yy_init))
Definition: qp-lex.c:893
int type
Definition: net_wins.c:85

Free all resources in the resource map.

Any memory allocated to the resource must be freed before freeing the resource.

Parameters
typeThe type of the resource. Must match the type parameter used for PR_RESMAP.
mapThe resource map.

Function Documentation

void PR_Resources_Clear ( progs_t pr)

Clear all resources before loading a new progs.

Calls the clear() callback of all registered resources.

Parameters
prThe VM of which the resources will be cleared.
void* PR_Resources_Find ( progs_t pr,
const char *  name 
)

Retrieve a resource registered with the VM.

Parameters
prThe VM from which the resource will be retrieved.
nameThe name of the resource.
Returns
The resource, or NULL if name does not match any resource registered with the VM.
void PR_Resources_Init ( progs_t pr)

Initialize the resource management fields.

Parameters
prThe VM of which the resource fields will be initialized.
void PR_Resources_Register ( progs_t pr,
const char *  name,
void *  data,
void(*)(progs_t *, void *)  clear 
)

Register a resource with a VM.

Parameters
prThe VM to which the resource will be associated.
nameThe name of the resource. Used for retrieving the resource.
dataThe resource data. callback.
clearCallback for performing any necessary cleanup. Called by PR_Resources_Clear(). The parameters are the current VM (pr) and data.
Note
The name should be unique to the VM, but no checking is done. If the name is not unique, the previous registered resource will break. The name of the sub-system registering the resource is a suitable name, and will probably be unique.