XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
sma.c
浏览该文件的文档.
1
13#include <xwos/standard.h>
14#include <xwos/lib/xwbop.h>
15#include <xwos/lib/xwaop.h>
16#include <xwos/mm/common.h>
17#include <xwos/mm/sma.h>
18
21 xwptr_t origin, xwsz_t size, xwsq_t pos,
22 const char * name)
23{
24 xwer_t rc;
25
26 // cppcheck-suppress [misra-c2012-17.8]
28 if (size < pos) {
29 rc = -EINVAL;
30 } else {
31 rc = XWOK;
32 sa->zone.origin = origin;
33 sa->zone.size = size;
34 sa->pos = pos;
35 sa->name = name;
36 }
37 return rc;
38}
39
41xwer_t xwmm_sma_alloc(struct xwmm_sma * sa, xwsz_t size, xwsz_t aligned,
42 void ** membuf)
43{
44 xwsq_t cpos;
45 xwsq_t npos;
46 xwsq_t tmp;
47 xwer_t rc;
48
49 XWOS_VALIDATE((sa), "nullptr", -EFAULT);
50 XWOS_VALIDATE((membuf), "nullptr", -EFAULT);
51
52 if ((xwsz_t)0 == size) {
53 rc = -EINVAL;
54 *membuf = NULL;
55 goto err_inval;
56 }
57
58 if ((xwsz_t)0 != (aligned & XWMM_UNALIGNED_MASK)) {
59 rc = -EINVAL;
60 *membuf = NULL;
61 goto err_inval;
62 }
63
64 if ((xwsz_t)0 == aligned) {
65 aligned = XWMM_ALIGNMENT; // cppcheck-suppress [misra-c2012-17.8]
66 }
67
68 // cppcheck-suppress [misra-c2012-17.8]
69 size = XWBOP_ALIGN(size, (xwsz_t)XWMM_ALIGNMENT);
70 do {
72 tmp = XWBOP_ALIGN((sa->zone.origin + cpos), aligned);
73 npos = size + tmp - sa->zone.origin;
74 if (npos > sa->zone.size) {
75 rc = -ENOMEM;
76 *membuf = NULL;
77 goto err_nomem;
78 }
79 *membuf = (void *)tmp;
80 } while (XWOK != xwaop_teq_then_write(xwsq_t, &sa->pos, cpos, npos, NULL));
81 return XWOK;
82
83err_nomem:
84err_inval:
85 return rc;
86}
87
89xwer_t xwmm_sma_free(struct xwmm_sma * sa, void * mem)
90{
91 XWOS_UNUSED(sa);
92 XWOS_UNUSED(mem);
93 return XWOK;
94}
#define XWMM_ALIGNMENT
Definition common.h:29
#define XWMM_UNALIGNED_MASK
Definition common.h:30
xwer_t xwmm_sma_alloc(struct xwmm_sma *sa, xwsz_t size, xwsz_t aligned, void **membuf)
XWMM API:从简单内存分配器中申请内存
Definition sma.c:41
xwer_t xwmm_sma_init(struct xwmm_sma *sa, xwptr_t origin, xwsz_t size, xwsq_t pos, const char *name)
XWMM API:初始化简单内存分配器对象
Definition sma.c:20
xwer_t xwmm_sma_free(struct xwmm_sma *sa, void *mem)
XWMM API:释放内存
Definition sma.c:89
#define __xwos_api
Definition compiler.h:175
#define EINVAL
Invalid argument
Definition errno.h:52
#define EFAULT
Bad address
Definition errno.h:44
#define ENOMEM
Not enough space
Definition errno.h:42
#define XWOK
No error
Definition errno.h:182
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
unsigned long xwsz_t
Definition type.h:339
unsigned long xwsq_t
Definition type.h:445
unsigned long xwptr_t
Definition type.h:375
@ xwaop_mo_acquire
Definition type.h:630
#define xwaop_teq_then_write(type, a, t, v, ov)
对原子变量进行原子操作:读取-测试“相等”-写
Definition xwaop.h:113
#define xwaop_load(type, a, memorder)
对原子变量进行原子操作:加载
Definition xwaop.h:45
#define XWBOP_ALIGN(x, n)
Definition xwbop.h:37
#define XWOS_VALIDATE(exp, errstr,...)
检查函数参数是否有效
Definition standard.h:76
#define XWOS_UNUSED(x)
Definition standard.h:66
XWOS内存管理:简单内存分配器
简单内存分配器
Definition sma.h:36
const char * name
Definition sma.h:38
struct xwmm_zone zone
Definition sma.h:37
atomic_xwsq_t pos
Definition sma.h:39
xwptr_t origin
Definition common.h:41
xwsz_t size
Definition common.h:42
XWOS通用库:原子操作
XWOS通用库:位操作
XWOS内存管理:通用定义
XWOS的标准头文件