PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
recycle.h
1/*
2--------------------------------------------------------------------
3By Bob Jenkins, September 1996. recycle.h
4You may use this code in any way you wish, and it is free. No warranty.
5
6This manages memory for commonly-allocated structures.
7It allocates RESTART to REMAX items at a time.
8Timings have shown that, if malloc is used for every new structure,
9 malloc will consume about 90% of the time in a program. This
10 module cuts down the number of mallocs by an order of magnitude.
11This also decreases memory fragmentation, and freeing all structures
12 only requires freeing the root.
13--------------------------------------------------------------------
14*/
15
16#ifndef STANDARD
17#include "standard.h"
18#endif
19
20#ifndef RECYCLE
21#define RECYCLE
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#define RESTART 0
28#define REMAX 32000
29
30struct recycle
31{
32 struct recycle *next;
33};
34typedef struct recycle recycle;
35
36struct reroot
37{
38 struct recycle *list; /* list of malloced blocks */
39 struct recycle *trash; /* list of deleted items */
40 size_t size; /* size of an item */
41 size_t logsize; /* log_2 of number of items in a block */
42 word numleft; /* number of bytes left in this block */
43};
44typedef struct reroot reroot;
45
46/* make a new recycling root */
47reroot *remkroot(/*_ size_t mysize _*/);
48
49/* free a recycling root and all the items it has made */
50void refree(/*_ struct reroot *r _*/);
51
52/* get a new (cleared) item from the root */
53#define renew(r) ((r)->numleft ? \
54 (((char *)((r)->list+1))+((r)->numleft-=(r)->size)) : renewx(r))
55
56char *renewx(/*_ struct reroot *r _*/);
57
58/* delete an item; let the root recycle it */
59/* void redel(/o_ struct reroot *r, struct recycle *item _o/); */
60#define redel(root,item) { \
61 ((recycle *)item)->next=(root)->trash; \
62 (root)->trash=(recycle *)(item); \
63}
64
65/* malloc, but exit program if no joy */
66/* use plain free() to free memory allocated by remalloc() */
67char *remalloc(/*_ size_t len _*/);
68
69#ifdef __cplusplus
70}
71#endif
72
73#endif /* RECYCLE */