record_render: base64 decoding of embedded records
[yaz-moved-to-github.git] / include / yaz / xmalloc.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /**
29  * \file xmalloc.h
30  * \brief Header for memory handling functions.
31  *
32  * This is a set of wrapper functions for the memory allocation routines
33  * from stdlib.h.. Such as malloc, free, realloc etc.
34  * These functions calls exit if memory allocation fails.
35  */
36
37 #ifndef XMALLOC_H
38 #define XMALLOC_H
39
40 #include <stddef.h>
41
42 #include <yaz/yconfig.h>
43
44 YAZ_BEGIN_CDECL
45
46 /** \brief utility macro which calls xrealloc_f */
47 #define xrealloc(o, x) xrealloc_f(o, x, __FILE__, __LINE__)
48 /** \brief utility macro which calls malloc_f */
49 #define xmalloc(x) xmalloc_f(x, __FILE__, __LINE__)
50 /** \brief utility macro which calls xcalloc_f */
51 #define xcalloc(x,y) xcalloc_f(x,y, __FILE__, __LINE__)
52 /** \brief utility macro which calls xfree_f */
53 #define xfree(x) xfree_f(x, __FILE__, __LINE__)
54 /** \brief utility macro which calls xstrdup_f */
55 #define xstrdup(s) xstrdup_f(s, __FILE__, __LINE__)
56 /** \brief utility macro which calls xstrndup_f */
57 #define xstrndup(s, n) xstrndup_f(s, n, __FILE__, __LINE__)
58 /** \brief utility macro which calls malloc_trav_f */
59 #define xmalloc_trav(s) xmalloc_trav_f(s, __FILE__, __LINE__)
60     
61 /** \brief realloc
62     \param o buffer to be reallocated
63     \param size size of buffer to be allocated
64     \param file fname location of use
65     \param line line location of use
66     \returns buffer
67
68     This function is invoked via macro xrealloc in which file and line are set
69     automatically.
70 */
71
72 YAZ_EXPORT void *xrealloc_f(void *o, size_t size, const char *file, int line);
73 /** \brief malloc
74     \param size size of buffer to be allocated
75     \param file fname location of use
76     \param line line location of use
77     \returns buffer
78
79     This function is invoked via macro xmalloc in which file and line are set
80     automatically.
81 */
82 YAZ_EXPORT void *xmalloc_f(size_t size, const char *file, int line);
83
84 /** \brief calloc
85     \param nmemb number of members
86     \param size size of member
87     \param file fname location of use
88     \param line line location of use
89     \returns buffer
90
91     This function is invoked via macro xcalloc in which file and line are set
92     automatically.
93 */
94 YAZ_EXPORT void *xcalloc_f(size_t nmemb, size_t size,
95                            const char *file, int line);
96 /** \brief strdup
97     \param p string to be cloned
98     \param file fname location of use
99     \param line line location of use
100     \returns resulting string
101
102     This function is invoked via macro xstrdup in which file and line are set
103     automatically.
104 */
105 YAZ_EXPORT char *xstrdup_f(const char *p, const char *file, int line);
106
107 /** \brief strndup
108     \param p string to be cloned
109     \param n max size of resulting string (excluding 0)
110     \param file fname location of use
111     \param line line location of use
112     \returns resulting string
113
114     This function is invoked via macro xstrndup in which file and line are set
115     automatically.
116 */
117 YAZ_EXPORT char *xstrndup_f(const char *p, size_t n,
118                             const char *file, int line);
119
120 /** \brief free
121     \param p string to be freed (might be NULL)
122     \param file fname location of use
123     \param line line location of use
124
125     This function is invoked via macro xfree in which file and line are set
126     automatically.
127 */
128 YAZ_EXPORT void xfree_f(void *p, const char *file, int line);
129
130 /** \brief logs all xmalloc buffers
131     \param s not used
132     \param file fname location of use
133     \param line line location of use
134
135     This function is invoked via macro xmalloc_trav in which file and line
136     are set automatically. Only if TRACE_XMALLOC > 1 this function
137     does any real work!
138 */
139 YAZ_EXPORT void xmalloc_trav_f(const char *s, const char *file, int line);
140
141 YAZ_END_CDECL
142
143 #endif
144 /*
145  * Local variables:
146  * c-basic-offset: 4
147  * c-file-style: "Stroustrup"
148  * indent-tabs-mode: nil
149  * End:
150  * vim: shiftwidth=4 tabstop=8 expandtab
151  */
152