📄 ar_membox.c
字号:
/*----------------------------------------------------------------------------
* A R T X - K e r n e l
*----------------------------------------------------------------------------
* Name: AR_MEMBOX.C
* Purpose: Interface functions for fixed memory block management system
* Rev.: V2.00 / 19-oct-2005
*----------------------------------------------------------------------------
* This code is part of the ARTX-ARM kernel package of Keil Software.
* Copyright (c) 2004-2005 Keil Software. All rights reserved.
*---------------------------------------------------------------------------*/
#include "Kernel\ARTX_Config.h"
//-------------------------------------------------------------------------
//
// Initialize memory block system
//
// 1. create a memory structure
// box_mem+0 : U32 : pointer to first free memory block
// box_mem+4 : U32 : pointer to memory block end
// box_mem+8 : U32 : blk_size
//
// 2. link all free blocks using offsets
//-------------------------------------------------------------------------
struct bm {
void *free;
void *end;
U32 blk_size;
};
/*----------------------------------------------------------------------------
* Global Functions
*---------------------------------------------------------------------------*/
/*--------------------------- _init_box -------------------------------------*/
int _init_box (void *box_mem, U32 box_size, U32 blk_size) {
/* Initialize memory block system, returns 0 if OK, 1 if fails. */
void *end;
void *blk;
void *next;
/* Create memory structure. */
blk_size = (blk_size + 3) & ~3;
if (blk_size == 0) {
return 1;
}
if ((blk_size + sizeof (struct bm)) > box_size) {
return 1;
}
blk = ((U8 *) box_mem) + sizeof (struct bm);
((struct bm *) box_mem)->free = blk;
end = ((U8 *) box_mem) + box_size;
((struct bm *) box_mem)->end = end;
((struct bm *) box_mem)->blk_size = blk_size;
/* Link free blocks. */
end = ((U8 *) end) - blk_size;
while (1) {
next = ((U8 *) blk) + blk_size;
if (next > end) break;
*((void **)blk) = next;
blk = next;
}
/* end marker */
*((void **)blk) = 0;
return (0);
}
/*--------------------------- _alloc_box ------------------------------------*/
void *_alloc_box (void *box_mem) __swi (6) {
/* Allocate a memory block and return start address. */
void **free;
free = ((struct bm *) box_mem)->free;
if (free) {
((struct bm *) box_mem)->free = *free;
}
return (free);
}
/*--------------------------- _calloc_box -----------------------------------*/
void *_calloc_box (void *box_mem) {
/* Allocate a memory block, set it to 0, and return start address. */
void *free;
U32 *p;
U32 i;
free = _alloc_box (box_mem);
if (free) {
p = free;
for (i = ((struct bm *) box_mem)->blk_size; i; i -= 4) {
*p = 0;
p++;
}
}
return (free);
}
/*--------------------------- _free_box -------------------------------------*/
int _free_box (void *box_mem, void *box) __swi (7) {
/* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
if (box < box_mem) {
return (1);
}
if (box > ((struct bm *) box_mem)->end) {
return (1);
}
*((void **)box) = ((struct bm *) box_mem)->free;
((struct bm *) box_mem)->free = box;
return (0);
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -