ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
recycle.c
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------
3 By Bob Jenkins, September 1996. recycle.c
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 structures
12  only requires freeing the root.
13 --------------------------------------------------------------------
14 */
15 
16 #include <string.h>
17 #include <malloc.h>
18 
19 #ifndef STANDARD
20 # include "standard.h"
21 #endif
22 #ifndef RECYCLE
23 # include "recycle.h"
24 #endif
25 
27 size_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 
38 void refree(r)
39 struct reroot *r;
40 {
41  recycle *temp;
42  if ((temp = r->list)) while (r->list)
43  {
44  temp = r->list->next;
45  free((char *)r->list);
46  r->list = temp;
47  }
48  free((char *)r);
49  return;
50 }
51 
52 /* to be called from the macro renew only */
53 char *renewx(r)
54 struct reroot *r;
55 {
56  recycle *temp;
57  if (r->trash)
58  { /* pull a node off the trash heap */
59  temp = r->trash;
60  r->trash = temp->next;
61  (void)memset((void *)temp, 0, r->size);
62  }
63  else
64  { /* allocate a new block of nodes */
65  r->numleft = r->size*((ub4)1<<r->logsize);
66  if (r->numleft < REMAX) ++r->logsize;
67  temp = (recycle *)remalloc(sizeof(recycle) + r->numleft);
68  temp->next = r->list;
69  r->list = temp;
70  r->numleft-=r->size;
71  temp = (recycle *)((char *)(r->list+1)+r->numleft);
72  }
73  return (char *)temp;
74 }
75 
76 char *remalloc(len)
77 size_t len;
78 {
79  char *x = (char *)malloc(len);
80  if (!x)
81  {
82  //exit(SUCCESS); // let's just bug silently...
83  }
84  return x;
85 }
86 
s32 x
Definition: libmouse.c:34
s8 align[6]
Definition: libpad.c:230
char * renewx(struct reroot *r)
Definition: recycle.c:53
char * remalloc(size_t len)
Definition: recycle.c:76
void refree(struct reroot *r)
Definition: recycle.c:38
reroot * remkroot(size_t size)
Definition: recycle.c:26
#define REMAX
Definition: recycle.h:28
#define RESTART
Definition: recycle.h:27
u32 ub4
Definition: standard.h:32
struct recycle * next
Definition: recycle.h:32
Definition: recycle.h:37
struct recycle * list
Definition: recycle.h:38
size_t logsize
Definition: recycle.h:41
struct recycle * trash
Definition: recycle.h:39
word numleft
Definition: recycle.h:42
size_t size
Definition: recycle.h:40