title: Simple C implementation to track memory malloc/free?
body: <p>programming language: C
  platform: ARM
  Compiler: ADS 1.2</p>
  
  <p>I need to keep track of simple <code>melloc/free</code> 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 <code>malloc/free</code> calls. In these wrappers I need to increment a current memory count when <code>malloc</code> is called and decrement it when <code>free</code> is called. The <code>malloc</code> case is straight forward as I have the size to allocate from the caller. I am wondering how to deal with the <code>free</code> 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. </p>
  
  <p>I am trying to avoid linking in any libraries so would prefer *.c/h implementation.</p>
  
  <p>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.</p>
  
  <p>EDIT: Purely for debugging and this code is not shipped with the product.</p>
  
  <p>EDIT: Initial implementation based on answer from Makis. I would appreciate feedback on this.</p>
  
  <p>EDIT: Reworked implementation</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;string.h&gt;
  #include &lt;limits.h&gt;
  
  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, &amp;nSize, sizeof(nSize));
  
      gnCurrentMemory += nSize;
  
      if (gnCurrentMemory &gt; 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 &gt;= *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 &lt; BUFFERSIZE; ++i)
    {
      mybuffer-&gt;buffer[i] = i;
      mybuffer-&gt;bools[i] = true;
      bufferinfo-&gt;whichbuffer = (unsigned int)(i/100);
    }
  
  
    MemFree(bufferinfo);
    MemFree(mybuffer);
  
    if(pCh)
    {
      MemFree(pCh);
    }
  
    return EXIT_SUCCESS;
  }
  </code></pre>