_free.c
1.39 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
#include <ultra64.h>
#include "string.h"
#include "_malloc.h"
int _free(void *msp, void *ptr)
{
unsigned int *next2;
unsigned int size2;
unsigned char *p2;
struct mallocST *malloc_st_ptr;
if (!ptr) return 1;
p2 = (unsigned char *)ptr - MALLOC_HEADSIZE;
malloc_st_ptr = (struct mallocST *)p2;
if (!malloc_st_ptr->flag) return 1; // 最初から開放済み
next2 = malloc_st_ptr->next; /* 次のメモリブロックが未使用なら加える処置 */
size2 = malloc_st_ptr->size;
malloc_st_ptr = (struct mallocST *)next2;
if (next2 && !malloc_st_ptr->flag) {
size2 += malloc_st_ptr->size + MALLOC_HEADSIZE;
next2 = (unsigned int *)malloc_st_ptr->next;
}
malloc_st_ptr = msp;
while(1) {
if ((unsigned char *)malloc_st_ptr->next == p2) {
if (!malloc_st_ptr->flag) {
p2 = (unsigned char *)malloc_st_ptr;
size2 += malloc_st_ptr->size + MALLOC_HEADSIZE;
}
break;
}
if ((unsigned char *)malloc_st_ptr > p2) break;
if (!malloc_st_ptr->next) break;
malloc_st_ptr = (struct mallocST *)malloc_st_ptr->next;
}
malloc_st_ptr = (struct mallocST *)p2; /* メモリ開放 */
malloc_st_ptr->next = next2;
malloc_st_ptr->size = size2;
malloc_st_ptr->flag = 0;
return 0;
}