📄 ip51_dmc.c
字号:
/* 这是一个用于内存管理的软件 这个软件由IP51_DMC.C、IP51_DMDEFS.H、IP51_EXTR.H、IP51_DMD.C组成
/* 作者:王庆林 北京NEC */
/* DM - Dynamic Memory Management */
/* */
/* DESCRIPTION */
/* */
/* This file contains the core routines for the Dynamic Memory */
/* Management component. */
/* */
/* DATA STRUCTURES */
/* */
/* None */
/* */
/* FUNCTIONS */
/* */
/* IP51_DMC_Create_Memory_Pool Create a dynamic memory pool */
/* IP51_DMC_Delete_Memory_Pool Delete a dynamic memory pool */
/* IP51_DMC_Allocate_Memory Allocate a memory block from */
/* a dynamic memory pool */
/* IP51_DMC_Deallocate_Memory Deallocate a memory block */
/* from a dynamic memory pool */
/* terminate condition */
/* */
/* DEPENDENCIES */
/* */
/* cs_extr.h Common Service functions */
/* tc_extr.h Thread Control functions */
/* dm_extr.h Partition functions */
/* hi_extr.h History functions */
/* */
/* HISTORY */
/* */
/* DATE REMARKS */
/* */
/*************************************************************************/
#include "ip51_cs_extr.h" /* Common service functions */
#include "ip51_dm_extr.h" /* Dynamic memory functions */
/* Define external inner-component global data references. */
extern CS_NODE *DMD_Created_Pools_List;
extern unsigned int DMD_Total_Pools;
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* IP51_DMC_Create_Memory_Pool */
/* */
/* DESCRIPTION */
/* */
/* This function creates a dynamic memory pool and then places it */
/* on the list of created dynamic memory pools. */
/* */
/* CALLED BY */
/* */
/* */
/* CALLS */
/* */
/* CSC_Place_On_List Add node to linked-list */
/* */
/* INPUTS */
/* */
/* pool_ptr Memory pool control block */
/* pointer */
/* name Memory pool name */
/* start_address Starting address of the pool */
/* pool_size Number of bytes in the pool */
/* min_allocation Minimum allocation size */
/* */
/* OUTPUTS */
/* */
/* SUCCESS */
/* */
/* HISTORY */
/* */
/* DATE REMARKS */
/* */
/* */
/*************************************************************************/
STATUS IP51_DMC_Create_Memory_Pool(IP51_MEMORY_POOL *pool_ptr, char *name,
void *start_address, unsigned int pool_size,
unsigned int min_allocation)
{
DM_PCB *pool; //声明一个动态内存控制块指针
int i; /* Working index variable */
DM_HEADER *header_ptr; /* Partition block header ptr*/
pool = (DM_PCB *) pool_ptr;
//动态内存池的ID号清0
pool -> dm_id = 0;
//添写动态内存池的名称
for (i = 0; i < DM_MAX_NAME; i++)
pool -> dm_name[i] = name[i];
//存储内存池起始地址和大小以及最小分配单元
pool -> dm_start_address = start_address;
pool -> dm_pool_size = pool_size;
pool -> dm_min_allocation = min_allocation;
//初始化连接指针
pool -> dm_created.cs_previous = NULL;
pool -> dm_created.cs_next = NULL;
//建立一个单一的包含所有内存的块
header_ptr = (DM_HEADER *) start_address;
//初始化内存参数,包括可用的大小、搜索指针、内存列表指针
pool -> dm_available = pool_size - (2 * DM_OVERHEAD);
pool -> dm_memory_list = header_ptr;
pool -> dm_search_ptr = header_ptr;
//创建内存块头
header_ptr -> dm_memory_pool = pool;
header_ptr -> dm_memory_free = TRUE;
header_ptr -> dm_next_memory = (DM_HEADER *)
(((BYTE_PTR) header_ptr) + pool -> dm_available + DM_OVERHEAD);
header_ptr -> dm_previous_memory = header_ptr -> dm_next_memory;
//创建尾部的禁止块,注意:列表循环的
header_ptr = header_ptr -> dm_next_memory;
header_ptr -> dm_next_memory = (DM_HEADER *) start_address;
header_ptr -> dm_previous_memory = (DM_HEADER *) start_address;
header_ptr -> dm_memory_pool = pool;
header_ptr -> dm_memory_free = FALSE;
//至此,动态内存池已经建立完毕,ID可以设置了
pool -> dm_id = DM_DYNAMIC_ID;
//把内存池放入链表并增加动态内存池的数量
CSC_Place_On_List(&DMD_Created_Pools_List, &(pool -> dm_created));
DMD_Total_Pools++;
/* Return successful completion. */
return(SUCCESS);
}
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* IP51_DMC_Delete_Memory_Pool */
/* */
/* DESCRIPTION */
/* */
/* This function deletes a dynamic memory pool and removes it from */
/* the list of created memory pools. All tasks suspended on the */
/* memory pool are resumed with the appropriate error status. */
/* Note that this function does not free any memory associated with */
/* either the pool area or the pool control block. */
/* */
/* CALLED BY */
/* */
/* */
/* CALLS */
/* */
/* CSC_Remove_From_List Remove node from list */
/* */
/* INPUTS */
/* */
/* pool_ptr Memory pool control block */
/* pointer */
/* */
/* OUTPUTS */
/* */
/* SUCCESS */
/* */
/* HISTORY */
/* */
/* DATE REMARKS */
/* */
/* 02-09-2004 Created initial version 1.0 */
/* */
/*************************************************************************/
STATUS IP51_DMC_Delete_Memory_Pool(IP51_MEMORY_POOL *pool_ptr)
{
DM_PCB *pool; /* Pool control block ptr */
pool = (DM_PCB *) pool_ptr;
pool -> dm_id = 0;
//从动态内存池列表中删除这个指定节点
CSC_Remove_From_List(&DMD_Created_Pools_List, &(pool -> dm_created));
/* Decrement the total number of created memory pools. */
DMD_Total_Pools--;
/* Return a successful completion. */
return(SUCCESS);
}
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* IP51_DMC_Allocate_Memory */
/* */
/* DESCRIPTION */
/* */
/* This function allocates memory from the specified dynamic memory */
/* pool. If dynamic memory is currently available, this function */
/* is completed immediately. Otherwise, if there is not enough */
/* memory currently available, task suspension is possible. */
/* */
/* CALLED BY */
/* */
/* */
/* CALLS */
/* */
/* CSC_Place_On_List Place on suspend list */
/* */
/* INPUTS */
/* */
/* pool_ptr Memory pool pointer */
/* return_pointer Pointer to the destination */
/* memory pointer */
/* size Number of bytes requested */
/* suspend Suspension option if full */
/* */
/* OUTPUTS */
/* */
/* SUCCESS If service is successful */
/* IP51_NO_MEMORY Memory not available */
/* TIMEOUT If timeout on service */
/* IP51_POOL_DELETED If memory pool deleted */
/* during suspension */
/* */
/* HISTORY */
/* */
/* DATE REMARKS */
/* */
/* 02-09-2004 Created initial version 1.0 */
/* */
/*************************************************************************/
STATUS IP51_DMC_Allocate_Memory(IP51_MEMORY_POOL *pool_ptr, void **return_pointer,
unsigned int size)
{
DM_PCB *pool; /* Pool control block ptr */
DM_HEADER *memory_ptr; /* Pointer to memory */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -