嵌入式技术
常见错误与预防
1 分配后忘记释放内存
void func(void) { p = malloc(len); do_something(p); return; /*错误!退出程序时没有释放内存*/ }预防:编写代码时malloc()和free()保证成对出现,避免忘记资源回收。
int func(void) { p = malloc(len); if (condition) return -1; /*错误!退出程序时没有释放内存*/ free(p); return 0; }预防:一旦使用动态内存分配,请仔细检查程序的退出分支是否已经释放该动态内存。
void func(void) { p = malloc(len); val = *p++; /*错误!动态内存句柄不可移动*/ free(p); }预防:千万不要修改动态内存句柄!可以另外赋值给其他指针变量,再对该动态内存进行访问操作。
void func(void) { len = strlen(str); p = malloc(len); strcpy(p, str); /*错误!str的’’写到动态内存外*/ }预防:分配内存前仔细思考长度是否足够,千万注意字符串拷贝占用内存比字符串长度大1。
/* Log of dynamic memory usage */ typedef struct _dmem_log { struct _dmem_log *p_stNext; /* Point to next log */ const void *p_vDMem; /* Point to allocated memory by this pointer */ INT32S iSize; /* Size of the allocated memory */ } DMEM_LOG;然后为该结构体开辟内存:
static DMEM_LOG *s_pstFreeLog; /* Point to free log pool by this pointer */ static INT8U s_byNumUsedLog; static DMEM_LOG *s_pstHeadLog; /* Point to used log chain by this pointer */ /* Pool of dynamic memory log */ #define NUM_DMEM_LOG 20 static DMEM_LOG s_astDMemLog[NUM_DMEM_LOG];下面是内存日志块的操作函数:初始化、插入日志和移除日志:
/********************************************************** * Initialize DMem Log * Description : Initialize log of dynamic memory * Arguments : void * Returns : void * Notes : **********************************************************/ static void InitDMemLog(void) { INT16S nCnt; /* Initialize pool of log */ for (nCnt = 0; nCnt < NUM_DMEM_LOG; ++nCnt) { /* Point to next one */ s_astDMemLog[nCnt].p_stNext = &s_astDMemLog[nCnt + 1]; } s_astDMemLog[NUM_DMEM_LOG - 1].p_stNext = NULL; s_pstFreeLog = &s_astDMemLog[0]; /* Point to the 1th log */ return; } /********************************************************** * Log DMem * Description : Join an allocated memory into log pool * Arguments : const void *p_vAddr point to address of this allocated memory by this pointer * INT32S iSize size of this allocated memory * Returns : void * Notes : **********************************************************/ static void LogDMem(const void *p_vAddr, INT32S iSize) { ASSERT(p_vAddr && iSize > 0); DMEM_LOG *p_stLog; #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr; #endif /* Get a log from free pool */ OS_ENTER_CRITICAL(); /* Avoid race condition on s_pstFreeLog */ if (!s_pstFreeLog) { OS_EXIT_CRITICAL(); PRINTF("Allocate DMemLog failed. "); return; } p_stLog = s_pstFreeLog; s_pstFreeLog = s_pstFreeLog->p_stNext; OS_EXIT_CRITICAL(); /* Don't need to protect this log that is free one currently */ p_stLog->p_vDMem = p_vAddr; p_stLog->iSize = iSize; /* Put this log into used chain */ OS_ENTER_CRITICAL(); /* Avoid race condition */ p_stLog->p_stNext = s_pstHeadLog; s_pstHeadLog = p_stLog; ++s_byNumUsedLog; OS_EXIT_CRITICAL(); return; } /********************************************************** * Unlog DMem * Description : Remove an allocated memory from log pool * Arguments : const void *p_vAddr point to address of this allocated memory by this pointer * Returns : void * Notes : **********************************************************/ static void UnlogDMem(const void *p_vAddr) { ASSERT(p_vAddr); DMEM_LOG *p_stLog, *p_stPrev; #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr; #endif /* Search the log */ OS_ENTER_CRITICAL(); /*Avoid race condition */ p_stLog = p_stPrev = s_pstHeadLog; while (p_stLog) { if (p_vAddr == p_stLog->p_vDMem) { break; /* Have found */ } p_stPrev = p_stLog; p_stLog = p_stLog->p_stNext; /* Move to next one */ } if (!p_stLog) { OS_EXIT_CRITICAL(); PRINTF("Search Log failed. "); return; } /* Remove from used pool */ if (p_stLog == s_pstHeadLog) { s_pstHeadLog = s_pstHeadLog->p_stNext; } else { p_stPrev->p_stNext = p_stLog->p_stNext; } --s_byNumUsedLog; OS_EXIT_CRITICAL(); /* Don't need to protect this log that is free one currently */ p_stLog->p_vDMem = NULL; p_stLog->iSize = 0; /* Add into free pool */ OS_ENTER_CRITICAL(); /* Avoid race condition */ p_stLog->p_stNext = s_pstFreeLog; s_pstFreeLog = p_stLog; OS_EXIT_CRITICAL(); return; }带日志记录功能的内存分配MallocExt()和内存释放FreeExt()函数:
/********************************************************* * Malloc Extension * Description : Malloc a block of memory and log it if need * Arguments : INT32S iSize size of desired allocate memory * Returns: void *NULL= failed, otherwise=pointer of allocated memory * Notes : **********************************************************/ void *MallocExt(INT32S iSize) { ASSERT(iSize > 0); void *p_vAddr; p_vAddr = malloc(iSize); if (!p_vAddr) { PRINTF("malloc failed at %s line %d. ", __FILE__, __LINE__); } else { #if (DMEM_DBG && DBG_VER) memset(p_vAddr, 0xA3, iSize); /* Fill gargage for debug */ LogDMem(p_vAddr, iSize); /* Log memory for debug */ #endif } return p_vAddr; } /********************************************************** * Free Extension * Description : Free a block of memory and unlog it if need * Arguments : void * p_vMem point to the memory by this pointer * Returns : void * Notes : **********************************************************/ void FreeExt(void *p_vMem) { ASSERT(p_vMem); free(p_vMem); #if (DMEM_DBG && DBG_VER) UnlogDMem(p_vMem); /* Remove memory from log */ #endif return; }编辑:黄飞
全部0条评论
快来发表一下你的评论吧 !