sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
recycle.h
Go to the documentation of this file.
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
20#ifndef RECYCLE_H
21#define RECYCLE_H
22
23#include "standard.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define RESTART 0
30#define REMAX 32000
31
32struct recycle
33{
34 struct recycle *next;
35};
36typedef struct recycle recycle;
37
38struct reroot
39{
40 struct recycle *list; /* list of malloced blocks */
41 struct recycle *trash; /* list of deleted items */
42 size_t size; /* size of an item */
43 size_t logsize; /* log_2 of number of items in a block */
44 int numleft; /* number of bytes left in this block */
45};
46typedef struct reroot reroot;
47
48/* make a new recycling root */
49extern reroot *remkroot(size_t mysize);
50
51/* free a recycling root and all the items it has made */
52extern void refree(struct reroot *r);
53
54/* get a new (cleared) item from the root */
55#define renew(r) ((r)->numleft ? \
56 (((char *)((r)->list+1))+((r)->numleft-=(r)->size)) : renewx(r))
57
58extern char *renewx(struct reroot *r);
59
60/* delete an item; let the root recycle it */
61/* void redel(/o_ struct reroot *r, struct recycle *item _o/); */
62#define redel(root,item) { \
63 ((recycle *)item)->next=(root)->trash; \
64 (root)->trash=(recycle *)(item); \
65}
66
67/* malloc, but exit program if no joy */
68/* use plain free() to free memory allocated by remalloc() */
69extern char *remalloc(size_t len);
70
71#ifdef __cplusplus
72}
73#endif
74
75#endif /* RECYCLE_H */