Return to typed_mem.h CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / mpd / src / contrib / libpdel / util |
1.1 misho 1: 2: /* 3: * Copyright (c) 2001-2002 Packet Design, LLC. 4: * All rights reserved. 5: * 6: * Subject to the following obligations and disclaimer of warranty, 7: * use and redistribution of this software, in source or object code 8: * forms, with or without modifications are expressly permitted by 9: * Packet Design; provided, however, that: 10: * 11: * (i) Any and all reproductions of the source or object code 12: * must include the copyright notice above and the following 13: * disclaimer of warranties; and 14: * (ii) No rights are granted, in any manner or form, to use 15: * Packet Design trademarks, including the mark "PACKET DESIGN" 16: * on advertising, endorsements, or otherwise except as such 17: * appears in the above copyright notice or in the software. 18: * 19: * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND 20: * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO 21: * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING 22: * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED 23: * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 24: * OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE, 25: * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS 26: * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, 27: * RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE 28: * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE 29: * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT, 30: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL 31: * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF 32: * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF 33: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 35: * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF 36: * THE POSSIBILITY OF SUCH DAMAGE. 37: * 38: * Author: Archie Cobbs <archie@freebsd.org> 39: */ 40: 41: #ifndef _PDEL_UTIL_TYPED_MEM_H_ 42: #define _PDEL_UTIL_TYPED_MEM_H_ 43: 1.1.1.2 ! misho 44: #include <sys/cdefs.h> ! 45: 1.1 misho 46: /* 47: * Memory type to use for temporary memory that is to be free'd 48: * before the calling function returns. Note: the type string 49: * includes the calling function name, so the allocation type 50: * and free type MUST be both specified within the same function. 51: */ 52: #ifdef __GNUC__ 53: #define TYPED_MEM_TEMP __FUNCTION__ 54: #else 55: #define TYPED_MEM_TEMP "TEMPORARY" 56: #endif 57: 58: /* 59: * Size of saved type buffer (including '\0' byte). Any characters in 60: * the type name that don't fit in a buffer this size are ignored. 61: */ 62: #define TYPED_MEM_TYPELEN 24 63: 64: /* 65: * Define this to print all (de)allocations to stderr. Doing 66: * so requires rebuilding the entire library and all user code. 67: */ 68: #define TYPED_MEM_TRACE 0 69: 70: /* Statistics reporting structure */ 71: struct typed_mem_typestats { 72: char type[TYPED_MEM_TYPELEN]; /* type string */ 73: u_int allocs; /* # blocks alloc'd */ 74: u_int bytes; /* # bytes alloc'd */ 75: }; 76: 77: DEFINE_STRUCTS_ARRAY(typed_mem_stats, struct typed_mem_typestats); 78: 79: /* 80: * Typed memory wrapper macros 81: */ 82: #if TYPED_MEM_TRACE 83: #define MALLOC(type, size) \ 84: typed_mem_realloc(__FILE__, __LINE__, type, NULL, size) 85: #define CALLOC(type, num, size) \ 86: typed_mem_calloc(__FILE__, __LINE__, type, num, size) 87: #define REALLOC(type, mem, size) \ 88: typed_mem_realloc(__FILE__, __LINE__, type, mem, size) 89: #define REALLOCF(type, mem, size) \ 90: typed_mem_reallocf(__FILE__, __LINE__, type, mem, size) 91: #define STRDUP(type, string) \ 92: typed_mem_strdup(__FILE__, __LINE__, type, string) 93: #define FREE(type, mem) \ 94: typed_mem_free(__FILE__, __LINE__, type, mem) 95: #define ASPRINTF(type, r, f, args...) \ 96: typed_mem_asprintf(__FILE__, __LINE__, type, r, f , ## args) 97: #define VASPRINTF(type, r, f, va) \ 98: typed_mem_vasprintf(__FILE__, __LINE__, type, r, f, va) 99: #else 100: #define MALLOC(type, size) typed_mem_realloc(type, NULL, size) 101: #define CALLOC(type, num, size) typed_mem_calloc(type, num, size) 102: #define REALLOC(type, mem, size) typed_mem_realloc(type, mem, size) 103: #define REALLOCF(type, mem, size) typed_mem_reallocf(type, mem, size) 104: #define STRDUP(type, string) typed_mem_strdup(type, string) 105: #define FREE(type, mem) typed_mem_free(type, mem) 106: #define ASPRINTF(type, r, f, args...) typed_mem_asprintf(type, r, f , ## args) 107: #define VASPRINTF(type, r, f, va) typed_mem_vasprintf(type, r, f, va) 108: #endif 109: 110: /* 111: * If TYPED_MEM_UNDEFINE_ORIGINALS is defined, then the including file 112: * wants us to prevent it from using any of the original libc functions. 113: */ 114: #ifdef TYPED_MEM_UNDEFINE_ORIGINALS 115: #undef malloc 116: #define malloc %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 117: #undef calloc 118: #define calloc %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 119: #undef realloc 120: #define realloc %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 121: #undef reallocf 122: #define reallocf %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 123: #undef free 124: #define free %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 125: #undef strdup 126: #define strdup %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 127: #undef asprintf 128: #define asprintf %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 129: #undef vasprintf 130: #define vasprintf %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%% 131: #endif 132: 133: __BEGIN_DECLS 134: 135: /* 136: * Typed memory allocation and freeing routines 137: */ 138: #if TYPED_MEM_TRACE 139: extern void *typed_mem_realloc(const char *file, u_int line, 140: const char *type, void *mem, size_t size); 141: extern void *typed_mem_reallocf(const char *file, u_int line, 142: const char *type, void *mem, size_t size); 143: extern void *typed_mem_calloc(const char *file, u_int line, 144: const char *type, size_t num, size_t size); 145: extern char *typed_mem_strdup(const char *file, u_int line, 146: const char *type, const char *string); 147: extern void typed_mem_free(const char *file, u_int line, 148: const char *type, void *mem); 149: extern int typed_mem_asprintf(const char *file, u_int line, 1.1.1.2 ! misho 150: const char *type, char **ret, const char *format, ...) ! 151: __printflike(5, 6); 1.1 misho 152: extern int typed_mem_vasprintf(const char *file, u_int line, 153: const char *type, char **ret, 1.1.1.2 ! misho 154: const char *format, va_list va) ! 155: __printflike(5, 0); 1.1 misho 156: #else 157: extern void *typed_mem_realloc(const char *type, void *mem, size_t size); 158: extern void *typed_mem_reallocf(const char *type, void *mem, size_t size); 159: extern void *typed_mem_calloc(const char *type, size_t num, size_t size); 160: extern char *typed_mem_strdup(const char *type, const char *string); 161: extern void typed_mem_free(const char *type, void *mem); 162: extern int typed_mem_asprintf(const char *type, 1.1.1.2 ! misho 163: char **ret, const char *format, ...) ! 164: __printflike(3, 4); 1.1 misho 165: extern int typed_mem_vasprintf(const char *type, char **ret, 1.1.1.2 ! misho 166: const char *format, va_list va) ! 167: __printflike(3, 0); 1.1 misho 168: #endif 169: 170: /* Typed memory must be enabled by calling this function before any ops */ 171: extern int typed_mem_enable(void); 172: 173: /* Typed statistics routines */ 1.1.1.2 ! misho 174: extern char *typed_mem_type(void *mem, char *typebuf); 1.1 misho 175: extern int typed_mem_usage(struct typed_mem_stats *stats); 176: #ifndef _KERNEL 177: extern void typed_mem_dump(FILE *fp); 178: #endif 179: 180: /* Structs type for 'struct typed_mem_stats' */ 181: extern const struct structs_type typed_mem_stats_type; 182: 183: __END_DECLS 184: 185: #endif /* _PDEL_UTIL_TYPED_MEM_H_ */ 186: