1 /* $NetBSD: prop_object.c,v 1.23 2008/11/30 00:17:07 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 <prop/prop_object.h>
33 #include "prop_object_impl.h"
45 * _prop_object_init --
46 * Initialize an object. Called when sub-classes create
50 _prop_object_init(struct _prop_object
*po
, const struct _prop_object_type
*pot
)
58 * _prop_object_fini --
59 * Finalize an object. Called when sub-classes destroy
64 _prop_object_fini(struct _prop_object
*po _PROP_ARG_UNUSED
)
66 /* Nothing to do, currently. */
70 * _prop_object_externalize_start_tag --
71 * Append an XML-style start tag to the externalize buffer.
74 _prop_object_externalize_start_tag(
75 struct _prop_object_externalize_context
*ctx
, const char *tag
)
79 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
80 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
83 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
84 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
85 _prop_object_externalize_append_char(ctx
, '>') == false)
92 * _prop_object_externalize_end_tag --
93 * Append an XML-style end tag to the externalize buffer.
96 _prop_object_externalize_end_tag(
97 struct _prop_object_externalize_context
*ctx
, const char *tag
)
100 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
101 _prop_object_externalize_append_char(ctx
, '/') == false ||
102 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
103 _prop_object_externalize_append_char(ctx
, '>') == false ||
104 _prop_object_externalize_append_char(ctx
, '\n') == false)
111 * _prop_object_externalize_empty_tag --
112 * Append an XML-style empty tag to the externalize buffer.
115 _prop_object_externalize_empty_tag(
116 struct _prop_object_externalize_context
*ctx
, const char *tag
)
120 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
121 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
125 if (_prop_object_externalize_append_char(ctx
, '<') == false ||
126 _prop_object_externalize_append_cstring(ctx
, tag
) == false ||
127 _prop_object_externalize_append_char(ctx
, '/') == false ||
128 _prop_object_externalize_append_char(ctx
, '>') == false ||
129 _prop_object_externalize_append_char(ctx
, '\n') == false)
136 * _prop_object_externalize_append_cstring --
137 * Append a C string to the externalize buffer.
140 _prop_object_externalize_append_cstring(
141 struct _prop_object_externalize_context
*ctx
, const char *cp
)
144 while (*cp
!= '\0') {
145 if (_prop_object_externalize_append_char(ctx
,
146 (unsigned char) *cp
) == false)
155 * _prop_object_externalize_append_encoded_cstring --
156 * Append an encoded C string to the externalize buffer.
159 _prop_object_externalize_append_encoded_cstring(
160 struct _prop_object_externalize_context
*ctx
, const char *cp
)
163 while (*cp
!= '\0') {
166 if (_prop_object_externalize_append_cstring(ctx
,
171 if (_prop_object_externalize_append_cstring(ctx
,
176 if (_prop_object_externalize_append_cstring(ctx
,
181 if (_prop_object_externalize_append_char(ctx
,
182 (unsigned char) *cp
) == false)
192 #define BUF_EXPAND 256
195 * _prop_object_externalize_append_char --
196 * Append a single character to the externalize buffer.
199 _prop_object_externalize_append_char(
200 struct _prop_object_externalize_context
*ctx
, unsigned char c
)
203 _PROP_ASSERT(ctx
->poec_capacity
!= 0);
204 _PROP_ASSERT(ctx
->poec_buf
!= NULL
);
205 _PROP_ASSERT(ctx
->poec_len
<= ctx
->poec_capacity
);
207 if (ctx
->poec_len
== ctx
->poec_capacity
) {
208 char *cp
= _PROP_REALLOC(ctx
->poec_buf
,
209 ctx
->poec_capacity
+ BUF_EXPAND
,
213 ctx
->poec_capacity
= ctx
->poec_capacity
+ BUF_EXPAND
;
217 ctx
->poec_buf
[ctx
->poec_len
++] = c
;
223 * _prop_object_externalize_header --
224 * Append the standard XML header to the externalize buffer.
227 _prop_object_externalize_header(struct _prop_object_externalize_context
*ctx
)
229 static const char _plist_xml_header
[] =
230 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
231 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
233 if (_prop_object_externalize_append_cstring(ctx
,
234 _plist_xml_header
) == false ||
235 _prop_object_externalize_start_tag(ctx
,
236 "plist version=\"1.0\"") == false ||
237 _prop_object_externalize_append_char(ctx
, '\n') == false)
244 * _prop_object_externalize_footer --
245 * Append the standard XML footer to the externalize buffer. This
246 * also NUL-terminates the buffer.
249 _prop_object_externalize_footer(struct _prop_object_externalize_context
*ctx
)
252 if (_prop_object_externalize_end_tag(ctx
, "plist") == false ||
253 _prop_object_externalize_append_char(ctx
, '\0') == false)
260 * _prop_object_externalize_context_alloc --
261 * Allocate an externalize context.
263 struct _prop_object_externalize_context
*
264 _prop_object_externalize_context_alloc(void)
266 struct _prop_object_externalize_context
*ctx
;
268 ctx
= _PROP_MALLOC(sizeof(*ctx
), M_TEMP
);
270 ctx
->poec_buf
= _PROP_MALLOC(BUF_EXPAND
, M_TEMP
);
271 if (ctx
->poec_buf
== NULL
) {
272 _PROP_FREE(ctx
, M_TEMP
);
276 ctx
->poec_capacity
= BUF_EXPAND
;
283 * _prop_object_externalize_context_free --
284 * Free an externalize context.
287 _prop_object_externalize_context_free(
288 struct _prop_object_externalize_context
*ctx
)
291 /* Buffer is always freed by the caller. */
292 _PROP_FREE(ctx
, M_TEMP
);
296 * _prop_object_internalize_skip_comment --
297 * Skip the body and end tag of a comment.
300 _prop_object_internalize_skip_comment(
301 struct _prop_object_internalize_context
*ctx
)
303 const char *cp
= ctx
->poic_cp
;
305 while (!_PROP_EOF(*cp
)) {
309 ctx
->poic_cp
= cp
+ 3;
315 return (false); /* ran out of buffer */
319 * _prop_object_internalize_find_tag --
320 * Find the next tag in an XML stream. Optionally compare the found
321 * tag to an expected tag name. State of the context is undefined
322 * if this routine returns false. Upon success, the context points
323 * to the first octet after the tag.
326 _prop_object_internalize_find_tag(struct _prop_object_internalize_context
*ctx
,
327 const char *tag
, _prop_tag_type_t type
)
333 taglen
= strlen(tag
);
341 * Find the start of the tag.
343 while (_PROP_ISSPACE(*cp
))
351 ctx
->poic_tag_start
= cp
++;
356 if (cp
[1] != '-' || cp
[2] != '-')
359 * Comment block -- only allowed if we are allowed to
360 * return a start tag.
362 if (type
== _PROP_TAG_TYPE_END
)
364 ctx
->poic_cp
= cp
+ 3;
365 if (_prop_object_internalize_skip_comment(ctx
) == false)
371 if (type
!= _PROP_TAG_TYPE_END
&&
372 type
!= _PROP_TAG_TYPE_EITHER
)
377 ctx
->poic_tag_type
= _PROP_TAG_TYPE_END
;
379 if (type
!= _PROP_TAG_TYPE_START
&&
380 type
!= _PROP_TAG_TYPE_EITHER
)
382 ctx
->poic_tag_type
= _PROP_TAG_TYPE_START
;
385 ctx
->poic_tagname
= cp
;
387 while (!_PROP_ISSPACE(*cp
) && *cp
!= '/' && *cp
!= '>')
392 ctx
->poic_tagname_len
= cp
- ctx
->poic_tagname
;
394 /* Make sure this is the tag we're looking for. */
396 (taglen
!= ctx
->poic_tagname_len
||
397 memcmp(tag
, ctx
->poic_tagname
, taglen
) != 0))
400 /* Check for empty tag. */
402 if (ctx
->poic_tag_type
!= _PROP_TAG_TYPE_START
)
403 return(false); /* only valid on start tags */
404 ctx
->poic_is_empty_element
= true;
406 if (_PROP_EOF(*cp
) || *cp
!= '>')
409 ctx
->poic_is_empty_element
= false;
411 /* Easy case of no arguments. */
413 ctx
->poic_tagattr
= NULL
;
414 ctx
->poic_tagattr_len
= 0;
415 ctx
->poic_tagattrval
= NULL
;
416 ctx
->poic_tagattrval_len
= 0;
417 ctx
->poic_cp
= cp
+ 1;
421 _PROP_ASSERT(!_PROP_EOF(*cp
));
426 while (_PROP_ISSPACE(*cp
))
431 ctx
->poic_tagattr
= cp
;
433 while (!_PROP_ISSPACE(*cp
) && *cp
!= '=')
438 ctx
->poic_tagattr_len
= cp
- ctx
->poic_tagattr
;
447 ctx
->poic_tagattrval
= cp
;
452 ctx
->poic_tagattrval_len
= cp
- ctx
->poic_tagattrval
;
458 ctx
->poic_cp
= cp
+ 1;
463 * _prop_object_internalize_decode_string --
464 * Decode an encoded string.
467 _prop_object_internalize_decode_string(
468 struct _prop_object_internalize_context
*ctx
,
469 char *target
, size_t targsize
, size_t *sizep
,
486 if ((c
= *src
) == '&') {
493 } else if (src
[1] == 'l' &&
498 } else if (src
[1] == 'g' &&
503 } else if (src
[1] == 'a' &&
510 } else if (src
[1] == 'q' &&
522 if (tarindex
>= targsize
)
524 target
[tarindex
] = c
;
529 _PROP_ASSERT(*src
== '<');
539 * _prop_object_internalize_match --
540 * Returns true if the two character streams match.
543 _prop_object_internalize_match(const char *str1
, size_t len1
,
544 const char *str2
, size_t len2
)
547 return (len1
== len2
&& memcmp(str1
, str2
, len1
) == 0);
550 #define INTERNALIZER(t, f) \
551 { t, sizeof(t) - 1, f }
553 static const struct _prop_object_internalizer
{
556 prop_object_internalizer_t poi_intern
;
557 } _prop_object_internalizer_table
[] = {
558 INTERNALIZER("array", _prop_array_internalize
),
560 INTERNALIZER("true", _prop_bool_internalize
),
561 INTERNALIZER("false", _prop_bool_internalize
),
563 INTERNALIZER("data", _prop_data_internalize
),
565 INTERNALIZER("dict", _prop_dictionary_internalize
),
567 INTERNALIZER("integer", _prop_number_internalize
),
569 INTERNALIZER("string", _prop_string_internalize
),
577 * _prop_object_internalize_by_tag --
578 * Determine the object type from the tag in the context and
582 _prop_object_internalize_by_tag(struct _prop_object_internalize_context
*ctx
)
584 const struct _prop_object_internalizer
*poi
;
585 prop_object_t obj
, parent_obj
;
587 prop_object_internalizer_continue_t iter_func
;
588 struct _prop_stack stack
;
590 _prop_stack_init(&stack
);
593 for (poi
= _prop_object_internalizer_table
;
594 poi
->poi_tag
!= NULL
; poi
++) {
595 if (_prop_object_internalize_match(ctx
->poic_tagname
,
596 ctx
->poic_tagname_len
,
602 while (_prop_stack_pop(&stack
, &obj
, &iter
, &data
, NULL
)) {
603 iter_func
= (prop_object_internalizer_continue_t
)iter
;
604 (*iter_func
)(&stack
, &obj
, ctx
, data
, NULL
);
611 if (!(*poi
->poi_intern
)(&stack
, &obj
, ctx
))
615 while (_prop_stack_pop(&stack
, &parent_obj
, &iter
, &data
, NULL
)) {
616 iter_func
= (prop_object_internalizer_continue_t
)iter
;
617 if (!(*iter_func
)(&stack
, &parent_obj
, ctx
, data
, obj
))
626 _prop_generic_internalize(const char *xml
, const char *master_tag
)
628 prop_object_t obj
= NULL
;
629 struct _prop_object_internalize_context
*ctx
;
631 ctx
= _prop_object_internalize_context_alloc(xml
);
635 /* We start with a <plist> tag. */
636 if (_prop_object_internalize_find_tag(ctx
, "plist",
637 _PROP_TAG_TYPE_START
) == false)
640 /* Plist elements cannot be empty. */
641 if (ctx
->poic_is_empty_element
)
645 * We don't understand any plist attributes, but Apple XML
646 * property lists often have a "version" attribute. If we
647 * see that one, we simply ignore it.
649 if (ctx
->poic_tagattr
!= NULL
&&
650 !_PROP_TAGATTR_MATCH(ctx
, "version"))
653 /* Next we expect to see opening master_tag. */
654 if (_prop_object_internalize_find_tag(ctx
, master_tag
,
655 _PROP_TAG_TYPE_START
) == false)
658 obj
= _prop_object_internalize_by_tag(ctx
);
663 * We've advanced past the closing master_tag.
664 * Now we want </plist>.
666 if (_prop_object_internalize_find_tag(ctx
, "plist",
667 _PROP_TAG_TYPE_END
) == false) {
668 prop_object_release(obj
);
673 _prop_object_internalize_context_free(ctx
);
678 * _prop_object_internalize_context_alloc --
679 * Allocate an internalize context.
681 struct _prop_object_internalize_context
*
682 _prop_object_internalize_context_alloc(const char *xml
)
684 struct _prop_object_internalize_context
*ctx
;
686 ctx
= _PROP_MALLOC(sizeof(struct _prop_object_internalize_context
),
691 ctx
->poic_xml
= ctx
->poic_cp
= xml
;
694 * Skip any whitespace and XML preamble stuff that we don't
695 * know about / care about.
698 while (_PROP_ISSPACE(*xml
))
700 if (_PROP_EOF(*xml
) || *xml
!= '<')
703 #define MATCH(str) (memcmp(&xml[1], str, sizeof(str) - 1) == 0)
706 * Skip over the XML preamble that Apple XML property
707 * lists usually include at the top of the file.
709 if (MATCH("?xml ") ||
710 MATCH("!DOCTYPE plist")) {
711 while (*xml
!= '>' && !_PROP_EOF(*xml
))
715 xml
++; /* advance past the '>' */
720 ctx
->poic_cp
= xml
+ 4;
721 if (_prop_object_internalize_skip_comment(ctx
) == false)
730 * We don't think we should skip it, so let's hope we can
739 _PROP_FREE(ctx
, M_TEMP
);
744 * _prop_object_internalize_context_free --
745 * Free an internalize context.
748 _prop_object_internalize_context_free(
749 struct _prop_object_internalize_context
*ctx
)
752 _PROP_FREE(ctx
, M_TEMP
);
756 * _prop_object_externalize_file_dirname --
757 * dirname(3), basically. We have to roll our own because the
758 * system dirname(3) isn't reentrant.
761 _prop_object_externalize_file_dirname(const char *path
, char *result
)
767 * If `path' is a NULL pointer or points to an empty string,
770 if (path
== NULL
|| *path
== '\0')
773 /* String trailing slashes, if any. */
774 lastp
= path
+ strlen(path
) - 1;
775 while (lastp
!= path
&& *lastp
== '/')
778 /* Terminate path at the last occurrence of '/'. */
781 /* Strip trailing slashes, if any. */
782 while (lastp
!= path
&& *lastp
== '/')
785 /* ...and copy the result into the result buffer. */
786 len
= (lastp
- path
) + 1 /* last char */;
787 if (len
> (PATH_MAX
- 1))
790 memcpy(result
, path
, len
);
794 } while (--lastp
>= path
);
796 /* No /'s found, return ".". */
802 * _prop_object_externalize_write_file --
803 * Write an externalized dictionary to the specified file.
804 * The file is written atomically from the caller's perspective,
805 * and the mode set to 0666 modified by the caller's umask.
807 * The 'compress' argument enables gzip (via zlib) compression
808 * for the file to be written.
811 _prop_object_externalize_write_file(const char *fname
, const char *xml
,
812 size_t len
, bool do_compress
)
815 char tname
[PATH_MAX
], *otname
;
820 if (len
> SSIZE_MAX
) {
826 * Get the directory name where the file is to be written
827 * and create the temporary file.
829 _prop_object_externalize_file_dirname(fname
, tname
);
832 if (strlcat(tname
, "/.plistXXXXXX", sizeof(tname
)) >= sizeof(tname
)) {
833 errno
= ENAMETOOLONG
;
837 otname
= strncat(tname
, "/.plistXXXXXX", sizeof(tname
));
839 if (sizeof(*otname
) >= sizeof(tname
)) {
840 errno
= ENAMETOOLONG
;
844 if ((fd
= mkstemp(tname
)) == -1)
848 if ((gzf
= gzdopen(fd
, "a")) == NULL
)
851 if (gzsetparams(gzf
, Z_BEST_COMPRESSION
, Z_DEFAULT_STRATEGY
))
854 if (gzwrite(gzf
, xml
, len
) != (ssize_t
)len
)
857 if (write(fd
, xml
, len
) != (ssize_t
)len
)
865 (void)umask(myumask
);
866 if (fchmod(fd
, 0666 & ~myumask
) == -1)
875 if (rename(tname
, fname
) == -1)
882 if (do_compress
&& gzf
!= NULL
)
886 (void) unlink(tname
);
892 * _prop_object_internalize_map_file --
893 * Map a file for the purpose of internalizing it.
895 struct _prop_object_internalize_mapped_file
*
896 _prop_object_internalize_map_file(const char *fname
)
899 struct _prop_object_internalize_mapped_file
*mf
;
900 size_t pgsize
= (size_t)sysconf(_SC_PAGESIZE
);
901 size_t pgmask
= pgsize
- 1;
902 bool need_guard
= false;
905 mf
= _PROP_MALLOC(sizeof(*mf
), M_TEMP
);
909 fd
= open(fname
, O_RDONLY
, 0400);
911 _PROP_FREE(mf
, M_TEMP
);
915 if (fstat(fd
, &sb
) == -1) {
917 _PROP_FREE(mf
, M_TEMP
);
920 mf
->poimf_mapsize
= ((size_t)sb
.st_size
+ pgmask
) & ~pgmask
;
921 if (mf
->poimf_mapsize
< (size_t)sb
.st_size
) {
923 _PROP_FREE(mf
, M_TEMP
);
928 * If the file length is an integral number of pages, then we
929 * need to map a guard page at the end in order to provide the
930 * necessary NUL-termination of the buffer.
932 if ((sb
.st_size
& pgmask
) == 0)
935 mf
->poimf_xml
= mmap(NULL
, need_guard
? mf
->poimf_mapsize
+ pgsize
937 PROT_READ
, MAP_FILE
|MAP_SHARED
, fd
, (off_t
)0);
939 if (mf
->poimf_xml
== MAP_FAILED
) {
940 _PROP_FREE(mf
, M_TEMP
);
943 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_SEQUENTIAL
);
946 if (mmap(mf
->poimf_xml
+ mf
->poimf_mapsize
,
948 MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
, -1,
949 (off_t
)0) == MAP_FAILED
) {
950 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
951 _PROP_FREE(mf
, M_TEMP
);
954 mf
->poimf_mapsize
+= pgsize
;
961 * _prop_object_internalize_unmap_file --
962 * Unmap a file previously mapped for internalizing.
965 _prop_object_internalize_unmap_file(
966 struct _prop_object_internalize_mapped_file
*mf
)
969 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_DONTNEED
);
970 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
971 _PROP_FREE(mf
, M_TEMP
);
975 * Retain / release serialization --
977 * Eventually we would like to use atomic operations. But until we have
978 * an MI API for them that is common to userland and the kernel, we will
979 * use a lock instead.
981 * We use a single global mutex for all serialization. In the kernel, because
982 * we are still under a biglock, this will basically never contend (properties
983 * cannot be manipulated at interrupt level). In userland, this will cost
984 * nothing for single-threaded programs. For multi-threaded programs, there
985 * could be contention, but it probably won't cost that much unless the program
986 * makes heavy use of property lists.
988 _PROP_MUTEX_DECL_STATIC(_prop_refcnt_mutex
)
989 #define _PROP_REFCNT_LOCK() _PROP_MUTEX_LOCK(_prop_refcnt_mutex)
990 #define _PROP_REFCNT_UNLOCK() _PROP_MUTEX_UNLOCK(_prop_refcnt_mutex)
993 * prop_object_retain --
994 * Increment the reference count on an object.
997 prop_object_retain(prop_object_t obj
)
999 struct _prop_object
*po
= obj
;
1002 _PROP_REFCNT_LOCK();
1003 ocnt
= po
->po_refcnt
++;
1004 _PROP_REFCNT_UNLOCK();
1006 _PROP_ASSERT(ocnt
!= 0xffffffffU
);
1010 * prop_object_release_emergency
1011 * A direct free with prop_object_release failed.
1012 * Walk down the tree until a leaf is found and
1013 * free that. Do not recurse to avoid stack overflows.
1015 * This is a slow edge condition, but necessary to
1016 * guarantee that an object can always be freed.
1019 prop_object_release_emergency(prop_object_t obj
)
1021 struct _prop_object
*po
;
1022 void (*unlock
)(void);
1023 prop_object_t parent
= NULL
;
1030 if (po
->po_type
->pot_lock
!= NULL
)
1031 po
->po_type
->pot_lock();
1033 /* Save pointerto unlock function */
1034 unlock
= po
->po_type
->pot_unlock
;
1036 _PROP_REFCNT_LOCK();
1037 ocnt
= po
->po_refcnt
--;
1038 _PROP_REFCNT_UNLOCK();
1040 _PROP_ASSERT(ocnt
!= 0);
1047 _PROP_ASSERT(po
->po_type
);
1048 if ((po
->po_type
->pot_free
)(NULL
, &obj
) ==
1049 _PROP_OBJECT_FREE_DONE
) {
1059 _PROP_REFCNT_LOCK();
1061 _PROP_REFCNT_UNLOCK();
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_REFCNT_LOCK();
1099 ocnt
= po
->po_refcnt
--;
1100 _PROP_REFCNT_UNLOCK();
1102 _PROP_ASSERT(ocnt
!= 0);
1110 ret
= (po
->po_type
->pot_free
)(&stack
, &obj
);
1115 if (ret
== _PROP_OBJECT_FREE_DONE
)
1118 _PROP_REFCNT_LOCK();
1120 _PROP_REFCNT_UNLOCK();
1121 } while (ret
== _PROP_OBJECT_FREE_RECURSE
);
1122 if (ret
== _PROP_OBJECT_FREE_FAILED
)
1123 prop_object_release_emergency(obj
);
1124 } while (_prop_stack_pop(&stack
, &obj
, NULL
, NULL
, NULL
));
1128 * prop_object_type --
1129 * Return the type of an object.
1132 prop_object_type(prop_object_t obj
)
1134 struct _prop_object
*po
= obj
;
1137 return (PROP_TYPE_UNKNOWN
);
1139 return (po
->po_type
->pot_type
);
1143 * prop_object_equals --
1144 * Returns true if thw two objects are equivalent.
1147 prop_object_equals(prop_object_t obj1
, prop_object_t obj2
)
1149 return (prop_object_equals_with_error(obj1
, obj2
, NULL
));
1153 prop_object_equals_with_error(prop_object_t obj1
, prop_object_t obj2
,
1156 struct _prop_object
*po1
;
1157 struct _prop_object
*po2
;
1158 void *stored_pointer1
, *stored_pointer2
;
1159 prop_object_t next_obj1
, next_obj2
;
1160 struct _prop_stack stack
;
1161 _prop_object_equals_rv_t ret
;
1163 _prop_stack_init(&stack
);
1165 *error_flag
= false;
1168 stored_pointer1
= NULL
;
1169 stored_pointer2
= NULL
;
1173 if (po1
->po_type
!= po2
->po_type
)
1177 ret
= (*po1
->po_type
->pot_equals
)(obj1
, obj2
,
1178 &stored_pointer1
, &stored_pointer2
,
1179 &next_obj1
, &next_obj2
);
1180 if (ret
== _PROP_OBJECT_EQUALS_FALSE
)
1182 if (ret
== _PROP_OBJECT_EQUALS_TRUE
) {
1183 if (!_prop_stack_pop(&stack
, &obj1
, &obj2
,
1184 &stored_pointer1
, &stored_pointer2
))
1186 goto continue_subtree
;
1188 _PROP_ASSERT(ret
== _PROP_OBJECT_EQUALS_RECURSE
);
1190 if (!_prop_stack_push(&stack
, obj1
, obj2
,
1191 stored_pointer1
, stored_pointer2
)) {
1201 while (_prop_stack_pop(&stack
, &obj1
, &obj2
, NULL
, NULL
)) {
1203 (*po1
->po_type
->pot_equals_finish
)(obj1
, obj2
);
1209 * prop_object_iterator_next --
1210 * Return the next item during an iteration.
1213 prop_object_iterator_next(prop_object_iterator_t pi
)
1216 return ((*pi
->pi_next_object
)(pi
));
1220 * prop_object_iterator_reset --
1221 * Reset the iterator to the first object so as to restart
1225 prop_object_iterator_reset(prop_object_iterator_t pi
)
1228 (*pi
->pi_reset
)(pi
);
1232 * prop_object_iterator_release --
1233 * Release the object iterator.
1236 prop_object_iterator_release(prop_object_iterator_t pi
)
1239 prop_object_release(pi
->pi_obj
);
1240 _PROP_FREE(pi
, M_TEMP
);