基本信息
源码名称:c语言 写的内存分配 示例源码
源码大小:4.27KB
文件格式:.rar
开发语言:C/C++
更新时间:2014-12-21
   源码介绍
自己写的内存分配
自己写的内存分配

自己写的内存分配

#include "MemoryAllo.h"


P_MEMORY_MANAGE MEM_ApplyMemory(unsigned int nBlock)
{
    P_MEMORY_MANAGE pstMemManage = NULL;
    int nIndex = 0;

    if(nBlock == 0)
    {
        return NULL;
    }
    pstMemManage = (P_MEMORY_MANAGE)malloc(sizeof(MEMORY_MANAGE)   sizeof(MEMORY_NODE) * nBlock);

    if(pstMemManage == NULL)
    {
        return NULL;
    }

    /* 初始化  */
    pstMemManage->pstFreeNode = (P_MEMORY_NODE*)(pstMemManage   1);
    pstMemManage->pstUseNode = NULL;
    pstMemManage->nCurBlock = 0;
    pstMemManage->nMaxBlock = nBlock;
    pstMemManage->pstNextManager = NULL;

    /* 初始化空闲链表 */
    for(nIndex = 0; nIndex < (int)(nBlock - 1); nIndex  )
    {
        (pstMemManage->pstFreeNode   nIndex)->pstNextNode = (pstMemManage->pstFreeNode   nIndex   1);
    }

    (pstMemManage->pstFreeNode   nBlock - 1)->pstNextNode = NULL;

    return pstMemManage;
}



int MEM_AddData(P_MEMORY_MANAGE pstMemManage, void *pvData, int nSize)
{
    P_MEMORY_NODE pstTmpUse = NULL;
    P_MEMORY_NODE pstTmpFree = NULL;
    P_MEMORY_MANAGE pstAddMemory = NULL;
    P_MEMORY_MANAGE pstLastMemory = NULL;
    P_MEMORY_MANAGE pstGetFreeMemory = NULL;

    if(pvData == NULL||pstMemManage == NULL)
    {
        return -1;
    }

    pstGetFreeMemory = pstMemManage;

    /* 查找空闲内存块 */
    while((pstGetFreeMemory != NULL
           &&(pstGetFreeMemory->nCurBlock >= pstGetFreeMemory->nMaxBlock)
           ))
    {
        pstGetFreeMemory = pstGetFreeMemory->pstNextManager;
    }

    /* 重新申请内存卡 */
    if(pstGetFreeMemory == NULL)
    {
        //TODO
        pstAddMemory = MEM_ApplyMemory(pstMemManage->nMaxBlock);
        if(pstAddMemory == NULL)
        {
            return -1;
        }

        pstLastMemory = pstMemManage;

        /* 找到最后一个节点 */
        while(pstLastMemory->pstNextManager != NULL)
        {
            pstLastMemory = pstLastMemory->pstNextManager;
        }

        pstLastMemory->pstNextManager = pstAddMemory;

        pstGetFreeMemory = pstAddMemory;
    }

    /* 空闲链表更新 */
    pstTmpFree = pstGetFreeMemory->pstFreeNode;
    pstGetFreeMemory->pstFreeNode = pstGetFreeMemory->pstFreeNode->pstNextNode;
    /* 在用链表更新 */
    pstTmpUse = pstGetFreeMemory->pstUseNode;
    pstGetFreeMemory->pstUseNode = pstTmpFree;
    pstGetFreeMemory->pstUseNode->pstNextNode = pstTmpUse;
    pstGetFreeMemory->nCurBlock  ;

    memcpy(pstGetFreeMemory->pstUseNode->acData, pvData, nSize);

    return 0;

}



int MEM_ShowData(P_MEMORY_MANAGE pstMemManage)
{
    P_MEMORY_NODE pstDataNode = NULL;
    P_MEMORY_MANAGE pstMemNode = NULL;

    if(pstMemManage == NULL)
    {
        return -1;
    }

    pstMemNode = pstMemManage;

    while(pstMemNode != NULL)
    {
        pstDataNode = pstMemNode->pstUseNode;
        while(pstDataNode != NULL)
        {
            printf("%c\n", *(char*)pstDataNode->acData);
            pstDataNode = pstDataNode->pstNextNode;
        }

        pstMemNode = pstMemNode->pstNextManager;

    }
    return 0;
}



int MEM_DeleteData(P_MEMORY_MANAGE pstMemManage, void *pvData, int nSize)
{
    P_MEMORY_NODE pstCurNode = NULL;
    P_MEMORY_NODE pstForeNode = NULL;
    P_MEMORY_NODE pstFreeNode = NULL;
    P_MEMORY_MANAGE pstMemNode = NULL;

    if(pstMemManage == NULL||pvData == NULL||pstMemManage->nCurBlock == 0)
    {
        return -1;
    }
    pstMemNode = pstMemManage;

    while(pstMemNode != NULL)
    {

        pstCurNode = pstMemNode->pstUseNode;
        while(pstCurNode != NULL)
        {

            if(memcmp(pstCurNode->acData, pvData, nSize) == 0)
            {
                if(pstForeNode == NULL)
                {
                    pstMemNode->pstUseNode = pstCurNode->pstNextNode;
                    pstFreeNode = pstMemNode->pstFreeNode;
                    pstMemNode->pstFreeNode = pstCurNode;
                    pstCurNode->pstNextNode = pstFreeNode;
                    pstMemNode->nCurBlock--;
                }
                else
                {
                    pstForeNode->pstNextNode = pstCurNode->pstNextNode;
                    pstFreeNode = pstMemNode->pstFreeNode;
                    pstMemNode->pstFreeNode = pstCurNode;
                    pstCurNode->pstNextNode = pstFreeNode;
                    pstMemNode->nCurBlock--;
                }
                break;
            }


            pstForeNode = pstCurNode;
            pstCurNode = pstCurNode->pstNextNode;
        }

        pstForeNode = NULL;
        pstMemNode = pstMemNode->pstNextManager;
    }


    return 0;
}


int MEM_FreeMemory(P_MEMORY_MANAGE pstMemManage)
{

    P_MEMORY_MANAGE pstForeMem = NULL;
    P_MEMORY_MANAGE pstNextMem = NULL;

    if(pstMemManage == NULL)
    {
        return -1;
    }

    pstNextMem = pstMemManage;

    while(pstNextMem != NULL)
    {
        pstForeMem = pstNextMem;
        free(pstNextMem);
        pstNextMem = pstForeMem->pstNextManager;
    }
    return 0;
}