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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*----------------------------------------------------------------------------*/
/* Xymon monitor library. */
/* */
/* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk> */
/* */
/* This program is released under the GNU General Public License (GPL), */
/* version 2. See the file "COPYING" for details. */
/* */
/*----------------------------------------------------------------------------*/
#ifndef __MEMORY_H__
#define __MEMORY_H__
#undef XYMON_MEMORY_WRAPPERS /* If defined, will wrap mem-alloc routines */
#undef MEMORY_DEBUG /* If defined, use debugging code */
typedef struct xmemory_t {
char *sdata;
size_t ssize;
struct xmemory_t *next;
} xmemory_t;
extern const char *xfreenullstr;
extern void add_to_memlist(void *ptr, size_t memsize);
extern void remove_from_memlist(void *ptr);
extern void *xcalloc(size_t nmemb, size_t size);
extern void *xmalloc(size_t size);
extern void *xrealloc(void *ptr, size_t size);
extern char *xstrdup(const char *s);
extern char *xstrcat(char *dest, const char *src);
extern char *xstrcpy(char *dest, const char *src);
extern char *xstrncat(char *dest, const char *src, size_t maxlen);
extern char *xstrncpy(char *dest, const char *src, size_t maxlen);
extern int xsprintf(char *dest, const char *fmt, ...);
extern char *xresultbuf(int maxsz);
#ifdef XYMON_MEMORY_WRAPPERS
#ifndef LIB_MEMORY_C_COMPILE
#undef calloc
#undef malloc
#undef realloc
#undef strdup
/*
* This arranges for all memory-allocation routines to
* go via a wrapper that checks for bogus input data
* and malloc() returning NULL when running out of memory.
* Errors caught here are fatal.
* Overhead is small, so this is turned on always.
*/
#define calloc(N,S) xcalloc((N), (S))
#define malloc(N) xmalloc((N))
#define realloc(P,S) xrealloc((P), (S))
#define strdup(P) xstrdup((P))
#endif /* LIB_MEMORY_C_COMPILE */
#endif /* XYMON_MEMORY_WRAPPERS */
#ifdef MEMORY_DEBUG
/*
* This arranges for all calls to potentially memory-overwriting routines
* to do strict allocation checks and overwrite checks. The performance
* overhead is significant, so it should only be turned on in debugging
* situations.
*/
#ifndef LIB_MEMORY_C_COMPILE
#define MEMDEFINE(P) { add_to_memlist((P), sizeof((P))); }
#define MEMUNDEFINE(P) { remove_from_memlist((P)); }
#define xfree(P) { remove_from_memlist((P)); free((P)); (P) = NULL; }
#undef strcat
#undef strncat
#undef strcpy
#undef strncpy
#undef sprintf
#define strcat(D,S) xstrcat((D), (S))
#define strncat(D,S,L) xstrncat((D), (S), (L))
#define strcpy(D,S) xstrcpy((D), (S))
#define strncpy(D,S,L) xstrncpy((D), (S), (L))
#define sprintf xsprintf
#endif
#else
/*
* This defines an "xfree()" macro, which checks for freeing of
* a NULL ptr and complains if that happens. It does not check if
* the pointer is valid.
* After being freed, the pointer is set to NULL to catch double-free's.
*/
#define xfree(P) { if ((P) == NULL) { errprintf(xfreenullstr); abort(); } free((P)); (P) = NULL; }
#define MEMDEFINE(P) do { } while (0);
#define MEMUNDEFINE(P) do { } while (0);
#endif /* MEMORY_DEBUG */
#endif
|