From 11ecf5438f2d3fc0f7d8f05649581071011e580d Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 10 Jul 2012 23:33:19 +0200 Subject: [PATCH] Import proplib-0.6.1. --- 3RDPARTY | 2 +- NEWS | 2 + lib/Makefile | 4 +- lib/portableproplib/prop/prop_array.h | 5 +- lib/portableproplib/prop/prop_dictionary.h | 5 + lib/portableproplib/prop/rbtree.h | 210 +++ lib/portableproplib/prop_array.c | 150 +-- lib/portableproplib/prop_array_util.c | 26 + lib/portableproplib/prop_bool.c | 39 +- lib/portableproplib/prop_dictionary.c | 242 +--- lib/portableproplib/prop_dictionary_util.c | 26 +- lib/portableproplib/prop_number.c | 107 +- lib/portableproplib/prop_object.c | 15 +- lib/portableproplib/prop_object_impl.h | 172 ++- lib/portableproplib/prop_rb.c | 1053 --------------- lib/portableproplib/prop_rb_impl.h | 21 +- lib/portableproplib/prop_zlib.c | 146 +++ lib/portableproplib/rb.c | 1335 ++++++++++++++++++++ 18 files changed, 2101 insertions(+), 1459 deletions(-) create mode 100644 lib/portableproplib/prop/rbtree.h delete mode 100644 lib/portableproplib/prop_rb.c create mode 100644 lib/portableproplib/prop_zlib.c create mode 100644 lib/portableproplib/rb.c diff --git a/3RDPARTY b/3RDPARTY index a483348e..ce44bf69 100644 --- a/3RDPARTY +++ b/3RDPARTY @@ -17,5 +17,5 @@ internal use in the code: - libfetch-2.31 from NetBSD: lib/fetch -- portableproplib-0.4.1 (lib/portableproplib) from +- portableproplib-0.6.1 (lib/portableproplib) from http://code.google.com/p/portableproplib diff --git a/NEWS b/NEWS index 2b3ee5ab..8ace3fc1 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,7 @@ xbps-0.16.4 (???): + * Imported proplib 0.6.1 from http://code.google.com/p/portableproplib. + * libxbps: when finding obsolete files also match against sha256, not just the filename. Also ignore symlinks found in rootfs to make the system transition to /usr fully work. diff --git a/lib/Makefile b/lib/Makefile index f577e679..97836ae1 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -9,10 +9,10 @@ LDFLAGS += $(LIBXBPS_LDFLAGS) -shared -Wl,-soname,libxbps.so.$(LIBXBPS_MAJOR) # portableproplib LIBPROP_OBJS = portableproplib/prop_array.o portableproplib/prop_bool.o LIBPROP_OBJS += portableproplib/prop_dictionary.o portableproplib/prop_ingest.o -LIBPROP_OBJS += portableproplib/prop_object.o portableproplib/prop_rb.o +LIBPROP_OBJS += portableproplib/prop_object.o portableproplib/rb.o LIBPROP_OBJS += portableproplib/prop_stack.o portableproplib/prop_string.o LIBPROP_OBJS += portableproplib/prop_array_util.o portableproplib/prop_number.o -LIBPROP_OBJS += portableproplib/prop_dictionary_util.o +LIBPROP_OBJS += portableproplib/prop_dictionary_util.o portableproplib/prop_zlib.o LIBPROP_OBJS += portableproplib/prop_data.o LIBPROP_CPPFLAGS = -D_GNU_SOURCE LIBPROP_CFLAGS = -Wno-old-style-definition -Wno-cast-qual -Wno-unused-parameter diff --git a/lib/portableproplib/prop/prop_array.h b/lib/portableproplib/prop/prop_array.h index 4baf7eb3..8a2e012e 100644 --- a/lib/portableproplib/prop/prop_array.h +++ b/lib/portableproplib/prop/prop_array.h @@ -129,13 +129,16 @@ bool prop_array_get_cstring(prop_array_t, unsigned int, char **); bool prop_array_set_cstring(prop_array_t, unsigned int, const char *); - +bool prop_array_add_cstring(prop_array_t, const char *); +bool prop_array_add_cstring_nocopy(prop_array_t, + const char *); bool prop_array_get_cstring_nocopy(prop_array_t, unsigned int, const char **); bool prop_array_set_cstring_nocopy(prop_array_t, unsigned int, const char *); +bool prop_array_add_and_rel(prop_array_t, prop_object_t); __END_DECLS diff --git a/lib/portableproplib/prop/prop_dictionary.h b/lib/portableproplib/prop/prop_dictionary.h index b4bc0290..7890e311 100644 --- a/lib/portableproplib/prop/prop_dictionary.h +++ b/lib/portableproplib/prop/prop_dictionary.h @@ -90,6 +90,8 @@ bool prop_dictionary_keysym_equals(prop_dictionary_keysym_t, * Utility routines to make it more convenient to work with values * stored in dictionaries. */ +bool prop_dictionary_get_dict(prop_dictionary_t, const char *, + prop_dictionary_t *); bool prop_dictionary_get_bool(prop_dictionary_t, const char *, bool *); bool prop_dictionary_set_bool(prop_dictionary_t, const char *, @@ -142,6 +144,9 @@ bool prop_dictionary_get_cstring_nocopy(prop_dictionary_t, bool prop_dictionary_set_cstring_nocopy(prop_dictionary_t, const char *, const char *); +bool prop_dictionary_set_and_rel(prop_dictionary_t, + const char *, + prop_object_t); __END_DECLS diff --git a/lib/portableproplib/prop/rbtree.h b/lib/portableproplib/prop/rbtree.h new file mode 100644 index 00000000..b32307d4 --- /dev/null +++ b/lib/portableproplib/prop/rbtree.h @@ -0,0 +1,210 @@ +/* $NetBSD: rbtree.h,v 1.1 2010/09/25 01:42:40 matt Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_RBTREE_H_ +#define _SYS_RBTREE_H_ + +#if defined(_KERNEL) || defined(_STANDALONE) +#include + +#else +#include +#include +#endif +#include + +#if __GNUC_PREREQ(2, 96) +# ifndef __predict_true +# define __predict_true(exp) __builtin_expect((exp), 1) +# endif +# ifndef __predict_false +# define __predict_false(exp) __builtin_expect((exp), 0) +# endif +#else +# ifndef __predict_true +# define __predict_true(exp) (exp) +# endif +# ifndef __predict_false +# define __predict_false(exp) (exp) +# endif +#endif + +__BEGIN_DECLS + +typedef struct rb_node { + struct rb_node *rb_nodes[2]; +#define RB_DIR_LEFT 0 +#define RB_DIR_RIGHT 1 +#define RB_DIR_OTHER 1 +#define rb_left rb_nodes[RB_DIR_LEFT] +#define rb_right rb_nodes[RB_DIR_RIGHT] + + /* + * rb_info contains the two flags and the parent back pointer. + * We put the two flags in the low two bits since we know that + * rb_node will have an alignment of 4 or 8 bytes. + */ + uintptr_t rb_info; +#define RB_FLAG_POSITION 0x2 +#define RB_FLAG_RED 0x1 +#define RB_FLAG_MASK (RB_FLAG_POSITION|RB_FLAG_RED) +#define RB_FATHER(rb) \ + ((struct rb_node *)((rb)->rb_info & ~RB_FLAG_MASK)) +#define RB_SET_FATHER(rb, father) \ + ((void)((rb)->rb_info = (uintptr_t)(father)|((rb)->rb_info & RB_FLAG_MASK))) + +#define RB_SENTINEL_P(rb) ((rb) == NULL) +#define RB_LEFT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_left) +#define RB_RIGHT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_right) +#define RB_FATHER_SENTINEL_P(rb) RB_SENTINEL_P(RB_FATHER((rb))) +#define RB_CHILDLESS_P(rb) \ + (RB_SENTINEL_P(rb) || (RB_LEFT_SENTINEL_P(rb) && RB_RIGHT_SENTINEL_P(rb))) +#define RB_TWOCHILDREN_P(rb) \ + (!RB_SENTINEL_P(rb) && !RB_LEFT_SENTINEL_P(rb) && !RB_RIGHT_SENTINEL_P(rb)) + +#define RB_POSITION(rb) \ + (((rb)->rb_info & RB_FLAG_POSITION) ? RB_DIR_RIGHT : RB_DIR_LEFT) +#define RB_RIGHT_P(rb) (RB_POSITION(rb) == RB_DIR_RIGHT) +#define RB_LEFT_P(rb) (RB_POSITION(rb) == RB_DIR_LEFT) +#define RB_RED_P(rb) (!RB_SENTINEL_P(rb) && ((rb)->rb_info & RB_FLAG_RED) != 0) +#define RB_BLACK_P(rb) (RB_SENTINEL_P(rb) || ((rb)->rb_info & RB_FLAG_RED) == 0) +#define RB_MARK_RED(rb) ((void)((rb)->rb_info |= RB_FLAG_RED)) +#define RB_MARK_BLACK(rb) ((void)((rb)->rb_info &= ~RB_FLAG_RED)) +#define RB_INVERT_COLOR(rb) ((void)((rb)->rb_info ^= RB_FLAG_RED)) +#define RB_ROOT_P(rbt, rb) ((rbt)->rbt_root == (rb)) +#define RB_SET_POSITION(rb, position) \ + ((void)((position) ? ((rb)->rb_info |= RB_FLAG_POSITION) : \ + ((rb)->rb_info &= ~RB_FLAG_POSITION))) +#define RB_ZERO_PROPERTIES(rb) ((void)((rb)->rb_info &= ~RB_FLAG_MASK)) +#define RB_COPY_PROPERTIES(dst, src) \ + ((void)((dst)->rb_info ^= ((dst)->rb_info ^ (src)->rb_info) & RB_FLAG_MASK)) +#define RB_SWAP_PROPERTIES(a, b) do { \ + uintptr_t xorinfo = ((a)->rb_info ^ (b)->rb_info) & RB_FLAG_MASK; \ + (a)->rb_info ^= xorinfo; \ + (b)->rb_info ^= xorinfo; \ + } while (/*CONSTCOND*/ 0) +#ifdef RBDEBUG + TAILQ_ENTRY(rb_node) rb_link; +#endif +} rb_node_t; + +#define RB_TREE_MIN(T) rb_tree_iterate((T), NULL, RB_DIR_LEFT) +#define RB_TREE_MAX(T) rb_tree_iterate((T), NULL, RB_DIR_RIGHT) +#define RB_TREE_FOREACH(N, T) \ + for ((N) = RB_TREE_MIN(T); (N); \ + (N) = rb_tree_iterate((T), (N), RB_DIR_RIGHT)) +#define RB_TREE_FOREACH_REVERSE(N, T) \ + for ((N) = RB_TREE_MAX(T); (N); \ + (N) = rb_tree_iterate((T), (N), RB_DIR_LEFT)) + +#ifdef RBDEBUG +TAILQ_HEAD(rb_node_qh, rb_node); + +#define RB_TAILQ_REMOVE(a, b, c) TAILQ_REMOVE(a, b, c) +#define RB_TAILQ_INIT(a) TAILQ_INIT(a) +#define RB_TAILQ_INSERT_HEAD(a, b, c) TAILQ_INSERT_HEAD(a, b, c) +#define RB_TAILQ_INSERT_BEFORE(a, b, c) TAILQ_INSERT_BEFORE(a, b, c) +#define RB_TAILQ_INSERT_AFTER(a, b, c, d) TAILQ_INSERT_AFTER(a, b, c, d) +#else +#define RB_TAILQ_REMOVE(a, b, c) do { } while (/*CONSTCOND*/0) +#define RB_TAILQ_INIT(a) do { } while (/*CONSTCOND*/0) +#define RB_TAILQ_INSERT_HEAD(a, b, c) do { } while (/*CONSTCOND*/0) +#define RB_TAILQ_INSERT_BEFORE(a, b, c) do { } while (/*CONSTCOND*/0) +#define RB_TAILQ_INSERT_AFTER(a, b, c, d) do { } while (/*CONSTCOND*/0) +#endif /* RBDEBUG */ + +/* + * rbto_compare_nodes_fn: + * return a positive value if the first node > the second node. + * return a negative value if the first node < the second node. + * return 0 if they are considered same. + * + * rbto_compare_key_fn: + * return a positive value if the node > the key. + * return a negative value if the node < the key. + * return 0 if they are considered same. + */ + +typedef signed int (*const rbto_compare_nodes_fn)(void *, + const void *, const void *); +typedef signed int (*const rbto_compare_key_fn)(void *, + const void *, const void *); + +typedef struct { + rbto_compare_nodes_fn rbto_compare_nodes; + rbto_compare_key_fn rbto_compare_key; + size_t rbto_node_offset; + void *rbto_context; +} rb_tree_ops_t; + +typedef struct rb_tree { + struct rb_node *rbt_root; + const rb_tree_ops_t *rbt_ops; + struct rb_node *rbt_minmax[2]; +#ifdef RBDEBUG + struct rb_node_qh rbt_nodes; +#endif +#ifdef RBSTATS + unsigned int rbt_count; + unsigned int rbt_insertions; + unsigned int rbt_removals; + unsigned int rbt_insertion_rebalance_calls; + unsigned int rbt_insertion_rebalance_passes; + unsigned int rbt_removal_rebalance_calls; + unsigned int rbt_removal_rebalance_passes; +#endif +} rb_tree_t; + +#ifdef RBSTATS +#define RBSTAT_INC(v) ((void)((v)++)) +#define RBSTAT_DEC(v) ((void)((v)--)) +#else +#define RBSTAT_INC(v) do { } while (/*CONSTCOND*/0) +#define RBSTAT_DEC(v) do { } while (/*CONSTCOND*/0) +#endif + +void rb_tree_init(rb_tree_t *, const rb_tree_ops_t *); +void * rb_tree_insert_node(rb_tree_t *, void *); +void * rb_tree_find_node(rb_tree_t *, const void *); +void * rb_tree_find_node_geq(rb_tree_t *, const void *); +void * rb_tree_find_node_leq(rb_tree_t *, const void *); +void rb_tree_remove_node(rb_tree_t *, void *); +void * rb_tree_iterate(rb_tree_t *, void *, const unsigned int); +#ifdef RBDEBUG +void rb_tree_check(const rb_tree_t *, bool); +#endif +#ifdef RBSTATS +void rb_tree_depths(const rb_tree_t *, size_t *); +#endif + +__END_DECLS + +#endif /* _SYS_RBTREE_H_*/ diff --git a/lib/portableproplib/prop_array.c b/lib/portableproplib/prop_array.c index 9a66acd4..8c7c94f4 100644 --- a/lib/portableproplib/prop_array.c +++ b/lib/portableproplib/prop_array.c @@ -1,30 +1,5 @@ /* $NetBSD: prop_array.c,v 1.20 2008/08/11 05:54:21 christos Exp $ */ -/*- - * Copyright (c) 2010 Juan Romero Pardines (zlib/gzip support). - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - /*- * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc. * All rights reserved. @@ -56,9 +31,11 @@ #include #include "prop_object_impl.h" -#include -#include +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#define __unused /* empty */ +#endif struct _prop_array { struct _prop_object pa_obj; @@ -364,7 +341,7 @@ static prop_object_t _prop_array_iterator_next_object(void *v) { struct _prop_array_iterator *pai = v; - prop_array_t pa = pai->pai_base.pi_obj; + prop_array_t pa __unused = pai->pai_base.pi_obj; prop_object_t po; _PROP_ASSERT(prop_object_is_array(pa)); @@ -391,7 +368,7 @@ static void _prop_array_iterator_reset(void *v) { struct _prop_array_iterator *pai = v; - prop_array_t pa = pai->pai_base.pi_obj; + prop_array_t pa __unused = pai->pai_base.pi_obj; _PROP_ASSERT(prop_object_is_array(pa)); @@ -889,6 +866,7 @@ prop_array_internalize(const char *xml) return _prop_generic_internalize(xml, "array"); } +#if !defined(_KERNEL) && !defined(_STANDALONE) /* * prop_array_externalize_to_file -- * Externalize an array to the specified file. @@ -903,8 +881,7 @@ prop_array_externalize_to_file(prop_array_t array, const char *fname) xml = prop_array_externalize(array); if (xml == NULL) return (false); - rv = _prop_object_externalize_write_file(fname, xml, - strlen(xml), false); + rv = _prop_object_externalize_write_file(fname, xml, strlen(xml), false); if (rv == false) save_errno = errno; _PROP_FREE(xml, M_TEMP); @@ -914,31 +891,6 @@ prop_array_externalize_to_file(prop_array_t array, const char *fname) return (rv); } -/* - * prop_array_externalize_to_zfile --- - * Externalize an array to the specified file, and on the fly - * compressing the result with gzip (via zlib). - */ -bool -prop_array_externalize_to_zfile(prop_array_t array, const char *fname) -{ - char *xml; - bool rv; - int save_errno = 0; - - xml = prop_array_externalize(array); - if (xml == NULL) - return false; - rv = _prop_object_externalize_write_file(fname, xml, strlen(xml), true); - if (rv == false) - save_errno = errno; - _PROP_FREE(xml, M_TEMP); - if (rv == false) - errno = save_errno; - - return rv; -} - /* * prop_array_internalize_from_file -- * Internalize an array from a file. @@ -957,88 +909,4 @@ prop_array_internalize_from_file(const char *fname) return (array); } - -#define _READ_CHUNK 512 -/* - * prop_array_internalize_from_zfile --- - * Internalize an array from a compressed gzip file. - */ -prop_array_t -prop_array_internalize_from_zfile(const char *fname) -{ - struct _prop_object_internalize_mapped_file *mf; - prop_array_t array; - z_stream strm; - unsigned char out[_READ_CHUNK]; - char *uncomp_xml = NULL; - size_t have; - ssize_t totalsize = 0; - int rv = 0; - - mf = _prop_object_internalize_map_file(fname); - if (mf == NULL) - return NULL; - - /* Decompress the mmap'ed buffer with zlib */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.avail_in = 0; - strm.next_in = Z_NULL; - - /* 15+16 to use gzip method */ - if (inflateInit2(&strm, 15+16) != Z_OK) { - _prop_object_internalize_unmap_file(mf); - return NULL; - } - - strm.avail_in = mf->poimf_mapsize; - strm.next_in = (unsigned char *)mf->poimf_xml; - - /* Output buffer (decompressed) */ - uncomp_xml = _PROP_MALLOC(_READ_CHUNK, M_TEMP); - if (uncomp_xml == NULL) { - _prop_object_internalize_unmap_file(mf); - (void)inflateEnd(&strm); - return NULL; - } - - /* Inflate the input buffer and copy into 'dest' */ - do { - strm.avail_out = _READ_CHUNK; - strm.next_out = out; - rv = inflate(&strm, Z_NO_FLUSH); - switch (rv) { - case Z_DATA_ERROR: - /* - * Wrong compressed data or uncompressed, try - * normal method as last resort. - */ - (void)inflateEnd(&strm); - _PROP_FREE(uncomp_xml, M_TEMP); - array = prop_array_internalize(mf->poimf_xml); - _prop_object_internalize_unmap_file(mf); - return array; - case Z_STREAM_ERROR: - case Z_NEED_DICT: - case Z_MEM_ERROR: - (void)inflateEnd(&strm); - _PROP_FREE(uncomp_xml, M_TEMP); - _prop_object_internalize_unmap_file(mf); - errno = rv; - return NULL; - } - have = _READ_CHUNK - strm.avail_out; - totalsize += have; - uncomp_xml = _PROP_REALLOC(uncomp_xml, totalsize, M_TEMP); - memcpy(uncomp_xml + totalsize - have, out, have); - } while (strm.avail_out == 0); - - /* we are done */ - (void)inflateEnd(&strm); - array = prop_array_internalize(uncomp_xml); - _PROP_FREE(uncomp_xml, M_TEMP); - _prop_object_internalize_unmap_file(mf); - - return array; -} +#endif /* _KERNEL && !_STANDALONE */ diff --git a/lib/portableproplib/prop_array_util.c b/lib/portableproplib/prop_array_util.c index 34bb226f..6c086bd4 100644 --- a/lib/portableproplib/prop_array_util.c +++ b/lib/portableproplib/prop_array_util.c @@ -218,6 +218,21 @@ prop_array_get_cstring ## variant (prop_array_t array, \ } \ \ bool \ +prop_array_add_cstring ## variant (prop_array_t array, \ + const char *cp) \ +{ \ + prop_string_t str; \ + bool rv; \ + \ + str = prop_string_create_cstring ## variant (cp); \ + if (str == NULL) \ + return false; \ + rv = prop_array_add(array, str); \ + prop_object_release(str); \ + return rv; \ +} \ + \ +bool \ prop_array_set_cstring ## variant (prop_array_t array, \ unsigned int indx, \ const char *cp) \ @@ -238,3 +253,14 @@ TEMPLATE(,) TEMPLATE(_nocopy,const) #undef TEMPLATE + +bool +prop_array_add_and_rel(prop_array_t array, prop_object_t po) +{ + bool ret; + if (po == NULL) + return false; + ret = prop_array_add(array, po); + prop_object_release(po); + return ret; +} diff --git a/lib/portableproplib/prop_bool.c b/lib/portableproplib/prop_bool.c index 4e2bc2bd..d9e912b2 100644 --- a/lib/portableproplib/prop_bool.c +++ b/lib/portableproplib/prop_bool.c @@ -1,4 +1,4 @@ -/* $NetBSD: prop_bool.c,v 1.16 2008/08/03 04:00:12 thorpej Exp $ */ +/* $NetBSD: prop_bool.c,v 1.17 2009/01/03 18:31:33 pooka Exp $ */ /*- * Copyright (c) 2006 The NetBSD Foundation, Inc. @@ -40,9 +40,6 @@ struct _prop_bool { static struct _prop_bool _prop_bool_true; static struct _prop_bool _prop_bool_false; -_PROP_MUTEX_DECL_STATIC(_prop_bool_initialized_mutex) -static bool _prop_bool_initialized; - static _prop_object_free_rv_t _prop_bool_free(prop_stack_t, prop_object_t *); static bool _prop_bool_externalize( @@ -109,27 +106,29 @@ _prop_bool_equals(prop_object_t v1, prop_object_t v2, return (_PROP_OBJECT_EQUALS_FALSE); } +_PROP_ONCE_DECL(_prop_bool_init_once) + +static int +_prop_bool_init(void) +{ + + _prop_object_init(&_prop_bool_true.pb_obj, + &_prop_object_type_bool); + _prop_bool_true.pb_value = true; + + _prop_object_init(&_prop_bool_false.pb_obj, + &_prop_object_type_bool); + _prop_bool_false.pb_value = false; + + return 0; +} + static prop_bool_t _prop_bool_alloc(bool val) { prop_bool_t pb; - if (! _prop_bool_initialized) { - _PROP_MUTEX_LOCK(_prop_bool_initialized_mutex); - if (! _prop_bool_initialized) { - _prop_object_init(&_prop_bool_true.pb_obj, - &_prop_object_type_bool); - _prop_bool_true.pb_value = true; - - _prop_object_init(&_prop_bool_false.pb_obj, - &_prop_object_type_bool); - _prop_bool_false.pb_value = false; - - _prop_bool_initialized = true; - } - _PROP_MUTEX_UNLOCK(_prop_bool_initialized_mutex); - } - + _PROP_ONCE_RUN(_prop_bool_init_once, _prop_bool_init); pb = val ? &_prop_bool_true : &_prop_bool_false; prop_object_retain(pb); diff --git a/lib/portableproplib/prop_dictionary.c b/lib/portableproplib/prop_dictionary.c index 4dddbc38..4c21fcb4 100644 --- a/lib/portableproplib/prop_dictionary.c +++ b/lib/portableproplib/prop_dictionary.c @@ -1,29 +1,4 @@ -/* $NetBSD: prop_dictionary.c,v 1.33 2008/11/30 00:17:07 haad Exp $ */ - -/*- - * Copyright (c) 2010 Juan Romero Pardines (zlib/gzip support). - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ +/* $NetBSD: prop_dictionary.c,v 1.37 2011/04/20 19:40:00 martin Exp $ */ /*- * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc. @@ -54,12 +29,16 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include +#include +#include +#include #include "prop_object_impl.h" #include "prop_rb_impl.h" +#if !defined(_KERNEL) && !defined(_STANDALONE) #include -#include +#define __unused /* empty */ +#endif /* * We implement these like arrays, but we keep them sorted by key. @@ -88,10 +67,6 @@ struct _prop_dictionary_keysym { /* actually variable length */ }; -#define RBNODE_TO_PDK(n) \ - ((struct _prop_dictionary_keysym *) \ - ((uintptr_t)n - offsetof(struct _prop_dictionary_keysym, pdk_link))) - /* pdk_key[1] takes care of the NUL */ #define PDK_SIZE_16 (sizeof(struct _prop_dictionary_keysym) + 16) #define PDK_SIZE_32 (sizeof(struct _prop_dictionary_keysym) + 32) @@ -198,35 +173,49 @@ struct _prop_dictionary_iterator { */ static int -_prop_dict_keysym_rb_compare_nodes(const struct rb_node *n1, - const struct rb_node *n2) +/*ARGSUSED*/ +_prop_dict_keysym_rb_compare_nodes(void *ctx __unused, + const void *n1, const void *n2) { - const prop_dictionary_keysym_t pdk1 = RBNODE_TO_PDK(n1); - const prop_dictionary_keysym_t pdk2 = RBNODE_TO_PDK(n2); + const struct _prop_dictionary_keysym *pdk1 = n1; + const struct _prop_dictionary_keysym *pdk2 = n2; - return (strcmp(pdk1->pdk_key, pdk2->pdk_key)); + return strcmp(pdk1->pdk_key, pdk2->pdk_key); } static int -_prop_dict_keysym_rb_compare_key(const struct rb_node *n, - const void *v) +/*ARGSUSED*/ +_prop_dict_keysym_rb_compare_key(void *ctx __unused, + const void *n, const void *v) { - const prop_dictionary_keysym_t pdk = RBNODE_TO_PDK(n); + const struct _prop_dictionary_keysym *pdk = n; const char *cp = v; - return (strcmp(pdk->pdk_key, cp)); + return strcmp(pdk->pdk_key, cp); } -static const struct rb_tree_ops _prop_dict_keysym_rb_tree_ops = { +static const rb_tree_ops_t _prop_dict_keysym_rb_tree_ops = { .rbto_compare_nodes = _prop_dict_keysym_rb_compare_nodes, - .rbto_compare_key = _prop_dict_keysym_rb_compare_key, + .rbto_compare_key = _prop_dict_keysym_rb_compare_key, + .rbto_node_offset = offsetof(struct _prop_dictionary_keysym, pdk_link), + .rbto_context = NULL }; static struct rb_tree _prop_dict_keysym_tree; -static bool _prop_dict_keysym_tree_initialized; +_PROP_ONCE_DECL(_prop_dict_init_once) _PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex) +static int +_prop_dict_init(void) +{ + + _PROP_MUTEX_INIT(_prop_dict_keysym_tree_mutex); + _prop_rb_tree_init(&_prop_dict_keysym_tree, + &_prop_dict_keysym_rb_tree_ops); + return 0; +} + static void _prop_dict_keysym_put(prop_dictionary_keysym_t pdk) { @@ -247,7 +236,7 @@ _prop_dict_keysym_free(prop_stack_t stack, prop_object_t *obj) { prop_dictionary_keysym_t pdk = *obj; - _prop_rb_tree_remove_node(&_prop_dict_keysym_tree, &pdk->pdk_link); + _prop_rb_tree_remove_node(&_prop_dict_keysym_tree, pdk); _prop_dict_keysym_put(pdk); return _PROP_OBJECT_FREE_DONE; @@ -294,28 +283,21 @@ _prop_dict_keysym_equals(prop_object_t v1, prop_object_t v2, static prop_dictionary_keysym_t _prop_dict_keysym_alloc(const char *key) { - prop_dictionary_keysym_t opdk, pdk; - const struct rb_node *n; + prop_dictionary_keysym_t opdk, pdk, rpdk; size_t size; - bool rv; + + _PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init); /* * Check to see if this already exists in the tree. If it does, * we just retain it and return it. */ _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex); - if (! _prop_dict_keysym_tree_initialized) { - _prop_rb_tree_init(&_prop_dict_keysym_tree, - &_prop_dict_keysym_rb_tree_ops); - _prop_dict_keysym_tree_initialized = true; - } else { - n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key); - if (n != NULL) { - opdk = RBNODE_TO_PDK(n); - prop_object_retain(opdk); - _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex); - return (opdk); - } + opdk = _prop_rb_tree_find(&_prop_dict_keysym_tree, key); + if (opdk != NULL) { + prop_object_retain(opdk); + _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex); + return (opdk); } _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex); @@ -347,16 +329,15 @@ _prop_dict_keysym_alloc(const char *key) * we have to check again if it is in the tree. */ _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex); - n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key); - if (n != NULL) { - opdk = RBNODE_TO_PDK(n); + opdk = _prop_rb_tree_find(&_prop_dict_keysym_tree, key); + if (opdk != NULL) { prop_object_retain(opdk); _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex); _prop_dict_keysym_put(pdk); return (opdk); } - rv = _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, &pdk->pdk_link); - _PROP_ASSERT(rv == true); + rpdk = _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, pdk); + _PROP_ASSERT(rpdk == pdk); _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex); return (pdk); } @@ -416,6 +397,9 @@ _prop_dictionary_free(prop_stack_t stack, prop_object_t *obj) static void _prop_dictionary_lock(void) { + + /* XXX: once necessary or paranoia? */ + _PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init); _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex); } @@ -540,8 +524,8 @@ _prop_dictionary_equals(prop_object_t v1, prop_object_t v2, *stored_pointer1 = (void *)(idx + 1); *stored_pointer2 = (void *)(idx + 1); - *next_obj1 = &dict1->pd_array[idx].pde_objref; - *next_obj2 = &dict2->pd_array[idx].pde_objref; + *next_obj1 = dict1->pd_array[idx].pde_objref; + *next_obj2 = dict2->pd_array[idx].pde_objref; if (!prop_dictionary_keysym_equals(dict1->pd_array[idx].pde_key, dict2->pd_array[idx].pde_key)) @@ -645,7 +629,7 @@ static prop_object_t _prop_dictionary_iterator_next_object(void *v) { struct _prop_dictionary_iterator *pdi = v; - prop_dictionary_t pd = pdi->pdi_base.pi_obj; + prop_dictionary_t pd __unused = pdi->pdi_base.pi_obj; prop_dictionary_keysym_t pdk; _PROP_ASSERT(prop_object_is_dictionary(pd)); @@ -672,7 +656,7 @@ static void _prop_dictionary_iterator_reset(void *v) { struct _prop_dictionary_iterator *pdi = v; - prop_dictionary_t pd = pdi->pdi_base.pi_obj; + prop_dictionary_t pd __unused = pdi->pdi_base.pi_obj; _PROP_RWLOCK_RDLOCK(pd->pd_rwlock); _prop_dictionary_iterator_reset_locked(pdi); @@ -949,7 +933,10 @@ _prop_dictionary_get(prop_dictionary_t pd, const char *key, bool locked) prop_object_t prop_dictionary_get(prop_dictionary_t pd, const char *key) { - prop_object_t po; + prop_object_t po = NULL; + + if (! prop_object_is_dictionary(pd)) + return (NULL); _PROP_RWLOCK_RDLOCK(pd->pd_rwlock); po = _prop_dictionary_get(pd, key, true); @@ -1388,6 +1375,7 @@ prop_dictionary_internalize(const char *xml) return _prop_generic_internalize(xml, "dict"); } +#if !defined(_KERNEL) && !defined(_STANDALONE) /* * prop_dictionary_externalize_to_file -- * Externalize a dictionary to the specified file. @@ -1402,8 +1390,8 @@ prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname) xml = prop_dictionary_externalize(dict); if (xml == NULL) return (false); - rv = _prop_object_externalize_write_file(fname, xml, - strlen(xml), false); + rv = _prop_object_externalize_write_file(fname, xml, strlen(xml), + false); if (rv == false) save_errno = errno; _PROP_FREE(xml, M_TEMP); @@ -1413,31 +1401,6 @@ prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname) return (rv); } -/* - * prop_dictionary_externalize_to_zfile --- - * Externalize a dictionary to the specified file and on the fly - * compressing the result with gzip (via zlib). - */ -bool -prop_dictionary_externalize_to_zfile(prop_dictionary_t dict, const char *fname) -{ - char *xml; - bool rv; - int save_errno = 0; - - xml = prop_dictionary_externalize(dict); - if (xml == NULL) - return false; - rv = _prop_object_externalize_write_file(fname, xml, strlen(xml), true); - if (rv == false) - save_errno = errno; - _PROP_FREE(xml, M_TEMP); - if (rv == false) - errno = save_errno; - - return rv; -} - /* * prop_dictionary_internalize_from_file -- * Internalize a dictionary from a file. @@ -1456,87 +1419,4 @@ prop_dictionary_internalize_from_file(const char *fname) return (dict); } - -#define _READ_CHUNK 512 -/* - * prop_dictionary_internalize_from_zfile --- - * Internalize a dictionary from a compressed gzip file. - */ -prop_dictionary_t -prop_dictionary_internalize_from_zfile(const char *fname) -{ - struct _prop_object_internalize_mapped_file *mf; - prop_dictionary_t dict; - z_stream strm; - unsigned char out[_READ_CHUNK]; - char *uncomp_xml = NULL; - size_t have; - ssize_t totalsize = 0; - int rv = 0; - - mf = _prop_object_internalize_map_file(fname); - if (mf == NULL) - return NULL; - - /* Decompress the mmap'ed buffer with zlib */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.avail_in = 0; - strm.next_in = Z_NULL; - - /* 15+16 to use gzip method */ - if (inflateInit2(&strm, 15+16) != Z_OK) { - _prop_object_internalize_unmap_file(mf); - return NULL; - } - strm.avail_in = mf->poimf_mapsize; - strm.next_in = (unsigned char *)mf->poimf_xml; - - /* Output buffer (uncompressed) */ - uncomp_xml = _PROP_MALLOC(_READ_CHUNK, M_TEMP); - if (uncomp_xml == NULL) { - (void)inflateEnd(&strm); - _prop_object_internalize_unmap_file(mf); - return NULL; - } - - /* Inflate the input buffer and copy into 'uncomp_xml' */ - do { - strm.avail_out = _READ_CHUNK; - strm.next_out = out; - rv = inflate(&strm, Z_NO_FLUSH); - switch (rv) { - case Z_DATA_ERROR: - /* - * Wrong compressed data or uncompressed, try - * normal method as last resort. - */ - (void)inflateEnd(&strm); - _PROP_FREE(uncomp_xml, M_TEMP); - dict = prop_dictionary_internalize(mf->poimf_xml); - _prop_object_internalize_unmap_file(mf); - return dict; - case Z_STREAM_ERROR: - case Z_NEED_DICT: - case Z_MEM_ERROR: - (void)inflateEnd(&strm); - _PROP_FREE(uncomp_xml, M_TEMP); - _prop_object_internalize_unmap_file(mf); - errno = rv; - return NULL; - } - have = _READ_CHUNK - strm.avail_out; - totalsize += have; - uncomp_xml = _PROP_REALLOC(uncomp_xml, totalsize, M_TEMP); - memcpy(uncomp_xml + totalsize - have, out, have); - } while (strm.avail_out == 0); - - /* we are done */ - (void)inflateEnd(&strm); - dict = prop_dictionary_internalize(uncomp_xml); - _PROP_FREE(uncomp_xml, M_TEMP); - _prop_object_internalize_unmap_file(mf); - - return dict; -} +#endif /* !_KERNEL && !_STANDALONE */ diff --git a/lib/portableproplib/prop_dictionary_util.c b/lib/portableproplib/prop_dictionary_util.c index a26443e4..65baf73c 100644 --- a/lib/portableproplib/prop_dictionary_util.c +++ b/lib/portableproplib/prop_dictionary_util.c @@ -1,4 +1,4 @@ -/* $NetBSD: prop_dictionary_util.c,v 1.3 2008/04/28 20:22:53 martin Exp $ */ +/* $NetBSD: prop_dictionary_util.c,v 1.4 2011/03/24 17:05:39 bouyer Exp $ */ /*- * Copyright (c) 2006 The NetBSD Foundation, Inc. @@ -41,6 +41,18 @@ #include #include "prop_object_impl.h" /* only to hide kernel vs. not-kernel */ +bool +prop_dictionary_get_dict(prop_dictionary_t dict, const char *key, prop_dictionary_t *dp) +{ + prop_object_t o; + o = prop_dictionary_get(dict, key); + if (o == NULL || prop_object_type(o) != PROP_TYPE_DICTIONARY) + return false; + *dp = o; + return true; + +} + bool prop_dictionary_get_bool(prop_dictionary_t dict, const char *key, @@ -206,3 +218,15 @@ TEMPLATE(,) TEMPLATE(_nocopy,const) #undef TEMPLATE + +bool +prop_dictionary_set_and_rel(prop_dictionary_t dict, const char *key, + prop_object_t po) +{ + bool ret; + if (po == NULL) + return false; + ret = prop_dictionary_set(dict, key, po); + prop_object_release(po); + return ret; +} diff --git a/lib/portableproplib/prop_number.c b/lib/portableproplib/prop_number.c index 059d8e38..dfd5e42a 100644 --- a/lib/portableproplib/prop_number.c +++ b/lib/portableproplib/prop_number.c @@ -1,4 +1,4 @@ -/* $NetBSD: prop_number.c,v 1.20 2008/11/30 00:17:07 haad Exp $ */ +/* $NetBSD: prop_number.c,v 1.23 2010/09/24 22:51:52 rmind Exp $ */ /*- * Copyright (c) 2006 The NetBSD Foundation, Inc. @@ -33,8 +33,16 @@ #include "prop_object_impl.h" #include "prop_rb_impl.h" +#if defined(_KERNEL) +#include +#elif defined(_STANDALONE) +#include +#include +#else #include #include +#define __unused /* empty */ +#endif struct _prop_number { struct _prop_object pn_obj; @@ -51,10 +59,6 @@ struct _prop_number { } pn_value; }; -#define RBNODE_TO_PN(n) \ - ((struct _prop_number *) \ - ((uintptr_t)n - offsetof(struct _prop_number, pn_link))) - _PROP_POOL_INIT(_prop_number_pool, sizeof(struct _prop_number), "propnmbr") static _prop_object_free_rv_t @@ -115,33 +119,34 @@ _prop_number_compare_values(const struct _prop_number_value *pnv1, } static int -_prop_number_rb_compare_nodes(const struct rb_node *n1, - const struct rb_node *n2) +/*ARGSUSED*/ +_prop_number_rb_compare_nodes(void *ctx __unused, + const void *n1, const void *n2) { - const prop_number_t pn1 = RBNODE_TO_PN(n1); - const prop_number_t pn2 = RBNODE_TO_PN(n2); + const struct _prop_number *pn1 = n1; + const struct _prop_number *pn2 = n2; - return (_prop_number_compare_values(&pn1->pn_value, &pn2->pn_value)); + return _prop_number_compare_values(&pn1->pn_value, &pn2->pn_value); } static int -_prop_number_rb_compare_key(const struct rb_node *n, - const void *v) +/*ARGSUSED*/ +_prop_number_rb_compare_key(void *ctx __unused, const void *n, const void *v) { - const prop_number_t pn = RBNODE_TO_PN(n); + const struct _prop_number *pn = n; const struct _prop_number_value *pnv = v; - return (_prop_number_compare_values(&pn->pn_value, pnv)); + return _prop_number_compare_values(&pn->pn_value, pnv); } -static const struct rb_tree_ops _prop_number_rb_tree_ops = { +static const rb_tree_ops_t _prop_number_rb_tree_ops = { .rbto_compare_nodes = _prop_number_rb_compare_nodes, - .rbto_compare_key = _prop_number_rb_compare_key, + .rbto_compare_key = _prop_number_rb_compare_key, + .rbto_node_offset = offsetof(struct _prop_number, pn_link), + .rbto_context = NULL }; static struct rb_tree _prop_number_tree; -static bool _prop_number_tree_initialized; - _PROP_MUTEX_DECL_STATIC(_prop_number_tree_mutex) /* ARGSUSED */ @@ -150,21 +155,34 @@ _prop_number_free(prop_stack_t stack, prop_object_t *obj) { prop_number_t pn = *obj; - _prop_rb_tree_remove_node(&_prop_number_tree, &pn->pn_link); + _prop_rb_tree_remove_node(&_prop_number_tree, pn); _PROP_POOL_PUT(_prop_number_pool, pn); return (_PROP_OBJECT_FREE_DONE); } -static void -_prop_number_lock() +_PROP_ONCE_DECL(_prop_number_init_once) + +static int +_prop_number_init(void) { + + _PROP_MUTEX_INIT(_prop_number_tree_mutex); + _prop_rb_tree_init(&_prop_number_tree, &_prop_number_rb_tree_ops); + return 0; +} + +static void +_prop_number_lock(void) +{ + /* XXX: init necessary? */ + _PROP_ONCE_RUN(_prop_number_init_once, _prop_number_init); _PROP_MUTEX_LOCK(_prop_number_tree_mutex); } static void -_prop_number_unlock() +_prop_number_unlock(void) { _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex); } @@ -178,8 +196,9 @@ _prop_number_externalize(struct _prop_object_externalize_context *ctx, /* * For the record: - * The original implementation used hex for signed numbers, - * but we changed it to be human readable. + * the original NetBSD implementation used hexadecimal for unsigned + * numbers, but in the portable proplib we changed it to be human + * readable (base 10). */ if (pn->pn_value.pnv_is_unsigned) sprintf(tmpstr, "%" PRIu64, pn->pn_value.pnv_unsigned); @@ -253,27 +272,20 @@ _prop_number_equals(prop_object_t v1, prop_object_t v2, static prop_number_t _prop_number_alloc(const struct _prop_number_value *pnv) { - prop_number_t opn, pn; - struct rb_node *n; - bool rv; + prop_number_t opn, pn, rpn; + + _PROP_ONCE_RUN(_prop_number_init_once, _prop_number_init); /* * Check to see if this already exists in the tree. If it does, * we just retain it and return it. */ _PROP_MUTEX_LOCK(_prop_number_tree_mutex); - if (! _prop_number_tree_initialized) { - _prop_rb_tree_init(&_prop_number_tree, - &_prop_number_rb_tree_ops); - _prop_number_tree_initialized = true; - } else { - n = _prop_rb_tree_find(&_prop_number_tree, pnv); - if (n != NULL) { - opn = RBNODE_TO_PN(n); - prop_object_retain(opn); - _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex); - return (opn); - } + opn = _prop_rb_tree_find(&_prop_number_tree, pnv); + if (opn != NULL) { + prop_object_retain(opn); + _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex); + return (opn); } _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex); @@ -294,16 +306,15 @@ _prop_number_alloc(const struct _prop_number_value *pnv) * we have to check again if it is in the tree. */ _PROP_MUTEX_LOCK(_prop_number_tree_mutex); - n = _prop_rb_tree_find(&_prop_number_tree, pnv); - if (n != NULL) { - opn = RBNODE_TO_PN(n); + opn = _prop_rb_tree_find(&_prop_number_tree, pnv); + if (opn != NULL) { prop_object_retain(opn); _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex); _PROP_POOL_PUT(_prop_number_pool, pn); return (opn); } - rv = _prop_rb_tree_insert_node(&_prop_number_tree, &pn->pn_link); - _PROP_ASSERT(rv == true); + rpn = _prop_rb_tree_insert_node(&_prop_number_tree, pn); + _PROP_ASSERT(rpn == pn); _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex); return (pn); } @@ -501,10 +512,14 @@ _prop_number_internalize_unsigned(struct _prop_object_internalize_context *ctx, _PROP_ASSERT(/*CONSTCOND*/sizeof(unsigned long long) == sizeof(uint64_t)); +#ifndef _KERNEL errno = 0; +#endif pnv->pnv_unsigned = (uint64_t) strtoull(ctx->poic_cp, &cp, 0); +#ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */ if (pnv->pnv_unsigned == UINT64_MAX && errno == ERANGE) return (false); +#endif pnv->pnv_is_unsigned = true; ctx->poic_cp = cp; @@ -519,11 +534,15 @@ _prop_number_internalize_signed(struct _prop_object_internalize_context *ctx, _PROP_ASSERT(/*CONSTCOND*/sizeof(long long) == sizeof(int64_t)); +#ifndef _KERNEL errno = 0; +#endif pnv->pnv_signed = (int64_t) strtoll(ctx->poic_cp, &cp, 0); +#ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */ if ((pnv->pnv_signed == INT64_MAX || pnv->pnv_signed == INT64_MIN) && errno == ERANGE) return (false); +#endif pnv->pnv_is_unsigned = false; ctx->poic_cp = cp; diff --git a/lib/portableproplib/prop_object.c b/lib/portableproplib/prop_object.c index 06c26e4c..087110c2 100644 --- a/lib/portableproplib/prop_object.c +++ b/lib/portableproplib/prop_object.c @@ -41,10 +41,6 @@ #include -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - /* * _prop_object_init -- * Initialize an object. Called when sub-classes create @@ -816,10 +812,7 @@ _prop_object_externalize_write_file(const char *fname, const char *xml, size_t len, bool do_compress) { gzFile gzf = NULL; - char tname[PATH_MAX]; -#ifndef HAVE_STRLCAT - char *otname; -#endif + char tname[PATH_MAX], *otname; int fd; int save_errno; mode_t myumask; @@ -865,7 +858,11 @@ _prop_object_externalize_write_file(const char *fname, const char *xml, goto bad; } +#ifdef HAVE_FDATASYNC + if (fdatasync(fd) == -1) +#else if (fsync(fd) == -1) +#endif goto bad; myumask = umask(0); @@ -1190,6 +1187,8 @@ prop_object_equals_with_error(prop_object_t obj1, prop_object_t obj2, if (!_prop_stack_pop(&stack, &obj1, &obj2, &stored_pointer1, &stored_pointer2)) return true; + po1 = obj1; + po2 = obj2; goto continue_subtree; } _PROP_ASSERT(ret == _PROP_OBJECT_EQUALS_RECURSE); diff --git a/lib/portableproplib/prop_object_impl.h b/lib/portableproplib/prop_object_impl.h index 4a204437..6251709f 100644 --- a/lib/portableproplib/prop_object_impl.h +++ b/lib/portableproplib/prop_object_impl.h @@ -1,4 +1,4 @@ -/* $NetBSD: prop_object_impl.h,v 1.28 2008/11/30 00:17:07 haad Exp $ */ +/* $NetBSD: prop_object_impl.h,v 1.30 2009/09/13 18:45:10 pooka Exp $ */ /*- * Copyright (c) 2006 The NetBSD Foundation, Inc. @@ -32,7 +32,11 @@ #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_ #define _PROPLIB_PROP_OBJECT_IMPL_H_ +#if defined(_KERNEL) || defined(_STANDALONE) +#include +#else #include +#endif #include "prop_stack.h" @@ -143,9 +147,9 @@ struct _prop_object_internalize_context * void _prop_object_internalize_context_free( struct _prop_object_internalize_context *); +#if !defined(_KERNEL) && !defined(_STANDALONE) bool _prop_object_externalize_write_file(const char *, - const char *, - size_t, bool); + const char *, size_t, bool); struct _prop_object_internalize_mapped_file { char * poimf_xml; @@ -156,6 +160,7 @@ struct _prop_object_internalize_mapped_file * _prop_object_internalize_map_file(const char *); void _prop_object_internalize_unmap_file( struct _prop_object_internalize_mapped_file *); +#endif /* !_KERNEL && !_STANDALONE */ typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *, struct _prop_object_internalize_context *); @@ -226,6 +231,110 @@ struct _prop_object_iterator { uint32_t pi_version; }; +#define _PROP_NOTHREAD_ONCE_DECL(x) static bool x = false; +#define _PROP_NOTHREAD_ONCE_RUN(x,f) \ + do { \ + if ((x) == false) { \ + f(); \ + x = true; \ + } \ + } while (/*CONSTCOND*/0) + +#if defined(_KERNEL) + +/* + * proplib in the kernel... + */ + +#include +#include +#include +#include +#include +#include + +#define _PROP_ASSERT(x) KASSERT(x) + +#define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK) +#define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO) +#define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK) +#define _PROP_FREE(v, t) free((v), (t)) + +#define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK) +#define _PROP_POOL_PUT(p, v) pool_put(&(p), (v)) + +struct prop_pool_init { + struct pool *pp; + size_t size; + const char *wchan; +}; +#define _PROP_POOL_INIT(pp, size, wchan) \ +struct pool pp; \ +static const struct prop_pool_init _link_ ## pp[1] = { \ + { &pp, size, wchan } \ +}; \ +__link_set_add_rodata(prop_linkpools, _link_ ## pp); + +#define _PROP_MALLOC_DEFINE(t, s, l) \ + MALLOC_DEFINE(t, s, l); + +#define _PROP_MUTEX_DECL_STATIC(x) static kmutex_t x; +#define _PROP_MUTEX_INIT(x) mutex_init(&(x),MUTEX_DEFAULT,IPL_NONE) +#define _PROP_MUTEX_LOCK(x) mutex_enter(&(x)) +#define _PROP_MUTEX_UNLOCK(x) mutex_exit(&(x)) + +#define _PROP_RWLOCK_DECL(x) krwlock_t x ; +#define _PROP_RWLOCK_INIT(x) rw_init(&(x)) +#define _PROP_RWLOCK_RDLOCK(x) rw_enter(&(x), RW_READER) +#define _PROP_RWLOCK_WRLOCK(x) rw_enter(&(x), RW_WRITER) +#define _PROP_RWLOCK_UNLOCK(x) rw_exit(&(x)) +#define _PROP_RWLOCK_DESTROY(x) rw_destroy(&(x)) + +#define _PROP_ONCE_DECL(x) static ONCE_DECL(x); +#define _PROP_ONCE_RUN(x,f) RUN_ONCE(&(x), f) + +#elif defined(_STANDALONE) + +/* + * proplib in a standalone environment... + */ + +#include + +void * _prop_standalone_calloc(size_t); +void * _prop_standalone_realloc(void *, size_t); + +#define _PROP_ASSERT(x) /* nothing */ + +#define _PROP_MALLOC(s, t) alloc((s)) +#define _PROP_CALLOC(s, t) _prop_standalone_calloc((s)) +#define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s)) +#define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */ + +#define _PROP_POOL_GET(p) alloc((p)) +#define _PROP_POOL_PUT(p, v) dealloc((v), (p)) + +#define _PROP_POOL_INIT(p, s, d) static const size_t p = s; + +#define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ + +#define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ +#define _PROP_MUTEX_INIT(x) /* nothing */ +#define _PROP_MUTEX_LOCK(x) /* nothing */ +#define _PROP_MUTEX_UNLOCK(x) /* nothing */ + +#define _PROP_RWLOCK_DECL(x) /* nothing */ +#define _PROP_RWLOCK_INIT(x) /* nothing */ +#define _PROP_RWLOCK_RDLOCK(x) /* nothing */ +#define _PROP_RWLOCK_WRLOCK(x) /* nothing */ +#define _PROP_RWLOCK_UNLOCK(x) /* nothing */ +#define _PROP_RWLOCK_DESTROY(x) /* nothing */ + +#define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) +#define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) + +#else + /* * proplib in user space... */ @@ -250,12 +359,53 @@ struct _prop_object_iterator { #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ +#if defined(__NetBSD__) && defined(_LIBPROP) +/* + * Use the same mechanism as libc; we get pthread mutexes for threaded + * programs and do-nothing stubs for non-threaded programs. + */ +#include "reentrant.h" +#define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x; +#define _PROP_MUTEX_INIT(x) mutex_init(&(x), NULL) +#define _PROP_MUTEX_LOCK(x) mutex_lock(&(x)) +#define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x)) + +#define _PROP_RWLOCK_DECL(x) rwlock_t x ; +#define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL) +#define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x)) +#define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x)) +#define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x)) +#define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x)) + +#define _PROP_ONCE_DECL(x) \ + static pthread_once_t x = PTHREAD_ONCE_INIT; +#define _PROP_ONCE_RUN(x,f) thr_once(&(x), (void(*)(void))f); + +#elif defined(HAVE_NBTOOL_CONFIG_H) +/* + * None of NetBSD's build tools are multi-threaded. + */ +#define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ +#define _PROP_MUTEX_INIT(x) /* nothing */ +#define _PROP_MUTEX_LOCK(x) /* nothing */ +#define _PROP_MUTEX_UNLOCK(x) /* nothing */ + +#define _PROP_RWLOCK_DECL(x) /* nothing */ +#define _PROP_RWLOCK_INIT(x) /* nothing */ +#define _PROP_RWLOCK_RDLOCK(x) /* nothing */ +#define _PROP_RWLOCK_WRLOCK(x) /* nothing */ +#define _PROP_RWLOCK_UNLOCK(x) /* nothing */ +#define _PROP_RWLOCK_DESTROY(x) /* nothing */ + +#define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) +#define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) +#else /* * Use pthread mutexes everywhere else. */ #include -#define _PROP_MUTEX_DECL_STATIC(x) \ - static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER; +#define _PROP_MUTEX_DECL_STATIC(x) static pthread_mutex_t x; +#define _PROP_MUTEX_INIT(x) pthread_mutex_init(&(x), NULL) #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x)) #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) @@ -266,9 +416,21 @@ struct _prop_object_iterator { #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x)) #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x)) +#define _PROP_ONCE_DECL(x) \ + static pthread_once_t x = PTHREAD_ONCE_INIT; +#define _PROP_ONCE_RUN(x,f) pthread_once(&(x),(void(*)(void))f) +#endif + +#endif /* _KERNEL */ + /* * Language features. */ +#if defined(__NetBSD__) +#include +#define _PROP_ARG_UNUSED __unused +#else #define _PROP_ARG_UNUSED /* delete */ +#endif /* __NetBSD__ */ #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */ diff --git a/lib/portableproplib/prop_rb.c b/lib/portableproplib/prop_rb.c deleted file mode 100644 index 3d9f1548..00000000 --- a/lib/portableproplib/prop_rb.c +++ /dev/null @@ -1,1053 +0,0 @@ -/* $NetBSD: prop_rb.c,v 1.9 2008/06/17 21:29:47 thorpej Exp $ */ - -/*- - * Copyright (c) 2001 The NetBSD Foundation, Inc. - * All rights reserved. - * - * This code is derived from software contributed to The NetBSD Foundation - * by Matt Thomas . - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include "prop_object_impl.h" -#include "prop_rb_impl.h" - -#undef KASSERT -#ifdef RBDEBUG -#define KASSERT(x) _PROP_ASSERT(x) -#else -#define KASSERT(x) /* nothing */ -#endif - -#ifndef __predict_false -#define __predict_false(x) (x) -#endif - -static void rb_tree_reparent_nodes(struct rb_tree *, struct rb_node *, - unsigned int); -static void rb_tree_insert_rebalance(struct rb_tree *, struct rb_node *); -static void rb_tree_removal_rebalance(struct rb_tree *, struct rb_node *, - unsigned int); -#ifdef RBDEBUG -static const struct rb_node *rb_tree_iterate_const(const struct rb_tree *, - const struct rb_node *, unsigned int); -static bool rb_tree_check_node(const struct rb_tree *, const struct rb_node *, - const struct rb_node *, bool); -#endif - -#ifdef RBDEBUG -#define RBT_COUNT_INCR(rbt) (rbt)->rbt_count++ -#define RBT_COUNT_DECR(rbt) (rbt)->rbt_count-- -#else -#define RBT_COUNT_INCR(rbt) /* nothing */ -#define RBT_COUNT_DECR(rbt) /* nothing */ -#endif - -#define RBUNCONST(a) ((void *)(unsigned long)(const void *)(a)) - -/* - * Rather than testing for the NULL everywhere, all terminal leaves are - * pointed to this node (and that includes itself). Note that by setting - * it to be const, that on some architectures trying to write to it will - * cause a fault. - */ -static const struct rb_node sentinel_node = { - .rb_nodes = { RBUNCONST(&sentinel_node), - RBUNCONST(&sentinel_node), - NULL }, - .rb_u = { .u_s = { .s_sentinel = 1 } }, -}; - -void -_prop_rb_tree_init(struct rb_tree *rbt, const struct rb_tree_ops *ops) -{ - RB_TAILQ_INIT(&rbt->rbt_nodes); -#ifdef RBDEBUG - rbt->rbt_count = 0; -#endif - rbt->rbt_ops = ops; - *((const struct rb_node **)&rbt->rbt_root) = &sentinel_node; -} - -/* - * Swap the location and colors of 'self' and its child @ which. The child - * can not be a sentinel node. - */ -/*ARGSUSED*/ -static void -rb_tree_reparent_nodes(struct rb_tree *rbt _PROP_ARG_UNUSED, - struct rb_node *old_father, unsigned int which) -{ - const unsigned int other = which ^ RB_NODE_OTHER; - struct rb_node * const grandpa = old_father->rb_parent; - struct rb_node * const old_child = old_father->rb_nodes[which]; - struct rb_node * const new_father = old_child; - struct rb_node * const new_child = old_father; - unsigned int properties; - - KASSERT(which == RB_NODE_LEFT || which == RB_NODE_RIGHT); - - KASSERT(!RB_SENTINEL_P(old_child)); - KASSERT(old_child->rb_parent == old_father); - - KASSERT(rb_tree_check_node(rbt, old_father, NULL, false)); - KASSERT(rb_tree_check_node(rbt, old_child, NULL, false)); - KASSERT(RB_ROOT_P(old_father) || rb_tree_check_node(rbt, grandpa, NULL, false)); - - /* - * Exchange descendant linkages. - */ - grandpa->rb_nodes[old_father->rb_position] = new_father; - new_child->rb_nodes[which] = old_child->rb_nodes[other]; - new_father->rb_nodes[other] = new_child; - - /* - * Update ancestor linkages - */ - new_father->rb_parent = grandpa; - new_child->rb_parent = new_father; - - /* - * Exchange properties between new_father and new_child. The only - * change is that new_child's position is now on the other side. - */ - properties = old_child->rb_properties; - new_father->rb_properties = old_father->rb_properties; - new_child->rb_properties = properties; - new_child->rb_position = other; - - /* - * Make sure to reparent the new child to ourself. - */ - if (!RB_SENTINEL_P(new_child->rb_nodes[which])) { - new_child->rb_nodes[which]->rb_parent = new_child; - new_child->rb_nodes[which]->rb_position = which; - } - - KASSERT(rb_tree_check_node(rbt, new_father, NULL, false)); - KASSERT(rb_tree_check_node(rbt, new_child, NULL, false)); - KASSERT(RB_ROOT_P(new_father) || rb_tree_check_node(rbt, grandpa, NULL, false)); -} - -bool -_prop_rb_tree_insert_node(struct rb_tree *rbt, struct rb_node *self) -{ - struct rb_node *parent, *tmp; - rb_compare_nodes_fn compare_nodes = rbt->rbt_ops->rbto_compare_nodes; - unsigned int position; - - self->rb_properties = 0; - tmp = rbt->rbt_root; - /* - * This is a hack. Because rbt->rbt_root is just a struct rb_node *, - * just like rb_node->rb_nodes[RB_NODE_LEFT], we can use this fact to - * avoid a lot of tests for root and know that even at root, - * updating rb_node->rb_parent->rb_nodes[rb_node->rb_position] will - * rbt->rbt_root. - */ - /* LINTED: see above */ - parent = (struct rb_node *)&rbt->rbt_root; - position = RB_NODE_LEFT; - - /* - * Find out where to place this new leaf. - */ - while (!RB_SENTINEL_P(tmp)) { - const int diff = (*compare_nodes)(tmp, self); - if (__predict_false(diff == 0)) { - /* - * Node already exists; don't insert. - */ - return false; - } - parent = tmp; - KASSERT(diff != 0); - if (diff < 0) { - position = RB_NODE_LEFT; - } else { - position = RB_NODE_RIGHT; - } - tmp = parent->rb_nodes[position]; - } - -#ifdef RBDEBUG - { - struct rb_node *prev = NULL, *next = NULL; - - if (position == RB_NODE_RIGHT) - prev = parent; - else if (tmp != rbt->rbt_root) - next = parent; - - /* - * Verify our sequential position - */ - KASSERT(prev == NULL || !RB_SENTINEL_P(prev)); - KASSERT(next == NULL || !RB_SENTINEL_P(next)); - if (prev != NULL && next == NULL) - next = TAILQ_NEXT(prev, rb_link); - if (prev == NULL && next != NULL) - prev = TAILQ_PREV(next, rb_node_qh, rb_link); - KASSERT(prev == NULL || !RB_SENTINEL_P(prev)); - KASSERT(next == NULL || !RB_SENTINEL_P(next)); - KASSERT(prev == NULL - || (*compare_nodes)(prev, self) > 0); - KASSERT(next == NULL - || (*compare_nodes)(self, next) > 0); - } -#endif - - /* - * Initialize the node and insert as a leaf into the tree. - */ - self->rb_parent = parent; - self->rb_position = position; - /* LINTED: rbt_root hack */ - if (__predict_false(parent == (struct rb_node *) &rbt->rbt_root)) { - RB_MARK_ROOT(self); - } else { - KASSERT(position == RB_NODE_LEFT || position == RB_NODE_RIGHT); - KASSERT(!RB_ROOT_P(self)); /* Already done */ - } - KASSERT(RB_SENTINEL_P(parent->rb_nodes[position])); - self->rb_left = parent->rb_nodes[position]; - self->rb_right = parent->rb_nodes[position]; - parent->rb_nodes[position] = self; - KASSERT(self->rb_left == &sentinel_node && - self->rb_right == &sentinel_node); - - /* - * Insert the new node into a sorted list for easy sequential access - */ - RBT_COUNT_INCR(rbt); -#ifdef RBDEBUG - if (RB_ROOT_P(self)) { - RB_TAILQ_INSERT_HEAD(&rbt->rbt_nodes, self, rb_link); - } else if (position == RB_NODE_LEFT) { - KASSERT((*compare_nodes)(self, self->rb_parent) > 0); - RB_TAILQ_INSERT_BEFORE(self->rb_parent, self, rb_link); - } else { - KASSERT((*compare_nodes)(self->rb_parent, self) > 0); - RB_TAILQ_INSERT_AFTER(&rbt->rbt_nodes, self->rb_parent, - self, rb_link); - } -#endif - -#if 0 - /* - * Validate the tree before we rebalance - */ - _prop_rb_tree_check(rbt, false); -#endif - - /* - * Rebalance tree after insertion - */ - rb_tree_insert_rebalance(rbt, self); - -#if 0 - /* - * Validate the tree after we rebalanced - */ - _prop_rb_tree_check(rbt, true); -#endif - - return true; -} - -static void -rb_tree_insert_rebalance(struct rb_tree *rbt, struct rb_node *self) -{ - RB_MARK_RED(self); - - while (!RB_ROOT_P(self) && RB_RED_P(self->rb_parent)) { - const unsigned int which = - (self->rb_parent == self->rb_parent->rb_parent->rb_left - ? RB_NODE_LEFT - : RB_NODE_RIGHT); - const unsigned int other = which ^ RB_NODE_OTHER; - struct rb_node * father = self->rb_parent; - struct rb_node * grandpa = father->rb_parent; - struct rb_node * const uncle = grandpa->rb_nodes[other]; - - KASSERT(!RB_SENTINEL_P(self)); - /* - * We are red and our parent is red, therefore we must have a - * grandfather and he must be black. - */ - KASSERT(RB_RED_P(self) - && RB_RED_P(father) - && RB_BLACK_P(grandpa)); - - if (RB_RED_P(uncle)) { - /* - * Case 1: our uncle is red - * Simply invert the colors of our parent and - * uncle and make our grandparent red. And - * then solve the problem up at his level. - */ - RB_MARK_BLACK(uncle); - RB_MARK_BLACK(father); - RB_MARK_RED(grandpa); - self = grandpa; - continue; - } - /* - * Case 2&3: our uncle is black. - */ - if (self == father->rb_nodes[other]) { - /* - * Case 2: we are on the same side as our uncle - * Swap ourselves with our parent so this case - * becomes case 3. Basically our parent becomes our - * child. - */ - rb_tree_reparent_nodes(rbt, father, other); - KASSERT(father->rb_parent == self); - KASSERT(self->rb_nodes[which] == father); - KASSERT(self->rb_parent == grandpa); - } - KASSERT(RB_RED_P(self) && RB_RED_P(father)); - KASSERT(grandpa->rb_nodes[which] == father); - /* - * Case 3: we are opposite a child of a black uncle. - * Swap our parent and grandparent. Since our grandfather - * is black, our father will become black and our new sibling - * (former grandparent) will become red. - */ - rb_tree_reparent_nodes(rbt, grandpa, which); - KASSERT(self->rb_parent == father); - KASSERT(self->rb_parent->rb_nodes[self->rb_position ^ RB_NODE_OTHER] == grandpa); - KASSERT(RB_RED_P(self)); - KASSERT(RB_BLACK_P(father)); - KASSERT(RB_RED_P(grandpa)); - break; - } - - /* - * Final step: Set the root to black. - */ - RB_MARK_BLACK(rbt->rbt_root); -} - -struct rb_node * -_prop_rb_tree_find(struct rb_tree *rbt, const void *key) -{ - struct rb_node *parent = rbt->rbt_root; - rb_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key; - - while (!RB_SENTINEL_P(parent)) { - const int diff = (*compare_key)(parent, key); - if (diff == 0) - return parent; - parent = parent->rb_nodes[diff > 0]; - } - - return NULL; -} - -static void -rb_tree_prune_node(struct rb_tree *rbt, struct rb_node *self, int rebalance) -{ - const unsigned int which = self->rb_position; - struct rb_node *father = self->rb_parent; - - KASSERT(rebalance || (RB_ROOT_P(self) || RB_RED_P(self))); - KASSERT(!rebalance || RB_BLACK_P(self)); - KASSERT(RB_CHILDLESS_P(self)); - KASSERT(rb_tree_check_node(rbt, self, NULL, false)); - - father->rb_nodes[which] = self->rb_left; - - /* - * Remove ourselves from the node list and decrement the count. - */ - RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); - RBT_COUNT_DECR(rbt); - - if (rebalance) - rb_tree_removal_rebalance(rbt, father, which); - KASSERT(RB_ROOT_P(self) || rb_tree_check_node(rbt, father, NULL, true)); -} - -static void -rb_tree_swap_prune_and_rebalance(struct rb_tree *rbt, struct rb_node *self, - struct rb_node *standin) -{ - unsigned int standin_which = standin->rb_position; - unsigned int standin_other = standin_which ^ RB_NODE_OTHER; - struct rb_node *standin_child; - struct rb_node *standin_father; - bool rebalance = RB_BLACK_P(standin); - - if (standin->rb_parent == self) { - /* - * As a child of self, any childen would be opposite of - * our parent (self). - */ - KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other])); - standin_child = standin->rb_nodes[standin_which]; - } else { - /* - * Since we aren't a child of self, any childen would be - * on the same side as our parent (self). - */ - KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_which])); - standin_child = standin->rb_nodes[standin_other]; - } - - /* - * the node we are removing must have two children. - */ - KASSERT(RB_TWOCHILDREN_P(self)); - /* - * If standin has a child, it must be red. - */ - KASSERT(RB_SENTINEL_P(standin_child) || RB_RED_P(standin_child)); - - /* - * Verify things are sane. - */ - KASSERT(rb_tree_check_node(rbt, self, NULL, false)); - KASSERT(rb_tree_check_node(rbt, standin, NULL, false)); - - if (!RB_SENTINEL_P(standin_child)) { - /* - * We know we have a red child so if we swap them we can - * void flipping standin's child to black afterwards. - */ - KASSERT(rb_tree_check_node(rbt, standin_child, NULL, true)); - rb_tree_reparent_nodes(rbt, standin, - standin_child->rb_position); - KASSERT(rb_tree_check_node(rbt, standin, NULL, true)); - KASSERT(rb_tree_check_node(rbt, standin_child, NULL, true)); - /* - * Since we are removing a red leaf, no need to rebalance. - */ - rebalance = false; - /* - * We know that standin can not be a child of self, so - * update before of that. - */ - KASSERT(standin->rb_parent != self); - standin_which = standin->rb_position; - standin_other = standin_which ^ RB_NODE_OTHER; - } - KASSERT(RB_CHILDLESS_P(standin)); - - /* - * If we are about to delete the standin's father, then when we call - * rebalance, we need to use ourselves as our father. Otherwise - * remember our original father. Also, if we are our standin's father - * we only need to reparent the standin's brother. - */ - if (standin->rb_parent == self) { - /* - * | R --> S | - * | Q S --> Q * | - * | --> | - */ - standin_father = standin; - KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other])); - KASSERT(!RB_SENTINEL_P(self->rb_nodes[standin_other])); - KASSERT(self->rb_nodes[standin_which] == standin); - /* - * Make our brother our son. - */ - standin->rb_nodes[standin_other] = self->rb_nodes[standin_other]; - standin->rb_nodes[standin_other]->rb_parent = standin; - KASSERT(standin->rb_nodes[standin_other]->rb_position == standin_other); - } else { - /* - * | P --> P | - * | S --> Q | - * | Q --> | - */ - standin_father = standin->rb_parent; - standin_father->rb_nodes[standin_which] = - standin->rb_nodes[standin_which]; - standin->rb_left = self->rb_left; - standin->rb_right = self->rb_right; - standin->rb_left->rb_parent = standin; - standin->rb_right->rb_parent = standin; - } - - /* - * Now copy the result of self to standin and then replace - * self with standin in the tree. - */ - standin->rb_parent = self->rb_parent; - standin->rb_properties = self->rb_properties; - standin->rb_parent->rb_nodes[standin->rb_position] = standin; - - /* - * Remove ourselves from the node list and decrement the count. - */ - RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); - RBT_COUNT_DECR(rbt); - - KASSERT(rb_tree_check_node(rbt, standin, NULL, false)); - KASSERT(rb_tree_check_node(rbt, standin_father, NULL, false)); - - if (!rebalance) - return; - - rb_tree_removal_rebalance(rbt, standin_father, standin_which); - KASSERT(rb_tree_check_node(rbt, standin, NULL, true)); -} - -/* - * We could do this by doing - * rb_tree_node_swap(rbt, self, which); - * rb_tree_prune_node(rbt, self, false); - * - * But it's more efficient to just evalate and recolor the child. - */ -/*ARGSUSED*/ -static void -rb_tree_prune_blackred_branch(struct rb_tree *rbt _PROP_ARG_UNUSED, - struct rb_node *self, unsigned int which) -{ - struct rb_node *parent = self->rb_parent; - struct rb_node *child = self->rb_nodes[which]; - - KASSERT(which == RB_NODE_LEFT || which == RB_NODE_RIGHT); - KASSERT(RB_BLACK_P(self) && RB_RED_P(child)); - KASSERT(!RB_TWOCHILDREN_P(child)); - KASSERT(RB_CHILDLESS_P(child)); - KASSERT(rb_tree_check_node(rbt, self, NULL, false)); - KASSERT(rb_tree_check_node(rbt, child, NULL, false)); - - /* - * Remove ourselves from the tree and give our former child our - * properties (position, color, root). - */ - parent->rb_nodes[self->rb_position] = child; - child->rb_parent = parent; - child->rb_properties = self->rb_properties; - - /* - * Remove ourselves from the node list and decrement the count. - */ - RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); - RBT_COUNT_DECR(rbt); - - KASSERT(RB_ROOT_P(self) || rb_tree_check_node(rbt, parent, NULL, true)); - KASSERT(rb_tree_check_node(rbt, child, NULL, true)); -} -/* - * - */ -void -_prop_rb_tree_remove_node(struct rb_tree *rbt, struct rb_node *self) -{ - struct rb_node *standin; - unsigned int which; - /* - * In the following diagrams, we (the node to be removed) are S. Red - * nodes are lowercase. T could be either red or black. - * - * Remember the major axiom of the red-black tree: the number of - * black nodes from the root to each leaf is constant across all - * leaves, only the number of red nodes varies. - * - * Thus removing a red leaf doesn't require any other changes to a - * red-black tree. So if we must remove a node, attempt to rearrange - * the tree so we can remove a red node. - * - * The simpliest case is a childless red node or a childless root node: - * - * | T --> T | or | R --> * | - * | s --> * | - */ - if (RB_CHILDLESS_P(self)) { - if (RB_RED_P(self) || RB_ROOT_P(self)) { - rb_tree_prune_node(rbt, self, false); - return; - } - rb_tree_prune_node(rbt, self, true); - return; - } - KASSERT(!RB_CHILDLESS_P(self)); - if (!RB_TWOCHILDREN_P(self)) { - /* - * The next simpliest case is the node we are deleting is - * black and has one red child. - * - * | T --> T --> T | - * | S --> R --> R | - * | r --> s --> * | - */ - which = RB_LEFT_SENTINEL_P(self) ? RB_NODE_RIGHT : RB_NODE_LEFT; - KASSERT(RB_BLACK_P(self)); - KASSERT(RB_RED_P(self->rb_nodes[which])); - KASSERT(RB_CHILDLESS_P(self->rb_nodes[which])); - rb_tree_prune_blackred_branch(rbt, self, which); - return; - } - KASSERT(RB_TWOCHILDREN_P(self)); - - /* - * We invert these because we prefer to remove from the inside of - * the tree. - */ - which = self->rb_position ^ RB_NODE_OTHER; - - /* - * Let's find the node closes to us opposite of our parent - * Now swap it with ourself, "prune" it, and rebalance, if needed. - */ - standin = _prop_rb_tree_iterate(rbt, self, which); - rb_tree_swap_prune_and_rebalance(rbt, self, standin); -} - -static void -rb_tree_removal_rebalance(struct rb_tree *rbt, struct rb_node *parent, - unsigned int which) -{ - KASSERT(!RB_SENTINEL_P(parent)); - KASSERT(RB_SENTINEL_P(parent->rb_nodes[which])); - KASSERT(which == RB_NODE_LEFT || which == RB_NODE_RIGHT); - - while (RB_BLACK_P(parent->rb_nodes[which])) { - unsigned int other = which ^ RB_NODE_OTHER; - struct rb_node *brother = parent->rb_nodes[other]; - - KASSERT(!RB_SENTINEL_P(brother)); - /* - * For cases 1, 2a, and 2b, our brother's children must - * be black and our father must be black - */ - if (RB_BLACK_P(parent) - && RB_BLACK_P(brother->rb_left) - && RB_BLACK_P(brother->rb_right)) { - /* - * Case 1: Our brother is red, swap its position - * (and colors) with our parent. This is now case 2b. - * - * B -> D - * x d -> b E - * C E -> x C - */ - if (RB_RED_P(brother)) { - KASSERT(RB_BLACK_P(parent)); - rb_tree_reparent_nodes(rbt, parent, other); - KASSERT(!RB_SENTINEL_P(brother)); - KASSERT(RB_BLACK_P(brother)); - KASSERT(RB_RED_P(parent)); - KASSERT(rb_tree_check_node(rbt, brother, NULL, false)); - KASSERT(rb_tree_check_node(rbt, parent, NULL, false)); - } else { - /* - * Both our parent and brother are black. - * Change our brother to red, advance up rank - * and go through the loop again. - * - * B -> B - * A D -> A d - * C E -> C E - */ - RB_MARK_RED(brother); - KASSERT(RB_BLACK_P(brother->rb_left)); - KASSERT(RB_BLACK_P(brother->rb_right)); - if (RB_ROOT_P(parent)) - return; - KASSERT(rb_tree_check_node(rbt, brother, NULL, false)); - KASSERT(rb_tree_check_node(rbt, parent, NULL, false)); - which = parent->rb_position; - parent = parent->rb_parent; - } - } else if (RB_RED_P(parent) - && RB_BLACK_P(brother) - && RB_BLACK_P(brother->rb_left) - && RB_BLACK_P(brother->rb_right)) { - KASSERT(RB_BLACK_P(brother)); - KASSERT(RB_BLACK_P(brother->rb_left)); - KASSERT(RB_BLACK_P(brother->rb_right)); - RB_MARK_BLACK(parent); - RB_MARK_RED(brother); - KASSERT(rb_tree_check_node(rbt, brother, NULL, true)); - break; /* We're done! */ - } else { - KASSERT(RB_BLACK_P(brother)); - KASSERT(!RB_CHILDLESS_P(brother)); - /* - * Case 3: our brother is black, our left nephew is - * red, and our right nephew is black. Swap our - * brother with our left nephew. This result in a - * tree that matches case 4. - * - * B -> D - * A D -> B E - * c e -> A C - */ - if (RB_BLACK_P(brother->rb_nodes[other])) { - KASSERT(RB_RED_P(brother->rb_nodes[which])); - rb_tree_reparent_nodes(rbt, brother, which); - KASSERT(brother->rb_parent == parent->rb_nodes[other]); - brother = parent->rb_nodes[other]; - KASSERT(RB_RED_P(brother->rb_nodes[other])); - } - /* - * Case 4: our brother is black and our right nephew - * is red. Swap our parent and brother locations and - * change our right nephew to black. (these can be - * done in either order so we change the color first). - * The result is a valid red-black tree and is a - * terminal case. - * - * B -> D - * A D -> B E - * c e -> A C - */ - RB_MARK_BLACK(brother->rb_nodes[other]); - rb_tree_reparent_nodes(rbt, parent, other); - break; /* We're done! */ - } - } - KASSERT(rb_tree_check_node(rbt, parent, NULL, true)); -} - -struct rb_node * -_prop_rb_tree_iterate(struct rb_tree *rbt, struct rb_node *self, - unsigned int direction) -{ - const unsigned int other = direction ^ RB_NODE_OTHER; - KASSERT(direction == RB_NODE_LEFT || direction == RB_NODE_RIGHT); - - if (self == NULL) { - self = rbt->rbt_root; - if (RB_SENTINEL_P(self)) - return NULL; - while (!RB_SENTINEL_P(self->rb_nodes[other])) - self = self->rb_nodes[other]; - return self; - } - KASSERT(!RB_SENTINEL_P(self)); - /* - * We can't go any further in this direction. We proceed up in the - * opposite direction until our parent is in direction we want to go. - */ - if (RB_SENTINEL_P(self->rb_nodes[direction])) { - while (!RB_ROOT_P(self)) { - if (other == self->rb_position) - return self->rb_parent; - self = self->rb_parent; - } - return NULL; - } - - /* - * Advance down one in current direction and go down as far as possible - * in the opposite direction. - */ - self = self->rb_nodes[direction]; - KASSERT(!RB_SENTINEL_P(self)); - while (!RB_SENTINEL_P(self->rb_nodes[other])) - self = self->rb_nodes[other]; - return self; -} - -#ifdef RBDEBUG -static const struct rb_node * -rb_tree_iterate_const(const struct rb_tree *rbt, const struct rb_node *self, - unsigned int direction) -{ - const unsigned int other = direction ^ RB_NODE_OTHER; - KASSERT(direction == RB_NODE_LEFT || direction == RB_NODE_RIGHT); - - if (self == NULL) { - self = rbt->rbt_root; - if (RB_SENTINEL_P(self)) - return NULL; - while (!RB_SENTINEL_P(self->rb_nodes[other])) - self = self->rb_nodes[other]; - return self; - } - KASSERT(!RB_SENTINEL_P(self)); - /* - * We can't go any further in this direction. We proceed up in the - * opposite direction until our parent is in direction we want to go. - */ - if (RB_SENTINEL_P(self->rb_nodes[direction])) { - while (!RB_ROOT_P(self)) { - if (other == self->rb_position) - return self->rb_parent; - self = self->rb_parent; - } - return NULL; - } - - /* - * Advance down one in current direction and go down as far as possible - * in the opposite direction. - */ - self = self->rb_nodes[direction]; - KASSERT(!RB_SENTINEL_P(self)); - while (!RB_SENTINEL_P(self->rb_nodes[other])) - self = self->rb_nodes[other]; - return self; -} - -static bool -rb_tree_check_node(const struct rb_tree *rbt, const struct rb_node *self, - const struct rb_node *prev, bool red_check) -{ - KASSERT(!self->rb_sentinel); - KASSERT(self->rb_left); - KASSERT(self->rb_right); - KASSERT(prev == NULL || - (*rbt->rbt_ops->rbto_compare_nodes)(prev, self) > 0); - - /* - * Verify our relationship to our parent. - */ - if (RB_ROOT_P(self)) { - KASSERT(self == rbt->rbt_root); - KASSERT(self->rb_position == RB_NODE_LEFT); - KASSERT(self->rb_parent->rb_nodes[RB_NODE_LEFT] == self); - KASSERT(self->rb_parent == (const struct rb_node *) &rbt->rbt_root); - } else { - KASSERT(self != rbt->rbt_root); - KASSERT(!RB_PARENT_SENTINEL_P(self)); - if (self->rb_position == RB_NODE_LEFT) { - KASSERT((*rbt->rbt_ops->rbto_compare_nodes)(self, self->rb_parent) > 0); - KASSERT(self->rb_parent->rb_nodes[RB_NODE_LEFT] == self); - } else { - KASSERT((*rbt->rbt_ops->rbto_compare_nodes)(self, self->rb_parent) < 0); - KASSERT(self->rb_parent->rb_nodes[RB_NODE_RIGHT] == self); - } - } - - /* - * Verify our position in the linked list against the tree itself. - */ - { - const struct rb_node *prev0 = rb_tree_iterate_const(rbt, self, RB_NODE_LEFT); - const struct rb_node *next0 = rb_tree_iterate_const(rbt, self, RB_NODE_RIGHT); - KASSERT(prev0 == TAILQ_PREV(self, rb_node_qh, rb_link)); - if (next0 != TAILQ_NEXT(self, rb_link)) - next0 = rb_tree_iterate_const(rbt, self, RB_NODE_RIGHT); - KASSERT(next0 == TAILQ_NEXT(self, rb_link)); - } - - /* - * The root must be black. - * There can never be two adjacent red nodes. - */ - if (red_check) { - KASSERT(!RB_ROOT_P(self) || RB_BLACK_P(self)); - if (RB_RED_P(self)) { - const struct rb_node *brother; - KASSERT(!RB_ROOT_P(self)); - brother = self->rb_parent->rb_nodes[self->rb_position ^ RB_NODE_OTHER]; - KASSERT(RB_BLACK_P(self->rb_parent)); - /* - * I'm red and have no children, then I must either - * have no brother or my brother also be red and - * also have no children. (black count == 0) - */ - KASSERT(!RB_CHILDLESS_P(self) - || RB_SENTINEL_P(brother) - || RB_RED_P(brother) - || RB_CHILDLESS_P(brother)); - /* - * If I'm not childless, I must have two children - * and they must be both be black. - */ - KASSERT(RB_CHILDLESS_P(self) - || (RB_TWOCHILDREN_P(self) - && RB_BLACK_P(self->rb_left) - && RB_BLACK_P(self->rb_right))); - /* - * If I'm not childless, thus I have black children, - * then my brother must either be black or have two - * black children. - */ - KASSERT(RB_CHILDLESS_P(self) - || RB_BLACK_P(brother) - || (RB_TWOCHILDREN_P(brother) - && RB_BLACK_P(brother->rb_left) - && RB_BLACK_P(brother->rb_right))); - } else { - /* - * If I'm black and have one child, that child must - * be red and childless. - */ - KASSERT(RB_CHILDLESS_P(self) - || RB_TWOCHILDREN_P(self) - || (!RB_LEFT_SENTINEL_P(self) - && RB_RIGHT_SENTINEL_P(self) - && RB_RED_P(self->rb_left) - && RB_CHILDLESS_P(self->rb_left)) - || (!RB_RIGHT_SENTINEL_P(self) - && RB_LEFT_SENTINEL_P(self) - && RB_RED_P(self->rb_right) - && RB_CHILDLESS_P(self->rb_right))); - - /* - * If I'm a childless black node and my parent is - * black, my 2nd closet relative away from my parent - * is either red or has a red parent or red children. - */ - if (!RB_ROOT_P(self) - && RB_CHILDLESS_P(self) - && RB_BLACK_P(self->rb_parent)) { - const unsigned int which = self->rb_position; - const unsigned int other = which ^ RB_NODE_OTHER; - const struct rb_node *relative0, *relative; - - relative0 = rb_tree_iterate_const(rbt, - self, other); - KASSERT(relative0 != NULL); - relative = rb_tree_iterate_const(rbt, - relative0, other); - KASSERT(relative != NULL); - KASSERT(RB_SENTINEL_P(relative->rb_nodes[which])); -#if 0 - KASSERT(RB_RED_P(relative) - || RB_RED_P(relative->rb_left) - || RB_RED_P(relative->rb_right) - || RB_RED_P(relative->rb_parent)); -#endif - } - } - /* - * A grandparent's children must be real nodes and not - * sentinels. First check out grandparent. - */ - KASSERT(RB_ROOT_P(self) - || RB_ROOT_P(self->rb_parent) - || RB_TWOCHILDREN_P(self->rb_parent->rb_parent)); - /* - * If we are have grandchildren on our left, then - * we must have a child on our right. - */ - KASSERT(RB_LEFT_SENTINEL_P(self) - || RB_CHILDLESS_P(self->rb_left) - || !RB_RIGHT_SENTINEL_P(self)); - /* - * If we are have grandchildren on our right, then - * we must have a child on our left. - */ - KASSERT(RB_RIGHT_SENTINEL_P(self) - || RB_CHILDLESS_P(self->rb_right) - || !RB_LEFT_SENTINEL_P(self)); - - /* - * If we have a child on the left and it doesn't have two - * children make sure we don't have great-great-grandchildren on - * the right. - */ - KASSERT(RB_TWOCHILDREN_P(self->rb_left) - || RB_CHILDLESS_P(self->rb_right) - || RB_CHILDLESS_P(self->rb_right->rb_left) - || RB_CHILDLESS_P(self->rb_right->rb_left->rb_left) - || RB_CHILDLESS_P(self->rb_right->rb_left->rb_right) - || RB_CHILDLESS_P(self->rb_right->rb_right) - || RB_CHILDLESS_P(self->rb_right->rb_right->rb_left) - || RB_CHILDLESS_P(self->rb_right->rb_right->rb_right)); - - /* - * If we have a child on the right and it doesn't have two - * children make sure we don't have great-great-grandchildren on - * the left. - */ - KASSERT(RB_TWOCHILDREN_P(self->rb_right) - || RB_CHILDLESS_P(self->rb_left) - || RB_CHILDLESS_P(self->rb_left->rb_left) - || RB_CHILDLESS_P(self->rb_left->rb_left->rb_left) - || RB_CHILDLESS_P(self->rb_left->rb_left->rb_right) - || RB_CHILDLESS_P(self->rb_left->rb_right) - || RB_CHILDLESS_P(self->rb_left->rb_right->rb_left) - || RB_CHILDLESS_P(self->rb_left->rb_right->rb_right)); - - /* - * If we are fully interior node, then our predecessors and - * successors must have no children in our direction. - */ - if (RB_TWOCHILDREN_P(self)) { - const struct rb_node *prev0; - const struct rb_node *next0; - - prev0 = rb_tree_iterate_const(rbt, self, RB_NODE_LEFT); - KASSERT(prev0 != NULL); - KASSERT(RB_RIGHT_SENTINEL_P(prev0)); - - next0 = rb_tree_iterate_const(rbt, self, RB_NODE_RIGHT); - KASSERT(next0 != NULL); - KASSERT(RB_LEFT_SENTINEL_P(next0)); - } - } - - return true; -} - -static unsigned int -rb_tree_count_black(const struct rb_node *self) -{ - unsigned int left, right; - - if (RB_SENTINEL_P(self)) - return 0; - - left = rb_tree_count_black(self->rb_left); - right = rb_tree_count_black(self->rb_right); - - KASSERT(left == right); - - return left + RB_BLACK_P(self); -} - -void -_prop_rb_tree_check(const struct rb_tree *rbt, bool red_check) -{ - const struct rb_node *self; - const struct rb_node *prev; - unsigned int count; - - KASSERT(rbt->rbt_root == NULL || rbt->rbt_root->rb_position == RB_NODE_LEFT); - - prev = NULL; - count = 0; - TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) { - rb_tree_check_node(rbt, self, prev, false); - count++; - } - KASSERT(rbt->rbt_count == count); - KASSERT(RB_SENTINEL_P(rbt->rbt_root) - || rb_tree_count_black(rbt->rbt_root)); - - /* - * The root must be black. - * There can never be two adjacent red nodes. - */ - if (red_check) { - KASSERT(rbt->rbt_root == NULL || RB_BLACK_P(rbt->rbt_root)); - TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) { - rb_tree_check_node(rbt, self, NULL, true); - } - } -} -#endif /* RBDEBUG */ diff --git a/lib/portableproplib/prop_rb_impl.h b/lib/portableproplib/prop_rb_impl.h index 4dd6801c..ee93451d 100644 --- a/lib/portableproplib/prop_rb_impl.h +++ b/lib/portableproplib/prop_rb_impl.h @@ -1,4 +1,4 @@ -/* $NetBSD: prop_rb_impl.h,v 1.7 2008/06/30 20:14:09 matt Exp $ */ +/* $NetBSD: prop_rb_impl.h,v 1.8 2010/09/25 01:42:38 matt Exp $ */ /*- * Copyright (c) 2001 The NetBSD Foundation, Inc. @@ -32,8 +32,23 @@ #ifndef _PROP_RB_IMPL_H_ #define _PROP_RB_IMPL_H_ +#if 1 +#include + +/* + * Define local names for common rb_tree functions. + */ +#define _prop_rb_tree_init rb_tree_init +#define _prop_rb_tree_insert_node rb_tree_insert_node +#define _prop_rb_tree_find rb_tree_find_node +#define _prop_rb_tree_remove_node rb_tree_remove_node +#define _prop_rb_tree_iterate rb_tree_iterate + +#else /* __NetBSD__ */ + #include -#include "queue.h" +#include +#include struct rb_node { struct rb_node *rb_nodes[3]; @@ -134,4 +149,6 @@ void _prop_rb_tree_check(const struct rb_tree *, bool); struct rb_node * _prop_rb_tree_iterate(struct rb_tree *, struct rb_node *, unsigned int); +#endif /* __NetBSD__ */ + #endif /* _PROP_RB_IMPL_H_*/ diff --git a/lib/portableproplib/prop_zlib.c b/lib/portableproplib/prop_zlib.c new file mode 100644 index 00000000..1f5c0e70 --- /dev/null +++ b/lib/portableproplib/prop_zlib.c @@ -0,0 +1,146 @@ +/*- + * Copyright (c) 2010-2012 Juan Romero Pardines. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "prop_object_impl.h" + +#include +#include + +#define _READ_CHUNK 8192 + +#define TEMPLATE(type) \ +bool \ +prop ## type ## _externalize_to_zfile(prop ## type ## _t obj, const char *fname) \ +{ \ + char *xml; \ + bool rv; \ + int save_errno = 0; \ + \ + xml = prop ## type ## _externalize(obj); \ + if (xml == NULL) \ + return false; \ + rv = _prop_object_externalize_write_file(fname, xml, strlen(xml), true); \ + if (rv == false) \ + save_errno = errno; \ + _PROP_FREE(xml, M_TEMP); \ + if (rv == false) \ + errno = save_errno; \ + \ + return rv; \ +} \ + \ +prop ## type ## _t \ +prop ## type ## _internalize_from_zfile(const char *fname) \ +{ \ + struct _prop_object_internalize_mapped_file *mf; \ + prop ## type ## _t obj; \ + z_stream strm; \ + unsigned char *out; \ + char *uncomp_xml = NULL; \ + size_t have; \ + ssize_t totalsize = 0; \ + int rv = 0; \ + \ + mf = _prop_object_internalize_map_file(fname); \ + if (mf == NULL) \ + return NULL; \ + \ + /* Output buffer (uncompressed) */ \ + uncomp_xml = _PROP_MALLOC(_READ_CHUNK, M_TEMP); \ + if (uncomp_xml == NULL) { \ + _prop_object_internalize_unmap_file(mf); \ + return NULL; \ + } \ + \ + /* temporary output buffer for inflate */ \ + out = _PROP_MALLOC(_READ_CHUNK, M_TEMP); \ + if (out == NULL) { \ + _PROP_FREE(uncomp_xml, M_TEMP); \ + _prop_object_internalize_unmap_file(mf); \ + return NULL; \ + } \ + \ + /* Decompress the mmap'ed buffer with zlib */ \ + strm.zalloc = Z_NULL; \ + strm.zfree = Z_NULL; \ + strm.opaque = Z_NULL; \ + strm.avail_in = 0; \ + strm.next_in = Z_NULL; \ + \ + /* 15+16 to use gzip method */ \ + if (inflateInit2(&strm, 15+16) != Z_OK) { \ + _PROP_FREE(out, M_TEMP); \ + _PROP_FREE(uncomp_xml, M_TEMP); \ + _prop_object_internalize_unmap_file(mf); \ + return NULL; \ + } \ + strm.avail_in = mf->poimf_mapsize; \ + strm.next_in = (unsigned char *)mf->poimf_xml; \ + \ + /* Inflate the input buffer and copy into 'uncomp_xml' */ \ + do { \ + strm.avail_out = _READ_CHUNK; \ + strm.next_out = out; \ + rv = inflate(&strm, Z_NO_FLUSH); \ + switch (rv) { \ + case Z_DATA_ERROR: \ + /* Wrong compressed data or uncompressed, try normal method. */ \ + (void)inflateEnd(&strm); \ + _PROP_FREE(out, M_TEMP); \ + _PROP_FREE(uncomp_xml, M_TEMP); \ + obj = prop ## type ## _internalize(mf->poimf_xml); \ + _prop_object_internalize_unmap_file(mf); \ + return obj; \ + case Z_STREAM_ERROR: \ + case Z_NEED_DICT: \ + case Z_MEM_ERROR: \ + (void)inflateEnd(&strm); \ + _PROP_FREE(out, M_TEMP); \ + _PROP_FREE(uncomp_xml, M_TEMP); \ + _prop_object_internalize_unmap_file(mf); \ + errno = rv; \ + return NULL; \ + } \ + have = _READ_CHUNK - strm.avail_out; \ + totalsize += have; \ + uncomp_xml = _PROP_REALLOC(uncomp_xml, totalsize, M_TEMP); \ + memcpy(uncomp_xml + totalsize - have, out, have); \ + } while (strm.avail_out == 0); \ + \ + /* we are done */ \ + (void)inflateEnd(&strm); \ + obj = prop ## type ## _internalize(uncomp_xml); \ + _PROP_FREE(out, M_TEMP); \ + _PROP_FREE(uncomp_xml, M_TEMP); \ + _prop_object_internalize_unmap_file(mf); \ + \ + return obj; \ +} + +TEMPLATE(_array) +TEMPLATE(_dictionary) + +#undef TEMPLATE diff --git a/lib/portableproplib/rb.c b/lib/portableproplib/rb.c new file mode 100644 index 00000000..8a6f3ed5 --- /dev/null +++ b/lib/portableproplib/rb.c @@ -0,0 +1,1335 @@ +/* $NetBSD: rb.c,v 1.9 2010/11/17 13:19:32 tron Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include +#include +#include +#include +#ifdef RBDEBUG +#define KASSERT(s) assert(s) +#else +#define KASSERT(s) do { } while (/*CONSTCOND*/ 0) +#endif +#else +#include +#endif + +#ifdef _LIBC +__weak_alias(rb_tree_init, _rb_tree_init) +__weak_alias(rb_tree_find_node, _rb_tree_find_node) +__weak_alias(rb_tree_find_node_geq, _rb_tree_find_node_geq) +__weak_alias(rb_tree_find_node_leq, _rb_tree_find_node_leq) +__weak_alias(rb_tree_insert_node, _rb_tree_insert_node) +__weak_alias(rb_tree_remove_node, _rb_tree_remove_node) +__weak_alias(rb_tree_iterate, _rb_tree_iterate) +#ifdef RBDEBUG +__weak_alias(rb_tree_check, _rb_tree_check) +__weak_alias(rb_tree_depths, _rb_tree_depths) +#endif + +#include "namespace.h" +#endif + +#ifdef RBTEST +#include "rbtree.h" +#else +#include +#endif + +static void rb_tree_insert_rebalance(struct rb_tree *, struct rb_node *); +static void rb_tree_removal_rebalance(struct rb_tree *, struct rb_node *, + unsigned int); +#ifdef RBDEBUG +static const struct rb_node *rb_tree_iterate_const(const struct rb_tree *, + const struct rb_node *, const unsigned int); +static bool rb_tree_check_node(const struct rb_tree *, const struct rb_node *, + const struct rb_node *, bool); +#else +#define rb_tree_check_node(a, b, c, d) true +#endif + +#define RB_NODETOITEM(rbto, rbn) \ + ((void *)((uintptr_t)(rbn) - (rbto)->rbto_node_offset)) +#define RB_ITEMTONODE(rbto, rbn) \ + ((rb_node_t *)((uintptr_t)(rbn) + (rbto)->rbto_node_offset)) + +#define RB_SENTINEL_NODE NULL + +void +rb_tree_init(struct rb_tree *rbt, const rb_tree_ops_t *ops) +{ + + rbt->rbt_ops = ops; + *((const struct rb_node **)&rbt->rbt_root) = RB_SENTINEL_NODE; + RB_TAILQ_INIT(&rbt->rbt_nodes); +#ifndef RBSMALL + rbt->rbt_minmax[RB_DIR_LEFT] = rbt->rbt_root; /* minimum node */ + rbt->rbt_minmax[RB_DIR_RIGHT] = rbt->rbt_root; /* maximum node */ +#endif +#ifdef RBSTATS + rbt->rbt_count = 0; + rbt->rbt_insertions = 0; + rbt->rbt_removals = 0; + rbt->rbt_insertion_rebalance_calls = 0; + rbt->rbt_insertion_rebalance_passes = 0; + rbt->rbt_removal_rebalance_calls = 0; + rbt->rbt_removal_rebalance_passes = 0; +#endif +} + +void * +rb_tree_find_node(struct rb_tree *rbt, const void *key) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_key_fn compare_key = rbto->rbto_compare_key; + struct rb_node *parent = rbt->rbt_root; + + while (!RB_SENTINEL_P(parent)) { + void *pobj = RB_NODETOITEM(rbto, parent); + const signed int diff = (*compare_key)(rbto->rbto_context, + pobj, key); + if (diff == 0) + return pobj; + parent = parent->rb_nodes[diff < 0]; + } + + return NULL; +} + +void * +rb_tree_find_node_geq(struct rb_tree *rbt, const void *key) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_key_fn compare_key = rbto->rbto_compare_key; + struct rb_node *parent = rbt->rbt_root, *last = NULL; + + while (!RB_SENTINEL_P(parent)) { + void *pobj = RB_NODETOITEM(rbto, parent); + const signed int diff = (*compare_key)(rbto->rbto_context, + pobj, key); + if (diff == 0) + return pobj; + if (diff > 0) + last = parent; + parent = parent->rb_nodes[diff < 0]; + } + + return RB_NODETOITEM(rbto, last); +} + +void * +rb_tree_find_node_leq(struct rb_tree *rbt, const void *key) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_key_fn compare_key = rbto->rbto_compare_key; + struct rb_node *parent = rbt->rbt_root, *last = NULL; + + while (!RB_SENTINEL_P(parent)) { + void *pobj = RB_NODETOITEM(rbto, parent); + const signed int diff = (*compare_key)(rbto->rbto_context, + pobj, key); + if (diff == 0) + return pobj; + if (diff < 0) + last = parent; + parent = parent->rb_nodes[diff < 0]; + } + + return RB_NODETOITEM(rbto, last); +} + +void * +rb_tree_insert_node(struct rb_tree *rbt, void *object) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes; + struct rb_node *parent, *tmp, *self = RB_ITEMTONODE(rbto, object); + unsigned int position; + bool rebalance; + + RBSTAT_INC(rbt->rbt_insertions); + + tmp = rbt->rbt_root; + /* + * This is a hack. Because rbt->rbt_root is just a struct rb_node *, + * just like rb_node->rb_nodes[RB_DIR_LEFT], we can use this fact to + * avoid a lot of tests for root and know that even at root, + * updating RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will + * update rbt->rbt_root. + */ + parent = (struct rb_node *)(void *)&rbt->rbt_root; + position = RB_DIR_LEFT; + + /* + * Find out where to place this new leaf. + */ + while (!RB_SENTINEL_P(tmp)) { + void *tobj = RB_NODETOITEM(rbto, tmp); + const signed int diff = (*compare_nodes)(rbto->rbto_context, + tobj, object); + if (__predict_false(diff == 0)) { + /* + * Node already exists; return it. + */ + return tobj; + } + parent = tmp; + position = (diff < 0); + tmp = parent->rb_nodes[position]; + } + +#ifdef RBDEBUG + { + struct rb_node *prev = NULL, *next = NULL; + + if (position == RB_DIR_RIGHT) + prev = parent; + else if (tmp != rbt->rbt_root) + next = parent; + + /* + * Verify our sequential position + */ + KASSERT(prev == NULL || !RB_SENTINEL_P(prev)); + KASSERT(next == NULL || !RB_SENTINEL_P(next)); + if (prev != NULL && next == NULL) + next = TAILQ_NEXT(prev, rb_link); + if (prev == NULL && next != NULL) + prev = TAILQ_PREV(next, rb_node_qh, rb_link); + KASSERT(prev == NULL || !RB_SENTINEL_P(prev)); + KASSERT(next == NULL || !RB_SENTINEL_P(next)); + KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0); + KASSERT(next == NULL || (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, self), RB_NODETOITEM(rbto, next)) < 0); + } +#endif + + /* + * Initialize the node and insert as a leaf into the tree. + */ + RB_SET_FATHER(self, parent); + RB_SET_POSITION(self, position); + if (__predict_false(parent == (struct rb_node *)(void *)&rbt->rbt_root)) { + RB_MARK_BLACK(self); /* root is always black */ +#ifndef RBSMALL + rbt->rbt_minmax[RB_DIR_LEFT] = self; + rbt->rbt_minmax[RB_DIR_RIGHT] = self; +#endif + rebalance = false; + } else { + KASSERT(position == RB_DIR_LEFT || position == RB_DIR_RIGHT); +#ifndef RBSMALL + /* + * Keep track of the minimum and maximum nodes. If our + * parent is a minmax node and we on their min/max side, + * we must be the new min/max node. + */ + if (parent == rbt->rbt_minmax[position]) + rbt->rbt_minmax[position] = self; +#endif /* !RBSMALL */ + /* + * All new nodes are colored red. We only need to rebalance + * if our parent is also red. + */ + RB_MARK_RED(self); + rebalance = RB_RED_P(parent); + } + KASSERT(RB_SENTINEL_P(parent->rb_nodes[position])); + self->rb_left = parent->rb_nodes[position]; + self->rb_right = parent->rb_nodes[position]; + parent->rb_nodes[position] = self; + KASSERT(RB_CHILDLESS_P(self)); + + /* + * Insert the new node into a sorted list for easy sequential access + */ + RBSTAT_INC(rbt->rbt_count); +#ifdef RBDEBUG + if (RB_ROOT_P(rbt, self)) { + RB_TAILQ_INSERT_HEAD(&rbt->rbt_nodes, self, rb_link); + } else if (position == RB_DIR_LEFT) { + KASSERT((*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, self), + RB_NODETOITEM(rbto, RB_FATHER(self))) < 0); + RB_TAILQ_INSERT_BEFORE(RB_FATHER(self), self, rb_link); + } else { + KASSERT((*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, RB_FATHER(self)), + RB_NODETOITEM(rbto, self)) < 0); + RB_TAILQ_INSERT_AFTER(&rbt->rbt_nodes, RB_FATHER(self), + self, rb_link); + } +#endif + KASSERT(rb_tree_check_node(rbt, self, NULL, !rebalance)); + + /* + * Rebalance tree after insertion + */ + if (rebalance) { + rb_tree_insert_rebalance(rbt, self); + KASSERT(rb_tree_check_node(rbt, self, NULL, true)); + } + + /* Succesfully inserted, return our node pointer. */ + return object; +} + +/* + * Swap the location and colors of 'self' and its child @ which. The child + * can not be a sentinel node. This is our rotation function. However, + * since it preserves coloring, it great simplifies both insertion and + * removal since rotation almost always involves the exchanging of colors + * as a separate step. + */ +/*ARGSUSED*/ +static void +rb_tree_reparent_nodes(struct rb_tree *rbt, struct rb_node *old_father, + const unsigned int which) +{ + const unsigned int other = which ^ RB_DIR_OTHER; + struct rb_node * const grandpa = RB_FATHER(old_father); + struct rb_node * const old_child = old_father->rb_nodes[which]; + struct rb_node * const new_father = old_child; + struct rb_node * const new_child = old_father; + + KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT); + + KASSERT(!RB_SENTINEL_P(old_child)); + KASSERT(RB_FATHER(old_child) == old_father); + + KASSERT(rb_tree_check_node(rbt, old_father, NULL, false)); + KASSERT(rb_tree_check_node(rbt, old_child, NULL, false)); + KASSERT(RB_ROOT_P(rbt, old_father) || + rb_tree_check_node(rbt, grandpa, NULL, false)); + + /* + * Exchange descendant linkages. + */ + grandpa->rb_nodes[RB_POSITION(old_father)] = new_father; + new_child->rb_nodes[which] = old_child->rb_nodes[other]; + new_father->rb_nodes[other] = new_child; + + /* + * Update ancestor linkages + */ + RB_SET_FATHER(new_father, grandpa); + RB_SET_FATHER(new_child, new_father); + + /* + * Exchange properties between new_father and new_child. The only + * change is that new_child's position is now on the other side. + */ +#if 0 + { + struct rb_node tmp; + tmp.rb_info = 0; + RB_COPY_PROPERTIES(&tmp, old_child); + RB_COPY_PROPERTIES(new_father, old_father); + RB_COPY_PROPERTIES(new_child, &tmp); + } +#else + RB_SWAP_PROPERTIES(new_father, new_child); +#endif + RB_SET_POSITION(new_child, other); + + /* + * Make sure to reparent the new child to ourself. + */ + if (!RB_SENTINEL_P(new_child->rb_nodes[which])) { + RB_SET_FATHER(new_child->rb_nodes[which], new_child); + RB_SET_POSITION(new_child->rb_nodes[which], which); + } + + KASSERT(rb_tree_check_node(rbt, new_father, NULL, false)); + KASSERT(rb_tree_check_node(rbt, new_child, NULL, false)); + KASSERT(RB_ROOT_P(rbt, new_father) || + rb_tree_check_node(rbt, grandpa, NULL, false)); +} + +static void +rb_tree_insert_rebalance(struct rb_tree *rbt, struct rb_node *self) +{ + struct rb_node * father = RB_FATHER(self); + struct rb_node * grandpa = RB_FATHER(father); + struct rb_node * uncle; + unsigned int which; + unsigned int other; + + KASSERT(!RB_ROOT_P(rbt, self)); + KASSERT(RB_RED_P(self)); + KASSERT(RB_RED_P(father)); + RBSTAT_INC(rbt->rbt_insertion_rebalance_calls); + + for (;;) { + KASSERT(!RB_SENTINEL_P(self)); + + KASSERT(RB_RED_P(self)); + KASSERT(RB_RED_P(father)); + /* + * We are red and our parent is red, therefore we must have a + * grandfather and he must be black. + */ + grandpa = RB_FATHER(father); + KASSERT(RB_BLACK_P(grandpa)); + KASSERT(RB_DIR_RIGHT == 1 && RB_DIR_LEFT == 0); + which = (father == grandpa->rb_right); + other = which ^ RB_DIR_OTHER; + uncle = grandpa->rb_nodes[other]; + + if (RB_BLACK_P(uncle)) + break; + + RBSTAT_INC(rbt->rbt_insertion_rebalance_passes); + /* + * Case 1: our uncle is red + * Simply invert the colors of our parent and + * uncle and make our grandparent red. And + * then solve the problem up at his level. + */ + RB_MARK_BLACK(uncle); + RB_MARK_BLACK(father); + if (__predict_false(RB_ROOT_P(rbt, grandpa))) { + /* + * If our grandpa is root, don't bother + * setting him to red, just return. + */ + KASSERT(RB_BLACK_P(grandpa)); + return; + } + RB_MARK_RED(grandpa); + self = grandpa; + father = RB_FATHER(self); + KASSERT(RB_RED_P(self)); + if (RB_BLACK_P(father)) { + /* + * If our greatgrandpa is black, we're done. + */ + KASSERT(RB_BLACK_P(rbt->rbt_root)); + return; + } + } + + KASSERT(!RB_ROOT_P(rbt, self)); + KASSERT(RB_RED_P(self)); + KASSERT(RB_RED_P(father)); + KASSERT(RB_BLACK_P(uncle)); + KASSERT(RB_BLACK_P(grandpa)); + /* + * Case 2&3: our uncle is black. + */ + if (self == father->rb_nodes[other]) { + /* + * Case 2: we are on the same side as our uncle + * Swap ourselves with our parent so this case + * becomes case 3. Basically our parent becomes our + * child. + */ + rb_tree_reparent_nodes(rbt, father, other); + KASSERT(RB_FATHER(father) == self); + KASSERT(self->rb_nodes[which] == father); + KASSERT(RB_FATHER(self) == grandpa); + self = father; + father = RB_FATHER(self); + } + KASSERT(RB_RED_P(self) && RB_RED_P(father)); + KASSERT(grandpa->rb_nodes[which] == father); + /* + * Case 3: we are opposite a child of a black uncle. + * Swap our parent and grandparent. Since our grandfather + * is black, our father will become black and our new sibling + * (former grandparent) will become red. + */ + rb_tree_reparent_nodes(rbt, grandpa, which); + KASSERT(RB_FATHER(self) == father); + KASSERT(RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER] == grandpa); + KASSERT(RB_RED_P(self)); + KASSERT(RB_BLACK_P(father)); + KASSERT(RB_RED_P(grandpa)); + + /* + * Final step: Set the root to black. + */ + RB_MARK_BLACK(rbt->rbt_root); +} + +static void +rb_tree_prune_node(struct rb_tree *rbt, struct rb_node *self, bool rebalance) +{ + const unsigned int which = RB_POSITION(self); + struct rb_node *father = RB_FATHER(self); +#ifndef RBSMALL + const bool was_root = RB_ROOT_P(rbt, self); +#endif + + KASSERT(rebalance || (RB_ROOT_P(rbt, self) || RB_RED_P(self))); + KASSERT(!rebalance || RB_BLACK_P(self)); + KASSERT(RB_CHILDLESS_P(self)); + KASSERT(rb_tree_check_node(rbt, self, NULL, false)); + + /* + * Since we are childless, we know that self->rb_left is pointing + * to the sentinel node. + */ + father->rb_nodes[which] = self->rb_left; + + /* + * Remove ourselves from the node list, decrement the count, + * and update min/max. + */ + RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); + RBSTAT_DEC(rbt->rbt_count); +#ifndef RBSMALL + if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self)) { + rbt->rbt_minmax[RB_POSITION(self)] = father; + /* + * When removing the root, rbt->rbt_minmax[RB_DIR_LEFT] is + * updated automatically, but we also need to update + * rbt->rbt_minmax[RB_DIR_RIGHT]; + */ + if (__predict_false(was_root)) { + rbt->rbt_minmax[RB_DIR_RIGHT] = father; + } + } + RB_SET_FATHER(self, NULL); +#endif + + /* + * Rebalance if requested. + */ + if (rebalance) + rb_tree_removal_rebalance(rbt, father, which); + KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true)); +} + +/* + * When deleting an interior node + */ +static void +rb_tree_swap_prune_and_rebalance(struct rb_tree *rbt, struct rb_node *self, + struct rb_node *standin) +{ + const unsigned int standin_which = RB_POSITION(standin); + unsigned int standin_other = standin_which ^ RB_DIR_OTHER; + struct rb_node *standin_son; + struct rb_node *standin_father = RB_FATHER(standin); + bool rebalance = RB_BLACK_P(standin); + + if (standin_father == self) { + /* + * As a child of self, any childen would be opposite of + * our parent. + */ + KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other])); + standin_son = standin->rb_nodes[standin_which]; + } else { + /* + * Since we aren't a child of self, any childen would be + * on the same side as our parent. + */ + KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_which])); + standin_son = standin->rb_nodes[standin_other]; + } + + /* + * the node we are removing must have two children. + */ + KASSERT(RB_TWOCHILDREN_P(self)); + /* + * If standin has a child, it must be red. + */ + KASSERT(RB_SENTINEL_P(standin_son) || RB_RED_P(standin_son)); + + /* + * Verify things are sane. + */ + KASSERT(rb_tree_check_node(rbt, self, NULL, false)); + KASSERT(rb_tree_check_node(rbt, standin, NULL, false)); + + if (__predict_false(RB_RED_P(standin_son))) { + /* + * We know we have a red child so if we flip it to black + * we don't have to rebalance. + */ + KASSERT(rb_tree_check_node(rbt, standin_son, NULL, true)); + RB_MARK_BLACK(standin_son); + rebalance = false; + + if (standin_father == self) { + KASSERT(RB_POSITION(standin_son) == standin_which); + } else { + KASSERT(RB_POSITION(standin_son) == standin_other); + /* + * Change the son's parentage to point to his grandpa. + */ + RB_SET_FATHER(standin_son, standin_father); + RB_SET_POSITION(standin_son, standin_which); + } + } + + if (standin_father == self) { + /* + * If we are about to delete the standin's father, then when + * we call rebalance, we need to use ourselves as our father. + * Otherwise remember our original father. Also, sincef we are + * our standin's father we only need to reparent the standin's + * brother. + * + * | R --> S | + * | Q S --> Q T | + * | t --> | + */ + KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other])); + KASSERT(!RB_SENTINEL_P(self->rb_nodes[standin_other])); + KASSERT(self->rb_nodes[standin_which] == standin); + /* + * Have our son/standin adopt his brother as his new son. + */ + standin_father = standin; + } else { + /* + * | R --> S . | + * | / \ | T --> / \ | / | + * | ..... | S --> ..... | T | + * + * Sever standin's connection to his father. + */ + standin_father->rb_nodes[standin_which] = standin_son; + /* + * Adopt the far son. + */ + standin->rb_nodes[standin_other] = self->rb_nodes[standin_other]; + RB_SET_FATHER(standin->rb_nodes[standin_other], standin); + KASSERT(RB_POSITION(self->rb_nodes[standin_other]) == standin_other); + /* + * Use standin_other because we need to preserve standin_which + * for the removal_rebalance. + */ + standin_other = standin_which; + } + + /* + * Move the only remaining son to our standin. If our standin is our + * son, this will be the only son needed to be moved. + */ + KASSERT(standin->rb_nodes[standin_other] != self->rb_nodes[standin_other]); + standin->rb_nodes[standin_other] = self->rb_nodes[standin_other]; + RB_SET_FATHER(standin->rb_nodes[standin_other], standin); + + /* + * Now copy the result of self to standin and then replace + * self with standin in the tree. + */ + RB_COPY_PROPERTIES(standin, self); + RB_SET_FATHER(standin, RB_FATHER(self)); + RB_FATHER(standin)->rb_nodes[RB_POSITION(standin)] = standin; + + /* + * Remove ourselves from the node list, decrement the count, + * and update min/max. + */ + RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); + RBSTAT_DEC(rbt->rbt_count); +#ifndef RBSMALL + if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self)) + rbt->rbt_minmax[RB_POSITION(self)] = RB_FATHER(self); + RB_SET_FATHER(self, NULL); +#endif + + KASSERT(rb_tree_check_node(rbt, standin, NULL, false)); + KASSERT(RB_FATHER_SENTINEL_P(standin) + || rb_tree_check_node(rbt, standin_father, NULL, false)); + KASSERT(RB_LEFT_SENTINEL_P(standin) + || rb_tree_check_node(rbt, standin->rb_left, NULL, false)); + KASSERT(RB_RIGHT_SENTINEL_P(standin) + || rb_tree_check_node(rbt, standin->rb_right, NULL, false)); + + if (!rebalance) + return; + + rb_tree_removal_rebalance(rbt, standin_father, standin_which); + KASSERT(rb_tree_check_node(rbt, standin, NULL, true)); +} + +/* + * We could do this by doing + * rb_tree_node_swap(rbt, self, which); + * rb_tree_prune_node(rbt, self, false); + * + * But it's more efficient to just evalate and recolor the child. + */ +static void +rb_tree_prune_blackred_branch(struct rb_tree *rbt, struct rb_node *self, + unsigned int which) +{ + struct rb_node *father = RB_FATHER(self); + struct rb_node *son = self->rb_nodes[which]; +#ifndef RBSMALL + const bool was_root = RB_ROOT_P(rbt, self); +#endif + + KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT); + KASSERT(RB_BLACK_P(self) && RB_RED_P(son)); + KASSERT(!RB_TWOCHILDREN_P(son)); + KASSERT(RB_CHILDLESS_P(son)); + KASSERT(rb_tree_check_node(rbt, self, NULL, false)); + KASSERT(rb_tree_check_node(rbt, son, NULL, false)); + + /* + * Remove ourselves from the tree and give our former child our + * properties (position, color, root). + */ + RB_COPY_PROPERTIES(son, self); + father->rb_nodes[RB_POSITION(son)] = son; + RB_SET_FATHER(son, father); + + /* + * Remove ourselves from the node list, decrement the count, + * and update minmax. + */ + RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link); + RBSTAT_DEC(rbt->rbt_count); +#ifndef RBSMALL + if (__predict_false(was_root)) { + KASSERT(rbt->rbt_minmax[which] == son); + rbt->rbt_minmax[which ^ RB_DIR_OTHER] = son; + } else if (rbt->rbt_minmax[RB_POSITION(self)] == self) { + rbt->rbt_minmax[RB_POSITION(self)] = son; + } + RB_SET_FATHER(self, NULL); +#endif + + KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true)); + KASSERT(rb_tree_check_node(rbt, son, NULL, true)); +} + +void +rb_tree_remove_node(struct rb_tree *rbt, void *object) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + struct rb_node *standin, *self = RB_ITEMTONODE(rbto, object); + unsigned int which; + + KASSERT(!RB_SENTINEL_P(self)); + RBSTAT_INC(rbt->rbt_removals); + + /* + * In the following diagrams, we (the node to be removed) are S. Red + * nodes are lowercase. T could be either red or black. + * + * Remember the major axiom of the red-black tree: the number of + * black nodes from the root to each leaf is constant across all + * leaves, only the number of red nodes varies. + * + * Thus removing a red leaf doesn't require any other changes to a + * red-black tree. So if we must remove a node, attempt to rearrange + * the tree so we can remove a red node. + * + * The simpliest case is a childless red node or a childless root node: + * + * | T --> T | or | R --> * | + * | s --> * | + */ + if (RB_CHILDLESS_P(self)) { + const bool rebalance = RB_BLACK_P(self) && !RB_ROOT_P(rbt, self); + rb_tree_prune_node(rbt, self, rebalance); + return; + } + KASSERT(!RB_CHILDLESS_P(self)); + if (!RB_TWOCHILDREN_P(self)) { + /* + * The next simpliest case is the node we are deleting is + * black and has one red child. + * + * | T --> T --> T | + * | S --> R --> R | + * | r --> s --> * | + */ + which = RB_LEFT_SENTINEL_P(self) ? RB_DIR_RIGHT : RB_DIR_LEFT; + KASSERT(RB_BLACK_P(self)); + KASSERT(RB_RED_P(self->rb_nodes[which])); + KASSERT(RB_CHILDLESS_P(self->rb_nodes[which])); + rb_tree_prune_blackred_branch(rbt, self, which); + return; + } + KASSERT(RB_TWOCHILDREN_P(self)); + + /* + * We invert these because we prefer to remove from the inside of + * the tree. + */ + which = RB_POSITION(self) ^ RB_DIR_OTHER; + + /* + * Let's find the node closes to us opposite of our parent + * Now swap it with ourself, "prune" it, and rebalance, if needed. + */ + standin = RB_ITEMTONODE(rbto, rb_tree_iterate(rbt, object, which)); + rb_tree_swap_prune_and_rebalance(rbt, self, standin); +} + +static void +rb_tree_removal_rebalance(struct rb_tree *rbt, struct rb_node *parent, + unsigned int which) +{ + KASSERT(!RB_SENTINEL_P(parent)); + KASSERT(RB_SENTINEL_P(parent->rb_nodes[which])); + KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT); + RBSTAT_INC(rbt->rbt_removal_rebalance_calls); + + while (RB_BLACK_P(parent->rb_nodes[which])) { + unsigned int other = which ^ RB_DIR_OTHER; + struct rb_node *brother = parent->rb_nodes[other]; + + RBSTAT_INC(rbt->rbt_removal_rebalance_passes); + + KASSERT(!RB_SENTINEL_P(brother)); + /* + * For cases 1, 2a, and 2b, our brother's children must + * be black and our father must be black + */ + if (RB_BLACK_P(parent) + && RB_BLACK_P(brother->rb_left) + && RB_BLACK_P(brother->rb_right)) { + if (RB_RED_P(brother)) { + /* + * Case 1: Our brother is red, swap its + * position (and colors) with our parent. + * This should now be case 2b (unless C or E + * has a red child which is case 3; thus no + * explicit branch to case 2b). + * + * B -> D + * A d -> b E + * C E -> A C + */ + KASSERT(RB_BLACK_P(parent)); + rb_tree_reparent_nodes(rbt, parent, other); + brother = parent->rb_nodes[other]; + KASSERT(!RB_SENTINEL_P(brother)); + KASSERT(RB_RED_P(parent)); + KASSERT(RB_BLACK_P(brother)); + KASSERT(rb_tree_check_node(rbt, brother, NULL, false)); + KASSERT(rb_tree_check_node(rbt, parent, NULL, false)); + } else { + /* + * Both our parent and brother are black. + * Change our brother to red, advance up rank + * and go through the loop again. + * + * B -> *B + * *A D -> A d + * C E -> C E + */ + RB_MARK_RED(brother); + KASSERT(RB_BLACK_P(brother->rb_left)); + KASSERT(RB_BLACK_P(brother->rb_right)); + if (RB_ROOT_P(rbt, parent)) + return; /* root == parent == black */ + KASSERT(rb_tree_check_node(rbt, brother, NULL, false)); + KASSERT(rb_tree_check_node(rbt, parent, NULL, false)); + which = RB_POSITION(parent); + parent = RB_FATHER(parent); + continue; + } + } + /* + * Avoid an else here so that case 2a above can hit either + * case 2b, 3, or 4. + */ + if (RB_RED_P(parent) + && RB_BLACK_P(brother) + && RB_BLACK_P(brother->rb_left) + && RB_BLACK_P(brother->rb_right)) { + KASSERT(RB_RED_P(parent)); + KASSERT(RB_BLACK_P(brother)); + KASSERT(RB_BLACK_P(brother->rb_left)); + KASSERT(RB_BLACK_P(brother->rb_right)); + /* + * We are black, our father is red, our brother and + * both nephews are black. Simply invert/exchange the + * colors of our father and brother (to black and red + * respectively). + * + * | f --> F | + * | * B --> * b | + * | N N --> N N | + */ + RB_MARK_BLACK(parent); + RB_MARK_RED(brother); + KASSERT(rb_tree_check_node(rbt, brother, NULL, true)); + break; /* We're done! */ + } else { + /* + * Our brother must be black and have at least one + * red child (it may have two). + */ + KASSERT(RB_BLACK_P(brother)); + KASSERT(RB_RED_P(brother->rb_nodes[which]) || + RB_RED_P(brother->rb_nodes[other])); + if (RB_BLACK_P(brother->rb_nodes[other])) { + /* + * Case 3: our brother is black, our near + * nephew is red, and our far nephew is black. + * Swap our brother with our near nephew. + * This result in a tree that matches case 4. + * (Our father could be red or black). + * + * | F --> F | + * | x B --> x B | + * | n --> n | + */ + KASSERT(RB_RED_P(brother->rb_nodes[which])); + rb_tree_reparent_nodes(rbt, brother, which); + KASSERT(RB_FATHER(brother) == parent->rb_nodes[other]); + brother = parent->rb_nodes[other]; + KASSERT(RB_RED_P(brother->rb_nodes[other])); + } + /* + * Case 4: our brother is black and our far nephew + * is red. Swap our father and brother locations and + * change our far nephew to black. (these can be + * done in either order so we change the color first). + * The result is a valid red-black tree and is a + * terminal case. (again we don't care about the + * father's color) + * + * If the father is red, we will get a red-black-black + * tree: + * | f -> f --> b | + * | B -> B --> F N | + * | n -> N --> | + * + * If the father is black, we will get an all black + * tree: + * | F -> F --> B | + * | B -> B --> F N | + * | n -> N --> | + * + * If we had two red nephews, then after the swap, + * our former father would have a red grandson. + */ + KASSERT(RB_BLACK_P(brother)); + KASSERT(RB_RED_P(brother->rb_nodes[other])); + RB_MARK_BLACK(brother->rb_nodes[other]); + rb_tree_reparent_nodes(rbt, parent, other); + break; /* We're done! */ + } + } + KASSERT(rb_tree_check_node(rbt, parent, NULL, true)); +} + +void * +rb_tree_iterate(struct rb_tree *rbt, void *object, const unsigned int direction) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + const unsigned int other = direction ^ RB_DIR_OTHER; + struct rb_node *self; + + KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT); + + if (object == NULL) { +#ifndef RBSMALL + if (RB_SENTINEL_P(rbt->rbt_root)) + return NULL; + return RB_NODETOITEM(rbto, rbt->rbt_minmax[direction]); +#else + self = rbt->rbt_root; + if (RB_SENTINEL_P(self)) + return NULL; + while (!RB_SENTINEL_P(self->rb_nodes[direction])) + self = self->rb_nodes[direction]; + return RB_NODETOITEM(rbto, self); +#endif /* !RBSMALL */ + } + self = RB_ITEMTONODE(rbto, object); + KASSERT(!RB_SENTINEL_P(self)); + /* + * We can't go any further in this direction. We proceed up in the + * opposite direction until our parent is in direction we want to go. + */ + if (RB_SENTINEL_P(self->rb_nodes[direction])) { + while (!RB_ROOT_P(rbt, self)) { + if (other == RB_POSITION(self)) + return RB_NODETOITEM(rbto, RB_FATHER(self)); + self = RB_FATHER(self); + } + return NULL; + } + + /* + * Advance down one in current direction and go down as far as possible + * in the opposite direction. + */ + self = self->rb_nodes[direction]; + KASSERT(!RB_SENTINEL_P(self)); + while (!RB_SENTINEL_P(self->rb_nodes[other])) + self = self->rb_nodes[other]; + return RB_NODETOITEM(rbto, self); +} + +#ifdef RBDEBUG +static const struct rb_node * +rb_tree_iterate_const(const struct rb_tree *rbt, const struct rb_node *self, + const unsigned int direction) +{ + const unsigned int other = direction ^ RB_DIR_OTHER; + KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT); + + if (self == NULL) { +#ifndef RBSMALL + if (RB_SENTINEL_P(rbt->rbt_root)) + return NULL; + return rbt->rbt_minmax[direction]; +#else + self = rbt->rbt_root; + if (RB_SENTINEL_P(self)) + return NULL; + while (!RB_SENTINEL_P(self->rb_nodes[direction])) + self = self->rb_nodes[direction]; + return self; +#endif /* !RBSMALL */ + } + KASSERT(!RB_SENTINEL_P(self)); + /* + * We can't go any further in this direction. We proceed up in the + * opposite direction until our parent is in direction we want to go. + */ + if (RB_SENTINEL_P(self->rb_nodes[direction])) { + while (!RB_ROOT_P(rbt, self)) { + if (other == RB_POSITION(self)) + return RB_FATHER(self); + self = RB_FATHER(self); + } + return NULL; + } + + /* + * Advance down one in current direction and go down as far as possible + * in the opposite direction. + */ + self = self->rb_nodes[direction]; + KASSERT(!RB_SENTINEL_P(self)); + while (!RB_SENTINEL_P(self->rb_nodes[other])) + self = self->rb_nodes[other]; + return self; +} + +static unsigned int +rb_tree_count_black(const struct rb_node *self) +{ + unsigned int left, right; + + if (RB_SENTINEL_P(self)) + return 0; + + left = rb_tree_count_black(self->rb_left); + right = rb_tree_count_black(self->rb_right); + + KASSERT(left == right); + + return left + RB_BLACK_P(self); +} + +static bool +rb_tree_check_node(const struct rb_tree *rbt, const struct rb_node *self, + const struct rb_node *prev, bool red_check) +{ + const rb_tree_ops_t *rbto = rbt->rbt_ops; + rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes; + + KASSERT(!RB_SENTINEL_P(self)); + KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0); + + /* + * Verify our relationship to our parent. + */ + if (RB_ROOT_P(rbt, self)) { + KASSERT(self == rbt->rbt_root); + KASSERT(RB_POSITION(self) == RB_DIR_LEFT); + KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self); + KASSERT(RB_FATHER(self) == (const struct rb_node *) &rbt->rbt_root); + } else { + int diff = (*compare_nodes)(rbto->rbto_context, + RB_NODETOITEM(rbto, self), + RB_NODETOITEM(rbto, RB_FATHER(self))); + + KASSERT(self != rbt->rbt_root); + KASSERT(!RB_FATHER_SENTINEL_P(self)); + if (RB_POSITION(self) == RB_DIR_LEFT) { + KASSERT(diff < 0); + KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self); + } else { + KASSERT(diff > 0); + KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_RIGHT] == self); + } + } + + /* + * Verify our position in the linked list against the tree itself. + */ + { + const struct rb_node *prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT); + const struct rb_node *next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT); + KASSERT(prev0 == TAILQ_PREV(self, rb_node_qh, rb_link)); + KASSERT(next0 == TAILQ_NEXT(self, rb_link)); +#ifndef RBSMALL + KASSERT(prev0 != NULL || self == rbt->rbt_minmax[RB_DIR_LEFT]); + KASSERT(next0 != NULL || self == rbt->rbt_minmax[RB_DIR_RIGHT]); +#endif + } + + /* + * The root must be black. + * There can never be two adjacent red nodes. + */ + if (red_check) { + KASSERT(!RB_ROOT_P(rbt, self) || RB_BLACK_P(self)); + (void) rb_tree_count_black(self); + if (RB_RED_P(self)) { + const struct rb_node *brother; + KASSERT(!RB_ROOT_P(rbt, self)); + brother = RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER]; + KASSERT(RB_BLACK_P(RB_FATHER(self))); + /* + * I'm red and have no children, then I must either + * have no brother or my brother also be red and + * also have no children. (black count == 0) + */ + KASSERT(!RB_CHILDLESS_P(self) + || RB_SENTINEL_P(brother) + || RB_RED_P(brother) + || RB_CHILDLESS_P(brother)); + /* + * If I'm not childless, I must have two children + * and they must be both be black. + */ + KASSERT(RB_CHILDLESS_P(self) + || (RB_TWOCHILDREN_P(self) + && RB_BLACK_P(self->rb_left) + && RB_BLACK_P(self->rb_right))); + /* + * If I'm not childless, thus I have black children, + * then my brother must either be black or have two + * black children. + */ + KASSERT(RB_CHILDLESS_P(self) + || RB_BLACK_P(brother) + || (RB_TWOCHILDREN_P(brother) + && RB_BLACK_P(brother->rb_left) + && RB_BLACK_P(brother->rb_right))); + } else { + /* + * If I'm black and have one child, that child must + * be red and childless. + */ + KASSERT(RB_CHILDLESS_P(self) + || RB_TWOCHILDREN_P(self) + || (!RB_LEFT_SENTINEL_P(self) + && RB_RIGHT_SENTINEL_P(self) + && RB_RED_P(self->rb_left) + && RB_CHILDLESS_P(self->rb_left)) + || (!RB_RIGHT_SENTINEL_P(self) + && RB_LEFT_SENTINEL_P(self) + && RB_RED_P(self->rb_right) + && RB_CHILDLESS_P(self->rb_right))); + + /* + * If I'm a childless black node and my parent is + * black, my 2nd closet relative away from my parent + * is either red or has a red parent or red children. + */ + if (!RB_ROOT_P(rbt, self) + && RB_CHILDLESS_P(self) + && RB_BLACK_P(RB_FATHER(self))) { + const unsigned int which = RB_POSITION(self); + const unsigned int other = which ^ RB_DIR_OTHER; + const struct rb_node *relative0, *relative; + + relative0 = rb_tree_iterate_const(rbt, + self, other); + KASSERT(relative0 != NULL); + relative = rb_tree_iterate_const(rbt, + relative0, other); + KASSERT(relative != NULL); + KASSERT(RB_SENTINEL_P(relative->rb_nodes[which])); +#if 0 + KASSERT(RB_RED_P(relative) + || RB_RED_P(relative->rb_left) + || RB_RED_P(relative->rb_right) + || RB_RED_P(RB_FATHER(relative))); +#endif + } + } + /* + * A grandparent's children must be real nodes and not + * sentinels. First check out grandparent. + */ + KASSERT(RB_ROOT_P(rbt, self) + || RB_ROOT_P(rbt, RB_FATHER(self)) + || RB_TWOCHILDREN_P(RB_FATHER(RB_FATHER(self)))); + /* + * If we are have grandchildren on our left, then + * we must have a child on our right. + */ + KASSERT(RB_LEFT_SENTINEL_P(self) + || RB_CHILDLESS_P(self->rb_left) + || !RB_RIGHT_SENTINEL_P(self)); + /* + * If we are have grandchildren on our right, then + * we must have a child on our left. + */ + KASSERT(RB_RIGHT_SENTINEL_P(self) + || RB_CHILDLESS_P(self->rb_right) + || !RB_LEFT_SENTINEL_P(self)); + + /* + * If we have a child on the left and it doesn't have two + * children make sure we don't have great-great-grandchildren on + * the right. + */ + KASSERT(RB_TWOCHILDREN_P(self->rb_left) + || RB_CHILDLESS_P(self->rb_right) + || RB_CHILDLESS_P(self->rb_right->rb_left) + || RB_CHILDLESS_P(self->rb_right->rb_left->rb_left) + || RB_CHILDLESS_P(self->rb_right->rb_left->rb_right) + || RB_CHILDLESS_P(self->rb_right->rb_right) + || RB_CHILDLESS_P(self->rb_right->rb_right->rb_left) + || RB_CHILDLESS_P(self->rb_right->rb_right->rb_right)); + + /* + * If we have a child on the right and it doesn't have two + * children make sure we don't have great-great-grandchildren on + * the left. + */ + KASSERT(RB_TWOCHILDREN_P(self->rb_right) + || RB_CHILDLESS_P(self->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_left->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_left->rb_right) + || RB_CHILDLESS_P(self->rb_left->rb_right) + || RB_CHILDLESS_P(self->rb_left->rb_right->rb_left) + || RB_CHILDLESS_P(self->rb_left->rb_right->rb_right)); + + /* + * If we are fully interior node, then our predecessors and + * successors must have no children in our direction. + */ + if (RB_TWOCHILDREN_P(self)) { + const struct rb_node *prev0; + const struct rb_node *next0; + + prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT); + KASSERT(prev0 != NULL); + KASSERT(RB_RIGHT_SENTINEL_P(prev0)); + + next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT); + KASSERT(next0 != NULL); + KASSERT(RB_LEFT_SENTINEL_P(next0)); + } + } + + return true; +} + +void +rb_tree_check(const struct rb_tree *rbt, bool red_check) +{ + const struct rb_node *self; + const struct rb_node *prev; +#ifdef RBSTATS + unsigned int count = 0; +#endif + + KASSERT(rbt->rbt_root != NULL); + KASSERT(RB_LEFT_P(rbt->rbt_root)); + +#if defined(RBSTATS) && !defined(RBSMALL) + KASSERT(rbt->rbt_count > 1 + || rbt->rbt_minmax[RB_DIR_LEFT] == rbt->rbt_minmax[RB_DIR_RIGHT]); +#endif + + prev = NULL; + TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) { + rb_tree_check_node(rbt, self, prev, false); +#ifdef RBSTATS + count++; +#endif + } +#ifdef RBSTATS + KASSERT(rbt->rbt_count == count); +#endif + if (red_check) { + KASSERT(RB_BLACK_P(rbt->rbt_root)); + KASSERT(RB_SENTINEL_P(rbt->rbt_root) + || rb_tree_count_black(rbt->rbt_root)); + + /* + * The root must be black. + * There can never be two adjacent red nodes. + */ + TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) { + rb_tree_check_node(rbt, self, NULL, true); + } + } +} +#endif /* RBDEBUG */ + +#ifdef RBSTATS +static void +rb_tree_mark_depth(const struct rb_tree *rbt, const struct rb_node *self, + size_t *depths, size_t depth) +{ + if (RB_SENTINEL_P(self)) + return; + + if (RB_TWOCHILDREN_P(self)) { + rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1); + rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1); + return; + } + depths[depth]++; + if (!RB_LEFT_SENTINEL_P(self)) { + rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1); + } + if (!RB_RIGHT_SENTINEL_P(self)) { + rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1); + } +} + +void +rb_tree_depths(const struct rb_tree *rbt, size_t *depths) +{ + rb_tree_mark_depth(rbt, rbt->rbt_root, depths, 1); +} +#endif /* RBSTATS */