PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
recycle.c
1/*
2--------------------------------------------------------------------
3By Bob Jenkins, September 1996. recycle.c
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 structures
12 only requires freeing the root.
13--------------------------------------------------------------------
14*/
15
16# include "recycle.h"
17
18#include <string.h>
19#include <stdlib.h>
20
21reroot *remkroot(
22 size_t size
23) {
24 reroot *r = (reroot *)remalloc(sizeof(reroot));
25 r->list = (recycle *)0;
26 r->trash = (recycle *)0;
27 r->size = align(size);
28 r->logsize = RESTART;
29 r->numleft = 0;
30 return r;
31}
32
33void refree(
34 struct reroot *r
35) {
36 while (r->list)
37 {
38 recycle *temp = r->list->next;
39 free((char *)r->list);
40 r->list = temp;
41 }
42 free((char *)r);
43 return;
44}
45
46/* to be called from the macro renew only */
47char *renewx(
48 struct reroot *r
49) {
50 recycle *temp;
51 if (r->trash)
52 { /* pull a node off the trash heap */
53 temp = r->trash;
54 r->trash = temp->next;
55 (void)memset((void *)temp, 0, r->size);
56 }
57 else
58 { /* allocate a new block of nodes */
59 r->numleft = r->size*((uint32_t)1<<r->logsize);
60 if (r->numleft < REMAX) ++r->logsize;
61 temp = (recycle *)remalloc(sizeof(recycle) + r->numleft);
62 temp->next = r->list;
63 r->list = temp;
64 r->numleft-=r->size;
65 temp = (recycle *)((char *)(r->list+1)+r->numleft);
66 }
67 return (char *)temp;
68}
69
70char *remalloc(
71 size_t len
72) {
73 char *x = (char *)malloc(len);
74 if (!x)
75 {
76 //exit(SUCCESS); // let's just bug silently...
77 }
78 return x;
79}
80