Change wording ot YAZ license the 'Revised BSD License'. YAZ has used a
[yaz-moved-to-github.git] / include / yaz / nmem.h
1 /*
2  * Copyright (c) 1995-2006, 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 /* $Id: nmem.h,v 1.22 2006-10-09 21:02:41 adam Exp $ */
28
29 /**
30  * \file nmem.h
31  * \brief Header for Nibble Memory functions
32  *
33  * This is a simple and fairly wasteful little module for nibble memory
34  * allocation. Evemtually we'll put in something better.
35  */
36 #ifndef NMEM_H
37 #define NMEM_H
38
39 #include <stddef.h>
40 #include <yaz/yconfig.h>
41
42 #if YAZ_HAVE_XML2
43 #include <libxml/parser.h>
44 #endif
45
46 #define NMEM_DEBUG 0
47
48 #ifndef NMEM_DEBUG
49 #define NMEM_DEBUG 0
50 #endif
51
52 YAZ_BEGIN_CDECL
53
54 /** \brief NMEM/YAZ MUTEX opaque pointer */
55 typedef struct nmem_mutex *NMEM_MUTEX;
56 /** \brief create Mutex */
57 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *);
58 /** \brief enter critical section / AKA lock */
59 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX);
60 /** \brief leave critical section / AKA unlock */
61 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX);
62 /** \brief destroy MUTEX */
63 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *);
64
65 /** \brief NMEM handle (an opaque pointer to memory) */
66 typedef struct nmem_control *NMEM;
67
68 /** \brief release all memory associaged with an NMEM handle */
69 YAZ_EXPORT void nmem_reset(NMEM n);
70 /** \brief returns size in bytes of memory for NMEM handle */
71 YAZ_EXPORT int nmem_total(NMEM n);
72
73 /** \brief allocates string on NMEM handle (similar strdup) */
74 YAZ_EXPORT char *nmem_strdup (NMEM mem, const char *src);
75 /** \brief allocates string on NMEM handle - allows NULL ptr buffer */
76 YAZ_EXPORT char *nmem_strdup_null (NMEM mem, const char *src);
77 /** \brief allocates string of certain size on NMEM handle */
78 YAZ_EXPORT char *nmem_strdupn (NMEM mem, const char *src, size_t n);
79
80 /** \brief allocates sub strings out of string using certain delimitors
81     \param nmem NMEM handle
82     \param delim delimitor chars (splits on each char in there) 
83     \param dstr string to be split
84     \param darray result string array for each sub string
85     \param num number of result strings
86 */
87 YAZ_EXPORT void nmem_strsplit(NMEM nmem, const char *delim,
88                               const char *dstr,
89                               char ***darray, int *num);
90
91 /** \brief splits string into sub strings delimited by blanks
92     \param nmem NMEM handle
93     \param dstr string to be split
94     \param darray result string array for each sub string
95     \param num number of result strings
96 */
97 YAZ_EXPORT void nmem_strsplit_blank(NMEM nmem, const char *dstr,
98                                     char ***darray, int *num);
99
100 #if YAZ_HAVE_XML2
101 /** \brief copies TEXT Libxml2 node data to NMEM */
102 YAZ_EXPORT char *nmem_text_node_cdata(const xmlNode *ptr, NMEM nmem);
103 #endif
104
105 /** \brief creates and allocates integer for NMEM */
106 YAZ_EXPORT int *nmem_intdup (NMEM mem, int v);
107
108 /** \brief transfers memory from one NMEM handle to another  */
109 YAZ_EXPORT void nmem_transfer (NMEM dst, NMEM src);
110
111 /** \brief internal (do not use) */
112 YAZ_EXPORT void nmem_critical_enter (void);
113 /** \brief internal (do not use) */
114 YAZ_EXPORT void nmem_critical_leave (void);
115
116 #if NMEM_DEBUG
117
118 YAZ_EXPORT NMEM nmem_create_f(const char *file, int line);
119 YAZ_EXPORT void nmem_destroy_f(const char *file, int line, NMEM n);
120 YAZ_EXPORT void *nmem_malloc_f(const char *file, int line, NMEM n, int size);
121 #define nmem_create() nmem_create_f(__FILE__, __LINE__)
122 #define nmem_destroy(x) nmem_destroy_f(__FILE__, __LINE__, (x))
123 #define nmem_malloc(x, y) nmem_malloc_f(__FILE__, __LINE__, (x), (y))
124
125 YAZ_EXPORT void nmem_print_list (void);
126 YAZ_EXPORT void nmem_print_list_l (int level);
127
128 #else
129
130 /** \brief returns new NMEM handle */
131 YAZ_EXPORT NMEM nmem_create(void);
132
133 /** \brief destroys NMEM handle and memory associated with it */
134 YAZ_EXPORT void nmem_destroy(NMEM n);
135
136 /** \brief allocate memory block on NMEM handle */
137 YAZ_EXPORT void *nmem_malloc(NMEM n, int size);
138
139 #define nmem_print_list()
140
141 #endif
142
143 /** \brief initializes NMEM system
144     This function increments a usage counter for NMEM.. Only
145     on first usage the system is initialized.. The \fn nmem_exit
146     decrements the counter. So these must be called in pairs
147 */
148 YAZ_EXPORT void nmem_init (void);
149
150 /** \brief destroys NMEM system */
151 YAZ_EXPORT void nmem_exit (void);
152
153 YAZ_EXPORT int yaz_errno (void);
154 YAZ_EXPORT void yaz_set_errno (int v);
155 YAZ_EXPORT void yaz_strerror(char *buf, int max);
156
157 /** \brief returns memory in use (by application) 
158     \param p pointer to size (in bytes)
159  */
160 YAZ_EXPORT void nmem_get_memory_in_use(size_t *p);
161 /** \brief returns memory in free (for later reuse) 
162  */
163 YAZ_EXPORT void nmem_get_memory_free(size_t *p);
164
165 YAZ_END_CDECL
166
167 #endif
168 /*
169  * Local variables:
170  * c-basic-offset: 4
171  * indent-tabs-mode: nil
172  * End:
173  * vim: shiftwidth=4 tabstop=8 expandtab
174  */
175