-
Notifications
You must be signed in to change notification settings - Fork 1
/
xpower-malloc.c
74 lines (63 loc) · 1.8 KB
/
xpower-malloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
** Copyright (C) 2011 XXX all rights reserved.
**
** This file define various trivial helper wrappers around
** standard functions, useful for debug.
**
** Created by [email protected] on 01/11/2011
**
*/
#include "xpower-config.h"
/*
* these memory routines are used to find any memory issues or memory
* optimization. For some reasons, the PAS need allocate memory space
* dynamically, it is important to guarantee the memory has been freed
* correctly.
*
*/
#ifdef _XDEBUG
static FILE *_xfp = NULL;
#define _XFP (_xfp ? _xfp : stdout)
/*!
* set the output destination for memory debug routines
*/
FILE *_xsetfp(FILE *fp){
FILE *tmp = _xfp;
_xfp = fp;
return (tmp);
}
/*!
* allocate memory from heap, print the address & length allocated
*/
void *_xmalloc(size_t size){
void *ret = malloc(size);
(void) fprintf(_XFP,"MEMFUN##### malloc : [0x%08lx] size=%d\n",(long)ret,size);
return (ret);
}
/*!
* re-allocate memory from heap, print the new address & length allocated,
* print the old address if success
*/
void *_xrealloc(void *ptr, size_t size){
void *ret = realloc(ptr,size);
(void) fprintf(_XFP,"MEMFUN##### realloc: [0x%08lx] size=%d\n",(long)ret,size);
if (ret && ptr)(void) fprintf(_XFP,"MEMFUN##### free : [0x%08lx]\n",(long)ptr);
return (ret);
}
/*!
* allocate & clean memory from heap, print the new address & length allocated
*/
void *_xcalloc(size_t nmem, size_t size){
void *ret = calloc(nmem,size);
(void) fprintf(_XFP,"MEMFUN##### calloc : [0x%08lx] size=%d\n",(long)ret,nmem*size);
return (ret);
}
/*!
* deallocate memory to heap, print the address that has been deallocated if it
* is not NULL
*/
void _xfree(void *ptr){
free(ptr);
if (ptr)(void) fprintf(_XFP,"MEMFUN##### free : [0x%08lx]\n",(long)ptr);
}
#endif/*_XDEBUG*/