bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

proposed xalloc-related changes to array-list, array-oset, etc.


From: Paul Eggert
Subject: proposed xalloc-related changes to array-list, array-oset, etc.
Date: Mon, 06 Nov 2006 15:29:41 -0800
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

By code inspection I noticed some other opportunities to use
XMALLOC instead of xmalloc, XCALLOC instead of xcalloc,
xmemdup instead of XNMALLOC+memcpy, and x2nrealloc instead of
a hand-written replacement.  Here's a proposed (but untested)
patch.

2006-11-06  Paul Eggert  <address@hidden>

        Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
        * lib/gl_anyavltree_list2.h (create_subtree_with_contents):
        (gl_tree_create, gl_tree_add_first, gl_tree_add_last):
        (gl_tree_add_before, gl_tree_add_after):
        Use XMALLOC instead of xmalloc, and XCALLOC instead of xzalloc.
        * lib/gl_anyhash_list2.h (hash_resize): Likewise.
        * lib/gl_anylinked_list2.h (gl_linked_create_empty, gl_linked_create):
        (gl_linked_add_first, gl_linked_add_last, gl_linked_add_before):
        (gl_linked_add_after, gl_linked_add_at): Likewise.
        * lib/gl_anyrbtree_list2.h (create_subtree_with_contents):
        (gl_tree_create, gl_tree_add_first, gl_tree_add_last):
        (gl_tree_add_before, gl_tree_add_after): Likewise.
        * lib/gl_anytree_list2.h (gl_tree_create_empty): Likewise.
        * lib/gl_anytree_oset.h (gl_tree_create_empty): Likewise.
        * lib/gl_anytreehash_list1.h (add_to_bucket): Likewise.

        * lib/gl_array_list.c (gl_array_create): Use xmemdup instead
        of XNMALLOC followed by memcpy.
        * lib/gl_carray_list.c (gl_carray_create): Likewise.

        * lib/gl_array_list.c (grow): Use x2nrealloc rather than
        doing the work ourselves.
        * lib/gl_array_oset.c (grow): Likewise.

