Macro for memory logging using __FILE__ and __LINE__

S

Spry

Hi,

I wanted to write macros for finding the number of memory allocations and
deallocations also wanted to find the locations.

The code I have is a pretty big one. I have a wrapper on top of malloc(int
x) as Xmalloc(int x)

Now I want to write a macro for Xmalloc which can log the location of the
file and the line and the number of bytes allocated.

I cannot use function I need to use #define since I want to use __FILE__ and
__LINE__ if I use a function then it will give the file name and the line of
that funciton.



File: main.c
--------------
#include <stdio.h>
#include "my_macro.h"

main()
{
int *p;
int i = 10;
p=Xmalloc(i);
Xfree(p);
}

File: Xalloc.h
------------------

void * Xmalloc(int x)
{
return (void *)(malloc(x));
}
void Xfree(void *p)
{
free(p);
}


File: my_macro.h
-------------------

#include "Xalloc.h"


//#define Xmalloc(x) Xmalloc(x)
#define Xfree(p) printf("FILE: %s LINE: %d Pointer:
%u",__FILE__,__LINE__,p );Xfree(p)



The problem I am facing is to write the macro for Xmalloc in the file
my_macro.h which allocates the memory and then logs the __FILE__ __LINE__
, the pointer value and the number of bytes.

Could some one please help in coming up with such an macro.


Regards,
Spry.
 
R

Richard Heathfield

Spry said:
Hi,

I wanted to write macros for finding the number of memory allocations and
deallocations also wanted to find the locations.

The code I have is a pretty big one. I have a wrapper on top of malloc(int
x) as Xmalloc(int x)

Now I want to write a macro for Xmalloc which can log the location of the
file and the line and the number of bytes allocated.

I cannot use function I need to use #define since I want to use __FILE__
and __LINE__ if I use a function then it will give the file name and the
line of that funciton.

You can find code to do precisely this in the Library With No Name, which is
downloadable from http://www.rjgh.co.uk/prg/c/wnn/index.php#Download

To save you the effort, though, here's a quote from the header:

/*! You can control whether wnn_Malloc, etc, call the standard library
routines or the CLINT wrappers by defining, or not defining, the
WNN_USE_STDALLOC macro.
*/
#ifdef WNN_USE_STDALLOC
/*! You have elected to define WNN_USE_STDALLOC, so the standard
library calls will be invoked directly.
*/
#define wnn_Malloc(wnn_SIZE) malloc(wnn_SIZE)
#define wnn_Calloc(wnn_COUNT, wnn_SIZE) calloc(wnn_COUNT, wnn_SIZE)
#define wnn_Realloc(wnn_OLD, wnn_NEW) realloc(wnn_OLD, wnn_NEW)
#define wnn_Free(wnn_OLD) free(wnn_OLD)
#else
/*! You have elected not to define WNN_USE_STDALLOC, so the standard
library calls will be wrapped by CLINT calls.
*/
#define wnn_Malloc(wnn_SIZE) wnn_malloc(wnn_SIZE, __FILE__, __LINE__)
#define wnn_Calloc(wnn_COUNT, wnn_SIZE) \
wnn_calloc(wnn_COUNT, wnn_SIZE, __FILE__, __LINE__)
#define wnn_Realloc(wnn_OLD, wnn_NEW) \
wnn_realloc(wnn_OLD, wnn_NEW, __FILE__, __LINE__)
#define wnn_Free(wnn_OLD) wnn_free(wnn_OLD, __FILE__, __LINE__)
#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top