Dont enable 64-bit on Windows
[yaz-moved-to-github.git] / include / yaz / nmem.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 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
30  * \brief Header for Nibble Memory functions
31  *
32  * This is a simple and fairly wasteful little module for nibble memory
33  * allocation. Eventually we'll put in something better.
34  */
35 #ifndef NMEM_H
36 #define NMEM_H
37
38 #include <stddef.h>
39 #include <yaz/yconfig.h>
40
41 YAZ_BEGIN_CDECL
42
43 /** \brief NMEM handle (an opaque pointer to memory) */
44 typedef struct nmem_control *NMEM;
45
46 #ifdef _MSC_VER
47 #define NMEM_64 0
48 #endif
49
50 /** \brief Set to 1 if YAZ BER integer is 64-bit ; 0 otherwise */
51 #ifndef NMEM_64
52 #define NMEM_64 1
53 #endif
54
55 #if NMEM_64
56 /** \brief BER/utility integer (64-bit or more) */
57 typedef long long int nmem_int_t;
58 /** \brief printf format for nmem_int_t type */
59 #define NMEM_INT_PRINTF "%lld"
60 #else
61 /** \brief BER/utility integer (32-bit on most platforms) */
62 typedef int nmem_int_t;
63 /** \brief printf format for nmem_int_t type */
64 #define NMEM_INT_PRINTF "%d"
65 #endif
66
67 /** \brief BER/utility boolean */
68 typedef int nmem_bool_t;
69
70 /** \brief releases memory associaged with an NMEM handle 
71     \param n NMEM handle
72 */
73 YAZ_EXPORT void nmem_reset(NMEM n);
74
75 /** \brief returns size in bytes of memory for NMEM handle
76     \returns number of bytes
77  */
78 YAZ_EXPORT int nmem_total(NMEM n);
79
80 /** \brief allocates string on NMEM handle (similar strdup) 
81     \param mem HNEM handle
82     \param src string
83     \returns duplicated string
84  */
85 YAZ_EXPORT char *nmem_strdup(NMEM mem, const char *src);
86 /** \brief allocates string on NMEM handle - allows NULL ptr buffer
87     \param mem HNEM handle
88     \param src string
89     \returns duplicated string or NULL if src was NULL
90  */
91 YAZ_EXPORT char *nmem_strdup_null(NMEM mem, const char *src);
92
93 /** \brief allocates string of certain size on NMEM handle
94     \param mem NMEM handle
95     \param src string
96     \param n size of string
97     \returns duplicated string (0 terminated)
98  */
99 YAZ_EXPORT char *nmem_strdupn(NMEM mem, const char *src, size_t n);
100
101 /** \brief allocates sub strings out of string using certain delimitors
102     \param nmem NMEM handle
103     \param delim delimitor chars (splits on each char in there) 
104     \param dstr string to be split
105     \param darray result string array for each sub string
106     \param num number of result strings
107 */
108 YAZ_EXPORT void nmem_strsplit(NMEM nmem, const char *delim,
109                               const char *dstr,
110                               char ***darray, int *num);
111
112 /** \brief splits string into sub strings delimited by blanks
113     \param nmem NMEM handle
114     \param dstr string to be split
115     \param darray result string array for each sub string
116     \param num number of result strings
117 */
118 YAZ_EXPORT void nmem_strsplit_blank(NMEM nmem, const char *dstr,
119                                     char ***darray, int *num);
120
121 /** \brief allocates and sets integer for NMEM
122     \param nmem NMEM handle
123     \param v integer value
124     \returns pointer to created integer
125 */
126 YAZ_EXPORT nmem_int_t *nmem_intdup(NMEM nmem, nmem_int_t v);
127
128 /** \brief allocates and sets boolean for NMEM
129     \param nmem NMEM handle
130     \param v value (0=false, != 0 true)
131     \returns pointer to created boolean
132 */
133 YAZ_EXPORT nmem_bool_t *nmem_booldup(NMEM nmem, nmem_bool_t v);
134
135 /** \brief transfers memory from one NMEM handle to another
136     \param src source NMEM handle
137     \param dst destination NMEM handle
138  */
139 YAZ_EXPORT void nmem_transfer(NMEM dst, NMEM src);
140
141 /** \brief returns new NMEM handle 
142     \returns NMEM handle
143  */
144 YAZ_EXPORT NMEM nmem_create(void);
145
146 /** \brief destroys NMEM handle and memory associated with it
147     \param n NMEM handle
148  */
149 YAZ_EXPORT void nmem_destroy(NMEM n);
150
151 /** \brief allocates memory block on NMEM handle
152     \param n NMEM handle
153     \param size number of bytes to be allocated
154     \returns pointer to allocated memory
155  */
156 YAZ_EXPORT void *nmem_malloc(NMEM n, int size);
157
158 YAZ_END_CDECL
159
160 #endif
161 /*
162  * Local variables:
163  * c-basic-offset: 4
164  * c-file-style: "Stroustrup"
165  * indent-tabs-mode: nil
166  * End:
167  * vim: shiftwidth=4 tabstop=8 expandtab
168  */
169