1 /* $NetBSD: prop_object.c,v 1.30 2015/05/12 14:59:35 christos 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 "prop_object_impl.h"
33 #include <prop/prop_object.h>
35 #ifdef _PROP_NEED_REFCNT_MTX
36 static pthread_mutex_t _prop_refcnt_mtx
= PTHREAD_MUTEX_INITIALIZER
;
37 #endif /* _PROP_NEED_REFCNT_MTX */
39 #if !defined(_KERNEL) && !defined(_STANDALONE)
48 #endif /* defined(__minix) */
53 _prop_standalone_calloc(size_t size
)
65 _prop_standalone_realloc(void *v
, size_t size
)
71 memcpy(rv
, v
, size
); /* XXX */
72 dealloc(v
, 0); /* XXX */
77 #endif /* _STANDALONE */
80 * _prop_object_init --
81 * Initialize an object. Called when sub-classes create
85 _prop_object_init(struct _prop_object
*po
, const struct _prop_object_type
*pot
)
93 * _prop_object_fini --
94 * Finalize an object. Called when sub-classes destroy
99 _prop_object_fini(struct _prop_object
*po _PROP_ARG_UNUSED
)
101 /* Nothing to do, currently. */
105 * _prop_object_externalize_start_tag --
106 * Append an XML-style start tag to the externalize buffer.
109 _prop_object_externalize_start_tag(
110 struct _prop_object_externalize_context
*ctx
, const char *tag
)
114 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
115 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
118 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
119 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
120 _prop_object_externalize_append_char(ctx
, '>') == false)
127 * _prop_object_externalize_end_tag --
128 * Append an XML-style end tag to the externalize buffer.
131 _prop_object_externalize_end_tag(
132 struct _prop_object_externalize_context
*ctx
, const char *tag
)
135 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
136 _prop_object_externalize_append_char(ctx
, '/') == false ||
137 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
138 _prop_object_externalize_append_char(ctx
, '>') == false ||
139 _prop_object_externalize_append_char(ctx
, '\n') == false)
146 * _prop_object_externalize_empty_tag --
147 * Append an XML-style empty tag to the externalize buffer.
150 _prop_object_externalize_empty_tag(
151 struct _prop_object_externalize_context
*ctx
, const char *tag
)
155 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
156 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
160 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
161 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
162 _prop_object_externalize_append_char(ctx
, '/') == false ||
163 _prop_object_externalize_append_char(ctx
, '>') == false ||
164 _prop_object_externalize_append_char(ctx
, '\n') == false)
171 * _prop_object_externalize_append_cstring --
172 * Append a C string to the externalize buffer.
175 _prop_object_externalize_append_cstring(
176 struct _prop_object_externalize_context
*ctx
, const char *cp
)
179 while (*cp
!= '\0') {
180 if (_prop_object_externalize_append_char(ctx
,
181 (unsigned char) *cp
) == false)
190 * _prop_object_externalize_append_encoded_cstring --
191 * Append an encoded C string to the externalize buffer.
194 _prop_object_externalize_append_encoded_cstring(
195 struct _prop_object_externalize_context
*ctx
, const char *cp
)
198 while (*cp
!= '\0') {
201 if (_prop_object_externalize_append_cstring(ctx
,
206 if (_prop_object_externalize_append_cstring(ctx
,
211 if (_prop_object_externalize_append_cstring(ctx
,
216 if (_prop_object_externalize_append_char(ctx
,
217 (unsigned char) *cp
) == false)
227 #define BUF_EXPAND 256
230 * _prop_object_externalize_append_char --
231 * Append a single character to the externalize buffer.
234 _prop_object_externalize_append_char(
235 struct _prop_object_externalize_context
*ctx
, unsigned char c
)
238 _PROP_ASSERT(ctx
->poec_capacity
!= 0);
239 _PROP_ASSERT(ctx
->poec_buf
!= NULL
);
240 _PROP_ASSERT(ctx
->poec_len
<= ctx
->poec_capacity
);
242 if (ctx
->poec_len
== ctx
->poec_capacity
) {
243 char *cp
= _PROP_REALLOC(ctx
->poec_buf
,
244 ctx
->poec_capacity
+ BUF_EXPAND
,
248 ctx
->poec_capacity
= ctx
->poec_capacity
+ BUF_EXPAND
;
252 ctx
->poec_buf
[ctx
->poec_len
++] = c
;
258 * _prop_object_externalize_header --
259 * Append the standard XML header to the externalize buffer.
262 _prop_object_externalize_header(struct _prop_object_externalize_context
*ctx
)
264 static const char _plist_xml_header
[] =
265 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
266 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
268 if (_prop_object_externalize_append_cstring(ctx
,
269 _plist_xml_header
) == false ||
270 _prop_object_externalize_start_tag(ctx
,
271 "plist version=\"1.0\"") == false ||
272 _prop_object_externalize_append_char(ctx
, '\n') == false)
279 * _prop_object_externalize_footer --
280 * Append the standard XML footer to the externalize buffer. This
281 * also NUL-terminates the buffer.
284 _prop_object_externalize_footer(struct _prop_object_externalize_context
*ctx
)
287 if (_prop_object_externalize_end_tag(ctx
, "plist") == false ||
288 _prop_object_externalize_append_char(ctx
, '\0') == false)
295 * _prop_object_externalize_context_alloc --
296 * Allocate an externalize context.
298 struct _prop_object_externalize_context
*
299 _prop_object_externalize_context_alloc(void)
301 struct _prop_object_externalize_context
*ctx
;
303 ctx
= _PROP_MALLOC(sizeof(*ctx
), M_TEMP
);
305 ctx
->poec_buf
= _PROP_MALLOC(BUF_EXPAND
, M_TEMP
);
306 if (ctx
->poec_buf
== NULL
) {
307 _PROP_FREE(ctx
, M_TEMP
);
311 ctx
->poec_capacity
= BUF_EXPAND
;
318 * _prop_object_externalize_context_free --
319 * Free an externalize context.
322 _prop_object_externalize_context_free(
323 struct _prop_object_externalize_context
*ctx
)
326 /* Buffer is always freed by the caller. */
327 _PROP_FREE(ctx
, M_TEMP
);
331 * _prop_object_internalize_skip_comment --
332 * Skip the body and end tag of a comment.
335 _prop_object_internalize_skip_comment(
336 struct _prop_object_internalize_context
*ctx
)
338 const char *cp
= ctx
->poic_cp
;
340 while (!_PROP_EOF(*cp
)) {
344 ctx
->poic_cp
= cp
+ 3;
350 return (false); /* ran out of buffer */
354 * _prop_object_internalize_find_tag --
355 * Find the next tag in an XML stream. Optionally compare the found
356 * tag to an expected tag name. State of the context is undefined
357 * if this routine returns false. Upon success, the context points
358 * to the first octet after the tag.
361 _prop_object_internalize_find_tag(struct _prop_object_internalize_context
*ctx
,
362 const char *tag
, _prop_tag_type_t type
)
368 taglen
= strlen(tag
);
376 * Find the start of the tag.
378 while (_PROP_ISSPACE(*cp
))
386 ctx
->poic_tag_start
= cp
++;
391 if (cp
[1] != '-' || cp
[2] != '-')
394 * Comment block -- only allowed if we are allowed to
395 * return a start tag.
397 if (type
== _PROP_TAG_TYPE_END
)
399 ctx
->poic_cp
= cp
+ 3;
400 if (_prop_object_internalize_skip_comment(ctx
) == false)
406 if (type
!= _PROP_TAG_TYPE_END
&&
407 type
!= _PROP_TAG_TYPE_EITHER
)
412 ctx
->poic_tag_type
= _PROP_TAG_TYPE_END
;
414 if (type
!= _PROP_TAG_TYPE_START
&&
415 type
!= _PROP_TAG_TYPE_EITHER
)
417 ctx
->poic_tag_type
= _PROP_TAG_TYPE_START
;
420 ctx
->poic_tagname
= cp
;
422 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
!= '=') {
475 ctx
->poic_tagattr_len
= cp
- ctx
->poic_tagattr
;
484 ctx
->poic_tagattrval
= cp
;
485 while (*cp
!= '\"') {
490 ctx
->poic_tagattrval_len
= cp
- ctx
->poic_tagattrval
;
496 ctx
->poic_cp
= cp
+ 1;
501 * _prop_object_internalize_decode_string --
502 * Decode an encoded string.
505 _prop_object_internalize_decode_string(
506 struct _prop_object_internalize_context
*ctx
,
507 char *target
, size_t targsize
, size_t *sizep
,
524 if ((c
= *src
) == '&') {
531 } else if (src
[1] == 'l' &&
536 } else if (src
[1] == 'g' &&
541 } else if (src
[1] == 'a' &&
548 } else if (src
[1] == 'q' &&
560 if (tarindex
>= targsize
)
562 target
[tarindex
] = c
;
567 _PROP_ASSERT(*src
== '<');
577 * _prop_object_internalize_match --
578 * Returns true if the two character streams match.
581 _prop_object_internalize_match(const char *str1
, size_t len1
,
582 const char *str2
, size_t len2
)
585 return (len1
== len2
&& memcmp(str1
, str2
, len1
) == 0);
588 #define INTERNALIZER(t, f) \
589 { t, sizeof(t) - 1, f }
591 static const struct _prop_object_internalizer
{
594 prop_object_internalizer_t poi_intern
;
595 } _prop_object_internalizer_table
[] = {
596 INTERNALIZER("array", _prop_array_internalize
),
598 INTERNALIZER("true", _prop_bool_internalize
),
599 INTERNALIZER("false", _prop_bool_internalize
),
601 INTERNALIZER("data", _prop_data_internalize
),
603 INTERNALIZER("dict", _prop_dictionary_internalize
),
605 INTERNALIZER("integer", _prop_number_internalize
),
607 INTERNALIZER("string", _prop_string_internalize
),
615 * _prop_object_internalize_by_tag --
616 * Determine the object type from the tag in the context and
620 _prop_object_internalize_by_tag(struct _prop_object_internalize_context
*ctx
)
622 const struct _prop_object_internalizer
*poi
;
623 prop_object_t obj
, parent_obj
;
625 prop_object_internalizer_continue_t iter_func
;
626 struct _prop_stack stack
;
628 _prop_stack_init(&stack
);
631 for (poi
= _prop_object_internalizer_table
;
632 poi
->poi_tag
!= NULL
; poi
++) {
633 if (_prop_object_internalize_match(ctx
->poic_tagname
,
634 ctx
->poic_tagname_len
,
639 if ((poi
== NULL
) || (poi
->poi_tag
== NULL
)) {
640 while (_prop_stack_pop(&stack
, &obj
, &iter
, &data
, NULL
)) {
641 iter_func
= (prop_object_internalizer_continue_t
)iter
;
642 (*iter_func
)(&stack
, &obj
, ctx
, data
, NULL
);
649 if (!(*poi
->poi_intern
)(&stack
, &obj
, ctx
))
653 while (_prop_stack_pop(&stack
, &parent_obj
, &iter
, &data
, NULL
)) {
654 iter_func
= (prop_object_internalizer_continue_t
)iter
;
655 if (!(*iter_func
)(&stack
, &parent_obj
, ctx
, data
, obj
))
664 _prop_generic_internalize(const char *xml
, const char *master_tag
)
666 prop_object_t obj
= NULL
;
667 struct _prop_object_internalize_context
*ctx
;
669 ctx
= _prop_object_internalize_context_alloc(xml
);
673 /* We start with a <plist> tag. */
674 if (_prop_object_internalize_find_tag(ctx
, "plist",
675 _PROP_TAG_TYPE_START
) == false)
678 /* Plist elements cannot be empty. */
679 if (ctx
->poic_is_empty_element
)
683 * We don't understand any plist attributes, but Apple XML
684 * property lists often have a "version" attribute. If we
685 * see that one, we simply ignore it.
687 if (ctx
->poic_tagattr
!= NULL
&&
688 !_PROP_TAGATTR_MATCH(ctx
, "version"))
691 /* Next we expect to see opening master_tag. */
692 if (_prop_object_internalize_find_tag(ctx
, master_tag
,
693 _PROP_TAG_TYPE_START
) == false)
696 obj
= _prop_object_internalize_by_tag(ctx
);
701 * We've advanced past the closing master_tag.
702 * Now we want </plist>.
704 if (_prop_object_internalize_find_tag(ctx
, "plist",
705 _PROP_TAG_TYPE_END
) == false) {
706 prop_object_release(obj
);
711 _prop_object_internalize_context_free(ctx
);
716 * _prop_object_internalize_context_alloc --
717 * Allocate an internalize context.
719 struct _prop_object_internalize_context
*
720 _prop_object_internalize_context_alloc(const char *xml
)
722 struct _prop_object_internalize_context
*ctx
;
724 ctx
= _PROP_MALLOC(sizeof(struct _prop_object_internalize_context
),
729 ctx
->poic_xml
= ctx
->poic_cp
= xml
;
732 * Skip any whitespace and XML preamble stuff that we don't
733 * know about / care about.
736 while (_PROP_ISSPACE(*xml
))
738 if (_PROP_EOF(*xml
) || *xml
!= '<')
741 #define MATCH(str) (memcmp(&xml[1], str, sizeof(str) - 1) == 0)
744 * Skip over the XML preamble that Apple XML property
745 * lists usually include at the top of the file.
747 if (MATCH("?xml ") ||
748 MATCH("!DOCTYPE plist")) {
749 while (*xml
!= '>' && !_PROP_EOF(*xml
))
753 xml
++; /* advance past the '>' */
758 ctx
->poic_cp
= xml
+ 4;
759 if (_prop_object_internalize_skip_comment(ctx
) == false)
768 * We don't think we should skip it, so let's hope we can
777 _PROP_FREE(ctx
, M_TEMP
);
782 * _prop_object_internalize_context_free --
783 * Free an internalize context.
786 _prop_object_internalize_context_free(
787 struct _prop_object_internalize_context
*ctx
)
790 _PROP_FREE(ctx
, M_TEMP
);
793 #if !defined(_KERNEL) && !defined(_STANDALONE)
795 * _prop_object_externalize_file_dirname --
796 * dirname(3), basically. We have to roll our own because the
797 * system dirname(3) isn't reentrant.
800 _prop_object_externalize_file_dirname(const char *path
, char *result
)
806 * If `path' is a NULL pointer or points to an empty string,
809 if (path
== NULL
|| *path
== '\0')
812 /* String trailing slashes, if any. */
813 lastp
= path
+ strlen(path
) - 1;
814 while (lastp
!= path
&& *lastp
== '/')
817 /* Terminate path at the last occurrence of '/'. */
820 /* Strip trailing slashes, if any. */
821 while (lastp
!= path
&& *lastp
== '/')
824 /* ...and copy the result into the result buffer. */
825 len
= (lastp
- path
) + 1 /* last char */;
826 if (len
> (PATH_MAX
- 1))
829 memcpy(result
, path
, len
);
833 } while (--lastp
>= path
);
835 /* No /'s found, return ".". */
841 * _prop_object_externalize_write_file --
842 * Write an externalized dictionary to the specified file.
843 * The file is written atomically from the caller's perspective,
844 * and the mode set to 0666 modified by the caller's umask.
847 _prop_object_externalize_write_file(const char *fname
, const char *xml
,
850 char tname
[PATH_MAX
];
855 if (len
> SSIZE_MAX
) {
861 * Get the directory name where the file is to be written
862 * and create the temporary file.
864 _prop_object_externalize_file_dirname(fname
, tname
);
865 #define PLISTTMP "/.plistXXXXXX"
866 if (strlen(tname
) + strlen(PLISTTMP
) >= sizeof(tname
)) {
867 errno
= ENAMETOOLONG
;
870 strcat(tname
, PLISTTMP
);
873 if ((fd
= mkstemp(tname
)) == -1)
876 if (write(fd
, xml
, len
) != (ssize_t
)len
)
883 (void)umask(myumask
);
884 if (fchmod(fd
, 0666 & ~myumask
) == -1)
890 if (rename(tname
, fname
) == -1)
899 (void) unlink(tname
);
905 * _prop_object_internalize_map_file --
906 * Map a file for the purpose of internalizing it.
908 struct _prop_object_internalize_mapped_file
*
909 _prop_object_internalize_map_file(const char *fname
)
912 struct _prop_object_internalize_mapped_file
*mf
;
913 size_t pgsize
= (size_t)sysconf(_SC_PAGESIZE
);
914 size_t pgmask
= pgsize
- 1;
915 bool need_guard
= false;
918 mf
= _PROP_MALLOC(sizeof(*mf
), M_TEMP
);
922 fd
= open(fname
, O_RDONLY
, 0400);
924 _PROP_FREE(mf
, M_TEMP
);
928 if (fstat(fd
, &sb
) == -1) {
930 _PROP_FREE(mf
, M_TEMP
);
933 mf
->poimf_mapsize
= ((size_t)sb
.st_size
+ pgmask
) & ~pgmask
;
934 if (mf
->poimf_mapsize
< (size_t)sb
.st_size
) {
936 _PROP_FREE(mf
, M_TEMP
);
941 * If the file length is an integral number of pages, then we
942 * need to map a guard page at the end in order to provide the
943 * necessary NUL-termination of the buffer.
945 if ((sb
.st_size
& pgmask
) == 0)
948 mf
->poimf_xml
= mmap(NULL
, need_guard
? mf
->poimf_mapsize
+ pgsize
950 PROT_READ
, MAP_FILE
|MAP_SHARED
, fd
, (off_t
)0);
952 if (mf
->poimf_xml
== MAP_FAILED
) {
953 _PROP_FREE(mf
, M_TEMP
);
956 #if !defined(__minix)
957 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_SEQUENTIAL
);
960 if (mmap(mf
->poimf_xml
+ mf
->poimf_mapsize
,
962 MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
, -1,
963 (off_t
)0) == MAP_FAILED
) {
964 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
965 _PROP_FREE(mf
, M_TEMP
);
968 mf
->poimf_mapsize
+= pgsize
;
970 #endif /* !defined(__minix) */
976 * _prop_object_internalize_unmap_file --
977 * Unmap a file previously mapped for internalizing.
982 #endif /* defined(__minix) */
983 _prop_object_internalize_unmap_file(
984 struct _prop_object_internalize_mapped_file
*mf
)
987 #if !defined(__minix)
988 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_DONTNEED
);
989 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
990 _PROP_FREE(mf
, M_TEMP
);
993 #endif /* !defined(__minix) */
995 #endif /* !_KERNEL && !_STANDALONE */
998 * prop_object_retain --
999 * Increment the reference count on an object.
1002 prop_object_retain(prop_object_t obj
)
1004 struct _prop_object
*po
= obj
;
1005 uint32_t ncnt __unused
;
1007 _PROP_ATOMIC_INC32_NV(&po
->po_refcnt
, ncnt
);
1008 _PROP_ASSERT(ncnt
!= 0);
1012 * prop_object_release_emergency
1013 * A direct free with prop_object_release failed.
1014 * Walk down the tree until a leaf is found and
1015 * free that. Do not recurse to avoid stack overflows.
1017 * This is a slow edge condition, but necessary to
1018 * guarantee that an object can always be freed.
1021 prop_object_release_emergency(prop_object_t obj
)
1023 struct _prop_object
*po
;
1024 void (*unlock
)(void);
1025 prop_object_t parent
= NULL
;
1032 if (po
->po_type
->pot_lock
!= NULL
)
1033 po
->po_type
->pot_lock();
1035 /* Save pointerto unlock function */
1036 unlock
= po
->po_type
->pot_unlock
;
1038 /* Dance a bit to make sure we always get the non-racy ocnt */
1039 _PROP_ATOMIC_DEC32_NV(&po
->po_refcnt
, ocnt
);
1041 _PROP_ASSERT(ocnt
!= 0);
1049 _PROP_ASSERT(po
->po_type
);
1050 if ((po
->po_type
->pot_free
)(NULL
, &obj
) ==
1051 _PROP_OBJECT_FREE_DONE
) {
1061 _PROP_ATOMIC_INC32(&po
->po_refcnt
);
1063 _PROP_ASSERT(parent
);
1064 /* One object was just freed. */
1066 (*po
->po_type
->pot_emergency_free
)(parent
);
1070 * prop_object_release --
1071 * Decrement the reference count on an object.
1073 * Free the object if we are releasing the final
1077 prop_object_release(prop_object_t obj
)
1079 struct _prop_object
*po
;
1080 struct _prop_stack stack
;
1081 void (*unlock
)(void);
1085 _prop_stack_init(&stack
);
1092 if (po
->po_type
->pot_lock
!= NULL
)
1093 po
->po_type
->pot_lock();
1095 /* Save pointer to object unlock function */
1096 unlock
= po
->po_type
->pot_unlock
;
1098 _PROP_ATOMIC_DEC32_NV(&po
->po_refcnt
, ocnt
);
1100 _PROP_ASSERT(ocnt
!= 0);
1109 ret
= (po
->po_type
->pot_free
)(&stack
, &obj
);
1114 if (ret
== _PROP_OBJECT_FREE_DONE
)
1117 _PROP_ATOMIC_INC32(&po
->po_refcnt
);
1118 } while (ret
== _PROP_OBJECT_FREE_RECURSE
);
1119 if (ret
== _PROP_OBJECT_FREE_FAILED
)
1120 prop_object_release_emergency(obj
);
1121 } while (_prop_stack_pop(&stack
, &obj
, NULL
, NULL
, NULL
));
1125 * prop_object_type --
1126 * Return the type of an object.
1129 prop_object_type(prop_object_t obj
)
1131 struct _prop_object
*po
= obj
;
1134 return (PROP_TYPE_UNKNOWN
);
1136 return (po
->po_type
->pot_type
);
1140 * prop_object_equals --
1141 * Returns true if thw two objects are equivalent.
1144 prop_object_equals(prop_object_t obj1
, prop_object_t obj2
)
1146 return (prop_object_equals_with_error(obj1
, obj2
, NULL
));
1150 prop_object_equals_with_error(prop_object_t obj1
, prop_object_t obj2
,
1153 struct _prop_object
*po1
;
1154 struct _prop_object
*po2
;
1155 void *stored_pointer1
, *stored_pointer2
;
1156 prop_object_t next_obj1
, next_obj2
;
1157 struct _prop_stack stack
;
1158 _prop_object_equals_rv_t ret
;
1160 _prop_stack_init(&stack
);
1162 *error_flag
= false;
1165 stored_pointer1
= NULL
;
1166 stored_pointer2
= NULL
;
1170 if (po1
->po_type
!= po2
->po_type
)
1174 ret
= (*po1
->po_type
->pot_equals
)(obj1
, obj2
,
1175 &stored_pointer1
, &stored_pointer2
,
1176 &next_obj1
, &next_obj2
);
1177 if (ret
== _PROP_OBJECT_EQUALS_FALSE
)
1179 if (ret
== _PROP_OBJECT_EQUALS_TRUE
) {
1180 if (!_prop_stack_pop(&stack
, &obj1
, &obj2
,
1181 &stored_pointer1
, &stored_pointer2
))
1185 goto continue_subtree
;
1187 _PROP_ASSERT(ret
== _PROP_OBJECT_EQUALS_RECURSE
);
1189 if (!_prop_stack_push(&stack
, obj1
, obj2
,
1190 stored_pointer1
, stored_pointer2
)) {
1200 while (_prop_stack_pop(&stack
, &obj1
, &obj2
, NULL
, NULL
)) {
1202 (*po1
->po_type
->pot_equals_finish
)(obj1
, obj2
);
1208 * prop_object_iterator_next --
1209 * Return the next item during an iteration.
1212 prop_object_iterator_next(prop_object_iterator_t pi
)
1215 return ((*pi
->pi_next_object
)(pi
));
1219 * prop_object_iterator_reset --
1220 * Reset the iterator to the first object so as to restart
1224 prop_object_iterator_reset(prop_object_iterator_t pi
)
1227 (*pi
->pi_reset
)(pi
);
1231 * prop_object_iterator_release --
1232 * Release the object iterator.
1235 prop_object_iterator_release(prop_object_iterator_t pi
)
1238 prop_object_release(pi
->pi_obj
);
1239 _PROP_FREE(pi
, M_TEMP
);