OSDN Git Service

skip 表示を修正, squirrel の呼び出しバグを修正
[unagi/old-svn-converted.git] / client / trunk / memory_manage.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "memory_manage.h"
5 enum{
6         MANAGE_NUM = 0x100
7 };
8
9 struct manage{
10         const char *file, *function;
11         int line;
12         void *addr;
13         int size;
14 };
15 static const struct manage EMPTY = {
16         .file = NULL, .line = 0, .function = NULL,
17         .addr = NULL, .size = 0
18 };
19 static struct manage management[MANAGE_NUM];
20 void mm_init(void)
21 {
22         int i;
23         for(i = 0; i < MANAGE_NUM; i++){
24                 management[i] = EMPTY;
25         }
26 }
27 void *mm_malloc(const char *file, int line, const char *function, int size)
28 {
29         int i;
30         struct manage *t = management;
31         for(i = 0; i < MANAGE_NUM; i++){
32                 if(t->addr == NULL){
33                         t->addr = malloc(size);
34                         t->size = size;
35                         t->file = file;
36                         t->line = line;
37                         t->function = function;
38                         return t->addr;
39                 }
40                 t++;
41         }
42         assert(0);
43         return NULL;
44 }
45 void mm_free(void *addr)
46 {
47         int i;
48         struct manage *t = management;
49         for(i = 0; i < MANAGE_NUM; i++){
50                 if(t->addr == addr){
51                         free(addr);
52                         *t = EMPTY;
53                         return;
54                 }
55                 t++;
56         }
57         assert(0);
58 }
59 void mm_end(void)
60 {
61         int i;
62         struct manage *t = management;
63         for(i = 0; i < MANAGE_NUM; i++){
64                 if(t->addr != NULL){
65                         printf("**free forgot** %s:%d %s() size %d\n", t->file, t->line, t->function, t->size);
66                 }
67                 t++;
68         }
69 }