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_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. If zlib support is disabled it's
812 _prop_object_externalize_write_file(const char *fname
, const char *xml
,
813 size_t len
, bool compress
)
816 char tname
[PATH_MAX
], *otname
;
821 if (len
> SSIZE_MAX
) {
827 * Get the directory name where the file is to be written
828 * and create the temporary file.
830 _prop_object_externalize_file_dirname(fname
, tname
);
833 if (strlcat(tname
, "/.plistXXXXXX", sizeof(tname
)) >= sizeof(tname
)) {
834 errno
= ENAMETOOLONG
;
838 otname
= strncat(tname
, "/.plistXXXXXX", sizeof(tname
));
840 if (sizeof(*otname
) >= sizeof(tname
)) {
841 errno
= ENAMETOOLONG
;
845 if ((fd
= mkstemp(tname
)) == -1)
848 if ((gzf
= gzdopen(fd
, "a")) == NULL
)
851 if (gzsetparams(gzf
, compress
? Z_BEST_COMPRESSION
: Z_NO_COMPRESSION
,
855 if (gzwrite(gzf
, xml
, len
) != (ssize_t
)len
)
862 (void)umask(myumask
);
863 if (fchmod(fd
, 0666 & ~myumask
) == -1)
869 if (rename(tname
, fname
) == -1)
878 (void) unlink(tname
);
884 * _prop_object_internalize_map_file --
885 * Map a file for the purpose of internalizing it.
887 struct _prop_object_internalize_mapped_file
*
888 _prop_object_internalize_map_file(const char *fname
)
891 struct _prop_object_internalize_mapped_file
*mf
;
892 size_t pgsize
= (size_t)sysconf(_SC_PAGESIZE
);
893 size_t pgmask
= pgsize
- 1;
894 bool need_guard
= false;
897 mf
= _PROP_MALLOC(sizeof(*mf
), M_TEMP
);
901 fd
= open(fname
, O_RDONLY
, 0400);
903 _PROP_FREE(mf
, M_TEMP
);
907 if (fstat(fd
, &sb
) == -1) {
909 _PROP_FREE(mf
, M_TEMP
);
912 mf
->poimf_mapsize
= ((size_t)sb
.st_size
+ pgmask
) & ~pgmask
;
913 if (mf
->poimf_mapsize
< sb
.st_size
) {
915 _PROP_FREE(mf
, M_TEMP
);
920 * If the file length is an integral number of pages, then we
921 * need to map a guard page at the end in order to provide the
922 * necessary NUL-termination of the buffer.
924 if ((sb
.st_size
& pgmask
) == 0)
927 mf
->poimf_xml
= mmap(NULL
, need_guard
? mf
->poimf_mapsize
+ pgsize
929 PROT_READ
, MAP_FILE
|MAP_SHARED
, fd
, (off_t
)0);
931 if (mf
->poimf_xml
== MAP_FAILED
) {
932 _PROP_FREE(mf
, M_TEMP
);
935 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_SEQUENTIAL
);
938 if (mmap(mf
->poimf_xml
+ mf
->poimf_mapsize
,
940 MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
, -1,
941 (off_t
)0) == MAP_FAILED
) {
942 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
943 _PROP_FREE(mf
, M_TEMP
);
946 mf
->poimf_mapsize
+= pgsize
;
953 * _prop_object_internalize_unmap_file --
954 * Unmap a file previously mapped for internalizing.
957 _prop_object_internalize_unmap_file(
958 struct _prop_object_internalize_mapped_file
*mf
)
961 (void) madvise(mf
->poimf_xml
, mf
->poimf_mapsize
, MADV_DONTNEED
);
962 (void) munmap(mf
->poimf_xml
, mf
->poimf_mapsize
);
963 _PROP_FREE(mf
->poimf_uncomp_xml
, M_TEMP
);
964 _PROP_FREE(mf
, M_TEMP
);
968 * Retain / release serialization --
970 * Eventually we would like to use atomic operations. But until we have
971 * an MI API for them that is common to userland and the kernel, we will
972 * use a lock instead.
974 * We use a single global mutex for all serialization. In the kernel, because
975 * we are still under a biglock, this will basically never contend (properties
976 * cannot be manipulated at interrupt level). In userland, this will cost
977 * nothing for single-threaded programs. For multi-threaded programs, there
978 * could be contention, but it probably won't cost that much unless the program
979 * makes heavy use of property lists.
981 _PROP_MUTEX_DECL_STATIC(_prop_refcnt_mutex
)
982 #define _PROP_REFCNT_LOCK() _PROP_MUTEX_LOCK(_prop_refcnt_mutex)
983 #define _PROP_REFCNT_UNLOCK() _PROP_MUTEX_UNLOCK(_prop_refcnt_mutex)
986 * prop_object_retain --
987 * Increment the reference count on an object.
990 prop_object_retain(prop_object_t obj
)
992 struct _prop_object
*po
= obj
;
996 ocnt
= po
->po_refcnt
++;
997 _PROP_REFCNT_UNLOCK();
999 _PROP_ASSERT(ocnt
!= 0xffffffffU
);
1003 * prop_object_release_emergency
1004 * A direct free with prop_object_release failed.
1005 * Walk down the tree until a leaf is found and
1006 * free that. Do not recurse to avoid stack overflows.
1008 * This is a slow edge condition, but necessary to
1009 * guarantee that an object can always be freed.
1012 prop_object_release_emergency(prop_object_t obj
)
1014 struct _prop_object
*po
;
1015 void (*unlock
)(void);
1016 prop_object_t parent
= NULL
;
1023 if (po
->po_type
->pot_lock
!= NULL
)
1024 po
->po_type
->pot_lock();
1026 /* Save pointerto unlock function */
1027 unlock
= po
->po_type
->pot_unlock
;
1029 _PROP_REFCNT_LOCK();
1030 ocnt
= po
->po_refcnt
--;
1031 _PROP_REFCNT_UNLOCK();
1033 _PROP_ASSERT(ocnt
!= 0);
1040 _PROP_ASSERT(po
->po_type
);
1041 if ((po
->po_type
->pot_free
)(NULL
, &obj
) ==
1042 _PROP_OBJECT_FREE_DONE
) {
1052 _PROP_REFCNT_LOCK();
1054 _PROP_REFCNT_UNLOCK();
1056 _PROP_ASSERT(parent
);
1057 /* One object was just freed. */
1059 (*po
->po_type
->pot_emergency_free
)(parent
);
1063 * prop_object_release --
1064 * Decrement the reference count on an object.
1066 * Free the object if we are releasing the final
1070 prop_object_release(prop_object_t obj
)
1072 struct _prop_object
*po
;
1073 struct _prop_stack stack
;
1074 void (*unlock
)(void);
1078 _prop_stack_init(&stack
);
1085 if (po
->po_type
->pot_lock
!= NULL
)
1086 po
->po_type
->pot_lock();
1088 /* Save pointer to object unlock function */
1089 unlock
= po
->po_type
->pot_unlock
;
1091 _PROP_REFCNT_LOCK();
1092 ocnt
= po
->po_refcnt
--;
1093 _PROP_REFCNT_UNLOCK();
1095 _PROP_ASSERT(ocnt
!= 0);
1103 ret
= (po
->po_type
->pot_free
)(&stack
, &obj
);
1108 if (ret
== _PROP_OBJECT_FREE_DONE
)
1111 _PROP_REFCNT_LOCK();
1113 _PROP_REFCNT_UNLOCK();
1114 } while (ret
== _PROP_OBJECT_FREE_RECURSE
);
1115 if (ret
== _PROP_OBJECT_FREE_FAILED
)
1116 prop_object_release_emergency(obj
);
1117 } while (_prop_stack_pop(&stack
, &obj
, NULL
, NULL
, NULL
));
1121 * prop_object_type --
1122 * Return the type of an object.
1125 prop_object_type(prop_object_t obj
)
1127 struct _prop_object
*po
= obj
;
1130 return (PROP_TYPE_UNKNOWN
);
1132 return (po
->po_type
->pot_type
);
1136 * prop_object_equals --
1137 * Returns true if thw two objects are equivalent.
1140 prop_object_equals(prop_object_t obj1
, prop_object_t obj2
)
1142 return (prop_object_equals_with_error(obj1
, obj2
, NULL
));
1146 prop_object_equals_with_error(prop_object_t obj1
, prop_object_t obj2
,
1149 struct _prop_object
*po1
;
1150 struct _prop_object
*po2
;
1151 void *stored_pointer1
, *stored_pointer2
;
1152 prop_object_t next_obj1
, next_obj2
;
1153 struct _prop_stack stack
;
1154 _prop_object_equals_rv_t ret
;
1156 _prop_stack_init(&stack
);
1158 *error_flag
= false;
1161 stored_pointer1
= NULL
;
1162 stored_pointer2
= NULL
;
1166 if (po1
->po_type
!= po2
->po_type
)
1170 ret
= (*po1
->po_type
->pot_equals
)(obj1
, obj2
,
1171 &stored_pointer1
, &stored_pointer2
,
1172 &next_obj1
, &next_obj2
);
1173 if (ret
== _PROP_OBJECT_EQUALS_FALSE
)
1175 if (ret
== _PROP_OBJECT_EQUALS_TRUE
) {
1176 if (!_prop_stack_pop(&stack
, &obj1
, &obj2
,
1177 &stored_pointer1
, &stored_pointer2
))
1179 goto continue_subtree
;
1181 _PROP_ASSERT(ret
== _PROP_OBJECT_EQUALS_RECURSE
);
1183 if (!_prop_stack_push(&stack
, obj1
, obj2
,
1184 stored_pointer1
, stored_pointer2
)) {
1194 while (_prop_stack_pop(&stack
, &obj1
, &obj2
, NULL
, NULL
)) {
1196 (*po1
->po_type
->pot_equals_finish
)(obj1
, obj2
);
1202 * prop_object_iterator_next --
1203 * Return the next item during an iteration.
1206 prop_object_iterator_next(prop_object_iterator_t pi
)
1209 return ((*pi
->pi_next_object
)(pi
));
1213 * prop_object_iterator_reset --
1214 * Reset the iterator to the first object so as to restart
1218 prop_object_iterator_reset(prop_object_iterator_t pi
)
1221 (*pi
->pi_reset
)(pi
);
1225 * prop_object_iterator_release --
1226 * Release the object iterator.
1229 prop_object_iterator_release(prop_object_iterator_t pi
)
1232 prop_object_release(pi
->pi_obj
);
1233 _PROP_FREE(pi
, M_TEMP
);