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