acl  3.5.3.0
acl_slice.h
浏览该文件的文档.
1 #ifndef ACL_SLICE_INCLUDE_H
2 #define ACL_SLICE_INCLUDE_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include "acl_define.h"
9 
10 #define ACL_SLICE_FLAG_OFF (0)
11 #define ACL_SLICE_FLAG_GC1 (1 << 0) /**< 空间节省, 但 gc 性能差 */
12 #define ACL_SLICE_FLAG_GC2 (1 << 1) /**< 空间中等, gc 比较好 */
13 #define ACL_SLICE_FLAG_GC3 (1 << 2) /**< 空间最大, gc 只当顺序时最好 */
14 #define ACL_SLICE_FLAG_RTGC_OFF (1 << 10) /**< 关闭实时内存释放 */
15 #define ACL_SLICE_FLAG_LP64_ALIGN (1 << 11) /**< 是否针对64位平台需要按8字节对齐 */
16 
17 /**
18  * 内存分片池的状态结构
19  */
20 typedef struct ACL_SLICE_STAT {
21  int nslots; /**< total slice count free in slots */
22  int islots; /**< current position of free slots slice */
23  int page_nslots; /**< count slice of each page */
24  int page_size; /**< length of each malloc */
25  int slice_length; /**< length of each slice from user's set */
26  int slice_size; /**< length of each slice really allocated */
27  int nbuf; /**< count of MEM_BUF allocated */
28  acl_uint64 length; /**< total size of all MEM_BUF's buf */
29  acl_uint64 used_length; /**< total size of used */
30  unsigned int flag; /**< same as the ACL_SLICE's flag been set when created */
32 
33 typedef struct ACL_SLICE ACL_SLICE;
34 
35 /**
36  * 创建内存片池对象
37  * @param name {const char*} 标识名称,以便于调试
38  * @param page_size {int} 分配内存时的分配内存页大小
39  * @param slice_size {int} 每个固定长度内存片的大小
40  * @param flag {unsigned int} 标志位,参见上述:ACL_SLICE_FLAG_xxx
41  * @return {ACL_SLICE*} 内存片池对象句柄
42  */
43 ACL_API ACL_SLICE *acl_slice_create(const char *name, int page_size,
44  int slice_size, unsigned int flag);
45 
46 /**
47  * 销毁一个内存片池对象
48  * @param slice {ACL_SLICE*} 内存片池对象
49  */
50 ACL_API void acl_slice_destroy(ACL_SLICE *slice);
51 
52 /**
53  * 该内存片池中有多少个内存片正在被使用
54  * @param slice {ACL_SLICE*} 内存片池对象
55  * @return {int} >= 0, 正在被使用的内存片个数
56  */
57 ACL_API int acl_slice_used(ACL_SLICE *slice);
58 
59 /**
60  * 分配一块内存片
61  * @param slice {ACL_SLICE*} 内存片池对象
62  * @return {void*} 内存片地址
63  */
64 ACL_API void *acl_slice_alloc(ACL_SLICE *slice);
65 
66 /**
67  * 分配一块内存片,且将内存片内容初始化为0
68  * @param slice {ACL_SLICE*} 内存片池对象
69  * @return {void*} 内存片地址
70  */
71 ACL_API void *acl_slice_calloc(ACL_SLICE *slice);
72 
73 /**
74  * 释放一块内存片
75  * @param slice {ACL_SLICE*} 内存片池对象
76  * @param ptr {void*} 内存片地址, 必须是由 acl_slice_alloc/acl_slice_calloc 所分配
77  */
78 ACL_API void acl_slice_free2(ACL_SLICE *slice, void *ptr);
79 
80 /**
81  * 释放一块内存片
82  * @param ptr {void*} 内存片地址, 必须是由 acl_slice_alloc/acl_slice_calloc 所分配
83  */
84 ACL_API void acl_slice_free(void *ptr);
85 
86 /**
87  * 查看内存片池的当前状态
88  * @param slice {ACL_SLICE*} 内存片池对象
89  * @param sbuf {ACL_SLICE_STAT*} 存储结果, 不能为空
90  */
91 ACL_API void acl_slice_stat(ACL_SLICE *slice, ACL_SLICE_STAT *sbuf);
92 
93 /**
94  * 手工将内存片池不用的内存进行释放
95  * @param slice {ACL_SLICE*} 内存片池对象
96  */
97 ACL_API int acl_slice_gc(ACL_SLICE *slice);
98 
99 /*----------------------------------------------------------------------------*/
100 
101 typedef struct ACL_SLICE_POOL {
102  ACL_SLICE **slices; /* the slice array */
103  int base; /* the base byte size */
104  int nslice; /* the max number of base size */
105  unsigned int slice_flag; /* flag: ACL_SLICE_FLAG_GC2[3] | ACL_SLICE_FLAG_RTGC_OFF */
107 
108 ACL_API void acl_slice_pool_init(ACL_SLICE_POOL *asp);
109 ACL_API ACL_SLICE_POOL *acl_slice_pool_create(int base, int nslice,
110  unsigned int slice_flag);
111 ACL_API void acl_slice_pool_destroy(ACL_SLICE_POOL *asp);
112 ACL_API int acl_slice_pool_used(ACL_SLICE_POOL *asp);
113 ACL_API void acl_slice_pool_clean(ACL_SLICE_POOL *asp);
114 ACL_API void acl_slice_pool_reset(ACL_SLICE_POOL *asp);
115 ACL_API void acl_slice_pool_free(const char *filename, int line, void *buf);
116 ACL_API void acl_slice_pool_gc(ACL_SLICE_POOL *asp);
117 ACL_API void *acl_slice_pool_alloc(const char *filename, int line,
118  ACL_SLICE_POOL *asp, size_t size);
119 ACL_API void *acl_slice_pool_calloc(const char *filename, int line,
120  ACL_SLICE_POOL *asp, size_t nmemb, size_t size);
121 ACL_API void *acl_slice_pool_realloc(const char *filename, int line,
122  ACL_SLICE_POOL *asp, void *ptr, size_t size);
123 ACL_API void *acl_slice_pool_memdup(const char *filename, int line,
124  ACL_SLICE_POOL *asp, const void *ptr, size_t len);
125 ACL_API char *acl_slice_pool_strdup(const char *filename, int line,
126  ACL_SLICE_POOL *asp, const char *str);
127 ACL_API char *acl_slice_pool_strndup(const char *filename, int line,
128  ACL_SLICE_POOL *asp, const char *str, size_t len);
129 
130 ACL_API void acl_slice_mem_hook(void *(*malloc_hook)(const char*, int, size_t),
131  void *(*calloc_hook)(const char*, int, size_t, size_t),
132  void *(*realloc_hook)(const char*, int, void*, size_t),
133  void (*free_hook)(const char*, int, void*));
134 ACL_API void acl_slice_mem_unhook(void);
135 
136 #ifdef __cplusplus
137 }
138 #endif
139 
140 #endif
ACL_API void * acl_slice_pool_calloc(const char *filename, int line, ACL_SLICE_POOL *asp, size_t nmemb, size_t size)
ACL_API void * acl_slice_alloc(ACL_SLICE *slice)
ACL_API int acl_slice_used(ACL_SLICE *slice)
ACL_API void acl_slice_pool_init(ACL_SLICE_POOL *asp)
ACL_API void acl_slice_pool_clean(ACL_SLICE_POOL *asp)
HTTP_API void const char * name
Definition: lib_http.h:620
ACL_API void * acl_slice_pool_realloc(const char *filename, int line, ACL_SLICE_POOL *asp, void *ptr, size_t size)
ACL_API void acl_slice_pool_reset(ACL_SLICE_POOL *asp)
ACL_API void * acl_slice_pool_memdup(const char *filename, int line, ACL_SLICE_POOL *asp, const void *ptr, size_t len)
ACL_API void acl_slice_destroy(ACL_SLICE *slice)
acl_uint64 used_length
Definition: acl_slice.h:29
ACL_API char * acl_slice_pool_strndup(const char *filename, int line, ACL_SLICE_POOL *asp, const char *str, size_t len)
int slice_length
Definition: acl_slice.h:25
ACL_API void acl_slice_pool_free(const char *filename, int line, void *buf)
ACL_API void acl_slice_free(void *ptr)
ACL_API void acl_slice_free2(ACL_SLICE *slice, void *ptr)
ACL_API int acl_slice_gc(ACL_SLICE *slice)
ACL_API void * acl_slice_pool_alloc(const char *filename, int line, ACL_SLICE_POOL *asp, size_t size)
ACL_API ACL_SLICE * acl_slice_create(const char *name, int page_size, int slice_size, unsigned int flag)
ACL_API void acl_slice_stat(ACL_SLICE *slice, ACL_SLICE_STAT *sbuf)
ACL_API void acl_slice_pool_destroy(ACL_SLICE_POOL *asp)
ACL_API void acl_slice_pool_gc(ACL_SLICE_POOL *asp)
ACL_API void * acl_slice_calloc(ACL_SLICE *slice)
acl_uint64 length
Definition: acl_slice.h:28
ACL_API void acl_slice_mem_unhook(void)
ACL_API int acl_slice_pool_used(ACL_SLICE_POOL *asp)
struct ACL_SLICE_STAT ACL_SLICE_STAT
ACL_API ACL_SLICE_POOL * acl_slice_pool_create(int base, int nslice, unsigned int slice_flag)
ACL_SLICE ** slices
Definition: acl_slice.h:102
struct ACL_SLICE ACL_SLICE
Definition: acl_slice.h:33
struct ACL_SLICE_POOL ACL_SLICE_POOL
ACL_API void acl_slice_mem_hook(void *(*malloc_hook)(const char *, int, size_t), void *(*calloc_hook)(const char *, int, size_t, size_t), void *(*realloc_hook)(const char *, int, void *, size_t), void(*free_hook)(const char *, int, void *))
unsigned int flag
Definition: acl_slice.h:30
ACL_API char * acl_slice_pool_strdup(const char *filename, int line, ACL_SLICE_POOL *asp, const char *str)
unsigned int slice_flag
Definition: acl_slice.h:105