XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
memslice.c
浏览该文件的文档.
1
13#include <xwos/standard.h>
14#include <xwos/lib/xwbop.h>
15#include <xwos/lib/xwaop.h>
16#include <xwos/lib/lfq.h>
17#include <xwos/mm/common.h>
18#include <xwos/mm/memslice.h>
19
22 xwsz_t total_size, xwsz_t card_size,
23 const char * name, ctor_f ctor, dtor_f dtor)
24{
25 xwsz_t num_max;
26 xwsz_t n;
27 xwsz_t nm;
28 xwptr_t curr;
29 xwptr_t next;
30 xwer_t rc;
31
32 XWOS_VALIDATE((msa), "nullptr", -EFAULT);
33
34 // cppcheck-suppress [misra-c2012-17.8]
35 card_size = XWBOP_ALIGN(card_size, (xwsz_t)XWMM_ALIGNMENT);
36 num_max = total_size / card_size;
37 if ((xwsz_t)0 == num_max) {
38 rc = -E2SMALL;
39 goto err_mem2small;
40 }
41
42 msa->zone.origin = origin;
43 msa->zone.size = total_size;
44 msa->name = name;
45 msa->card_size = card_size;
46 msa->num_max = num_max;
47 msa->num_free = num_max;
48 msa->ctor = ctor;
49 msa->dtor = dtor;
50
51 /* 构造所有对象 */
52 if (NULL != ctor) {
53 curr = origin;
54 next = curr;
55 for (n = 0; n < num_max; n++) {
56 next += card_size;
57 ctor((void *)curr);
58 curr = next;
59 }
60 msa->backup = *((xwptr_t *)origin);
61 } else {
62 msa->backup = 0;
63 }
64
66 curr = origin;
67 next = origin;
68 nm = num_max;
69 for (n = 0; n < nm; n++) {
70 next += card_size;
73 curr = next;
74 }
75
76 return XWOK;
77
78err_mem2small:
79 return rc;
80}
81
83xwer_t xwmm_memslice_alloc(struct xwmm_memslice * msa, void ** membuf)
84{
85 xwlfq_t * card;
86 xwer_t rc;
87
88 XWOS_VALIDATE((msa), "nullptr", -EFAULT);
89 XWOS_VALIDATE((membuf), "nullptr", -EFAULT);
90
91 card = xwlib_lfq_pop(&msa->free_list);
92 if (NULL == card) {
93 rc = -ENOMEM;
94 *membuf = NULL;
95 goto err_lfq_pop;
96 }
97 xwaop_sub(xwsz_t, &msa->num_free, 1, NULL, NULL);
98 *(xwptr_t *)card = msa->backup; /* restore original data */
99 *membuf = card;
100 return XWOK;
101
102err_lfq_pop:
103 return rc;
104}
105
107xwer_t xwmm_memslice_free(struct xwmm_memslice * msa, void * mem)
108{
109 xwer_t rc;
110 atomic_xwlfq_t * card;
111
112 XWOS_VALIDATE((msa), "nullptr", -EFAULT);
113 XWOS_VALIDATE((mem), "nullptr", -EFAULT);
114
115 if (((xwptr_t)mem < msa->zone.origin) ||
116 (((xwptr_t)mem - msa->zone.origin) > msa->zone.size)) {
117 rc = -EOWNER;
118 } else {
119 if (NULL != msa->dtor) {
120 msa->dtor(mem);
121 }
122 card = (atomic_xwlfq_t *)mem;
123 xwlib_lfq_push(&msa->free_list, card);
124 xwaop_add(xwsz_t, &msa->num_free, 1, NULL, NULL);
125 rc = XWOK;
126 }
127
128 return rc;
129}
#define XWMM_ALIGNMENT
Definition common.h:29
xwer_t xwmm_memslice_free(struct xwmm_memslice *msa, void *mem)
XWMM API:释放内存切片
Definition memslice.c:107
xwer_t xwmm_memslice_init(struct xwmm_memslice *msa, xwptr_t origin, xwsz_t total_size, xwsz_t card_size, const char *name, ctor_f ctor, dtor_f dtor)
XWMM API:静态方式初始化内存切片分配器
Definition memslice.c:21
xwer_t xwmm_memslice_alloc(struct xwmm_memslice *msa, void **membuf)
XWMM API:申请内存切片
Definition memslice.c:83
#define __xwos_api
Definition compiler.h:175
#define EFAULT
Bad address
Definition errno.h:44
#define E2SMALL
Too small
Definition errno.h:200
#define ENOMEM
Not enough space
Definition errno.h:42
#define XWOK
No error
Definition errno.h:182
#define EOWNER
Owner error
Definition errno.h:202
static void xwlib_lfq_push(atomic_xwlfq_t *h, atomic_xwlfq_t *n)
将一个节点推入无锁队列(栈式队列)
Definition lfq.h:42
static void xwlib_lfq_init(atomic_xwlfq_t *n)
初始化无锁队列节点
Definition lfq.h:31
static xwlfq_t * xwlib_lfq_pop(atomic_xwlfq_t *h)
弹出无锁队列中的第一个节点(栈式队列)
Definition lfq.h:53
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
__xwcc_atomic xwlfq_t atomic_xwlfq_t
Definition type.h:594
unsigned long xwsz_t
Definition type.h:339
xwptr_t xwlfq_t
Definition type.h:591
unsigned long xwptr_t
Definition type.h:375
void(* ctor_f)(void *)
Definition type.h:604
void(* dtor_f)(void *)
Definition type.h:605
#define xwaop_add(type, a, v, nv, ov)
对原子变量进行原子操作:读取-相加-回写
Definition xwaop.h:330
#define xwaop_sub(type, a, v, nv, ov)
对原子变量进行原子操作:读取-相减-回写
Definition xwaop.h:598
#define XWBOP_ALIGN(x, n)
Definition xwbop.h:37
#define XWOS_VALIDATE(exp, errstr,...)
检查函数参数是否有效
Definition standard.h:76
XWOS通用库:无锁队列
XWOS内存管理:内存切片分配器
内存切片分配器
Definition memslice.h:44
dtor_f dtor
Definition memslice.h:58
xwsz_t card_size
Definition memslice.h:47
atomic_xwsz_t num_free
Definition memslice.h:48
ctor_f ctor
Definition memslice.h:57
atomic_xwsz_t num_max
Definition memslice.h:49
const char * name
Definition memslice.h:46
struct xwmm_zone zone
Definition memslice.h:45
atomic_xwlfq_t free_list
Definition memslice.h:50
xwptr_t backup
Definition memslice.h:51
xwptr_t origin
Definition common.h:41
xwsz_t size
Definition common.h:42
XWOS通用库:原子操作
XWOS通用库:位操作
XWOS内存管理:通用定义
XWOS的标准头文件