1 /* $NetBSD: prop_object.c,v 1.26 2009/03/30 07:42:51 haad Exp $ */
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <libprop/prop_object.h>
33 #include "prop_object_impl.h"
35 #if !defined(_KERNEL) && !defined(_STANDALONE)
44 /* XXX: netbsd's _nv stuff effectively does addfetch, not fetchadd */
45 #define atomic_inc_32_nv(x) (atomic_fetchadd_int(x, 1)+1)
46 #define atomic_dec_32_nv(x) (atomic_fetchadd_int(x, -1)-1)
47 #define atomic_inc_32(x) atomic_add_int(x, 1)
48 #define atomic_dec_32(x) atomic_add_int(x, 1)
50 #include <machine/atomic.h>
54 _prop_standalone_calloc(size_t size
)
66 _prop_standalone_realloc(void *v
, size_t size
)
72 memcpy(rv
, v
, size
); /* XXX */
73 dealloc(v
, 0); /* XXX */
78 #endif /* _STANDALONE */
81 * _prop_object_init --
82 * Initialize an object. Called when sub-classes create
86 _prop_object_init(struct _prop_object
*po
, const struct _prop_object_type
*pot
)
94 * _prop_object_fini --
95 * Finalize an object. Called when sub-classes destroy
100 _prop_object_fini(struct _prop_object
*po _PROP_ARG_UNUSED
)
102 /* Nothing to do, currently. */
106 * _prop_object_externalize_start_tag --
107 * Append an XML-style start tag to the externalize buffer.
110 _prop_object_externalize_start_tag(
111 struct _prop_object_externalize_context
*ctx
, const char *tag
)
115 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
116 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
119 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
120 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
121 _prop_object_externalize_append_char(ctx
, '>') == false)
128 * _prop_object_externalize_end_tag --
129 * Append an XML-style end tag to the externalize buffer.
132 _prop_object_externalize_end_tag(
133 struct _prop_object_externalize_context
*ctx
, const char *tag
)
136 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
137 _prop_object_externalize_append_char(ctx
, '/') == false ||
138 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
139 _prop_object_externalize_append_char(ctx
, '>') == false ||
140 _prop_object_externalize_append_char(ctx
, '\n') == false)
147 * _prop_object_externalize_empty_tag --
148 * Append an XML-style empty tag to the externalize buffer.
151 _prop_object_externalize_empty_tag(
152 struct _prop_object_externalize_context
*ctx
, const char *tag
)
156 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
157 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
161 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
162 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
163 _prop_object_externalize_append_char(ctx
, '/') == false ||
164 _prop_object_externalize_append_char(ctx
, '>') == false ||
165 _prop_object_externalize_append_char(ctx
, '\n') == false)
172 * _prop_object_externalize_append_cstring --
173 * Append a C string to the externalize buffer.
176 _prop_object_externalize_append_cstring(
177 struct _prop_object_externalize_context
*ctx
, const char *cp
)
180 while (*cp
!= '\0') {
181 if (_prop_object_externalize_append_char(ctx
,
182 (unsigned char) *cp
) == false)
191 * _prop_object_externalize_append_encoded_cstring --
192 * Append an encoded C string to the externalize buffer.
195 _prop_object_externalize_append_encoded_cstring(
196 struct _prop_object_externalize_context
*ctx
, const char *cp
)
199 while (*cp
!= '\0') {
202 if (_prop_object_externalize_append_cstring(ctx
,
207 if (_prop_object_externalize_append_cstring(ctx
,
212 if (_prop_object_externalize_append_cstring(ctx
,
217 if (_prop_object_externalize_append_char(ctx
,
218 (unsigned char) *cp
) == false)
228 #define BUF_EXPAND 256
231 * _prop_object_externalize_append_char --
232 * Append a single character to the externalize buffer.
235 _prop_object_externalize_append_char(
236 struct _prop_object_externalize_context
*ctx
, unsigned char c
)
239 _PROP_ASSERT(ctx
->poec_capacity
!= 0);
240 _PROP_ASSERT(ctx
->poec_buf
!= NULL
);
241 _PROP_ASSERT(ctx
->poec_len
<= ctx
->poec_capacity
);
243 if (ctx
->poec_len
== ctx
->poec_capacity
) {
244 char *cp
= _PROP_REALLOC(ctx
->poec_buf
,
245 ctx
->poec_capacity
+ BUF_EXPAND
,
249 ctx
->poec_capacity
= ctx
->poec_capacity
+ BUF_EXPAND
;
253 ctx
->poec_buf
[ctx
->poec_len
++] = c
;
259 * _prop_object_externalize_header --
260 * Append the standard XML header to the externalize buffer.
263 _prop_object_externalize_header(struct _prop_object_externalize_context
*ctx
)
265 static const char _plist_xml_header
[] =
266 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
267 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
269 if (_prop_object_externalize_append_cstring(ctx
,
270 _plist_xml_header
) == false ||
271 _prop_object_externalize_start_tag(ctx
,
272 "plist version=\"1.0\"") == false ||
273 _prop_object_externalize_append_char(ctx
, '\n') == false)
280 * _prop_object_externalize_footer --
281 * Append the standard XML footer to the externalize buffer. This
282 * also NUL-terminates the buffer.
285 _prop_object_externalize_footer(struct _prop_object_externalize_context
*ctx
)
288 if (_prop_object_externalize_end_tag(ctx
, "plist") == false ||
289 _prop_object_externalize_append_char(ctx
, '\0') == false)
296 * _prop_object_externalize_context_alloc --
297 * Allocate an externalize context.
299 struct _prop_object_externalize_context
*
300 _prop_object_externalize_context_alloc(void)
302 struct _prop_object_externalize_context
*ctx
;
304 ctx
= _PROP_MALLOC(sizeof(*ctx
), M_TEMP
);
306 ctx
->poec_buf
= _PROP_MALLOC(BUF_EXPAND
, M_TEMP
);
307 if (ctx
->poec_buf
== NULL
) {
308 _PROP_FREE(ctx
, M_TEMP
);
312 ctx
->poec_capacity
= BUF_EXPAND
;
319 * _prop_object_externalize_context_free --
320 * Free an externalize context.
323 _prop_object_externalize_context_free(
324 struct _prop_object_externalize_context
*ctx
)
327 /* Buffer is always freed by the caller. */
328 _PROP_FREE(ctx
, M_TEMP
);
332 * _prop_object_internalize_skip_comment --
333 * Skip the body and end tag of a comment.
336 _prop_object_internalize_skip_comment(
337 struct _prop_object_internalize_context
*ctx
)
339 const char *cp
= ctx
->poic_cp
;
341 while (!_PROP_EOF(*cp
)) {
345 ctx
->poic_cp
= cp
+ 3;
351 return (false); /* ran out of buffer */
355 * _prop_object_internalize_find_tag --
356 * Find the next tag in an XML stream. Optionally compare the found
357 * tag to an expected tag name. State of the context is undefined
358 * if this routine returns false. Upon success, the context points
359 * to the first octet after the tag.
362 _prop_object_internalize_find_tag(struct _prop_object_internalize_context
*ctx
,
363 const char *tag
, _prop_tag_type_t type
)
369 taglen
= strlen(tag
);
377 * Find the start of the tag.
379 while (_PROP_ISSPACE(*cp
))
387 ctx
->poic_tag_start
= cp
++;
392 if (cp
[1] != '-' || cp
[2] != '-')
395 * Comment block -- only allowed if we are allowed to
396 * return a start tag.
398 if (type
== _PROP_TAG_TYPE_END
)
400 ctx
->poic_cp
= cp
+ 3;
401 if (_prop_object_internalize_skip_comment(ctx
) == false)
407 if (type
!= _PROP_TAG_TYPE_END
&&
408 type
!= _PROP_TAG_TYPE_EITHER
)
413 ctx
->poic_tag_type
= _PROP_TAG_TYPE_END
;
415 if (type
!= _PROP_TAG_TYPE_START
&&
416 type
!= _PROP_TAG_TYPE_EITHER
)
418 ctx
->poic_tag_type
= _PROP_TAG_TYPE_START
;
421 ctx
->poic_tagname
= cp
;
423 while (!_PROP_ISSPACE(*cp
) && *cp
!= '/' && *cp
!= '>')
428 ctx
->poic_tagname_len
= cp
- ctx
->poic_tagname
;
430 /* Make sure this is the tag we're looking for. */
432 (taglen
!= ctx
->poic_tagname_len
||
433 memcmp(tag
, ctx
->poic_tagname
, taglen
) != 0))
436 /* Check for empty tag. */
438 if (ctx
->poic_tag_type
!= _PROP_TAG_TYPE_START
)
439 return(false); /* only valid on start tags */
440 ctx
->poic_is_empty_element
= true;
442 if (_PROP_EOF(*cp
) || *cp
!= '>')
445 ctx
->poic_is_empty_element
= false;
447 /* Easy case of no arguments. */
449 ctx
->poic_tagattr
= NULL
;
450 ctx
->poic_tagattr_len
= 0;
451 ctx
->poic_tagattrval
= NULL
;
452 ctx
->poic_tagattrval_len
= 0;
453 ctx
->poic_cp
= cp
+ 1;
457 _PROP_ASSERT(!_PROP_EOF(*cp
));
462 while (_PROP_ISSPACE(*cp
))
467 ctx
->poic_tagattr
= cp
;
469 while (!_PROP_ISSPACE(*cp
) && *cp
!= '=')
474 ctx
->poic_tagattr_len
= cp
- ctx
->poic_tagattr
;
483 ctx
->poic_tagattrval
= cp
;
488 ctx
->poic_tagattrval_len
= cp
- ctx
->poic_tagattrval
;
494 ctx
->poic_cp
= cp
+ 1;
499 * _prop_object_internalize_decode_string --
500 * Decode an encoded string.
503 _prop_object_internalize_decode_string(
504 struct _prop_object_internalize_context
*ctx
,
505 char *target
, size_t targsize
, size_t *sizep
,
522 if ((c
= *src
) == '&') {
529 } else if (src
[1] == 'l' &&
534 } else if (src
[1] == 'g' &&
539 } else if (src
[1] == 'a' &&
546 } else if (src
[1] == 'q' &&
558 if (tarindex
>= targsize
)
560 target
[tarindex
] = c
;
565 _PROP_ASSERT(*src
== '<');
575 * _prop_object_internalize_match --
576 * Returns true if the two character streams match.
579 _prop_object_internalize_match(const char *str1
, size_t len1
,
580 const char *str2
, size_t len2
)
583 return (len1
== len2
&& memcmp(str1
, str2
, len1
) == 0);
586 #define INTERNALIZER(t, f) \
587 { t, sizeof(t) - 1, f }
589 static const struct _prop_object_internalizer
{
592 prop_object_internalizer_t poi_intern
;
593 } _prop_object_internalizer_table
[] = {
594 INTERNALIZER("array", _prop_array_internalize
),
596 INTERNALIZER("true", _prop_bool_internalize
),
597 INTERNALIZER("false", _prop_bool_internalize
),
599 INTERNALIZER("data", _prop_data_internalize
),
601 INTERNALIZER("dict", _prop_dictionary_internalize
),
603 INTERNALIZER("integer", _prop_number_internalize
),
605 INTERNALIZER("string", _prop_string_internalize
),
613 * _prop_object_internalize_by_tag --
614 * Determine the object type from the tag in the context and
618 _prop_object_internalize_by_tag(struct _prop_object_internalize_context
*ctx
)
620 const struct _prop_object_internalizer
*poi
;
621 prop_object_t obj
, parent_obj
;
623 prop_object_internalizer_continue_t iter_func
;
624 struct _prop_stack stack
;
626 _prop_stack_init(&stack
);
629 for (poi
= _prop_object_internalizer_table
;
630 poi
->poi_tag
!= NULL
; poi
++) {
631 if (_prop_object_internalize_match(ctx
->poic_tagname
,
632 ctx
->poic_tagname_len
,
637 if ((poi
== NULL
) || (poi
->poi_tag
== NULL
)) {
638 while (_prop_stack_pop(&stack
, &obj
, &iter
, &data
, NULL
)) {
639 iter_func
= (prop_object_internalizer_continue_t
)iter
;
640 (*iter_func
)(&stack
, &obj
, ctx
, data
, NULL
);
647 if (!(*poi
->poi_intern
)(&stack
, &obj
, ctx
))
651 while (_prop_stack_pop(&stack
, &parent_obj
, &iter
, &data
, NULL
)) {
652 iter_func
= (prop_object_internalizer_continue_t
)iter
;
653 if (!(*iter_func
)(&stack
, &parent_obj
, ctx
, data
, obj
))
662 _prop_generic_internalize(const char *xml
, const char *master_tag
)
664 prop_object_t obj
= NULL
;
665 struct _prop_object_internalize_context
*ctx
;
667 ctx
= _prop_object_internalize_context_alloc(xml
);
671 /* We start with a <plist> tag. */
672 if (_prop_object_internalize_find_tag(ctx
, "plist",
673 _PROP_TAG_TYPE_START
) == false)
676 /* Plist elements cannot be empty. */
677 if (ctx
->poic_is_empty_element
)
681 * We don't understand any plist attributes, but Apple XML
682 * property lists often have a "version" attribute. If we
683 * see that one, we simply ignore it.
685 if (ctx
->poic_tagattr
!= NULL
&&
686 !_PROP_TAGATTR_MATCH(ctx
, "version"))
689 /* Next we expect to see opening master_tag. */
690 if (_prop_object_internalize_find_tag(ctx
, master_tag
,
691 _PROP_TAG_TYPE_START
) == false)
694 obj
= _prop_object_internalize_by_tag(ctx
);
699 * We've advanced past the closing master_tag.
700 * Now we want </plist>.
702 if (_prop_object_internalize_find_tag(ctx
, "plist",
703 _PROP_TAG_TYPE_END
) == false) {
704 prop_object_release(obj
);
709 _prop_object_internalize_context_free(ctx
);
714 * _prop_object_internalize_context_alloc --
715 * Allocate an internalize context.
717 struct _prop_object_internalize_context
*
718 _prop_object_internalize_context_alloc(const char *xml
)
720 struct _prop_object_internalize_context
*ctx
;
722 ctx
= _PROP_MALLOC(sizeof(struct _prop_object_internalize_context
),
727 ctx
->poic_xml
= ctx
->poic_cp
= xml
;
730 * Skip any whitespace and XML preamble stuff that we don't
731 * know about / care about.
734 while (_PROP_ISSPACE(*xml
))
736 if (_PROP_EOF(*xml
) || *xml
!= '<')
739 #define MATCH(str) (memcmp(&xml[1], str, sizeof(str) - 1) == 0)
742 * Skip over the XML preamble that Apple XML property
743 * lists usually include at the top of the file.
745 if (MATCH("?xml ") ||
746 MATCH("!DOCTYPE plist")) {
747 while (*xml
!= '>' && !_PROP_EOF(*xml
))
751 xml
++; /* advance past the '>' */
756 ctx
->poic_cp
= xml
+ 4;
757 if (_prop_object_internalize_skip_comment(ctx
) == false)
766 * We don't think we should skip it, so let's hope we can
775 _PROP_FREE(ctx
, M_TEMP
);
780 * _prop_object_internalize_context_free --
781 * Free an internalize context.
784 _prop_object_internalize_context_free(
785 struct _prop_object_internalize_context
*ctx
)
788 _PROP_FREE(ctx
, M_TEMP
);
791 #if !defined(_KERNEL) && !defined(_STANDALONE)
793 * _prop_object_externalize_file_dirname --
794 * dirname(3), basically. We have to roll our own because the
795 * system dirname(3) isn't reentrant.
798 _prop_object_externalize_file_dirname(const char *path
, char *result
)
804 * If `path' is a NULL pointer or points to an empty string,
807 if (path
== NULL
|| *path
== '\0')
810 /* String trailing slashes, if any. */
811 lastp
= path
+ strlen(path
) - 1;
812 while (lastp
!= path
&& *lastp
== '/')
815 /* Terminate path at the last occurrence of '/'. */
818 /* Strip trailing slashes, if any. */
819 while (lastp
!= path
&& *lastp
== '/')
822 /* ...and copy the result into the result buffer. */
823 len
= (lastp
- path
) + 1 /* last char */;
824 if (len
> (PATH_MAX
- 1))
827 memcpy(result
, path
, len
);
831 } while (--lastp
>= path
);
833 /* No /'s found, return ".". */
839 * _prop_object_externalize_write_file --
840 * Write an externalized dictionary to the specified file.
841 * The file is written atomically from the caller's perspective,
842 * and the mode set to 0666 modified by the caller's umask.
845 _prop_object_externalize_write_file(const char *fname
, const char *xml
,
848 char tname
[PATH_MAX
];
853 if (len
> SSIZE_MAX
) {
859 * Get the directory name where the file is to be written
860 * and create the temporary file.
862 _prop_object_externalize_file_dirname(fname
, tname
);
863 if (strlcat(tname
, "/.plistXXXXXX", sizeof(tname
)) >= sizeof(tname
)) {
864 errno
= ENAMETOOLONG
;
867 if ((fd
= mkstemp(tname
)) == -1)
870 if (write(fd
, xml
, len
) != (ssize_t
)len
)
877 (void)umask(myumask
);
878 if (fchmod(fd
, 0666 & ~myumask
) == -1)
884 if (rename(tname
, fname
) == -1)
893 (void) unlink(tname
);
899 * _prop_object_internalize_map_file --
900 * Map a file for the purpose of internalizing it.
902 struct _prop_object_internalize_mapped_file
*
903 _prop_object_internalize_map_file(const char *fname
)
906 struct _prop_object_internalize_mapped_file
*mf
;
907 size_t pgsize
= (size_t)sysconf(_SC_PAGESIZE
);
908 size_t pgmask
= pgsize
- 1;
909 bool need_guard
= false;
912 mf
= _PROP_MALLOC(sizeof(*mf
), M_TEMP
);
916 fd
= open(fname
, O_RDONLY
, 0400);
918 _PROP_FREE(mf
, M_TEMP
);
922 if (fstat(fd
, &sb
) == -1) {
924 _PROP_FREE(mf
, M_TEMP
);
927 mf
->poimf_mapsize
= ((size_t)sb
.st_size
+ pgmask
) & ~pgmask
;
928 if (mf
->poimf_mapsize
< (size_t)sb
.st_size
) {
930 _PROP_FREE(mf
, M_TEMP
);
935 * If the file length is an integral number of pages, then we
936 * need to map a guard page at the end in order to provide the
937 * necessary NUL-termination of the buffer.
939 if ((sb
.st_size
& pgmask
) == 0)
942 mf
->poimf_xml
= mmap(NULL
, need_guard
? mf
->poimf_mapsize
+ pgsize
944 PROT_READ
, MAP_FILE
|MAP_SHARED
, fd
, (off_t
)0);
946 if (mf
->poimf_xml
== MAP_FAILED
) {
947 _PROP_FREE(mf
, M_TEMP
);
950 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_SEQUENTIAL
);
953 if (mmap(mf
->poimf_xml
+ mf
->poimf_mapsize
,
955 MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
, -1,
956 (off_t
)0) == MAP_FAILED
) {
957 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
958 _PROP_FREE(mf
, M_TEMP
);
961 mf
->poimf_mapsize
+= pgsize
;
968 * _prop_object_internalize_unmap_file --
969 * Unmap a file previously mapped for internalizing.
972 _prop_object_internalize_unmap_file(
973 struct _prop_object_internalize_mapped_file
*mf
)
976 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_DONTNEED
);
977 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
978 _PROP_FREE(mf
, M_TEMP
);
980 #endif /* !_KERNEL && !_STANDALONE */
983 * prop_object_retain --
984 * Increment the reference count on an object.
987 prop_object_retain(prop_object_t obj
)
989 struct _prop_object
*po
= obj
;
992 ncnt
= atomic_inc_32_nv(&po
->po_refcnt
);
993 _PROP_ASSERT(ncnt
!= 0);
997 * prop_object_release_emergency
998 * A direct free with prop_object_release failed.
999 * Walk down the tree until a leaf is found and
1000 * free that. Do not recurse to avoid stack overflows.
1002 * This is a slow edge condition, but necessary to
1003 * guarantee that an object can always be freed.
1006 prop_object_release_emergency(prop_object_t obj
)
1008 struct _prop_object
*po
;
1009 void (*unlock
)(void);
1010 prop_object_t parent
= NULL
;
1017 if (po
->po_type
->pot_lock
!= NULL
)
1018 po
->po_type
->pot_lock();
1020 /* Save pointerto unlock function */
1021 unlock
= po
->po_type
->pot_unlock
;
1023 /* Dance a bit to make sure we always get the non-racy ocnt */
1024 ocnt
= atomic_dec_32_nv(&po
->po_refcnt
);
1026 _PROP_ASSERT(ocnt
!= 0);
1034 _PROP_ASSERT(po
->po_type
);
1035 if ((po
->po_type
->pot_free
)(NULL
, &obj
) ==
1036 _PROP_OBJECT_FREE_DONE
) {
1046 atomic_inc_32(&po
->po_refcnt
);
1048 _PROP_ASSERT(parent
);
1049 /* One object was just freed. */
1051 (*po
->po_type
->pot_emergency_free
)(parent
);
1055 * prop_object_release --
1056 * Decrement the reference count on an object.
1058 * Free the object if we are releasing the final
1062 prop_object_release(prop_object_t obj
)
1064 struct _prop_object
*po
;
1065 struct _prop_stack stack
;
1066 void (*unlock
)(void);
1070 _prop_stack_init(&stack
);
1077 if (po
->po_type
->pot_lock
!= NULL
)
1078 po
->po_type
->pot_lock();
1080 /* Save pointer to object unlock function */
1081 unlock
= po
->po_type
->pot_unlock
;
1083 ocnt
= atomic_dec_32_nv(&po
->po_refcnt
);
1085 _PROP_ASSERT(ocnt
!= 0);
1094 ret
= (po
->po_type
->pot_free
)(&stack
, &obj
);
1099 if (ret
== _PROP_OBJECT_FREE_DONE
)
1102 atomic_inc_32(&po
->po_refcnt
);
1103 } while (ret
== _PROP_OBJECT_FREE_RECURSE
);
1104 if (ret
== _PROP_OBJECT_FREE_FAILED
)
1105 prop_object_release_emergency(obj
);
1106 } while (_prop_stack_pop(&stack
, &obj
, NULL
, NULL
, NULL
));
1110 * prop_object_type --
1111 * Return the type of an object.
1114 prop_object_type(prop_object_t obj
)
1116 struct _prop_object
*po
= obj
;
1119 return (PROP_TYPE_UNKNOWN
);
1121 return (po
->po_type
->pot_type
);
1125 * prop_object_equals --
1126 * Returns true if thw two objects are equivalent.
1129 prop_object_equals(prop_object_t obj1
, prop_object_t obj2
)
1131 return (prop_object_equals_with_error(obj1
, obj2
, NULL
));
1135 prop_object_equals_with_error(prop_object_t obj1
, prop_object_t obj2
,
1138 struct _prop_object
*po1
;
1139 struct _prop_object
*po2
;
1140 void *stored_pointer1
, *stored_pointer2
;
1141 prop_object_t next_obj1
, next_obj2
;
1142 struct _prop_stack stack
;
1143 _prop_object_equals_rv_t ret
;
1145 _prop_stack_init(&stack
);
1147 *error_flag
= false;
1150 stored_pointer1
= NULL
;
1151 stored_pointer2
= NULL
;
1155 if (po1
->po_type
!= po2
->po_type
)
1159 ret
= (*po1
->po_type
->pot_equals
)(obj1
, obj2
,
1160 &stored_pointer1
, &stored_pointer2
,
1161 &next_obj1
, &next_obj2
);
1162 if (ret
== _PROP_OBJECT_EQUALS_FALSE
)
1164 if (ret
== _PROP_OBJECT_EQUALS_TRUE
) {
1165 if (!_prop_stack_pop(&stack
, &obj1
, &obj2
,
1166 &stored_pointer1
, &stored_pointer2
))
1170 goto continue_subtree
;
1172 _PROP_ASSERT(ret
== _PROP_OBJECT_EQUALS_RECURSE
);
1174 if (!_prop_stack_push(&stack
, obj1
, obj2
,
1175 stored_pointer1
, stored_pointer2
)) {
1185 while (_prop_stack_pop(&stack
, &obj1
, &obj2
, NULL
, NULL
)) {
1187 (*po1
->po_type
->pot_equals_finish
)(obj1
, obj2
);
1193 * prop_object_iterator_next --
1194 * Return the next item during an iteration.
1197 prop_object_iterator_next(prop_object_iterator_t pi
)
1200 return ((*pi
->pi_next_object
)(pi
));
1204 * prop_object_iterator_reset --
1205 * Reset the iterator to the first object so as to restart
1209 prop_object_iterator_reset(prop_object_iterator_t pi
)
1212 (*pi
->pi_reset
)(pi
);
1216 * prop_object_iterator_release --
1217 * Release the object iterator.
1220 prop_object_iterator_release(prop_object_iterator_t pi
)
1223 prop_object_release(pi
->pi_obj
);
1224 _PROP_FREE(pi
, M_TEMP
);