XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
bma.c 文件参考

XWOS内存管理:伙伴算法内存块分配器 更多...

#include <xwos/standard.h>
#include <xwos/lib/xwlog.h>
#include <xwos/lib/bclst.h>
#include <xwos/lib/xwbop.h>
#include <xwos/osal/lock/spinlock.h>
#include <xwos/mm/common.h>
#include <xwos/mm/bma.h>
bma.c 的引用(Include)关系图:

浏览源代码.

宏定义

#define xwmm_bmalogf(lv, fmt, ...)
 

函数

static struct xwmm_bma_bcbxwmm_bma_mem_to_bcb (struct xwmm_bma *bma, void *mem)
 从内存块首地址获得其控制块的指针
 
static void * xwmm_bma_bcb_to_mem (struct xwmm_bma *bma, struct xwmm_bma_bcb *bcb)
 从内存块的控制块指针获得内存块的首地址
 
static struct xwmm_bma_bcbxwmm_bma_find_buddy (struct xwmm_bma *bma, struct xwmm_bma_bcb *bcb)
 寻找内存块的伙伴
 
static void xwmm_bma_orderlist_add (struct xwmm_bma *bma, struct xwmm_bma_orderlist *ol, xwu8_t odr, struct xwmm_bma_bcb *bcb)
 将一块内存加入到阶链表
 
static xwer_t xwmm_bma_orderlist_remove (struct xwmm_bma *bma, struct xwmm_bma_orderlist *ol, xwu8_t odr, struct xwmm_bma_bcb *bcb)
 从阶链表中删除一块内存
 
static struct xwmm_bma_bcbxwmm_bma_orderlist_choose (struct xwmm_bma *bma, struct xwmm_bma_orderlist *ol)
 从阶链表中选择一块内存,并返回其块控制块
 
static void xwmm_bma_divide_block (struct xwmm_bma *bma, struct xwmm_bma_bcb *bcb, xwsq_t target_odr, struct xwmm_bma_orderlist *curr_ol)
 将大内存块分割成小块
 
static void xwmm_bma_combine (struct xwmm_bma *bma, struct xwmm_bma_bcb *bcb)
 合并内存块
 
xwer_t xwmm_bma_init (struct xwmm_bma *bma, const char *name, xwptr_t origin, xwsz_t size, xwsz_t blksize, xwsz_t blkodr)
 XWMM API:初始化伙伴算法内存块分配器
 
xwer_t xwmm_bma_alloc (struct xwmm_bma *bma, xwsq_t order, void **membuf)
 XWMM API:申请一块连续的内存
 
xwer_t xwmm_bma_free (struct xwmm_bma *bma, void *mem)
 XWMM API:释放内存块
 

详细描述

XWOS内存管理:伙伴算法内存块分配器

作者

在文件 bma.c 中定义.

宏定义说明

◆ xwmm_bmalogf

#define xwmm_bmalogf (   lv,
  fmt,
  ... 
)

< 调试日志开关

在文件 bma.c25 行定义.

函数说明

◆ xwmm_bma_bcb_to_mem()

static void * xwmm_bma_bcb_to_mem ( struct xwmm_bma bma,
struct xwmm_bma_bcb bcb 
)
static

从内存块的控制块指针获得内存块的首地址

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]bcb内存块的控制块指针
返回
内存块的首地址

在文件 bma.c138 行定义.

139{
140 xwptr_t idx;
141 xwptr_t mem;
142
143 idx = ((xwptr_t)bcb - (xwptr_t)bma->bcbs) / sizeof(struct xwmm_bma_bcb);
144 mem = (idx * bma->blksize) + bma->zone.origin;
145 return (void *)mem;
146}
unsigned long xwptr_t
Definition type.h:375
块控制块
Definition bma.h:70
struct xwmm_zone zone
Definition bma.h:81
struct xwmm_bma_bcb * bcbs
Definition bma.h:87
xwsz_t blksize
Definition bma.h:83
xwptr_t origin
Definition common.h:41
这是这个函数的调用关系图:

◆ xwmm_bma_combine()

static void xwmm_bma_combine ( struct xwmm_bma bma,
struct xwmm_bma_bcb bcb 
)
static

合并内存块

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]bcb内存块的控制块的指针
返回
错误码

< 阶的掩码

< 块已被合并

< 块正在被使用

< 报告BUG

< 当条件x成立时报告BUG

< 块已被合并

< 块正在被使用

< 块正在被使用

< 块已被合并

