From 8fe59d16da6be1fbd1a3a9e507d620bd381b00da Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 13 Dec 2009 20:27:44 +0100 Subject: [PATCH] Move the malloc checking from module 'oset' to new module 'xoset'. --- ChangeLog | 57 +++++++++++++++++++++++++++++++++++++++ NEWS | 5 ++++ lib/gl_anytree_oset.h | 27 ++++++++++++------- lib/gl_anytreehash_list1.h | 21 +++++++++------ lib/gl_array_oset.c | 50 +++++++++++++++++++--------------- lib/gl_avltree_oset.c | 32 ++++++++++++++-------- lib/gl_oset.c | 15 ++++++----- lib/gl_oset.h | 43 +++++++++++++++++++---------- lib/gl_rbtree_oset.c | 32 ++++++++++++++-------- lib/gl_xoset.c | 46 +++++++++++++++++++++++++++++++ lib/gl_xoset.h | 67 ++++++++++++++++++++++++++++++++++++++++++++++ modules/array-oset | 1 - modules/avltree-oset | 1 - modules/avltree-oset-tests | 1 - modules/rbtree-oset | 1 - modules/rbtree-oset-tests | 1 - modules/xoset | 27 +++++++++++++++++++ tests/test-array_oset.c | 9 ++++--- tests/test-avltree_oset.c | 10 ++++--- tests/test-rbtree_oset.c | 12 +++++---- 20 files changed, 357 insertions(+), 101 deletions(-) create mode 100644 lib/gl_xoset.c create mode 100644 lib/gl_xoset.h create mode 100644 modules/xoset diff --git a/ChangeLog b/ChangeLog index 91fd096a9..60412a840 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,60 @@ +2009-12-13 Bruno Haible + + Move the malloc checking from module 'oset' to new module 'xoset'. + * modules/xoset: New file. + * lib/gl_xoset.h: New file. + * lib/gl_xoset.c: New file. + * lib/gl_oset.h (gl_oset_create_empty, gl_oset_add): Disable + declarations. + (gl_oset_nx_create_empty, gl_oset_nx_add): New declarations. + (struct gl_oset_implementation): Rename and change methods accordingly. + (gl_oset_nx_create_empty): Renamed from gl_oset_create_empty. + (gl_oset_nx_add): Renamed from gl_oset_add. Change return type to + 'int'. Mark as __warn_unused_result__. + * lib/gl_oset.c (gl_oset_nx_create_empty): Renamed from + gl_oset_create_empty. + (gl_oset_nx_add): Renamed from gl_oset_add. Change return type to + 'int'. + * lib/gl_array_oset.c: Don't include xalloc.h. + (gl_array_nx_create_empty): Renamed from gl_array_create_empty. Use + malloc, not xmalloc. + (grow): Change return type to 'int'. Don't call xalloc_die. + (gl_array_nx_add_at): Renamed from gl_array_add_at. Change return type + to 'int'. + (gl_array_nx_add): Renamed from gl_array_add. Change return type to + 'int'. + (gl_array_oset_implementation): Update. + * lib/gl_anytree_oset.h (gl_tree_nx_create_empty): Renamed from + gl_tree_create_empty. + (gl_tree_nx_add): Renamed from gl_tree_add. Change return type to + 'int'. + * lib/gl_avltree_oset.c: Don't include xalloc.h. + (gl_tree_nx_add_first): Renamed from gl_tree_add_first. Use malloc, not + xmalloc. + (gl_tree_nx_add_before): Renamed from gl_tree_add_before. Use malloc, + not xmalloc. + (gl_tree_nx_add_after): Renamed from gl_tree_add_after. Use malloc, not + xmalloc. + (gl_avltree_oset_implementation): Update. + * lib/gl_rbtree_oset.c: Don't include xalloc.h. + (gl_tree_nx_add_first): Renamed from gl_tree_add_first. Use malloc, not + xmalloc. + (gl_tree_nx_add_before): Renamed from gl_tree_add_before. Use malloc, + not xmalloc. + (gl_tree_nx_add_after): Renamed from gl_tree_add_after. Use malloc, not + xmalloc. + (gl_rbtree_oset_implementation): Update. + * modules/array-oset (Depends-on): Remove xalloc. + * modules/avltree-oset (Depends-on): Likewise. + * modules/rbtree-oset (Depends-on): Likewise. + * tests/test-array_oset.c: Use gl_oset_nx_* functions where possible. + * tests/test-avltree_oset.c: Likewise. + * tests/test-rbtree_oset.c: Likewise. + * lib/gl_anytreehash_list1.h (add_to_bucket): Likewise. + * modules/avltree-oset-tests (Makefile.am): Don't link with @LIBINTL@. + * modules/rbtree-oset-tests (Makefile.am): Likewise. + * NEWS: Mention the change. + 2009-12-05 Alfred M. Szmidt maint.mk: allow a project to override release-prep commands diff --git a/NEWS b/NEWS index 21fd1f722..b45216f12 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,11 @@ User visible incompatible changes Date Modules Changes +2009-12-13 oset The module does not define functions any more that + call xalloc_die() in out-of-memory situations. + Use module 'xoset' and include file "gl_xoset.h" + instead. + 2009-12-10 * Most source code files have been converted to indentation by spaces (rather than tabs). Patches of gnulib source code needs to be updated. diff --git a/lib/gl_anytree_oset.h b/lib/gl_anytree_oset.h index bbcd7a40e..46f57c916 100644 --- a/lib/gl_anytree_oset.h +++ b/lib/gl_anytree_oset.h @@ -1,5 +1,5 @@ /* Ordered set data type implemented by a binary tree. - Copyright (C) 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2009 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -28,11 +28,15 @@ typedef struct typedef iterstack_item_t iterstack_t[MAXHEIGHT]; static gl_oset_t -gl_tree_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn, - gl_setelement_dispose_fn dispose_fn) +gl_tree_nx_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { - struct gl_oset_impl *set = XMALLOC (struct gl_oset_impl); + struct gl_oset_impl *set = + (struct gl_oset_impl *) malloc (sizeof (struct gl_oset_impl)); + + if (set == NULL) + return NULL; set->base.vtable = implementation; set->base.compar_fn = compar_fn; @@ -132,15 +136,16 @@ gl_tree_search_node (gl_oset_t set, const void *elt) return NULL; } -static bool -gl_tree_add (gl_oset_t set, const void *elt) +static int +gl_tree_nx_add (gl_oset_t set, const void *elt) { gl_setelement_compar_fn compar; gl_oset_node_t node = set->root; if (node == NULL) { - gl_tree_add_first (set, elt); + if (gl_tree_nx_add_first (set, elt) == NULL) + return -1; return true; } @@ -157,7 +162,8 @@ gl_tree_add (gl_oset_t set, const void *elt) { if (node->right == NULL) { - gl_tree_add_after (set, node, elt); + if (gl_tree_nx_add_after (set, node, elt) == NULL) + return -1; return true; } node = node->right; @@ -166,7 +172,8 @@ gl_tree_add (gl_oset_t set, const void *elt) { if (node->left == NULL) { - gl_tree_add_before (set, node, elt); + if (gl_tree_nx_add_before (set, node, elt) == NULL) + return -1; return true; } node = node->left; diff --git a/lib/gl_anytreehash_list1.h b/lib/gl_anytreehash_list1.h index 45418cfb2..ab2f1a675 100644 --- a/lib/gl_anytreehash_list1.h +++ b/lib/gl_anytreehash_list1.h @@ -1,5 +1,5 @@ /* Sequential list data type implemented by a hash table with a binary tree. - Copyright (C) 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2009 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -101,8 +101,8 @@ gl_oset_first (gl_oset_t set) /* Add a node to the hash table structure. If duplicates are allowed, this function performs in average time - O((log n)^2): gl_oset_add may need to add an element to an ordered set of - size O(n), needing O(log n) comparison function calls. The comparison + O((log n)^2): gl_oset_nx_add may need to add an element to an ordered set + of size O(n), needing O(log n) comparison function calls. The comparison function is compare_by_position, which is O(log n) worst-case. If duplicates are forbidden, this function is O(1). */ static void @@ -134,7 +134,8 @@ add_to_bucket (gl_list_t list, gl_list_node_t new_node) { /* Found already multiple nodes with the same value. Add the new_node to it. */ - gl_oset_add (nodes, new_node); + if (gl_oset_nx_add (nodes, new_node) < 0) + xalloc_die (); return; } } @@ -150,11 +151,15 @@ add_to_bucket (gl_list_t list, gl_list_node_t new_node) struct gl_multiple_nodes *multi_entry; nodes = - gl_oset_create_empty (OSET_TREE_FLAVOR, - compare_by_position, NULL); + gl_oset_nx_create_empty (OSET_TREE_FLAVOR, + compare_by_position, NULL); + if (nodes == NULL) + xalloc_die (); - gl_oset_add (nodes, node); - gl_oset_add (nodes, new_node); + if (gl_oset_nx_add (nodes, node) < 0) + xalloc_die (); + if (gl_oset_nx_add (nodes, new_node) < 0) + xalloc_die (); multi_entry = XMALLOC (struct gl_multiple_nodes); multi_entry->h.hash_next = entry->hash_next; diff --git a/lib/gl_array_oset.c b/lib/gl_array_oset.c index 959de3f22..1246f3a87 100644 --- a/lib/gl_array_oset.c +++ b/lib/gl_array_oset.c @@ -1,5 +1,5 @@ /* Ordered set data type implemented by an array. - Copyright (C) 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2009 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -22,8 +22,6 @@ #include -#include "xalloc.h" - /* Checked size_t computations. */ #include "xsize.h" @@ -41,11 +39,15 @@ struct gl_oset_impl }; static gl_oset_t -gl_array_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn, - gl_setelement_dispose_fn dispose_fn) +gl_array_nx_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { - struct gl_oset_impl *set = XMALLOC (struct gl_oset_impl); + struct gl_oset_impl *set = + (struct gl_oset_impl *) malloc (sizeof (struct gl_oset_impl)); + + if (set == NULL) + return NULL; set->base.vtable = implementation; set->base.compar_fn = compar_fn; @@ -155,8 +157,9 @@ gl_array_search_atleast (gl_oset_t set, return false; } -/* Ensure that set->allocated > set->count. */ -static void +/* Ensure that set->allocated > set->count. + Return 0 upon success, -1 upon out-of-memory. */ +static int grow (gl_oset_t set) { size_t new_allocated; @@ -168,31 +171,35 @@ grow (gl_oset_t set) memory_size = xtimes (new_allocated, sizeof (const void *)); if (size_overflow_p (memory_size)) /* Overflow, would lead to out of memory. */ - xalloc_die (); - memory = (const void **) xrealloc (set->elements, memory_size); + return -1; + memory = (const void **) realloc (set->elements, memory_size); if (memory == NULL) /* Out of memory. */ - xalloc_die (); + return -1; set->elements = memory; set->allocated = new_allocated; + return 0; } /* Add the given element ELT at the given position, - 0 <= position <= gl_oset_size (set). */ -static inline void -gl_array_add_at (gl_oset_t set, size_t position, const void *elt) + 0 <= position <= gl_oset_size (set). + Return 1 upon success, -1 upon out-of-memory. */ +static inline int +gl_array_nx_add_at (gl_oset_t set, size_t position, const void *elt) { size_t count = set->count; const void **elements; size_t i; if (count == set->allocated) - grow (set); + if (grow (set) < 0) + return -1; elements = set->elements; for (i = count; i > position; i--) elements[i] = elements[i - 1]; elements[position] = elt; set->count = count + 1; + return 1; } /* Remove the element at the given position, @@ -212,8 +219,8 @@ gl_array_remove_at (gl_oset_t set, size_t position) set->count = count - 1; } -static bool -gl_array_add (gl_oset_t set, const void *elt) +static int +gl_array_nx_add (gl_oset_t set, const void *elt) { size_t count = set->count; size_t low = 0; @@ -244,8 +251,7 @@ gl_array_add (gl_oset_t set, const void *elt) } while (low < high); } - gl_array_add_at (set, low, elt); - return true; + return gl_array_nx_add_at (set, low, elt); } static bool @@ -338,11 +344,11 @@ gl_array_iterator_free (gl_oset_iterator_t *iterator) const struct gl_oset_implementation gl_array_oset_implementation = { - gl_array_create_empty, + gl_array_nx_create_empty, gl_array_size, gl_array_search, gl_array_search_atleast, - gl_array_add, + gl_array_nx_add, gl_array_remove, gl_array_free, gl_array_iterator, diff --git a/lib/gl_avltree_oset.c b/lib/gl_avltree_oset.c index 36e2d5811..058316d51 100644 --- a/lib/gl_avltree_oset.c +++ b/lib/gl_avltree_oset.c @@ -1,5 +1,5 @@ /* Ordered set data type implemented by a binary tree. - Copyright (C) 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2009 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -22,8 +22,6 @@ #include -#include "xalloc.h" - /* An AVL tree is a binary tree where 1. The height of each node is calculated as heightof(node) = 1 + max (heightof(node.left), heightof(node.right)). @@ -306,10 +304,14 @@ rebalance (gl_oset_t set, } static gl_oset_node_t -gl_tree_add_first (gl_oset_t set, const void *elt) +gl_tree_nx_add_first (gl_oset_t set, const void *elt) { /* Create new node. */ - gl_oset_node_t new_node = XMALLOC (struct gl_oset_node_impl); + gl_oset_node_t new_node = + (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl)); + + if (new_node == NULL) + return NULL; new_node->left = NULL; new_node->right = NULL; @@ -343,12 +345,16 @@ gl_tree_add_first (gl_oset_t set, const void *elt) } static gl_oset_node_t -gl_tree_add_before (gl_oset_t set, gl_oset_node_t node, const void *elt) +gl_tree_nx_add_before (gl_oset_t set, gl_oset_node_t node, const void *elt) { /* Create new node. */ - gl_oset_node_t new_node = XMALLOC (struct gl_oset_node_impl); + gl_oset_node_t new_node = + (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl)); bool height_inc; + if (new_node == NULL) + return NULL; + new_node->left = NULL; new_node->right = NULL; new_node->balance = 0; @@ -380,12 +386,16 @@ gl_tree_add_before (gl_oset_t set, gl_oset_node_t node, const void *elt) } static gl_oset_node_t -gl_tree_add_after (gl_oset_t set, gl_oset_node_t node, const void *elt) +gl_tree_nx_add_after (gl_oset_t set, gl_oset_node_t node, const void *elt) { /* Create new node. */ - gl_oset_node_t new_node = XMALLOC (struct gl_oset_node_impl); + gl_oset_node_t new_node = + (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl)); bool height_inc; + if (new_node == NULL) + return NULL; + new_node->left = NULL; new_node->right = NULL; new_node->balance = 0; @@ -560,11 +570,11 @@ gl_avltree_oset_check_invariants (gl_oset_t set) const struct gl_oset_implementation gl_avltree_oset_implementation = { - gl_tree_create_empty, + gl_tree_nx_create_empty, gl_tree_size, gl_tree_search, gl_tree_search_atleast, - gl_tree_add, + gl_tree_nx_add, gl_tree_remove, gl_tree_oset_free, gl_tree_iterator, diff --git a/lib/gl_oset.c b/lib/gl_oset.c index 7182f282a..75bcf9603 100644 --- a/lib/gl_oset.c +++ b/lib/gl_oset.c @@ -27,11 +27,12 @@ Use #define to avoid a warning because of extern vs. static. */ gl_oset_t -gl_oset_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn, - gl_setelement_dispose_fn dispose_fn) +gl_oset_nx_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { - return implementation->create_empty (implementation, compar_fn, dispose_fn); + return implementation->nx_create_empty (implementation, compar_fn, + dispose_fn); } size_t @@ -55,10 +56,10 @@ gl_oset_search_atleast (gl_oset_t set, ->search_atleast (set, threshold_fn, threshold, eltp); } -bool -gl_oset_add (gl_oset_t set, const void *elt) +int +gl_oset_nx_add (gl_oset_t set, const void *elt) { - return ((const struct gl_oset_impl_base *) set)->vtable->add (set, elt); + return ((const struct gl_oset_impl_base *) set)->vtable->nx_add (set, elt); } bool diff --git a/lib/gl_oset.h b/lib/gl_oset.h index 42f46ecbc..92f3476b5 100644 --- a/lib/gl_oset.h +++ b/lib/gl_oset.h @@ -89,9 +89,15 @@ typedef const struct gl_oset_implementation * gl_oset_implementation_t; IMPLEMENTATION is one of GL_ARRAY_OSET, GL_AVLTREE_OSET, GL_RBTREE_OSET. COMPAR_FN is an element comparison function or NULL. DISPOSE_FN is an element disposal function or NULL. */ +#if 0 /* declared in gl_xoset.h */ extern gl_oset_t gl_oset_create_empty (gl_oset_implementation_t implementation, gl_setelement_compar_fn compar_fn, gl_setelement_dispose_fn dispose_fn); +#endif +/* Likewise. Return NULL upon out-of-memory. */ +extern gl_oset_t gl_oset_nx_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn); /* Return the current number of elements in an ordered set. */ extern size_t gl_oset_size (gl_oset_t set); @@ -111,8 +117,16 @@ extern bool gl_oset_search_atleast (gl_oset_t set, const void **eltp); /* Add an element to an ordered set. - Return true if it was not already in the set and added. */ + Return true if it was not already in the set and added, false otherwise. */ +#if 0 /* declared in gl_xoset.h */ extern bool gl_oset_add (gl_oset_t set, const void *elt); +#endif +/* Likewise. Return -1 upon out-of-memory. */ +extern int gl_oset_nx_add (gl_oset_t set, const void *elt) +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) + __attribute__ ((__warn_unused_result__)) +#endif + ; /* Remove an element from an ordered set. Return true if it was found and removed. */ @@ -159,15 +173,15 @@ extern void gl_oset_iterator_free (gl_oset_iterator_t *iterator); struct gl_oset_implementation { /* gl_oset_t functions. */ - gl_oset_t (*create_empty) (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn, - gl_setelement_dispose_fn dispose_fn); + gl_oset_t (*nx_create_empty) (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn); size_t (*size) (gl_oset_t set); bool (*search) (gl_oset_t set, const void *elt); bool (*search_atleast) (gl_oset_t set, gl_setelement_threshold_fn threshold_fn, const void *threshold, const void **eltp); - bool (*add) (gl_oset_t set, const void *elt); + int (*nx_add) (gl_oset_t set, const void *elt); bool (*remove_elt) (gl_oset_t set, const void *elt); void (*oset_free) (gl_oset_t set); /* gl_oset_iterator_t functions. */ @@ -189,13 +203,14 @@ struct gl_oset_impl_base struct gl_oset_implementation. Use #define to avoid a warning because of extern vs. static. */ -# define gl_oset_create_empty gl_oset_create_empty_inline +# define gl_oset_nx_create_empty gl_oset_nx_create_empty_inline static inline gl_oset_t -gl_oset_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn, - gl_setelement_dispose_fn dispose_fn) +gl_oset_nx_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { - return implementation->create_empty (implementation, compar_fn, dispose_fn); + return implementation->nx_create_empty (implementation, compar_fn, + dispose_fn); } # define gl_oset_size gl_oset_size_inline @@ -222,11 +237,11 @@ gl_oset_search_atleast (gl_oset_t set, ->search_atleast (set, threshold_fn, threshold, eltp); } -# define gl_oset_add gl_oset_add_inline -static inline bool -gl_oset_add (gl_oset_t set, const void *elt) +# define gl_oset_nx_add gl_oset_nx_add_inline +static inline int +gl_oset_nx_add (gl_oset_t set, const void *elt) { - return ((const struct gl_oset_impl_base *) set)->vtable->add (set, elt); + return ((const struct gl_oset_impl_base *) set)->vtable->nx_add (set, elt); } # define gl_oset_remove gl_oset_remove_inline diff --git a/lib/gl_rbtree_oset.c b/lib/gl_rbtree_oset.c index 6247972a3..11fdec35e 100644 --- a/lib/gl_rbtree_oset.c +++ b/lib/gl_rbtree_oset.c @@ -1,5 +1,5 @@ /* Ordered set data type implemented by a binary tree. - Copyright (C) 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2009 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -22,8 +22,6 @@ #include -#include "xalloc.h" - /* A red-black tree is a binary tree where every node is colored black or red such that 1. The root is black. @@ -538,10 +536,14 @@ rebalance_after_remove (gl_oset_t set, gl_oset_node_t child, gl_oset_node_t pare } static gl_oset_node_t -gl_tree_add_first (gl_oset_t set, const void *elt) +gl_tree_nx_add_first (gl_oset_t set, const void *elt) { /* Create new node. */ - gl_oset_node_t new_node = XMALLOC (struct gl_oset_node_impl); + gl_oset_node_t new_node = + (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl)); + + if (new_node == NULL) + return NULL; new_node->left = NULL; new_node->right = NULL; @@ -573,10 +575,14 @@ gl_tree_add_first (gl_oset_t set, const void *elt) } static gl_oset_node_t -gl_tree_add_before (gl_oset_t set, gl_oset_node_t node, const void *elt) +gl_tree_nx_add_before (gl_oset_t set, gl_oset_node_t node, const void *elt) { /* Create new node. */ - gl_oset_node_t new_node = XMALLOC (struct gl_oset_node_impl); + gl_oset_node_t new_node = + (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl)); + + if (new_node == NULL) + return NULL; new_node->left = NULL; new_node->right = NULL; @@ -601,10 +607,14 @@ gl_tree_add_before (gl_oset_t set, gl_oset_node_t node, const void *elt) } static gl_oset_node_t -gl_tree_add_after (gl_oset_t set, gl_oset_node_t node, const void *elt) +gl_tree_nx_add_after (gl_oset_t set, gl_oset_node_t node, const void *elt) { /* Create new node. */ - gl_oset_node_t new_node = XMALLOC (struct gl_oset_node_impl); + gl_oset_node_t new_node = + (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl)); + + if (new_node == NULL) + return NULL; new_node->left = NULL; new_node->right = NULL; @@ -791,11 +801,11 @@ gl_rbtree_oset_check_invariants (gl_oset_t set) const struct gl_oset_implementation gl_rbtree_oset_implementation = { - gl_tree_create_empty, + gl_tree_nx_create_empty, gl_tree_size, gl_tree_search, gl_tree_search_atleast, - gl_tree_add, + gl_tree_nx_add, gl_tree_remove, gl_tree_oset_free, gl_tree_iterator, diff --git a/lib/gl_xoset.c b/lib/gl_xoset.c new file mode 100644 index 000000000..0da077002 --- /dev/null +++ b/lib/gl_xoset.c @@ -0,0 +1,46 @@ +/* Abstract ordered set data type, with out-of-memory checking. + Copyright (C) 2009 Free Software Foundation, Inc. + Written by Bruno Haible , 2009. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include "gl_xoset.h" + +#if !HAVE_INLINE + +gl_oset_t +gl_oset_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) +{ + gl_oset_t result = + gl_oset_nx_create_empty (implementation, compar_fn, dispose_fn); + if (result == NULL) + xalloc_die (); + return result; +} + +bool +gl_oset_add (gl_oset_t set, const void *elt) +{ + int result = gl_oset_nx_add (set, elt); + if (result < 0) + xalloc_die (); + return result; +} + +#endif diff --git a/lib/gl_xoset.h b/lib/gl_xoset.h new file mode 100644 index 000000000..d9701b71f --- /dev/null +++ b/lib/gl_xoset.h @@ -0,0 +1,67 @@ +/* Abstract ordered set data type, with out-of-memory checking. + Copyright (C) 2009 Free Software Foundation, Inc. + Written by Bruno Haible , 2009. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef _GL_XOSET_H +#define _GL_XOSET_H + +#include "gl_oset.h" +#include "xalloc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* These functions are thin wrappers around the corresponding functions with + _nx_ infix from gl_oset.h. Upon out-of-memory, they invoke xalloc_die (), + instead of returning an error indicator. */ +extern gl_oset_t gl_oset_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn); +extern bool gl_oset_add (gl_oset_t set, const void *elt); + +#if HAVE_INLINE + +# define gl_oset_create_empty gl_oset_create_empty_inline +static inline gl_oset_t +gl_oset_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) +{ + gl_oset_t result = + gl_oset_nx_create_empty (implementation, compar_fn, dispose_fn); + if (result == NULL) + xalloc_die (); + return result; +} + +# define gl_oset_add gl_oset_add_inline +static inline bool +gl_oset_add (gl_oset_t set, const void *elt) +{ + int result = gl_oset_nx_add (set, elt); + if (result < 0) + xalloc_die (); + return result; +} + +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _GL_XOSET_H */ diff --git a/modules/array-oset b/modules/array-oset index 7d05a2f9c..2f264ede9 100644 --- a/modules/array-oset +++ b/modules/array-oset @@ -7,7 +7,6 @@ lib/gl_array_oset.c Depends-on: oset -xalloc xsize configure.ac: diff --git a/modules/avltree-oset b/modules/avltree-oset index 4a7a3f855..ee16eb5dc 100644 --- a/modules/avltree-oset +++ b/modules/avltree-oset @@ -8,7 +8,6 @@ lib/gl_anytree_oset.h Depends-on: oset -xalloc configure.ac: diff --git a/modules/avltree-oset-tests b/modules/avltree-oset-tests index bda7f98ed..a33b6c674 100644 --- a/modules/avltree-oset-tests +++ b/modules/avltree-oset-tests @@ -10,4 +10,3 @@ configure.ac: Makefile.am: TESTS += test-avltree_oset check_PROGRAMS += test-avltree_oset -test_avltree_oset_LDADD = $(LDADD) @LIBINTL@ diff --git a/modules/rbtree-oset b/modules/rbtree-oset index 63253d314..0630673eb 100644 --- a/modules/rbtree-oset +++ b/modules/rbtree-oset @@ -8,7 +8,6 @@ lib/gl_anytree_oset.h Depends-on: oset -xalloc configure.ac: diff --git a/modules/rbtree-oset-tests b/modules/rbtree-oset-tests index 17e6a8308..0ac783d58 100644 --- a/modules/rbtree-oset-tests +++ b/modules/rbtree-oset-tests @@ -10,4 +10,3 @@ configure.ac: Makefile.am: TESTS += test-rbtree_oset check_PROGRAMS += test-rbtree_oset -test_rbtree_oset_LDADD = $(LDADD) @LIBINTL@ diff --git a/modules/xoset b/modules/xoset new file mode 100644 index 000000000..ba4eab776 --- /dev/null +++ b/modules/xoset @@ -0,0 +1,27 @@ +Description: +Abstract ordered set data type, with out-of-memory checking. + +Files: +lib/gl_xoset.h +lib/gl_xoset.c +m4/gl_list.m4 + +Depends-on: +inline +stdbool +xalloc-die + +configure.ac: +gl_LIST + +Makefile.am: +lib_SOURCES += gl_xoset.h gl_xoset.c + +Include: +"gl_xoset.h" + +License: +GPL + +Maintainer: +Bruno Haible diff --git a/tests/test-array_oset.c b/tests/test-array_oset.c index 32fd191a1..20025305c 100644 --- a/tests/test-array_oset.c +++ b/tests/test-array_oset.c @@ -1,5 +1,5 @@ /* Test of ordered set data type implementation. - Copyright (C) 2006-2008 Free Software Foundation, Inc. + Copyright (C) 2006-2009 Free Software Foundation, Inc. Written by Bruno Haible , 2007. This program is free software: you can redistribute it and/or modify @@ -96,7 +96,8 @@ main (int argc, char *argv[]) unsigned int repeat; /* Create set1. */ - set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); + set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); + ASSERT (set1 != NULL); /* Create set2. */ set2 = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false); @@ -107,7 +108,7 @@ main (int argc, char *argv[]) for (i = 0; i < initial_size; i++) { const char *obj = RANDOM_OBJECT (); - ASSERT (gl_oset_add (set1, obj) + ASSERT (gl_oset_nx_add (set1, obj) == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL ? false : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true))); @@ -129,7 +130,7 @@ main (int argc, char *argv[]) case 1: { const char *obj = RANDOM_OBJECT (); - ASSERT (gl_oset_add (set1, obj) + ASSERT (gl_oset_nx_add (set1, obj) == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL ? false : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true))); diff --git a/tests/test-avltree_oset.c b/tests/test-avltree_oset.c index 987237812..c07281ae4 100644 --- a/tests/test-avltree_oset.c +++ b/tests/test-avltree_oset.c @@ -96,10 +96,12 @@ main (int argc, char *argv[]) unsigned int repeat; /* Create set1. */ - set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); + set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); + ASSERT (set1 != NULL); /* Create set2. */ - set2 = gl_oset_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL); + set2 = gl_oset_nx_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL); + ASSERT (set2 != NULL); check_all (set1, set2); @@ -107,7 +109,7 @@ main (int argc, char *argv[]) for (i = 0; i < initial_size; i++) { const char *obj = RANDOM_OBJECT (); - ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj)); + ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj)); check_all (set1, set2); } @@ -125,7 +127,7 @@ main (int argc, char *argv[]) case 1: { const char *obj = RANDOM_OBJECT (); - ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj)); + ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj)); } break; case 2: diff --git a/tests/test-rbtree_oset.c b/tests/test-rbtree_oset.c index 77a783876..79bcc89a0 100644 --- a/tests/test-rbtree_oset.c +++ b/tests/test-rbtree_oset.c @@ -1,5 +1,5 @@ /* Test of ordered set data type implementation. - Copyright (C) 2006-2008 Free Software Foundation, Inc. + Copyright (C) 2006-2009 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -96,10 +96,12 @@ main (int argc, char *argv[]) unsigned int repeat; /* Create set1. */ - set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); + set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); + ASSERT (set1 != NULL); /* Create set2. */ - set2 = gl_oset_create_empty (GL_RBTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL); + set2 = gl_oset_nx_create_empty (GL_RBTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL); + ASSERT (set2 != NULL); check_all (set1, set2); @@ -107,7 +109,7 @@ main (int argc, char *argv[]) for (i = 0; i < initial_size; i++) { const char *obj = RANDOM_OBJECT (); - ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj)); + ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj)); check_all (set1, set2); } @@ -125,7 +127,7 @@ main (int argc, char *argv[]) case 1: { const char *obj = RANDOM_OBJECT (); - ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj)); + ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj)); } break; case 2: -- 2.11.0