1 /*-------------------------------------------------------------------------
2  * Copyright (C) 2000 Caldera Systems, Inc
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *    Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *    Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  *    Neither the name of Caldera Systems nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CALDERA
24  * SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *-------------------------------------------------------------------------*/
32 
33 /** Header file for debug memory allocator.
34  *
35  * @file       slp_xmalloc.h
36  * @author     Matthew Peterson, John Calcote (jcalcote@novell.com)
37  * @attention  Please submit patches to http://www.openslp.org
38  * @ingroup    CommonCodeXMalloc
39  */
40 
41 #ifndef SLP_XMALLOC_H_INCLUDED
42 #define SLP_XMALLOC_H_INCLUDED
43 
44 /*!@defgroup CommonCodeXMalloc Memory
45  * @ingroup CommonCodeDebug
46  * @{
47  */
48 
49 #include "slp_types.h"
50 
51 #ifdef DEBUG
52 
53 #include "slp_linkedlist.h"
54 
55 #define SLPXMALLOC_MAX_WHERE_LEN    256
56 #define SLPXMALLOC_MAX_BUF_LOG_LEN  32
57 
58 typedef struct xallocation
59 {
60    SLPListItem listitem;
61    char where[SLPXMALLOC_MAX_WHERE_LEN];
62    void * buf;
63    size_t size;
64 } xallocation_t;
65 
66 void * _xmalloc(const char * file, int line, size_t size);
67 void * _xcalloc(const char * file, int line, int numblks, size_t size);
68 void * _xrealloc(const char * file, int line, void * ptr, size_t size);
69 char * _xstrdup(const char * file, int line, const char * str);
70 void * _xmemdup(const char * file, int line, const void * ptr, size_t size);
71 void   _xfree(const char * file, int line, void * ptr);
72 
73 int xmalloc_init(const char * filename, size_t freemem);
74 int xmalloc_report(void);
75 void xmalloc_deinit(void);
76 
77 #define xmalloc(s)      _xmalloc(__FILE__,__LINE__,(s))
78 #define xcalloc(n,s)    _xcalloc(__FILE__,__LINE__,(n),(s))
79 #define xrealloc(p,s)   _xrealloc(__FILE__,__LINE__,(p),(s))
80 #define xfree(p)        _xfree(__FILE__,__LINE__,(p))
81 #define xstrdup(p)      _xstrdup(__FILE__,__LINE__,(p))
82 #define xmemdup(p,s)    _xmemdup(__FILE__,__LINE__,(p),(s))
83 
84 #else    /* ?DEBUG */
85 
86 void * _xmemdup(const void * ptr, size_t srclen);
87 
88 #define xmalloc   malloc
89 #define xcalloc   calloc
90 #define xrealloc  realloc
91 #define xfree     free
92 #define xstrdup   strdup
93 #define xmemdup   _xmemdup
94 
95 #endif   /* ?DEBUG */
96 
97 /*! @} */
98 
99 #endif   /* SLP_XMALLOC_H_INCLUDED */
100 
101 /*=========================================================================*/
102