Annotation of embedaddon/libpdel/util/typed_mem.h, revision 1.1.1.1

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: 
                     44: /*
                     45:  * Memory type to use for temporary memory that is to be free'd
                     46:  * before the calling function returns. Note: the type string
                     47:  * includes the calling function name, so the allocation type
                     48:  * and free type MUST be both specified within the same function.
                     49:  */
                     50: #ifdef __GNUC__
                     51: #define TYPED_MEM_TEMP         __FUNCTION__
                     52: #else
                     53: #define TYPED_MEM_TEMP         "TEMPORARY"
                     54: #endif
                     55: 
                     56: /*
                     57:  * Size of saved type buffer (including '\0' byte). Any characters in
                     58:  * the type name that don't fit in a buffer this size are ignored.
                     59:  */
                     60: #define TYPED_MEM_TYPELEN      24
                     61: 
                     62: /*
                     63:  * Define this to print all (de)allocations to stderr. Doing
                     64:  * so requires rebuilding the entire library and all user code.
                     65:  */
                     66: #define TYPED_MEM_TRACE                0
                     67: 
                     68: /* Statistics reporting structure */
                     69: struct typed_mem_typestats {
                     70:        char            type[TYPED_MEM_TYPELEN];        /* type string */
                     71:        u_int           allocs;                         /* # blocks alloc'd */
                     72:        u_int           bytes;                          /* # bytes alloc'd */
                     73: };
                     74: 
                     75: DEFINE_STRUCTS_ARRAY(typed_mem_stats, struct typed_mem_typestats);
                     76: 
                     77: /*
                     78:  * Typed memory wrapper macros
                     79:  */
                     80: #if TYPED_MEM_TRACE
                     81: #define MALLOC(type, size)                                     \
                     82:        typed_mem_realloc(__FILE__, __LINE__, type, NULL, size)
                     83: #define CALLOC(type, num, size)                                        \
                     84:        typed_mem_calloc(__FILE__, __LINE__, type, num, size)
                     85: #define REALLOC(type, mem, size)                               \
                     86:        typed_mem_realloc(__FILE__, __LINE__, type, mem, size)
                     87: #define REALLOCF(type, mem, size)                              \
                     88:        typed_mem_reallocf(__FILE__, __LINE__, type, mem, size)
                     89: #define STRDUP(type, string)                                   \
                     90:        typed_mem_strdup(__FILE__, __LINE__, type, string)
                     91: #define FREE(type, mem)                                                \
                     92:        typed_mem_free(__FILE__, __LINE__, type, mem)
                     93: #define ASPRINTF(type, r, f, args...)                          \
                     94:        typed_mem_asprintf(__FILE__, __LINE__, type, r, f , ## args)
                     95: #define VASPRINTF(type, r, f, va)                              \
                     96:        typed_mem_vasprintf(__FILE__, __LINE__, type, r, f, va)
                     97: #else
                     98: #define MALLOC(type, size)             typed_mem_realloc(type, NULL, size)
                     99: #define CALLOC(type, num, size)                typed_mem_calloc(type, num, size)
                    100: #define REALLOC(type, mem, size)       typed_mem_realloc(type, mem, size)
                    101: #define REALLOCF(type, mem, size)      typed_mem_reallocf(type, mem, size)
                    102: #define STRDUP(type, string)           typed_mem_strdup(type, string)
                    103: #define FREE(type, mem)                        typed_mem_free(type, mem)
                    104: #define ASPRINTF(type, r, f, args...)  typed_mem_asprintf(type, r, f , ## args)
                    105: #define VASPRINTF(type, r, f, va)      typed_mem_vasprintf(type, r, f, va)
                    106: #endif
                    107: 
                    108: /*
                    109:  * If TYPED_MEM_UNDEFINE_ORIGINALS is defined, then the including file
                    110:  * wants us to prevent it from using any of the original libc functions.
                    111:  */
                    112: #ifdef TYPED_MEM_UNDEFINE_ORIGINALS
                    113: #undef malloc
                    114: #define malloc         %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    115: #undef calloc
                    116: #define calloc         %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    117: #undef realloc
                    118: #define realloc                %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    119: #undef reallocf
                    120: #define reallocf       %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    121: #undef free
                    122: #define free           %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    123: #undef strdup
                    124: #define strdup         %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    125: #undef asprintf
                    126: #define asprintf       %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    127: #undef vasprintf
                    128: #define vasprintf      %%%_USE_TYPED_MEM_MACROS_INSTEAD_%%%
                    129: #endif
                    130: 
                    131: __BEGIN_DECLS
                    132: 
                    133: /*
                    134:  * Typed memory allocation and freeing routines
                    135:  */
                    136: #if TYPED_MEM_TRACE
                    137: extern void    *typed_mem_realloc(const char *file, u_int line,
                    138:                        const char *type, void *mem, size_t size);
                    139: extern void    *typed_mem_reallocf(const char *file, u_int line,
                    140:                        const char *type, void *mem, size_t size);
                    141: extern void    *typed_mem_calloc(const char *file, u_int line,
                    142:                        const char *type, size_t num, size_t size);
                    143: extern char    *typed_mem_strdup(const char *file, u_int line,
                    144:                        const char *type, const char *string);
                    145: extern void    typed_mem_free(const char *file, u_int line,
                    146:                        const char *type, void *mem);
                    147: extern int     typed_mem_asprintf(const char *file, u_int line,
                    148:                        const char *type, char **ret, const char *format, ...);
                    149: extern int     typed_mem_vasprintf(const char *file, u_int line,
                    150:                        const char *type, char **ret,
                    151:                        const char *format, va_list va);
                    152: #else
                    153: extern void    *typed_mem_realloc(const char *type, void *mem, size_t size);
                    154: extern void    *typed_mem_reallocf(const char *type, void *mem, size_t size);
                    155: extern void    *typed_mem_calloc(const char *type, size_t num, size_t size);
                    156: extern char    *typed_mem_strdup(const char *type, const char *string);
                    157: extern void    typed_mem_free(const char *type, void *mem);
                    158: extern int     typed_mem_asprintf(const char *type,
                    159:                        char **ret, const char *format, ...);
                    160: extern int     typed_mem_vasprintf(const char *type, char **ret,
                    161:                        const char *format, va_list va);
                    162: #endif
                    163: 
                    164: /* Typed memory must be enabled by calling this function before any ops */
                    165: extern int     typed_mem_enable(void);
                    166: 
                    167: /* Typed statistics routines */
                    168: extern char    *typed_mem_type(const void *mem, char *typebuf);
                    169: extern int     typed_mem_usage(struct typed_mem_stats *stats);
                    170: #ifndef _KERNEL
                    171: extern void    typed_mem_dump(FILE *fp);
                    172: #endif
                    173: 
                    174: /* Structs type for 'struct typed_mem_stats' */
                    175: extern const   struct structs_type typed_mem_stats_type;
                    176: 
                    177: __END_DECLS
                    178: 
                    179: #endif /* _PDEL_UTIL_TYPED_MEM_H_ */
                    180: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>