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