-
Notifications
You must be signed in to change notification settings - Fork 25
/
MEM.h
80 lines (68 loc) · 2.17 KB
/
MEM.h
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
#ifndef PUBLIC_MEM_H
#define PUBLIC_MEM_H
/**
* 内存出错之后的处理方式
*/
typedef enum {
MEM_FAIL_AND_EXIT,
MEM_FAIL_AND_RETURN
} MEM_FailMode;
typedef struct MEM_Controller_tag *MEM_Controller;
typedef struct MEM_Storage_tag *MEM_Storage;
extern MEM_Controller mem_default_controller;
#ifdef MEM_CONTROLLER
#define MEM_CURRENT_CONTROLLER MEM_CONTROLLER
#else /* MEM_CONTROLLER */
#define MEM_CURRENT_CONTROLLER mem_default_controller
#endif /* MEM_CONTROLLER */
/**
* 内存出错处理器
*/
typedef void (*MEM_ErrorHandler)(MEM_Controller, char *, int, char *);
/**
* 分配内存的函数
*/
void *MEM_malloc_func(MEM_Controller controller,
char *filename, int line, size_t size);
/**
* 对已经分配的一段内存已经扩充
*/
void *MEM_realloc_func(MEM_Controller controller,
char *filename, int line, void *ptr, size_t size);
/**
* 把当前所有的内存详情都 dump 出来的函数
*/
void MEM_dump_blocks_func(MEM_Controller controller, FILE *fp);
/**
* 创建一个新的内存 storage
*/
MEM_Storage MEM_open_storage_func(MEM_Controller controller, char* filename, int line, int page_size);
/**
* 从 storage 里面分配一块内存出来
*/
void* MEM_storage_malloc_func(MEM_Controller controller,
char* filename,
int line,
MEM_Storage storage,
size_t size);
/**
* 销毁一个内存 storage
*/
void MEM_dispose_storage_func(MEM_Controller controller, MEM_Storage storage);
/* 定义外部更简单的调用方法 */
#define MEM_malloc(size)\
(MEM_malloc_func(MEM_CURRENT_CONTROLLER,\
__FILE__, __LINE__, size))
#define MEM_realloc(size)\
(MEM_realloc_func(MEM_CURRENT_CONTROLLER, __FILE__, __LINE__, ptr, size))
#define MEM_dump_blocks(fp)\
(MEM_dump_blocks_func(MEM_CURRENT_CONTROLLER, fp))
#define MEM_open_storage(page_size)\
(MEM_open_storage_func(MEM_CURRENT_CONTROLLER,\
__FILE__, __LINE__, page_size))
#define MEM_storage_malloc(storage, size)\
(MEM_storage_malloc_func(MEM_CURRENT_CONTROLLER, storage,\
__FILE__, __LINE__, size))
#define MEM_dispose_storage(storage)\
(MEM_dispose_storage_func(MEM_CURRENT_CONTROLLER, storage))
#endif