Index: lib/gl_anyavltree_list2.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_anyavltree_list2.h,v
retrieving revision 1.1
diff -p -c -r1.1 gl_anyavltree_list2.h
*** lib/gl_anyavltree_list2.h   17 Jul 2006 11:27:18 -0000      1.1
--- lib/gl_anyavltree_list2.h   6 Nov 2006 23:25:34 -0000
*************** create_subtree_with_contents (size_t cou
*** 28,35 ****
    size_t half1 = (count - 1) / 2;
    size_t half2 = count / 2;
    /* Note: half1 + half2 = count - 1.  */
!   gl_list_node_t node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    if (half1 > 0)
      {
--- 28,34 ----
    size_t half1 = (count - 1) / 2;
    size_t half2 = count / 2;
    /* Note: half1 + half2 = count - 1.  */
!   gl_list_node_t node = XMALLOC (struct gl_list_node_impl);
  
    if (half1 > 0)
      {
*************** gl_tree_create (gl_list_implementation_t
*** 67,74 ****
                bool allow_duplicates,
                size_t count, const void **contents)
  {
!   struct gl_list_impl *list =
!     (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
--- 66,72 ----
                bool allow_duplicates,
                size_t count, const void **contents)
  {
!   struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
*************** gl_tree_create (gl_list_implementation_t
*** 80,87 ****
      if (estimate < 10)
        estimate = 10;
      list->table_size = next_prime (estimate);
!     list->table =
!       (gl_hash_entry_t *) xzalloc (list->table_size * sizeof 
(gl_hash_entry_t));
    }
  #endif
    if (count > 0)
--- 78,84 ----
      if (estimate < 10)
        estimate = 10;
      list->table_size = next_prime (estimate);
!     list->table = XCALLOC (list->table_size, gl_hash_entry_t);
    }
  #endif
    if (count > 0)
*************** static gl_list_node_t
*** 374,381 ****
  gl_tree_add_first (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    new_node->left = NULL;
    new_node->right = NULL;
--- 371,377 ----
  gl_tree_add_first (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    new_node->left = NULL;
    new_node->right = NULL;
*************** static gl_list_node_t
*** 434,441 ****
  gl_tree_add_last (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    new_node->left = NULL;
    new_node->right = NULL;
--- 430,436 ----
  gl_tree_add_last (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    new_node->left = NULL;
    new_node->right = NULL;
*************** static gl_list_node_t
*** 494,501 ****
  gl_tree_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
    bool height_inc;
  
    new_node->left = NULL;
--- 489,495 ----
  gl_tree_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
    bool height_inc;
  
    new_node->left = NULL;
*************** static gl_list_node_t
*** 554,561 ****
  gl_tree_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
    bool height_inc;
  
    new_node->left = NULL;
--- 548,554 ----
  gl_tree_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
    bool height_inc;
  
    new_node->left = NULL;
Index: lib/gl_anyhash_list2.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_anyhash_list2.h,v
retrieving revision 1.2
diff -p -c -r1.2 gl_anyhash_list2.h
*** lib/gl_anyhash_list2.h      10 Oct 2006 12:48:57 -0000      1.2
--- lib/gl_anyhash_list2.h      6 Nov 2006 23:25:34 -0000
*************** hash_resize (gl_list_t list, size_t esti
*** 100,107 ****
      {
        gl_hash_entry_t *old_table = list->table;
        /* Allocate the new table.  */
!       gl_hash_entry_t *new_table =
!       (gl_hash_entry_t *) xzalloc (new_size * sizeof (gl_hash_entry_t));
        size_t i;
  
        /* Iterate through the entries of the old table.  */
--- 100,106 ----
      {
        gl_hash_entry_t *old_table = list->table;
        /* Allocate the new table.  */
!       gl_hash_entry_t *new_table = XCALLOC (new_size, gl_hash_entry_t);
        size_t i;
  
        /* Iterate through the entries of the old table.  */
Index: lib/gl_anylinked_list2.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_anylinked_list2.h,v
retrieving revision 1.6
diff -p -c -r1.6 gl_anylinked_list2.h
*** lib/gl_anylinked_list2.h    10 Oct 2006 12:48:57 -0000      1.6
--- lib/gl_anylinked_list2.h    6 Nov 2006 23:25:34 -0000
*************** gl_linked_create_empty (gl_list_implemen
*** 43,50 ****
                        gl_listelement_hashcode_fn hashcode_fn,
                        bool allow_duplicates)
  {
!   struct gl_list_impl *list =
!     (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
--- 43,49 ----
                        gl_listelement_hashcode_fn hashcode_fn,
                        bool allow_duplicates)
  {
!   struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
*************** gl_linked_create_empty (gl_list_implemen
*** 52,59 ****
    list->base.allow_duplicates = allow_duplicates;
  #if WITH_HASHTABLE
    list->table_size = 11;
!   list->table =
!     (gl_hash_entry_t *) xzalloc (list->table_size * sizeof (gl_hash_entry_t));
  #endif
    list->root.next = &list->root;
    list->root.prev = &list->root;
--- 51,57 ----
    list->base.allow_duplicates = allow_duplicates;
  #if WITH_HASHTABLE
    list->table_size = 11;
!   list->table = XCALLOC (list->table_size, gl_hash_entry_t);
  #endif
    list->root.next = &list->root;
    list->root.prev = &list->root;
*************** gl_linked_create (gl_list_implementation
*** 69,76 ****
                  bool allow_duplicates,
                  size_t count, const void **contents)
  {
!   struct gl_list_impl *list =
!     (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
    gl_list_node_t tail;
  
    list->base.vtable = implementation;
--- 67,73 ----
                  bool allow_duplicates,
                  size_t count, const void **contents)
  {
!   struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
    gl_list_node_t tail;
  
    list->base.vtable = implementation;
*************** gl_linked_create (gl_list_implementation
*** 83,99 ****
      if (estimate < 10)
        estimate = 10;
      list->table_size = next_prime (estimate);
!     list->table =
!       (gl_hash_entry_t *) xzalloc (list->table_size * sizeof 
(gl_hash_entry_t));
    }
  #endif
    list->count = count;
    tail = &list->root;
    for (; count > 0; contents++, count--)
      {
!       gl_list_node_t node =
!       (struct gl_list_node_impl *)
!       xmalloc (sizeof (struct gl_list_node_impl));
  
        node->value = *contents;
  #if WITH_HASHTABLE
--- 80,93 ----
      if (estimate < 10)
        estimate = 10;
      list->table_size = next_prime (estimate);
!     list->table = XCALLOC (list->table_size, gl_hash_entry_t);
    }
  #endif
    list->count = count;
    tail = &list->root;
    for (; count > 0; contents++, count--)
      {
!       gl_list_node_t node = XMALLOC (struct gl_list_node_impl);
  
        node->value = *contents;
  #if WITH_HASHTABLE
*************** gl_linked_indexof_from_to (gl_list_t lis
*** 497,504 ****
  static gl_list_node_t
  gl_linked_add_first (gl_list_t list, const void *elt)
  {
!   gl_list_node_t node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    ASYNCSAFE(const void *) node->value = elt;
  #if WITH_HASHTABLE
--- 491,497 ----
  static gl_list_node_t
  gl_linked_add_first (gl_list_t list, const void *elt)
  {
!   gl_list_node_t node = XMALLOC (struct gl_list_node_impl);
  
    ASYNCSAFE(const void *) node->value = elt;
  #if WITH_HASHTABLE
*************** gl_linked_add_first (gl_list_t list, con
*** 528,535 ****
  static gl_list_node_t
  gl_linked_add_last (gl_list_t list, const void *elt)
  {
!   gl_list_node_t node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    ASYNCSAFE(const void *) node->value = elt;
  #if WITH_HASHTABLE
--- 521,527 ----
  static gl_list_node_t
  gl_linked_add_last (gl_list_t list, const void *elt)
  {
!   gl_list_node_t node = XMALLOC (struct gl_list_node_impl);
  
    ASYNCSAFE(const void *) node->value = elt;
  #if WITH_HASHTABLE
*************** gl_linked_add_last (gl_list_t list, cons
*** 559,566 ****
  static gl_list_node_t
  gl_linked_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  {
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    ASYNCSAFE(const void *) new_node->value = elt;
  #if WITH_HASHTABLE
--- 551,557 ----
  static gl_list_node_t
  gl_linked_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  {
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    ASYNCSAFE(const void *) new_node->value = elt;
  #if WITH_HASHTABLE
*************** gl_linked_add_before (gl_list_t list, gl
*** 590,597 ****
  static gl_list_node_t
  gl_linked_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  {
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    ASYNCSAFE(const void *) new_node->value = elt;
  #if WITH_HASHTABLE
--- 581,587 ----
  static gl_list_node_t
  gl_linked_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  {
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    ASYNCSAFE(const void *) new_node->value = elt;
  #if WITH_HASHTABLE
*************** gl_linked_add_at (gl_list_t list, size_t
*** 628,635 ****
      /* Invalid argument.  */
      abort ();
  
!   new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
    ASYNCSAFE(const void *) new_node->value = elt;
  #if WITH_HASHTABLE
    new_node->h.hashcode =
--- 618,624 ----
      /* Invalid argument.  */
      abort ();
  
!   new_node = XMALLOC (struct gl_list_node_impl);
    ASYNCSAFE(const void *) new_node->value = elt;
  #if WITH_HASHTABLE
    new_node->h.hashcode =
Index: lib/gl_anyrbtree_list2.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_anyrbtree_list2.h,v
retrieving revision 1.1
diff -p -c -r1.1 gl_anyrbtree_list2.h
*** lib/gl_anyrbtree_list2.h    17 Jul 2006 11:27:18 -0000      1.1
--- lib/gl_anyrbtree_list2.h    6 Nov 2006 23:25:34 -0000
*************** create_subtree_with_contents (unsigned i
*** 31,38 ****
    size_t half1 = (count - 1) / 2;
    size_t half2 = count / 2;
    /* Note: half1 + half2 = count - 1.  */
!   gl_list_node_t node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    if (half1 > 0)
      {
--- 31,37 ----
    size_t half1 = (count - 1) / 2;
    size_t half2 = count / 2;
    /* Note: half1 + half2 = count - 1.  */
!   gl_list_node_t node = XMALLOC (struct gl_list_node_impl);
  
    if (half1 > 0)
      {
*************** gl_tree_create (gl_list_implementation_t
*** 72,79 ****
                bool allow_duplicates,
                size_t count, const void **contents)
  {
!   struct gl_list_impl *list =
!     (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
--- 71,77 ----
                bool allow_duplicates,
                size_t count, const void **contents)
  {
!   struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
*************** gl_tree_create (gl_list_implementation_t
*** 85,92 ****
      if (estimate < 10)
        estimate = 10;
      list->table_size = next_prime (estimate);
!     list->table =
!       (gl_hash_entry_t *) xzalloc (list->table_size * sizeof 
(gl_hash_entry_t));
    }
  #endif
    if (count > 0)
--- 83,89 ----
      if (estimate < 10)
        estimate = 10;
      list->table_size = next_prime (estimate);
!     list->table = XCALLOC (list->table_size, gl_hash_entry_t);
    }
  #endif
    if (count > 0)
*************** static gl_list_node_t
*** 599,606 ****
  gl_tree_add_first (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    new_node->left = NULL;
    new_node->right = NULL;
--- 596,602 ----
  gl_tree_add_first (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    new_node->left = NULL;
    new_node->right = NULL;
*************** static gl_list_node_t
*** 657,664 ****
  gl_tree_add_last (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    new_node->left = NULL;
    new_node->right = NULL;
--- 653,659 ----
  gl_tree_add_last (gl_list_t list, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    new_node->left = NULL;
    new_node->right = NULL;
*************** static gl_list_node_t
*** 715,722 ****
  gl_tree_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    new_node->left = NULL;
    new_node->right = NULL;
--- 710,716 ----
  gl_tree_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    new_node->left = NULL;
    new_node->right = NULL;
*************** static gl_list_node_t
*** 766,773 ****
  gl_tree_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node =
!     (struct gl_list_node_impl *) xmalloc (sizeof (struct gl_list_node_impl));
  
    new_node->left = NULL;
    new_node->right = NULL;
--- 760,766 ----
  gl_tree_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  {
    /* Create new node.  */
!   gl_list_node_t new_node = XMALLOC (struct gl_list_node_impl);
  
    new_node->left = NULL;
    new_node->right = NULL;
Index: lib/gl_anytree_list2.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_anytree_list2.h,v
retrieving revision 1.4
diff -p -c -r1.4 gl_anytree_list2.h
*** lib/gl_anytree_list2.h      6 Oct 2006 12:06:07 -0000       1.4
--- lib/gl_anytree_list2.h      6 Nov 2006 23:25:34 -0000
*************** gl_tree_create_empty (gl_list_implementa
*** 25,32 ****
                      gl_listelement_hashcode_fn hashcode_fn,
                      bool allow_duplicates)
  {
!   struct gl_list_impl *list =
!     (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
--- 25,31 ----
                      gl_listelement_hashcode_fn hashcode_fn,
                      bool allow_duplicates)
  {
!   struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
  
    list->base.vtable = implementation;
    list->base.equals_fn = equals_fn;
*************** gl_tree_create_empty (gl_list_implementa
*** 34,41 ****
    list->base.allow_duplicates = allow_duplicates;
  #if WITH_HASHTABLE
    list->table_size = 11;
!   list->table =
!     (gl_hash_entry_t *) xzalloc (list->table_size * sizeof (gl_hash_entry_t));
  #endif
    list->root = NULL;
  
--- 33,39 ----
    list->base.allow_duplicates = allow_duplicates;
  #if WITH_HASHTABLE
    list->table_size = 11;
!   list->table = XCALLOC (list->table_size, gl_hash_entry_t);
  #endif
    list->root = NULL;
  
Index: lib/gl_anytree_oset.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_anytree_oset.h,v
retrieving revision 1.3
diff -p -c -r1.3 gl_anytree_oset.h
*** lib/gl_anytree_oset.h       4 Oct 2006 17:28:15 -0000       1.3
--- lib/gl_anytree_oset.h       6 Nov 2006 23:25:34 -0000
*************** static gl_oset_t
*** 32,39 ****
  gl_tree_create_empty (gl_oset_implementation_t implementation,
                      gl_setelement_compar_fn compar_fn)
  {
!   struct gl_oset_impl *set =
!     (struct gl_oset_impl *) xmalloc (sizeof (struct gl_oset_impl));
  
    set->base.vtable = implementation;
    set->base.compar_fn = compar_fn;
--- 32,38 ----
  gl_tree_create_empty (gl_oset_implementation_t implementation,
                      gl_setelement_compar_fn compar_fn)
  {
!   struct gl_oset_impl *set = XMALLOC (struct gl_oset_impl);
  
    set->base.vtable = implementation;
    set->base.compar_fn = compar_fn;
Index: lib/gl_anytreehash_list1.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_anytreehash_list1.h,v
retrieving revision 1.3
diff -p -c -r1.3 gl_anytreehash_list1.h
*** lib/gl_anytreehash_list1.h  10 Oct 2006 12:48:57 -0000      1.3
--- lib/gl_anytreehash_list1.h  6 Nov 2006 23:25:34 -0000
*************** add_to_bucket (gl_list_t list, gl_list_n
*** 157,164 ****
                      gl_oset_add (nodes, node);
                      gl_oset_add (nodes, new_node);
  
!                     multi_entry =
!                       (struct gl_multiple_nodes *) xmalloc (sizeof (struct 
gl_multiple_nodes));
                      multi_entry->h.hash_next = entry->hash_next;
                      multi_entry->h.hashcode = entry->hashcode;
                      multi_entry->magic = MULTIPLE_NODES_MAGIC;
--- 157,163 ----
                      gl_oset_add (nodes, node);
                      gl_oset_add (nodes, new_node);
  
!                     multi_entry = XMALLOC (struct gl_multiple_nodes);
                      multi_entry->h.hash_next = entry->hash_next;
                      multi_entry->h.hashcode = entry->hashcode;
                      multi_entry->magic = MULTIPLE_NODES_MAGIC;
Index: lib/gl_array_list.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_array_list.c,v
retrieving revision 1.8
diff -p -c -r1.8 gl_array_list.c
*** lib/gl_array_list.c 6 Nov 2006 13:03:51 -0000       1.8
--- lib/gl_array_list.c 6 Nov 2006 23:25:34 -0000
*************** gl_array_create (gl_list_implementation_
*** 84,96 ****
    list->base.equals_fn = equals_fn;
    list->base.hashcode_fn = hashcode_fn;
    list->base.allow_duplicates = allow_duplicates;
!   if (count > 0)
!     {
!       list->elements = XNMALLOC (count, const void *);
!       memcpy (list->elements, contents, count * sizeof (const void *));
!     }
!   else
!     list->elements = NULL;
    list->count = count;
    list->allocated = count;
  
--- 84,92 ----
    list->base.equals_fn = equals_fn;
    list->base.hashcode_fn = hashcode_fn;
    list->base.allow_duplicates = allow_duplicates;
!   list->elements = (count == 0
!                   ? NULL
!                   : xmemdup (contents, count * sizeof (const void *)));
    list->count = count;
    list->allocated = count;
  
*************** gl_array_search_from_to (gl_list_t list,
*** 218,239 ****
  static void
  grow (gl_list_t list)
  {
!   size_t new_allocated;
!   size_t memory_size;
!   const void **memory;
! 
!   new_allocated = xtimes (list->allocated, 2);
!   new_allocated = xsum (new_allocated, 1);
!   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 (list->elements, memory_size);
!   if (memory == NULL)
!     /* Out of memory.  */
!     xalloc_die ();
!   list->elements = memory;
!   list->allocated = new_allocated;
  }
  
  static gl_list_node_t
--- 214,221 ----
  static void
  grow (gl_list_t list)
  {
!   list->elements = x2nrealloc (list->elements, &list->allocated,
!                              sizeof list->elements[0]);
  }
  
  static gl_list_node_t
Index: lib/gl_array_oset.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_array_oset.c,v
retrieving revision 1.7
diff -p -c -r1.7 gl_array_oset.c
*** lib/gl_array_oset.c 6 Nov 2006 13:03:10 -0000       1.7
--- lib/gl_array_oset.c 6 Nov 2006 23:25:34 -0000
*************** gl_array_search_atleast (gl_oset_t set,
*** 158,179 ****
  static void
  grow (gl_oset_t set)
  {
!   size_t new_allocated;
!   size_t memory_size;
!   const void **memory;
! 
!   new_allocated = xtimes (set->allocated, 2);
!   new_allocated = xsum (new_allocated, 1);
!   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);
!   if (memory == NULL)
!     /* Out of memory.  */
!     xalloc_die ();
!   set->elements = memory;
!   set->allocated = new_allocated;
  }
  
  /* Add the given element ELT at the given position,
--- 158,165 ----
  static void
  grow (gl_oset_t set)
  {
!   set->elements = x2nrealloc (set->elements, &set->allocated,
!                             sizeof set->elements[0]);
  }
  
  /* Add the given element ELT at the given position,
Index: lib/gl_carray_list.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gl_carray_list.c,v
retrieving revision 1.7
diff -p -c -r1.7 gl_carray_list.c
*** lib/gl_carray_list.c        6 Nov 2006 13:03:10 -0000       1.7
--- lib/gl_carray_list.c        6 Nov 2006 23:25:34 -0000
*************** gl_carray_create (gl_list_implementation
*** 88,100 ****
    list->base.equals_fn = equals_fn;
    list->base.hashcode_fn = hashcode_fn;
    list->base.allow_duplicates = allow_duplicates;
!   if (count > 0)
!     {
!       list->elements = XNMALLOC (count, const void *);
!       memcpy (list->elements, contents, count * sizeof (const void *));
!     }
!   else
!     list->elements = NULL;
    list->offset = 0;
    list->count = count;
    list->allocated = count;
--- 88,96 ----
    list->base.equals_fn = equals_fn;
    list->base.hashcode_fn = hashcode_fn;
    list->base.allow_duplicates = allow_duplicates;
!   list->elements = (count == 0
!                   ? NULL
!                   : xmemdup (contents, count * sizeof (const void *)));
    list->offset = 0;
    list->count = count;
    list->allocated = count;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]