< 块正在被使用

< 块正在被使用

在文件 bma.c358 行定义.

359{
360 struct xwmm_bma_bcb * buddy;
361 xwsq_t curr_odr;
362 xwsq_t target_odr;
363 xwer_t rc;
364
365 curr_odr = (bcb->order & XWMM_BMA_ORDER_MASK);
366 target_odr = curr_odr + (xwsq_t)1;
367 while (target_odr <= bma->blkodr) {
368 buddy = xwmm_bma_find_buddy(bma, bcb);
369 xwmm_bmalogf(DEBUG,
370 "[FREE][M] bcb(idx:0x%lX,odr:0x%X),"
371 "buddy(idx:0x%lX,odr:0x%X)\n",
372 (((xwptr_t)bcb - (xwptr_t)xwmm_bma->bcbs) /
373 sizeof(struct xwmm_bma_bcb)),
374 bcb->order,
375 (((xwptr_t)buddy - (xwptr_t)xwmm_bma->bcbs) /
376 sizeof(struct xwmm_bma_bcb)),
377 buddy->order);
380 &bma->orderlists[curr_odr],
381 curr_odr,
382 buddy);
383 if (rc < 0) {
384 break;
385 }
386 /* 此时 `buddy->order` 以及 `bcb->order` 都是本地数据。 */
387 if (buddy > bcb) {
389 bcb->order = (xwu8_t)target_odr | XWMM_BMA_INUSED;
390 } else {
392 buddy->order = (xwu8_t)target_odr | XWMM_BMA_INUSED;
393 bcb = buddy;
394 }
395 curr_odr = target_odr;
396 target_odr++;
397 }
398 xwmm_bma_orderlist_add(bma, &bma->orderlists[curr_odr], curr_odr, bcb);
399}
static void xwmm_bma_orderlist_add(struct xwmm_bma *bma, struct xwmm_bma_orderlist *ol, xwu8_t odr, struct xwmm_bma_bcb *bcb)
将一块内存加入到阶链表
Definition bma.c:175
#define xwmm_bmalogf(lv, fmt,...)
Definition bma.c:25
static struct xwmm_bma_bcb * xwmm_bma_find_buddy(struct xwmm_bma *bma, struct xwmm_bma_bcb *bcb)
寻找内存块的伙伴
Definition bma.c:155
static xwer_t xwmm_bma_orderlist_remove(struct xwmm_bma *bma, struct xwmm_bma_orderlist *ol, xwu8_t odr, struct xwmm_bma_bcb *bcb)
从阶链表中删除一块内存
Definition bma.c:209
#define XWMM_BMA_ORDER_MASK
Definition bma.h:46
#define XWMM_BMA_INUSED
Definition bma.h:47
#define XWMM_BMA_COMBINED
Definition bma.h:45
signed long xwer_t
Definition type.h:554
uint8_t xwu8_t
Definition type.h:194
unsigned long xwsq_t
Definition type.h:445
#define XWOS_BUG_ON(x)
Definition standard.h:57
xwu8_t order
Definition bma.h:71
伙伴算法内存块分配器
Definition bma.h:80
struct xwmm_bma_orderlist * orderlists
Definition bma.h:86
函数调用图:
这是这个函数的调用关系图:

◆ xwmm_bma_divide_block()

static void xwmm_bma_divide_block ( struct xwmm_bma bma,
struct xwmm_bma_bcb bcb,
xwsq_t  target_odr,
struct xwmm_bma_orderlist curr_ol 
)
static

将大内存块分割成小块

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]bcb大块的内存块的控制块的指针
[in]target_odr目标块数量的阶
[in]curr_ol当前被分割的大块所在的阶链表

< 阶的掩码

< 块已被合并

< 块正在被使用

< 报告BUG

< 当条件x成立时报告BUG

< 块正在被使用

< 块正在被使用

在文件 bma.c276 行定义.

