test_mem.c
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*************************************************************
test_mem.c : Nintendo 64 Music Tools Library Sample
(c) Copyright 1998, Software Creations (Holdings) Ltd.
Version 3.11
N64DD demo memory manager source file.
**************************************************************/
/* include system header files */
#ifndef F3DEX_GBI
#define F3DEX_GBI
#endif
#include <ultra64.h>
/* mimimum fragment size to maintain */
#define MINSIZE 1024+16
/* alignment - must be a power of two */
#define PADDING 16 /* cache align */
/* must be multiple of 16 bytes long */
typedef struct _malloc_s_
{
struct _malloc_s_ *previous;
struct _malloc_s_ *next;
unsigned long size;
unsigned long used;
} malloc_t;
static malloc_t *current;
/* free memory starts at end of code segment */
extern char _codeSegmentEnd[];
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[GLOBAL FUNCTION]
MemInit()
[Explantion]
Initialise memory manager.
[Return value]
NONE
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void MemInit(void)
{
unsigned long start,end;
unsigned long memsize;
/* start position is end of code segment */
start = (unsigned long) &_codeSegmentEnd[0];
start = (start+(PADDING-1))&(~(PADDING-1));
/* now calc length */
end = (start&0xff000000)+(unsigned long)osMemSize;
current = (malloc_t *)start;
current->size = end-start;
current->previous = NULL;
current->next = NULL;
current->used = 0;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[GLOBAL FUNCTION]
MemMalloc(size)
[Parameters]
size number of bytes requested
[Explantion]
Claim a block of memory from the applications heap.
[Return value]
Address of memory block or NULL if not enough memory is free
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void *MemMalloc(unsigned long size)
{
unsigned long claimsize, free;
malloc_t *list, *next;
claimsize = ((size+(PADDING-1))&(~(PADDING-1)))+sizeof(malloc_t);
list = current;
while ((list->used || list->size<claimsize) && list->next)
list=list->next;
if (!list->used && list->size>=claimsize)
{
free = list->size-claimsize;
if (free<MINSIZE)
{
claimsize=list->size;
free = 0;
}
list->used = size;
list->size = claimsize;
if (free)
{
next = (malloc_t *)((unsigned long)list+claimsize);
next->previous = list;
next->next = list->next;
list->next = next;
next->used = 0;
next->size = free;
}
return ((void *)(list+1));
}
return (NULL);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[GLOBAL FUNCTION]
MemFree(memory)
[Parameters]
memory address of a memory block returned from MemMalloc()
[Explantion]
Free a block of memory previously claimed from the applications heap.
[Return value]
NONE
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void MemFree(void *memory)
{
malloc_t *thisone, *previous, *next;
thisone = memory;
thisone->used = 0;
previous = thisone->previous;
next = previous->next;
if (previous && !previous->used)
{
previous->size += thisone->size;
previous->next = next;
thisone = previous;
previous=thisone->previous;
}
if (next && !next->used)
{
thisone->size += next->size;
thisone->next = next->next;
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[GLOBAL FUNCTION]
MemShowStatus()
[Explantion]
Display current status of applications heap.
[Return value]
NONE
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void MemShowStatus(void)
{
malloc_t *thisone;
thisone = current;
while (thisone)
{
if (thisone->used)
osSyncPrintf("USED 0x%08x size=0x%08x used=0x%08x\n",thisone,thisone->size,thisone->used);
else
osSyncPrintf("FREE 0x%08x size=0x%08x\n",thisone,thisone->size);
thisone = thisone->next;
}
}
/* end of file */