programming language: C platform: ARM Compiler: ADS 1.2
I need to track simple melloc/free calls in my project. I just need to get a general idea of โโhow much heap memory is required when the program has allocated all its resources. So I provided a wrapper for malloc/free calls. In these shells, I need to increase the current amount of memory when malloc is called and decreases it when free is called. The malloc case is direct, as I have a size to allocate from the caller. I am wondering how to deal with the free tag, since I need to keep the location of the pointer / size somewhere. This is C, I do not have a standard card to implement this easily.
I am trying to avoid the link in some libraries, so I prefer the implementation of .c / h.
So, I wonder if there is already a simple implementation that I can lead to. If not, this is the motivation for implementation and implementation.
EDIT: Purely for debugging, and this code does not ship with the product.
EDIT: An initial implementation based on a response from Makis. I would appreciate feedback on this.
EDIT: revised 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; }
c dynamic-memory-allocation
dubnde May 12 '09 at 10:10 2009-05-12 10:10
source share