278{
279 xwsq_t blk_odr;
280 xwsq_t blk_idx;
281 xwsq_t blk_ofs;
282 struct xwmm_bma_bcb * buddy;
283
284 blk_odr = (xwsq_t)bcb->order & XWMM_BMA_ORDER_MASK;
285 blk_idx = ((xwptr_t)bcb - (xwptr_t)bma->bcbs) / sizeof(struct xwmm_bma_bcb);
286
287 while (blk_odr > target_odr) {
288 curr_ol--;
289 blk_odr--;
290 blk_ofs = 1U << blk_odr;
291 buddy = &bma->bcbs[blk_idx + blk_ofs];
292 /* 加入阶链表之前, `buddy` 是本地数据。*/
293 XWOS_BUG_ON((XWMM_BMA_COMBINED | XWMM_BMA_INUSED) != buddy->order);
294 buddy->order = (xwu8_t)blk_odr | XWMM_BMA_INUSED;
295 xwmm_bma_orderlist_add(bma, curr_ol, blk_odr, buddy);
296 /* `bcb` 是本地数据。*/
297 bcb->order = (xwu8_t)blk_odr | XWMM_BMA_INUSED;
298 xwmm_bmalogf(DEBUG,
299 "[ALLOC][D] bcb(idx:0x%lX,odr:0x%X),"
300 "buddy(idx:0x%lX,odr:0x%X)\n",
301 (((xwptr_t)bcb - (xwptr_t)xwmm_bma->bcbs) /
302 sizeof(struct xwmm_bma_bcb)),
303 bcb->order,
304 (((xwptr_t)buddy - (xwptr_t)xwmm_bma->bcbs) /
305 sizeof(struct xwmm_bma_bcb)),
306 buddy->order);
307 }
308}
函数调用图:
这是这个函数的调用关系图:

◆ xwmm_bma_find_buddy()

static struct xwmm_bma_bcb * xwmm_bma_find_buddy ( struct xwmm_bma bma,
struct xwmm_bma_bcb bcb 
)
static

寻找内存块的伙伴

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]bcb内存块的控制块指针
返回
伙伴内存块的控制块指针

< 阶的掩码

在文件 bma.c155 行定义.

157{
158 xwsq_t seq;
159 xwsq_t odr;
160
161 seq = ((xwptr_t)bcb - (xwptr_t)bma->bcbs) / sizeof(struct xwmm_bma_bcb);
162 odr = (xwsq_t)bcb->order & XWMM_BMA_ORDER_MASK;
163 seq ^= (1U << odr);
164 return &bma->bcbs[seq];
165}
这是这个函数的调用关系图:

◆ xwmm_bma_mem_to_bcb()

static struct xwmm_bma_bcb * xwmm_bma_mem_to_bcb ( struct xwmm_bma bma,
void *  mem 
)
static

从内存块首地址获得其控制块的指针

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]mem内存块的首地址
返回
内存块的控制块指针或类型为指针的错误码

在文件 bma.c115 行定义.

116{
117 struct xwmm_bma_bcb * bcb;
118 xwptr_t ofs;
119 xwptr_t idx;
120
121 ofs = (xwptr_t)mem - bma->zone.origin;
122 idx = ofs / (xwptr_t)bma->blksize;
123 if (ofs == (idx * bma->blksize)) {
124 bcb = &bma->bcbs[idx];
125 } else {
126 bcb = err_ptr(-EINVAL);
127 }
128 return bcb;
129}
#define EINVAL
Invalid argument
Definition errno.h:52
static __xwcc_inline void *__xwcc_must_check err_ptr(xwer_t err)
将错误码转换为指针
Definition error.h:42
函数调用图:
这是这个函数的调用关系图:

◆ xwmm_bma_orderlist_add()

static void xwmm_bma_orderlist_add ( struct xwmm_bma bma,
struct xwmm_bma_orderlist ol,
xwu8_t  odr,
struct xwmm_bma_bcb bcb 
)
static

将一块内存加入到阶链表

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]ol阶链表的指针
[in]odr阶链表的阶
[in]bcb内存块的控制块指针

< 块正在被使用

< 阶的掩码

在文件 bma.c175 行定义.

179{
180 struct xwlib_bclst_node * n;
181
182 /* 即将加入的块不在阶链表中,是本地数据。*/
183 if ((XWMM_BMA_INUSED | odr) == bcb->order) {
184 n = xwmm_bma_bcb_to_mem(bma, bcb);
185 xwmm_bmalogf(DEBUG,
186 "[OL:0x%lX][+] bcb(idx:0x%lX,odr:0x%X),mem(0x%lX)\n",
187 (xwptr_t)ol,
188 (((xwptr_t)bcb - (xwptr_t)xwmm_bma->bcbs) /
189 sizeof(struct xwmm_bma_bcb)),
190 bcb->order, (xwptr_t)n);
192 xwlib_bclst_add_head(&ol->head, n);
193 /* 当块被加入到阶链表中,就变成共享数据。*/
195 }
196}
static void * xwmm_bma_bcb_to_mem(struct xwmm_bma *bma, struct xwmm_bma_bcb *bcb)
从内存块的控制块指针获得内存块的首地址
Definition bma.c:138
static void xwlib_bclst_init_node(struct xwlib_bclst_node *n)
初始化一个链表节点。
Definition bclst.h:240
static void xwlib_bclst_add_head(struct xwlib_bclst_node *head, struct xwlib_bclst_node *newn)
将一个节点加入链表头部(链表头的后面)
Definition bclst.h:345
双循环链表的节点
Definition bclst.h:27
函数调用图:
这是这个函数的调用关系图:

