From e0e4b153c936c1e8f2ca506b3e671da9fb80cd83 Mon Sep 17 00:00:00 2001 From: thorpej Date: Tue, 17 Jun 2008 21:29:47 +0000 Subject: [PATCH] _prop_rb_tree_insert_node() now returns true/false to indicate if the insertion succeeded. Update existing usage that arranges for insertions to always succeed to assert that they do. --- common/lib/libprop/prop_dictionary.c | 6 ++++-- common/lib/libprop/prop_number.c | 6 ++++-- common/lib/libprop/prop_rb.c | 12 ++++++++++-- common/lib/libprop/prop_rb_impl.h | 4 ++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/common/lib/libprop/prop_dictionary.c b/common/lib/libprop/prop_dictionary.c index d9dc7f69ecf5..49760d5c9240 100644 --- a/common/lib/libprop/prop_dictionary.c +++ b/common/lib/libprop/prop_dictionary.c @@ -1,4 +1,4 @@ -/* $NetBSD: prop_dictionary.c,v 1.30 2008/05/24 14:32:48 yamt Exp $ */ +/* $NetBSD: prop_dictionary.c,v 1.31 2008/06/17 21:29:47 thorpej Exp $ */ /*- * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc. @@ -268,6 +268,7 @@ _prop_dict_keysym_alloc(const char *key) prop_dictionary_keysym_t opdk, pdk; const struct rb_node *n; size_t size; + bool rv; /* * Check to see if this already exists in the tree. If it does, @@ -325,7 +326,8 @@ _prop_dict_keysym_alloc(const char *key) _prop_dict_keysym_put(pdk); return (opdk); } - _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, &pdk->pdk_link); + rv = _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, &pdk->pdk_link); + _PROP_ASSERT(rv == true); _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex); return (pdk); } diff --git a/common/lib/libprop/prop_number.c b/common/lib/libprop/prop_number.c index d1fcbd1635a8..89e132128ef6 100644 --- a/common/lib/libprop/prop_number.c +++ b/common/lib/libprop/prop_number.c @@ -1,4 +1,4 @@ -/* $NetBSD: prop_number.c,v 1.17 2008/04/28 20:22:53 martin Exp $ */ +/* $NetBSD: prop_number.c,v 1.18 2008/06/17 21:29:47 thorpej Exp $ */ /*- * Copyright (c) 2006 The NetBSD Foundation, Inc. @@ -244,6 +244,7 @@ _prop_number_alloc(const struct _prop_number_value *pnv) { prop_number_t opn, pn; struct rb_node *n; + bool rv; /* * Check to see if this already exists in the tree. If it does, @@ -290,7 +291,8 @@ _prop_number_alloc(const struct _prop_number_value *pnv) _PROP_POOL_PUT(_prop_number_pool, pn); return (opn); } - _prop_rb_tree_insert_node(&_prop_number_tree, &pn->pn_link); + rv = _prop_rb_tree_insert_node(&_prop_number_tree, &pn->pn_link); + _PROP_ASSERT(rv == true); _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex); return (pn); } diff --git a/common/lib/libprop/prop_rb.c b/common/lib/libprop/prop_rb.c index 10e084f633f1..83657bc288f4 100644 --- a/common/lib/libprop/prop_rb.c +++ b/common/lib/libprop/prop_rb.c @@ -1,4 +1,4 @@ -/* $NetBSD: prop_rb.c,v 1.8 2008/04/28 20:22:53 martin Exp $ */ +/* $NetBSD: prop_rb.c,v 1.9 2008/06/17 21:29:47 thorpej Exp $ */ /*- * Copyright (c) 2001 The NetBSD Foundation, Inc. @@ -151,7 +151,7 @@ rb_tree_reparent_nodes(struct rb_tree *rbt _PROP_ARG_UNUSED, KASSERT(RB_ROOT_P(new_father) || rb_tree_check_node(rbt, grandpa, NULL, false)); } -void +bool _prop_rb_tree_insert_node(struct rb_tree *rbt, struct rb_node *self) { struct rb_node *parent, *tmp; @@ -176,6 +176,12 @@ _prop_rb_tree_insert_node(struct rb_tree *rbt, struct rb_node *self) */ 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) { @@ -267,6 +273,8 @@ _prop_rb_tree_insert_node(struct rb_tree *rbt, struct rb_node *self) */ _prop_rb_tree_check(rbt, true); #endif + + return true; } static void diff --git a/common/lib/libprop/prop_rb_impl.h b/common/lib/libprop/prop_rb_impl.h index d04ab227c694..fcb083201936 100644 --- a/common/lib/libprop/prop_rb_impl.h +++ b/common/lib/libprop/prop_rb_impl.h @@ -1,4 +1,4 @@ -/* $NetBSD: prop_rb_impl.h,v 1.5 2008/04/28 20:22:53 martin Exp $ */ +/* $NetBSD: prop_rb_impl.h,v 1.6 2008/06/17 21:29:47 thorpej Exp $ */ /*- * Copyright (c) 2001 The NetBSD Foundation, Inc. @@ -125,7 +125,7 @@ struct rb_tree { }; void _prop_rb_tree_init(struct rb_tree *, const struct rb_tree_ops *); -void _prop_rb_tree_insert_node(struct rb_tree *, struct rb_node *); +bool _prop_rb_tree_insert_node(struct rb_tree *, struct rb_node *); struct rb_node * _prop_rb_tree_find(struct rb_tree *, const void *); void _prop_rb_tree_remove_node(struct rb_tree *, struct rb_node *);