?? memory_management.cpp
字號:
#include "vxworks.h"
#include "stdio.h"
#include "semlib.h"
#include "tasklib.h"
#include "msgQLib.h"
#include "logLib.h"
/* 參數(shù)定義 */
#define MAX_BUF_NUM 256 /* 內(nèi)存池消息隊列最大內(nèi)存?zhèn)€數(shù)*/
#define STACK_SIZE 20000 /* 任務堆棧大小 */
#define MAX_LOG_MSGS 100 /* 最大logMsg個數(shù) */
#define MAX_ALLOC_NUM 50 /* 一次申請最多*/
/* 全局變量的定義 */
unsigned int alloc_num_Buffer_16, alloc_num_Buffer_256; /* 預先設置16和256的緩沖池大小 */
char *pbuf16_store[256]; /* 用來存放已經(jīng)申請的16內(nèi)存塊地址 */
int buf_16_num = 0;
char *pbuf256_store[256]; /* 用來存放已經(jīng)申請的256內(nèi)存塊地址 */
int buf_256_num = 0;
char *pbufsys_store[256]; /* 用來存放已經(jīng)申請的系統(tǒng)內(nèi)存塊地址 */
int buf_sys_num = 0;
/* 消息隊列ID */
MSG_Q_ID msgQId_pool16; /* 存放靜態(tài)16內(nèi)存池塊地址的消息隊列 */
MSG_Q_ID msgQId_pool256; /* 存放靜態(tài)256內(nèi)存池塊地址的消息隊列 */
/* 消息結(jié)構(gòu) */
typedef struct _PBUFFER
{
char *buffer;
}PBUFFER;
/* 內(nèi)存分區(qū)ID */
char *pTotalPool;
PART_ID pPart16;
PART_ID pPart256;
/* LogMsg */
#ifdef INCLUDE_LOGGING
logInit(consolefd, MAX_LOG_MSGS);
#endif
/* 任務標識 */
int tidTask1;
/* 互斥信號量標識 */
SEM_ID semId = NULL; /* 保證臨界資源的安全 */
/* 函數(shù)聲名 */
STATUS tTask1(); /* 任務1 */
STATUS Total_MemPool_Initial(); /* 初始化靜態(tài)內(nèi)存池函數(shù) */
STATUS bufGet(unsigned int &alloc_buf_size, unsigned int &alloc_num); /* 申請內(nèi)存塊函數(shù) */
STATUS bufReturn(unsigned int &buffer_type,unsigned int &free_num); /* 釋放內(nèi)存塊函數(shù) */
STATUS bufStatus(); /* 查看內(nèi)存使用情況函數(shù) */
STATUS exit(); /* 系統(tǒng)退出函數(shù) */
/****************************************************************************/
/************************** 程序入口函數(shù) progStart **************************/
/****************************************************************************/
STATUS progStart(void)
{
/* 創(chuàng)建互斥信號量 */
semId = semBCreate(SEM_Q_FIFO,SEM_FULL);
/* 創(chuàng)建內(nèi)存池消息隊列 */
msgQId_pool16 = msgQCreate(MAX_BUF_NUM, sizeof(PBUFFER), MSG_Q_FIFO); /* 16內(nèi)存緩沖池 */
if(msgQId_pool16 == NULL)
return (ERROR);
msgQId_pool256 = msgQCreate(MAX_BUF_NUM, sizeof(PBUFFER), MSG_Q_FIFO); /* 256內(nèi)存緩沖池 */
if(msgQId_pool256 == NULL)
return (ERROR);
/* 創(chuàng)建進程 */
tidTask1 = taskSpawn("tTask1", 200, 0, STACK_SIZE, (FUNCPTR)tTask1, /*創(chuàng)建Task1進程 優(yōu)先級220*/
0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return (OK);
}
/****************************************************************************/
/**************************** 系統(tǒng)主進程 tTask1 *****************************/
/****************************************************************************/
STATUS tTask1()
{
int option; /* 用戶選擇操作 */
/* 調(diào)用初始化靜態(tài)內(nèi)存池函數(shù) */
if(Total_MemPool_Initial() == ERROR)
{
printf("\nERROR: Memory pool failed to initialize.\n");
return (ERROR);
}
FOREVER
{
/* 等待用戶選擇操作 */
/* 操作1: Alloc Buffers -- 申請若干個自定義大小的內(nèi)存塊 */
/* 操作2: Free Buffers -- 釋放若干個自定義類型的內(nèi)存塊 */
/* 操作3: Status -- 查看當前內(nèi)存池使用狀態(tài) */
/* 操作4: Exit -- 釋放內(nèi)存池并退出系統(tǒng) */
printf("\nPlease choose your option: (1~4)\n1.\tAlloc Buffers\n2.\tFree buffers\n3.\tStatus\n4.\tExit\n");
scanf("%d",&option);
/* 判斷用戶輸入 */
switch (option) {
/* 如果選擇1 */
case 1:
/* 用戶輸入需要申請的內(nèi)存塊大小和個數(shù) */
unsigned int alloc_buf_size; /* 需要申請內(nèi)存塊大小 */
unsigned int alloc_num; /* 需要申請內(nèi)存塊個數(shù) */
printf("Please Input the SIZE of the buffer to alloc:\n");
scanf("%u",&alloc_buf_size);
printf("Please Input the AMOUNT of buffers to alloc:\n");
scanf("%u",&alloc_num);
/* 調(diào)用bufGet()即申請內(nèi)存塊函數(shù) */
if(bufGet(alloc_buf_size,alloc_num) == ERROR)
{
printf("\nERROR: Memory allocation Fail.\n");
return (ERROR);
}
break;
/* 如果選擇2 */
case 2:
/* 用戶輸入需要釋放的內(nèi)存塊類型和個數(shù) */
unsigned int buffer_type; /* 釋放類型 */
unsigned int free_num; /* 釋放個數(shù) */
printf("Please Input the TYPE of buffer to free:\n 1. 16_buffer\t2. 256_buffer\t3. system buffer\n");
scanf("%u",&buffer_type);
/* 如果是釋放系統(tǒng)內(nèi)存則默認全部刪除 */
if(buffer_type == 1 || buffer_type == 2)
{
printf("Please Input the AMOUNT of buffers to free:\n");
scanf("%u",&free_num);
}
/* 調(diào)用bufReturn()即內(nèi)存釋放函數(shù) */
if(bufReturn(buffer_type, free_num) == ERROR)
{
printf("\nERROR: Memory Free Fail.\n");
return (ERROR);
}
break;
/* 如果選擇3 */
case 3:
/* 調(diào)用bufStatus()即監(jiān)視函數(shù) */
if(bufStatus()== ERROR)
{
printf("\nERROR: Status Check fail.\n");
return (ERROR);
}
break;
/* 如果選擇4 */
case 4:
/* 調(diào)用系統(tǒng)退出函數(shù) */
if(exit() == ERROR)
{
return (ERROR);
}
break;
/* 其它 */
default:
printf("Wrong Input, Try again.\n");
break;
}
}
return (OK);
}
/****************************************************************************/
/******************** 功能函數(shù) Total_MemPool_Initial() **********************/
/****************************************************************************/
/* 實現(xiàn)功能: 根據(jù)用戶配置的大小初始化兩個不同類型的內(nèi)存池, */
/* 分別為大小為16和大小為256的內(nèi)存池,保存內(nèi)存池中各內(nèi)存 */
/* 塊的首地址,并發(fā)送到已經(jīng)創(chuàng)建的兩個內(nèi)存池消息隊列中. */
STATUS Total_MemPool_Initial()
{
/* 初始化參數(shù)和消息發(fā)送對象 */
int Total_MemPool_Size;
PBUFFER pBuff16_sd;
PBUFFER pBuff256_sd;
/* 提示用戶輸入兩種類型內(nèi)存池的大小 */
printf("\n****************************Initialization*****************************\n");
printf("Please Input the amount of 16 buffers.(3~256)\n");
scanf("%d",&alloc_num_Buffer_16);
printf("Please Input the amount of 256 buffers.(3~256)\n");
scanf("%d",&alloc_num_Buffer_256);
semTake(semId, WAIT_FOREVER); /* 互斥信號量 */
/* 初始化內(nèi)存池 */
if(alloc_num_Buffer_16 >= 3 && alloc_num_Buffer_256 >= 3
&& alloc_num_Buffer_16 <= MAX_BUF_NUM
&& alloc_num_Buffer_256 <= MAX_BUF_NUM) /* 判斷輸入大小,合法為3~MAX_BUF_NUM個 */
{
/* 申請總內(nèi)存池pTotalPool */
Total_MemPool_Size = (16+8) * alloc_num_Buffer_16 + (256+8) * alloc_num_Buffer_256 + 10000;
pTotalPool = (char *)malloc(Total_MemPool_Size);
/* 在總內(nèi)存池當中創(chuàng)建兩個分區(qū) */
pPart16 = memPartCreate((char*)pTotalPool, (16+8) * alloc_num_Buffer_16);
pPart256 = memPartCreate((char*)pTotalPool, (256+8) * alloc_num_Buffer_256 + 10000);
/* 初始化塊大小為16的內(nèi)存池 */
/* 在分區(qū)中分配內(nèi)存塊,并輸出地址 */
printf("\n16_Buffer address:\n");
for(int i = 0; i < alloc_num_Buffer_16; ++i)
{
pBuff16_sd.buffer = (char*)memPartAlloc(pPart16, 16);
printf("0x%07x\t",pBuff16_sd.buffer);
if((i + 1) % 5 == 0)
printf("\n");
/* 將申請到的內(nèi)存塊地址發(fā)送到對應的內(nèi)存池消息對列中 */
if(msgQSend(msgQId_pool16,(char*)&pBuff16_sd, sizeof(PBUFFER), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
{
printf("Buffer 16 allocation failed.\n");
return (ERROR);
}
}
/* 初始化塊大小為256的內(nèi)存池 */
/* 同上 */
printf("\n256_Buffer address:\n");
for(int j = 0; j < alloc_num_Buffer_256; ++j)
{
pBuff256_sd.buffer = (char*)memPartAlloc(pPart256, 256);
printf("0x%07x\t",pBuff256_sd.buffer);
if((j + 1) % 5 == 0)
printf("\n");
if(msgQSend(msgQId_pool256, (char*)&pBuff256_sd, sizeof(PBUFFER), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
{
printf("Buffer 256 allocation failed.\n");
return (ERROR);
}
}
printf("\n*************************************************\nYou have successfully allocated Total Memory Pool\nwith %d 16_buffers and %d 256_buffers.\nNow Program Starting......\n*************************************************\n", alloc_num_Buffer_16, alloc_num_Buffer_256);
} else {
printf("Total Memory Pool allocation failed.\nPlease input the valid amount of buffers.\n");
return (ERROR);
}
semGive(semId); /* 互斥信號量 */
return (OK);
}
/****************************************************************************/
/**************************** 功能函數(shù) bufGet() *****************************/
/****************************************************************************/
/* 實現(xiàn)功能: 按照用戶需求分配若干個自定義大小內(nèi)存塊, */
/* 并輸出分配到的內(nèi)存塊的地址.內(nèi)存的分配實現(xiàn)統(tǒng)一管理 */
STATUS bufGet(unsigned int &alloc_buf_size, unsigned int &alloc_num)
{
/* 初始化接收消息對象 */
PBUFFER pBuff16_rc;
PBUFFER pBuff256_rc;
/* 內(nèi)存申請 */
/* 1.如果申請內(nèi)存塊大小小于16 */
if(alloc_buf_size > 0 && alloc_buf_size <= 16)
{
/* 如果需要分配的內(nèi)存塊個數(shù)小于當前16內(nèi)存池中未分配內(nèi)存塊的個數(shù) */
/* 則直接在16內(nèi)存池中申請 */
if(alloc_num > 0 && alloc_num <= msgQNumMsgs(msgQId_pool16))
{
for(int i = 0; i < alloc_num; ++i)
{
/* 從消息隊列中接收內(nèi)存塊地址 */
if(msgQReceive(msgQId_pool16, (char*)&pBuff16_rc, sizeof(PBUFFER), WAIT_FOREVER)==ERROR)
{
return(ERROR);
}
/* 將已分配地址保存并輸出 */
pbuf16_store[buf_16_num] = pBuff16_rc.buffer;
printf("0x%07x\t",pbuf16_store[buf_16_num]);
++buf_16_num;
if((i + 1) % 5 == 0)
printf("\n");
}
/* 內(nèi)存不足告警提示信息 */
if(msgQNumMsgs(msgQId_pool16) <= 3)
{
printf("\nCAUTION:You got %d 16_buffer left.\n",msgQNumMsgs(msgQId_pool16));
}
/* 如果需要分配的內(nèi)存塊個數(shù)大于當前16內(nèi)存池未分配內(nèi)存塊的個數(shù), */
/* 但小于16內(nèi)存池和256內(nèi)存池中未分配的內(nèi)存總數(shù)之和,則先在16內(nèi)存 */
/* 池中申請完所有的內(nèi)存塊,再在256內(nèi)存池中申請剩下的內(nèi)存塊 */
} else if (alloc_num > msgQNumMsgs(msgQId_pool16)
&& alloc_num <= msgQNumMsgs(msgQId_pool16) + msgQNumMsgs(msgQId_pool256)) {
int b16_num = msgQNumMsgs(msgQId_pool16);
int b256_num = alloc_num - msgQNumMsgs(msgQId_pool16);
printf("\nCAUTION:No 16_buffer left. So 256_buffer will be used.\n");
printf("16_buffer address:\n");
/* 先在16內(nèi)存池中申請 */
for(int i = 0; i < b16_num; ++i)
{
/* 從消息隊列中接收內(nèi)存塊地址 */
if(msgQReceive(msgQId_pool16,(char*)&pBuff16_rc,sizeof(PBUFFER),WAIT_FOREVER)==ERROR)
{
return(ERROR);
}
/* 將已分配地址保存并輸出 */
pbuf16_store[buf_16_num] = pBuff16_rc.buffer;
printf("0x%07x\t",pbuf16_store[buf_16_num]);
++buf_16_num;
if((i + 1) % 5 == 0)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -