prop_object_impl.h revision 1.4 1 /* $NetBSD: prop_object_impl.h,v 1.4 2006/08/21 04:13:28 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
40 #define _PROPLIB_PROP_OBJECT_IMPL_H_
41
42 struct _prop_object_externalize_context {
43 char * poec_buf; /* string buffer */
44 size_t poec_capacity; /* capacity of buffer */
45 size_t poec_len; /* current length of string */
46 unsigned int poec_depth; /* nesting depth */
47 };
48
49 boolean_t _prop_object_externalize_start_tag(
50 struct _prop_object_externalize_context *,
51 const char *);
52 boolean_t _prop_object_externalize_end_tag(
53 struct _prop_object_externalize_context *,
54 const char *);
55 boolean_t _prop_object_externalize_empty_tag(
56 struct _prop_object_externalize_context *,
57 const char *);
58 boolean_t _prop_object_externalize_append_cstring(
59 struct _prop_object_externalize_context *,
60 const char *);
61 boolean_t _prop_object_externalize_append_encoded_cstring(
62 struct _prop_object_externalize_context *,
63 const char *);
64 boolean_t _prop_object_externalize_append_char(
65 struct _prop_object_externalize_context *,
66 unsigned char);
67 boolean_t _prop_object_externalize_header(
68 struct _prop_object_externalize_context *);
69 boolean_t _prop_object_externalize_footer(
70 struct _prop_object_externalize_context *);
71
72 struct _prop_object_externalize_context *
73 _prop_object_externalize_context_alloc(void);
74 void _prop_object_externalize_context_free(
75 struct _prop_object_externalize_context *);
76
77 typedef enum {
78 _PROP_TAG_TYPE_START, /* e.g. <dict> */
79 _PROP_TAG_TYPE_END, /* e.g. </dict> */
80 _PROP_TAG_TYPE_EITHER
81 } _prop_tag_type_t;
82
83 struct _prop_object_internalize_context {
84 const char *poic_xml;
85 const char *poic_cp;
86
87 const char *poic_tag_start;
88
89 const char *poic_tagname;
90 size_t poic_tagname_len;
91 const char *poic_tagattr;
92 size_t poic_tagattr_len;
93 const char *poic_tagattrval;
94 size_t poic_tagattrval_len;
95
96 boolean_t poic_is_empty_element;
97 _prop_tag_type_t poic_tag_type;
98 };
99
100 #define _PROP_EOF(c) ((c) == '\0')
101 #define _PROP_ISSPACE(c) \
102 ((c) == ' ' || (c) == '\t' || (c) == '\n' || _PROP_EOF(c))
103
104 #define _PROP_TAG_MATCH(ctx, t) \
105 _prop_object_internalize_match((ctx)->poic_tagname, \
106 (ctx)->poic_tagname_len, \
107 (t), strlen(t))
108
109 #define _PROP_TAGATTR_MATCH(ctx, a) \
110 _prop_object_internalize_match((ctx)->poic_tagattr, \
111 (ctx)->poic_tagattr_len, \
112 (a), strlen(a))
113
114 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \
115 _prop_object_internalize_match((ctx)->poic_tagattrval, \
116 (ctx)->poic_tagattrval_len,\
117 (a), strlen(a))
118
119 boolean_t _prop_object_internalize_find_tag(
120 struct _prop_object_internalize_context *,
121 const char *, _prop_tag_type_t);
122 boolean_t _prop_object_internalize_match(const char *, size_t,
123 const char *, size_t);
124 prop_object_t _prop_object_internalize_by_tag(
125 struct _prop_object_internalize_context *);
126 boolean_t _prop_object_internalize_decode_string(
127 struct _prop_object_internalize_context *,
128 char *, size_t, size_t *, const char **);
129
130 struct _prop_object_internalize_context *
131 _prop_object_internalize_context_alloc(const char *);
132 void _prop_object_internalize_context_free(
133 struct _prop_object_internalize_context *);
134
135 #if !defined(_KERNEL) && !defined(_STANDALONE)
136 boolean_t _prop_object_externalize_write_file(const char *,
137 const char *, size_t);
138
139 struct _prop_object_internalize_mapped_file {
140 char * poimf_xml;
141 size_t poimf_mapsize;
142 };
143
144 struct _prop_object_internalize_mapped_file *
145 _prop_object_internalize_map_file(const char *);
146 void _prop_object_internalize_unmap_file(
147 struct _prop_object_internalize_mapped_file *);
148 #endif /* !_KERNEL && !_STANDALONE */
149
150 /* These are here because they're required by shared code. */
151 prop_object_t _prop_array_internalize(
152 struct _prop_object_internalize_context *);
153 prop_object_t _prop_bool_internalize(
154 struct _prop_object_internalize_context *);
155 prop_object_t _prop_data_internalize(
156 struct _prop_object_internalize_context *);
157 prop_object_t _prop_dictionary_internalize(
158 struct _prop_object_internalize_context *);
159 prop_object_t _prop_number_internalize(
160 struct _prop_object_internalize_context *);
161 prop_object_t _prop_string_internalize(
162 struct _prop_object_internalize_context *);
163
164 struct _prop_object_type {
165 uint32_t pot_type; /* type indicator */
166 void (*pot_free)(void *); /* func to free object */
167 boolean_t (*pot_extern) /* func to externalize object */
168 (struct _prop_object_externalize_context *,
169 void *);
170 boolean_t (*pot_equals) /* func to test quality */
171 (void *, void *);
172 };
173
174 struct _prop_object {
175 const struct _prop_object_type *po_type;/* type descriptor */
176 uint32_t po_refcnt; /* reference count */
177 };
178
179 void _prop_object_init(struct _prop_object *,
180 const struct _prop_object_type *);
181 void _prop_object_fini(struct _prop_object *);
182
183 struct _prop_object_iterator {
184 prop_object_t (*pi_next_object)(void *);
185 void (*pi_reset)(void *);
186 prop_object_t pi_obj;
187 uint32_t pi_version;
188 };
189
190 #if defined(_KERNEL)
191
192 /*
193 * proplib in the kernel...
194 */
195
196 #include <sys/malloc.h>
197 #include <sys/pool.h>
198 #include <sys/systm.h>
199 #include <sys/lock.h>
200
201 #define _PROP_ASSERT(x) KASSERT(x)
202
203 #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK)
204 #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO)
205 #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK)
206 #define _PROP_FREE(v, t) free((v), (t))
207
208 #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK)
209 #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v))
210
211 #define _PROP_POOL_INIT(p, s, d) \
212 POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr);
213
214 #define _PROP_MALLOC_DEFINE(t, s, l) \
215 MALLOC_DEFINE(t, s, l);
216
217 #define _PROP_MUTEX_DECL(x) \
218 static struct simplelock x = SIMPLELOCK_INITIALIZER;
219 #define _PROP_MUTEX_LOCK(x) simple_lock(&(x))
220 #define _PROP_MUTEX_UNLOCK(x) simple_unlock(&(x))
221
222 #elif defined(_STANDALONE)
223
224 /*
225 * proplib in a standalone environment...
226 */
227
228 #include <lib/libsa/stand.h>
229 #include <lib/libkern/libkern.h>
230
231 void * _prop_standalone_calloc(size_t);
232 void * _prop_standalone_realloc(void *, size_t);
233
234 #define _PROP_ASSERT(x) /* nothing */
235
236 #define _PROP_MALLOC(s, t) alloc((s))
237 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
238 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
239 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
240
241 #define _PROP_POOL_GET(p) alloc((p))
242 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
243
244 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
245
246 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
247
248 #define _PROP_MUTEX_DECL(x) /* nothing */
249 #define _PROP_MUTEX_LOCK(x) /* nothing */
250 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
251
252 #else
253
254 /*
255 * proplib in user space...
256 */
257
258 #include <assert.h>
259 #include <string.h>
260 #include <stdio.h>
261 #include <stdlib.h>
262
263 #define _PROP_ASSERT(x) assert(x)
264
265 #define _PROP_MALLOC(s, t) malloc((s))
266 #define _PROP_CALLOC(s, t) calloc(1, (s))
267 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
268 #define _PROP_FREE(v, t) free((v))
269
270 #define _PROP_POOL_GET(p) malloc((p))
271 #define _PROP_POOL_PUT(p, v) free((v))
272
273 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
274
275 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
276
277 #if defined(__NetBSD__) && defined(_LIBPROP)
278 /*
279 * Use the same mechanism as libc; we get pthread mutexes for threaded
280 * programs and do-nothing stubs for non-threaded programs.
281 */
282 #include "reentrant.h"
283 #define _PROP_MUTEX_DECL(x) static mutex_t x = MUTEX_INITIALIZER;
284 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
285 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
286 #elif defined(HAVE_NBTOOL_CONFIG_H)
287 /*
288 * None of NetBSD's build tools are multi-threaded.
289 */
290 #define _PROP_MUTEX_DECL(x) /* nothing */
291 #define _PROP_MUTEX_LOCK(x) /* nothing */
292 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
293 #else
294 /*
295 * Use pthread mutexes everywhere else.
296 */
297 #include <pthread.h>
298 #define _PROP_MUTEX_DECL(x) \
299 static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
300 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
301 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
302 #endif
303
304 #endif /* _KERNEL */
305
306 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
307