简单的C实施跟踪内存的malloc /免费的吗? [英] Simple C implementation to track memory malloc/free?

查看:234
本文介绍了简单的C实施跟踪内存的malloc /免费的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编程语言:C
平台:ARM
编译:ADS 1.2

我需要跟踪的简单 melloc /免费调用我的项目。我只需要获得当程序已分配其所有资源多少堆内存需要非常基本的想法。因此,我提供了一个包装的的malloc /免费来电。在这些包装我需要增加时,的malloc 被称为当前内存容量和递减时,它被称为免费。在的malloc 情况下是直线前进,因为我有大小从呼叫者分配。我想知道如何应对免费情况下我需要存储指针/尺寸映射的地方。这是C,我没有一个标准的地图可以轻松实现这一点。

我试图避免任何库链接,以便将preFER的* .c / h的实现。

所以我想知道如果已经有一个简单的实现人们可以带领我。如果不是,这是动力继续前进,实现一个。

编辑:纯粹为了调试,这code未随产品

编辑:基于来自的Makis答案初步实施。我想在这个AP preciate反馈。

编辑:返工实施

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&ASSERT.H GT;
#包括LT&;&string.h中GT;
#包括LT&;&limits.h中GT;静态为size_t gnCurrentMemory = 0;
静态为size_t gnPeakMemory = 0;无效* MemAlloc(为size_t n大小)
{
  无效* PMEM =的malloc(sizeof的(为size_t)+ n大小);  如果(PMEM)
  {
    为size_t * pSize =(为size_t *)PMEM;    的memcpy(pSize,&安培; n大小,sizeof的(n大小));    gnCurrentMemory + = n大小;    如果(gnCurrentMemory> gnPeakMemory)
    {
      gnPeakMemory = gnCurrentMemory;
    }    的printf(PMemAlloc(%#X) - 尺寸(%D),电流(%D),匹克(%D)\\ n
           pSize + 1,n大小,gnCurrentMemory,gnPeakMemory);    返回(pSize + 1);
  }  返回NULL;
}无效MemFree(无效* PMEM)
{
  如果(PMEM)
  {
    为size_t * pSize =(为size_t *)PMEM;    //获取大小
    --pSize;    断言(gnCurrentMemory> = * pSize);    的printf(PMemFree(%#X) - 尺寸(%D),电流(%D),匹克(%D)\\ n
           PMEM,* pSize,gnCurrentMemory,gnPeakMemory);    gnCurrentMemory - = * pSize;    免费(pSize);
  }
}#定义BUFFERSIZE(1024 * 1024)typedef结构
{
  布尔标志;
  INT缓冲区[BUFFERSIZE]
  布尔布尔变量[BUFFERSIZE]
} sample_buffer;typedef结构
{
  无符号整型whichbuffer;
  焦炭CH;
} buffer_info;
INT主要(无效)
{
  无符号整型我;
  buffer_info * bufferinfo;  sample_buffer * mybuffer;  字符* PCH;  的printf(Tesint MemAlloc - MemFree \\ n);  mybuffer =(sample_buffer *)MemAlloc(的sizeof(sample_buffer));  如果(mybuffer == NULL)
  {
    的printf(ERROR分配mybuffer \\ n);    返回EXIT_FAILURE;
  }  bufferinfo =(buffer_info *)MemAlloc(的sizeof(buffer_info));  如果(bufferinfo == NULL)
  {
    的printf(ERROR分配bufferinfo \\ n);    MemFree(mybuffer);    返回EXIT_FAILURE;
  }  PCH =(字符*)MemAlloc(的sizeof(字符));  的printf(完成的malloc \\ n);  //填写分配的内存与整数和回读一些值
  对于(i = 0; I< BUFFERSIZE ++ I)
  {
    mybuffer->缓冲区[我] =我;
    mybuffer->的bool [I] =真;
    bufferinfo-> whichbuffer =(unsigned int类型)(I / 100);
  }
  MemFree(bufferinfo);
  MemFree(mybuffer);  如果(PCH)
  {
    MemFree(PCH);
  }  返回EXIT_SUCCESS;
}


解决方案

您可以分配在包装一些额外的字节,并把任何一个ID(如果你希望能够对夫妇的malloc()和free())或只是有大小。刚刚的malloc(),它更多的内存,存储信息在你的内存块的开始,并移动光标返回很多字节着。

这可以,顺便说一句,也很容易被用于围栏指针/指模及这样的。

programming language: C platform: ARM Compiler: ADS 1.2

I need to keep track of simple melloc/free calls in my project. I just need to get very basic idea of how much heap memory is required when the program has allocated all its resources. Therefore, I have provided a wrapper for the malloc/free calls. In these wrappers I need to increment a current memory count when malloc is called and decrement it when free is called. The malloc case is straight forward as I have the size to allocate from the caller. I am wondering how to deal with the free case as I need to store the pointer/size mapping somewhere. This being C, I do not have a standard map to implement this easily.

I am trying to avoid linking in any libraries so would prefer *.c/h implementation.

So I am wondering if there already is a simple implementation one may lead me to. If not, this is motivation to go ahead and implement one.

EDIT: Purely for debugging and this code is not shipped with the product.

EDIT: Initial implementation based on answer from Makis. I would appreciate feedback on this.

EDIT: Reworked implementation

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <limits.h>

static size_t gnCurrentMemory = 0;
static size_t gnPeakMemory    = 0;

void *MemAlloc (size_t nSize)
{
  void *pMem = malloc(sizeof(size_t) + nSize);

  if (pMem)
  {
    size_t *pSize = (size_t *)pMem;

    memcpy(pSize, &nSize, sizeof(nSize));

    gnCurrentMemory += nSize;

    if (gnCurrentMemory > gnPeakMemory)
    {
      gnPeakMemory = gnCurrentMemory;
    }

    printf("PMemAlloc (%#X) - Size (%d), Current (%d), Peak (%d)\n",
           pSize + 1, nSize, gnCurrentMemory, gnPeakMemory);

    return(pSize + 1);
  }

  return NULL;
}

void  MemFree (void *pMem)
{
  if(pMem)
  {
    size_t *pSize = (size_t *)pMem;

    // Get the size
    --pSize;

    assert(gnCurrentMemory >= *pSize);

    printf("PMemFree (%#X) - Size (%d), Current (%d), Peak (%d)\n",
           pMem,  *pSize, gnCurrentMemory, gnPeakMemory);

    gnCurrentMemory -= *pSize;

    free(pSize);
  }
}

#define BUFFERSIZE (1024*1024)

typedef struct
{
  bool flag;
  int buffer[BUFFERSIZE];
  bool bools[BUFFERSIZE];
} sample_buffer;

typedef struct
{
  unsigned int whichbuffer;
  char ch;
} buffer_info;


int main(void)
{
  unsigned int i;
  buffer_info *bufferinfo;

  sample_buffer  *mybuffer;

  char *pCh;

  printf("Tesint MemAlloc - MemFree\n");

  mybuffer = (sample_buffer *) MemAlloc(sizeof(sample_buffer));

  if (mybuffer == NULL)
  {
    printf("ERROR ALLOCATING mybuffer\n");

    return EXIT_FAILURE;
  }

  bufferinfo = (buffer_info *) MemAlloc(sizeof(buffer_info));

  if (bufferinfo == NULL)
  {
    printf("ERROR ALLOCATING bufferinfo\n");

    MemFree(mybuffer);

    return EXIT_FAILURE;
  }

  pCh = (char *)MemAlloc(sizeof(char));

  printf("finished malloc\n");

  // fill allocated memory with integers and read back some values
  for(i = 0; i < BUFFERSIZE; ++i)
  {
    mybuffer->buffer[i] = i;
    mybuffer->bools[i] = true;
    bufferinfo->whichbuffer = (unsigned int)(i/100);
  }


  MemFree(bufferinfo);
  MemFree(mybuffer);

  if(pCh)
  {
    MemFree(pCh);
  }

  return EXIT_SUCCESS;
}

解决方案

You could allocate a few extra bytes in your wrapper and put either an id (if you want to be able to couple malloc() and free()) or just the size there. Just malloc() that much more memory, store the information at the beginning of your memory block and and move the pointer you return that many bytes forward.

This can, btw, also easily be used for fence pointers/finger-prints and such.

这篇关于简单的C实施跟踪内存的malloc /免费的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