◆ xwmm_bma_orderlist_choose()

static struct xwmm_bma_bcb * xwmm_bma_orderlist_choose ( struct xwmm_bma bma,
struct xwmm_bma_orderlist ol 
)
static

从阶链表中选择一块内存,并返回其块控制块

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]ol阶链表的指针
返回
块控制块指针或类型为指针的错误码
返回值
-ENOENT阶链表为空

< 块正在被使用

在文件 bma.c244 行定义.

246{
247 struct xwlib_bclst_node * n;
248 struct xwmm_bma_bcb * bcb;
249
250 if (xwlib_bclst_tst_empty(&ol->head)) {
251 bcb = err_ptr(-ENOENT);
252 } else {
253 n = ol->head.next;
255 bcb = xwmm_bma_mem_to_bcb(bma, n);
256 xwmm_bmalogf(DEBUG,
257 "[OL:0x%lX][C] bcb(idx:0x%lX,odr:0x%X)\n",
258 (xwptr_t)ol,
259 (((xwptr_t)bcb - (xwptr_t)xwmm_bma->bcbs) /
260 sizeof(struct xwmm_bma_bcb)),
261 bcb->order);
262 /* 即将被选择的块在阶链表中,是共享数据。*/
263 bcb->order |= XWMM_BMA_INUSED;
264 }
265 return bcb;
266}
static struct xwmm_bma_bcb * xwmm_bma_mem_to_bcb(struct xwmm_bma *bma, void *mem)
从内存块首地址获得其控制块的指针
Definition bma.c:115
static bool xwlib_bclst_tst_empty(const struct xwlib_bclst_node *h)
测试链表是否为空。
Definition bclst.h:253
static void xwlib_bclst_del_init(struct xwlib_bclst_node *node)
删除一个节点,并重新初始化它
Definition bclst.h:391
#define ENOENT
No such file or directory
Definition errno.h:32
struct xwlib_bclst_node * next
Definition bclst.h:28
struct xwlib_bclst_node head
Definition bma.h:64
函数调用图:
这是这个函数的调用关系图:

◆ xwmm_bma_orderlist_remove()

static xwer_t xwmm_bma_orderlist_remove ( struct xwmm_bma bma,
struct xwmm_bma_orderlist ol,
xwu8_t  odr,
struct xwmm_bma_bcb bcb 
)
static

从阶链表中删除一块内存

参数
[in]bma伙伴算法内存块分配器对象的指针
[in]ol阶链表的指针
[in]odr阶链表的阶
[in]bcb内存块的控制块指针
返回
错误码
返回值
XWOK删除成功
-ESRCH阶链表中不存在此内存块

< 去除未使用变量的警告

< 块正在被使用

<No error

在文件 bma.c209 行定义.

213{
214 struct xwlib_bclst_node * n;
215 xwer_t rc;
216
217 XWOS_UNUSED(ol);
218 n = xwmm_bma_bcb_to_mem(bma, bcb);
219 xwmm_bmalogf(DEBUG,
220 "[OL:0x%lX][-] bcb(idx:0x%lX,odr:0x%X),mem(0x%lX)\n",
221 (xwptr_t)ol,
222 (((xwptr_t)bcb - (xwptr_t)xwmm_bma->bcbs) /
223 sizeof(struct xwmm_bma_bcb)),
224 bcb->order, (xwptr_t)n);
225 /* 即将被删除的块在阶链表中,是共享数据。*/
226 if (odr != bcb->order) {
227 rc = -ESRCH;
228 } else {
230 bcb->order |= XWMM_BMA_INUSED;
231 rc = XWOK;
232 }
233 return rc;
234}
#define ESRCH
No such process
Definition errno.h:33
#define XWOK
No error
Definition errno.h:182
#define XWOS_UNUSED(x)
Definition standard.h:66
函数调用图:
这是这个函数的调用关系图: