📄 ssp_slab.c
字号:
g_ulStaticMemPageStart -= page_num; for(i = 0;i < page_num;i++) { g_pstPages[g_ulStaticMemPageStart + i].status = PAGE_MNG; g_pstPages[g_ulStaticMemPageStart + i].buf_id = U16_BUTT; g_pstPages[g_ulStaticMemPageStart + i].slabp = (SLAB_S *)NULL; } return (SLAB_S *)g_ulStaticMemStart;}AOS_INLINE U32 __mem_slab_s_release( SLAB_S *pSlab, U32 size ){ if(pSlab == NULL) { return AOS_FAIL; } if( (U32)pSlab < ((U32)g_pPageOffset+MEM_RESERVED_PAGE_NUM*MEM_PAGE_SIZE) ) { g_pSlabPool -= size; g_ulSlabPoolSize += size; } slab_dbg_info.is_release_slab = TRUE; return AOS_SUCC;}AOS_INLINE U32 __mem_obj_verify( VOID *obj_p, MEM_BUF_S **ppBuf, SLAB_S **ppSlab, U32 *pulObjNo ){ PAGE_S *pPage; MEM_BUF_S *pBuf; SLAB_S *pSlab; OBJ_CTRL_S *pObjCtrl; U8 *objp = (U8*)obj_p; U32 pageNo, buf_id, objNo; AOS_ASSERT( (NULL != ppBuf) && (NULL != ppSlab) && (NULL != pulObjNo) ); if((ppBuf == NULL) || (ppSlab == NULL) || (pulObjNo == NULL)) { return MEM_OBJ_INVALID; } if( (U32)objp < (U32)g_pPageOffset ) { aos_printf( MPE_MM, "MM->:obj address too small,objp=0x%x," "it should not be less than 0x%x",objp, g_pPageOffset ); return MEM_OBJ_INVALID; } pageNo = ((U32)objp-(U32)g_pPageOffset)>>MEM_PAGE_ORDER; if( pageNo >= g_ulPageNum ) { aos_printf( MPE_MM, "MM->:obj address too large,objp=0x%x",objp ); return MEM_OBJ_INVALID; } pPage = &g_pstPages[pageNo]; if( PAGE_FREE == pPage->status ) { aos_printf( MPE_MM, "MM->:page is free, objp=0x%x, page status=%d",objp, pPage->status ); return MEM_PAGE_FREE; } if( PAGE_SLAB != pPage->status ) { aos_printf( MPE_MM, "MM->:page isn't used for slab, objp=0x%x, page status=%d",objp, pPage->status ); return MEM_PAGE_FREE; } buf_id = pPage->buf_id; if( (buf_id >= CONFIG_MEM_BUF_NUM) || (FALSE == g_pstMemBuf[buf_id].status) ) { aos_printf( MPE_MM, "MM->:buf is free, objp=0x%x, buf_id=%d",objp, buf_id ); return MEM_BUF_INVALID; } pBuf = &g_pstMemBuf[buf_id]; pSlab = pPage->slabp; objp -= sizeof(U32); AOS_ASSERT( 0 == (((U32)(objp-pSlab->s_mem))%pBuf->objsize) ); objNo = ((U32)(objp-pSlab->s_mem))/pBuf->objsize; pObjCtrl = &pSlab->obj_ctrl[objNo]; if( 0 == pObjCtrl->ref ) { aos_printf( MPE_MM, "MM->:obj is free, objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", objp+sizeof(U32), buf_id, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); return MEM_OBJ_FREE; } if( RED_MAGIC1 != *(U32*)objp ) { aos_printf( MPE_MM, "MM->:write before start, objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", objp+sizeof(U32), buf_id, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); return MEM_OBJ_OVERWRITE; } if( RED_MAGIC1 != *(U32*)(objp+pBuf->objsize-sizeof(U32) ) ) { aos_printf( MPE_MM, "MM->:write past end,objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", objp+sizeof(U32), buf_id, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); return MEM_OBJ_OVERWRITE; } *ppBuf = pBuf; *ppSlab = pSlab; *pulObjNo = objNo; return AOS_SUCC;}U32 __mem_slab_check( MEM_BUF_S *pBuf, SLAB_S *pSlab ){ U32 index, obj_num; AOS_ASSERT( NULL!= pBuf && NULL != pSlab ); if((pBuf == NULL) || (pSlab == NULL)) { return FALSE; } if( (U32)pSlab < (U32)g_pPageOffset || (U32)pSlab > ((U32)g_pPageOffset + (g_ulPageNum<<MEM_PAGE_ORDER)) ) { aos_printf( MPE_MM, "MM->:slab check error, address invalid of slab 0x%x " "of buf %s at 0x%x", pSlab, pBuf->name, pBuf ); return FALSE; } index = pSlab->free; obj_num = 0; while( OBJ_CTRL_END != index ) { if( index >= pBuf->objnum ) { aos_printf( MPE_MM, "MM->:slab check error, free list error 1 of slab 0x%x " "of buf %s at 0x%x", pSlab, pBuf->name, pBuf ); return FALSE; } if( 0 != pSlab->obj_ctrl[index].ref ) { aos_printf( MPE_MM, "MM->:slab check error, allocated obj in free list of slab 0x%x " "of buf %s at 0x%x", pSlab, pBuf->name, pBuf ); return FALSE; } obj_num++; index = pSlab->obj_ctrl[index].next; if( obj_num > pBuf->objnum ) { aos_printf( MPE_MM, "MM->:slab check error, free list error 2 of slab 0x%x " "of buf %s at 0x%x", pSlab, pBuf->name, pBuf ); return FALSE; } } if( (obj_num + pSlab->inuse) > pBuf->objnum ) { aos_printf( MPE_MM, "MM->:slab check error, free list error 3 of slab 0x%x " "of buf %s at 0x%x", pSlab, pBuf->name, pBuf ); return FALSE; } else if ( (obj_num + pSlab->inuse) < pBuf->objnum ) { aos_printf( MPE_MM, "MM->:slab check error, obj lost of slab 0x%x " "of buf %s at 0x%x", pSlab, pBuf->name, pBuf ); return FALSE; } return TRUE;}void mem_check_all(S8* fl, int ln){ U32 i,j,cnt; SLAB_S *s_Tmp; U8 *objp; MEM_BUF_S *pBuf; OBJ_CTRL_S *pObjCtrl; cnt = 0; for( i = 0; i < CONFIG_MEM_BUF_NUM; i++ ) { pBuf = &g_pstMemBuf[i]; if(FALSE == pBuf->status) { continue; } for( s_Tmp = pBuf->s_list_head; s_Tmp != NULL; s_Tmp = s_Tmp->s_next ) { for(j = 0; j < pBuf->objnum; j++) { pObjCtrl= &s_Tmp->obj_ctrl[j]; objp = s_Tmp->s_mem + j*pBuf->objsize; cnt++; if( (cnt %1000) == 0 ) { clear_watch_dog(); } if( 0 == pObjCtrl->ref ) { if( RED_MAGIC2 != *(U32*)objp ) { aos_printf( MPE_MM, "\r\n mem_check_all: file:%s,line:%u: MM->:write before start, freed objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", fl, ln, objp+sizeof(U32), i, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); debug_mem_show(0, (U32)objp, 64); } if( RED_MAGIC2 != *(U32*)(objp+pBuf->objsize-sizeof(U32) ) ) { aos_printf( MPE_MM, "\r\n mem_check_all: file:%s,line:%u: : MM->:write past end,freed objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", fl, ln, objp+sizeof(U32), i, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); debug_mem_show(0, (U32)objp+pBuf->objsize-64, 64); } } else { if( RED_MAGIC1 != *(U32*)objp ) { aos_printf( MPE_MM, "\r\n mem_check_all: file:%s,line:%u: : MM->:write before start, objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", fl,ln, objp+sizeof(U32), i, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); } if( RED_MAGIC1 != *(U32*)(objp+pBuf->objsize-sizeof(U32) ) ) { aos_printf( MPE_MM, "\r\n mem_check_all: file:%s,line:%u: : MM->:write past end,objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", fl,ln, objp+sizeof(U32), i, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); } } } } }}U32 __mem_buf_check( MEM_BUF_S *pBuf ){ SLAB_S *pSlab; U32 slab_num; AOS_ASSERT( NULL != pBuf ); if(pBuf == NULL) { return FALSE; } pSlab = pBuf->s_list_head; slab_num = 0; while( NULL != pSlab ) { slab_num++; if( NULL != pSlab->s_next ) { if( pSlab->s_next->s_prev != pSlab ) { aos_printf( MPE_MM, "MM->:buf check1 error, list error of buf %s at 0x%x", pBuf->name, pBuf ); return FALSE; } } pSlab = pSlab->s_next; if( slab_num > pBuf->slab_num ) { aos_printf( MPE_MM, "MM->:buf check1 error, slab num over of buf %s at 0x%x", pBuf->name, pBuf ); return FALSE; } } if( slab_num != pBuf->slab_num ) { aos_printf( MPE_MM, "MM->:buf check1 error, slab num small of buf %s at 0x%x", pBuf->name, pBuf ); return FALSE; } pSlab = pBuf->s_list_tail; slab_num = 0; while( NULL != pSlab ) { slab_num++; if( NULL != pSlab->s_prev) { if( pSlab->s_prev->s_next != pSlab ) { aos_printf( MPE_MM, "MM->:buf check2 error, list error of buf %s at 0x%x", pBuf->name, pBuf ); return FALSE; } } pSlab = pSlab->s_prev; if( slab_num > pBuf->slab_num ) { aos_printf( MPE_MM, "MM->:buf check2 error, slab num over of buf %s at 0x%x", pBuf->name, pBuf ); return FALSE; } } if( slab_num != pBuf->slab_num ) { aos_printf( MPE_MM, "MM->:buf check2 error, slab num small of buf %s at 0x%x", pBuf->name, pBuf ); return FALSE; } return TRUE;}VOID *mem_obj_head( VOID*objp ){ PAGE_S *pPage; SLAB_S *pSlab; U32 pageNo,buf_id,objNo; if( (U32)objp < (U32)g_pPageOffset ) { return NULL; } pageNo = ((U32)objp-(U32)g_pPageOffset)>>MEM_PAGE_ORDER; if( pageNo >= g_ulPageNum ) { return NULL; } pPage = &g_pstPages[pageNo]; if( PAGE_SLAB != pPage->status ) { return NULL; } buf_id = pPage->buf_id; pSlab = pPage->slabp; if( (buf_id >= CONFIG_MEM_BUF_NUM) || (FALSE == g_pstMemBuf[buf_id].status) ) { return NULL; } objNo = ((U32)((U8*)objp - pSlab->s_mem))/g_pstMemBuf[buf_id].objsize; return pSlab->s_mem + objNo * g_pstMemBuf[buf_id].objsize + sizeof(U32);}U32 mem_obj_size( VOID*objp ){ PAGE_S *pPage; U32 pageNo,buf_id; if( (U32)objp < (U32)g_pPageOffset ) { return 0; } pageNo = ((U32)objp-(U32)g_pPageOffset)>>MEM_PAGE_ORDER; if( pageNo >= g_ulPageNum ) { return 0; } pPage = &g_pstPages[pageNo]; if( PAGE_SLAB != pPage->status ) { return 0; } buf_id = pPage->buf_id; if( (buf_id >= CONFIG_MEM_BUF_NUM) || (FALSE == g_pstMemBuf[buf_id].status) ) { return 0; } return g_pstMemBuf[buf_id].objsize - 8;}U32 __ssp_check_all_slabs(){ U32 index = 0, lockKey; SLAB_S *pSlab; lockKey = aos_int_lock(); for( index = 0; index < CONFIG_MEM_BUF_NUM; index++ ) { if( TRUE == g_pstMemBuf[index].status ) { pSlab = g_pstMemBuf[index].s_list_head; while( NULL!=pSlab ) { if( FALSE == __mem_slab_check(&g_pstMemBuf[index], pSlab) ) { return FALSE; } pSlab = pSlab->s_next; } } } aos_int_unlock(lockKey); return TRUE;}U32 mem_obj_room( VOID*objp ){ PAGE_S *pPage; U32 pageNo,buf_id,objNo; SLAB_S *pSlab; U8 *pHead, *pEnd; OBJ_CTRL_S *pObjCtrl; if( (U32)objp < (U32)g_pPageOffset ) { return U32_BUTT; } pageNo = ((U32)objp-(U32)g_pPageOffset)>>MEM_PAGE_ORDER; if( pageNo >= g_ulPageNum ) { return U32_BUTT; } pPage = &g_pstPages[pageNo]; if( PAGE_SLAB != pPage->status ) { return U32_BUTT; } buf_id = pPage->buf_id; pSlab = pPage->slabp; if( (buf_id >= CONFIG_MEM_BUF_NUM) || (FALSE == g_pstMemBuf[buf_id].status) ) { return U32_BUTT; } objNo = ((U32)((U8*)objp - pSlab->s_mem))/g_pstMemBuf[buf_id].objsize; pObjCtrl = &pSlab->obj_ctrl[objNo]; if( 0 == pObjCtrl->ref ) { aos_printf( MPE_MM, "MM-ROOM->:obj is free, objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", objp, buf_id, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); return 0; } pHead = pSlab->s_mem + objNo * g_pstMemBuf[buf_id].objsize; pEnd = pSlab->s_mem + (objNo+1) * g_pstMemBuf[buf_id].objsize - sizeof(U32); if( RED_MAGIC1 != *(U32*)pHead ) { aos_printf( MPE_MM, "MM-ROOM->:write before start,objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", objp, buf_id, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); } if( RED_MAGIC1 != *(U32*)pEnd ) { aos_printf( MPE_MM, "MM-ROOM->:write past end,objp=0x%x, buf_id=%d, mpe=%d, sid=%d" ", file=%s, line=%d", objp, buf_id, pObjCtrl->mpe, pObjCtrl->sid, pObjCtrl->file, pObjCtrl->line ); } return (U32)pEnd - (U32)objp;}OBJ_CTRL_S *mem_obj_ctrl( VOID*objp ){ PAGE_S *pPage; SLAB_S *pSlab; U32 pageNo,buf_id,objNo; if( (U32)objp < (U32)g_pPageOffset ) { return NULL; } pageNo = ((U32)objp-(U32)g_pPageOffset)>>MEM_PAGE_ORDER; if( pageNo >= g_ulPageNum ) { return NULL; } pPage = &g_pstPages[pageNo]; if( PAGE_SLAB != pPage->status ) { return NULL; } buf_id = pPage->buf_id; pSlab = pPage->slabp; if( (buf_id >= CONFIG_MEM_BUF_NUM) || (FALSE == g_pstMemBuf[buf_id].status) ) { return NULL; } objNo = ((U32)((U8*)objp - pSlab->s_mem))/g_pstMemBuf[buf_id].objsize; return &pSlab->obj_ctrl[objNo];}U32 mem_is_insufficient( ){ return (slab_dbg_info.is_slab_s_insufficient || slab_dbg_info.is_page_insufficient);}U8 mem_get_dmem_use_rate(VOID){ U32 i, ulObjInUse = 0, ulMemInUse = 0; SLAB_S *s_Tmp; for(i = 0;i < CONFIG_MEM_BUF_NUM;i++) { if(g_pstMemBuf[i].status == FALSE) { continue; } ulObjInUse = 0; for( s_Tmp = g_pstMemBuf[i].s_list_head; s_Tmp != NULL; s_Tmp = s_Tmp->s_next ) { ulObjInUse += s_Tmp->inuse; } ulMemInUse += (g_pstMemBuf[i].objsize - 8) * ulObjInUse; } return ulMemInUse/(g_ulStaticMemStart - (U32)g_pPageOffset - CONFIG_AOS_RESERVED_MEM_SIZE) * 100;}#ifdef __cplusplus}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -