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