]> Savannah Git Hosting - gnulib.git/commitdiff
flexmember: Make it easier to use.
authorBruno Haible <bruno@clisp.org>
Fri, 26 May 2023 22:18:57 +0000 (00:18 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 26 May 2023 22:18:57 +0000 (00:18 +0200)
* lib/flexmember.h (FLEXNSIZEOF): New macro.
* lib/hamt.c (alloc_bucket, alloc_subtrie): Fix FLEXSIZEOF invocation.
Use FLEXNSIZEOF instead of FLEXSIZEOF.
* lib/ssfmalloc.h (init_small_block_page_pool): Use FLEXNSIZEOF instead
of FLEXSIZEOF.

ChangeLog
lib/flexmember.h
lib/hamt.c
lib/ssfmalloc.h

index 29072ee48beafca3b4542175c23eb4fc6158a6de..fc12332373bbce3ba8653db7199b56a8c8dc47c2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2023-05-26  Bruno Haible  <bruno@clisp.org>
+
+       flexmember: Make it easier to use.
+       * lib/flexmember.h (FLEXNSIZEOF): New macro.
+       * lib/hamt.c (alloc_bucket, alloc_subtrie): Fix FLEXSIZEOF invocation.
+       Use FLEXNSIZEOF instead of FLEXSIZEOF.
+       * lib/ssfmalloc.h (init_small_block_page_pool): Use FLEXNSIZEOF instead
+       of FLEXSIZEOF.
+
 2023-05-26  Bruno Haible  <bruno@clisp.org>
 
        diffseq: Silence another gcc warning.
index 8c5915ecf9cd89d927a288d6f4d1f2aea82f2d6a..8df441953929715c713e5b7c265470bcdd228a4d 100644 (file)
@@ -43,7 +43,7 @@
    followed by N bytes of other data.  The result is suitable as an
    argument to malloc.  For example:
 
-     struct s { int n; char d[FLEXIBLE_ARRAY_MEMBER]; };
+     struct s { int a; char d[FLEXIBLE_ARRAY_MEMBER]; };
      struct s *p = malloc (FLEXSIZEOF (struct s, d, n * sizeof (char)));
 
    FLEXSIZEOF (TYPE, MEMBER, N) is not simply (sizeof (TYPE) + N),
 #define FLEXSIZEOF(type, member, n) \
    ((offsetof (type, member) + FLEXALIGNOF (type) - 1 + (n)) \
     & ~ (FLEXALIGNOF (type) - 1))
+
+/* Yield a properly aligned upper bound on the size of a struct of
+   type TYPE with a flexible array member named MEMBER that has N
+   elements.  The result is suitable as an argument to malloc.
+   For example:
+
+     struct s { int a; double d[FLEXIBLE_ARRAY_MEMBER]; };
+     struct s *p = malloc (FLEXNSIZEOF (struct s, d, n));
+ */
+#define FLEXNSIZEOF(type, member, n) \
+  FLEXSIZEOF (type, member, (n) * sizeof (((type *) 0)->member[0]))
index 8cbca387f0956242f246bf9e765e6b35d04ebd98..6f39b77c2f2fc24a4a45a1cc197ae011d8465e24 100644 (file)
@@ -214,8 +214,7 @@ static struct bucket *
 alloc_bucket (size_t elt_count)
 {
   struct bucket *bucket
-    = xmalloc (FLEXSIZEOF (struct bucket, elts,
-                           sizeof (Hamt_entry) * elt_count));
+    = xmalloc (FLEXNSIZEOF (struct bucket, elts, elt_count));
   init_ref_counter (&bucket->ref_counter, bucket_entry);
   bucket->elt_count = elt_count;
   return bucket;
@@ -251,8 +250,7 @@ static struct subtrie *
 alloc_subtrie (int node_count)
 {
   struct subtrie *subtrie
-    = xmalloc (FLEXSIZEOF (struct subtrie, nodes,
-                           sizeof (Hamt_entry) * node_count));
+    = xmalloc (FLEXNSIZEOF (struct subtrie, nodes, node_count));
   init_ref_counter (&subtrie->ref_count, subtrie_entry);
   return subtrie;
 }
index 87f44014e45990e38228a3f0aa7edb5565c58fca..5dd69c8199490bd660407eafc2aa842ff51b029b 100644 (file)
@@ -332,8 +332,8 @@ static unsigned int small_block_page_num_bitmap_words;
 struct small_page_header
 {
   struct dissected_page_header common;
-  /* Two bitmaps, each with small_block_page_num_bitmap_words. In each a bit
-     represents ALIGNMENT bytes.
+  /* Two bitmaps, each with small_block_page_num_bitmap_words words. In each
+     a bit represents ALIGNMENT bytes.
        - available_bitmap: bit set means available, bit clear means allocated.
        - blockend_bitmap: bit set means the an allocated block ends here.  */
   uint32_t bitmap_words[FLEXIBLE_ARRAY_MEMBER];
@@ -365,8 +365,8 @@ init_small_block_page_pool (struct page_pool *pool)
     {
       num_bitmap_words = (num_bits + 32 - 1) / 32;
       blocks_start =
-        (FLEXSIZEOF (struct small_page_header, bitmap_words,
-                     2 * num_bitmap_words * sizeof (uint32_t))
+        (FLEXNSIZEOF (struct small_page_header, bitmap_words,
+                      2 * num_bitmap_words)
          + ALIGNMENT - 1) & -ALIGNMENT;
       unsigned int num_bits_r = (unsigned int) (PAGESIZE - blocks_start) / ALIGNMENT;
       if (num_bits_r >= num_bits)