]> git.ozlabs.org Git - ccan/commitdiff
jmap: Use TCON_WRAP instead of TCON
authorDavid Gibson <david@gibson.dropbear.id.au>
Sun, 23 Jul 2017 05:11:33 +0000 (15:11 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Sun, 23 Jul 2017 11:31:05 +0000 (21:31 +1000)
TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension.  TCON_WRAP() avoids this and so
is more portable.

This doesn't change the jmap interface, only its internals.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/jmap/jmap.h
ccan/jmap/test/run.c

index e30a6ecef4a4c3e98b4522ce5db5b2b0c6e358ff..40147acef556dbe143469bfa3b0463fa837bdb54 100644 (file)
@@ -41,8 +41,7 @@ struct jmap {
  *     };
  */
 #define JMAP_MEMBERS(itype, ctype)             \
  *     };
  */
 #define JMAP_MEMBERS(itype, ctype)             \
-       struct jmap raw;                        \
-       TCON(itype icanary; ctype ccanary)
+       TCON_WRAP(struct jmap, itype icanary; ctype ccanary) jmap_
 
 /**
  * jmap_new - create a new, empty jmap.
 
 /**
  * jmap_new - create a new, empty jmap.
@@ -61,6 +60,12 @@ struct jmap {
  */
 #define jmap_new(type) ((type *)jmap_new_(sizeof(type)))
 
  */
 #define jmap_new(type) ((type *)jmap_new_(sizeof(type)))
 
+/**
+ * jmap_raw_ - unwrap the typed map (no type checking)
+ * @map: the typed jmap
+ */
+#define jmap_raw_(map) tcon_unwrap(&(map)->jmap_)
+
 /**
  * jmap_free - destroy a jmap.
  * @map: the map returned from jmap_new.
 /**
  * jmap_free - destroy a jmap.
  * @map: the map returned from jmap_new.
@@ -68,7 +73,7 @@ struct jmap {
  * Example:
  *     jmap_free(map);
  */
  * Example:
  *     jmap_free(map);
  */
-#define jmap_free(map) jmap_free_(&(map)->raw)
+#define jmap_free(map) jmap_free_(jmap_raw_(map))
 
 /**
  * jmap_error - test for an error in the a previous jmap_ operation.
 
 /**
  * jmap_error - test for an error in the a previous jmap_ operation.
@@ -88,7 +93,7 @@ struct jmap {
  *     if (errstr)
  *             errx(1, "Woah, error on newly created map?! %s", errstr);
  */
  *     if (errstr)
  *             errx(1, "Woah, error on newly created map?! %s", errstr);
  */
-#define jmap_error(map) jmap_error_(&(map)->raw)
+#define jmap_error(map) jmap_error_(jmap_raw_(map))
 
 /**
  * jmap_rawi - unwrap the typed map and check the index type
 
 /**
  * jmap_rawi - unwrap the typed map and check the index type
@@ -99,7 +104,8 @@ struct jmap {
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
-#define jmap_rawi(map, expr) (&tcon_check((map), icanary, (expr))->raw)
+#define jmap_rawi(map, expr) \
+       tcon_unwrap(tcon_check(&(map)->jmap_, icanary, (expr)))
 
 /**
  * jmap_rawc - unwrap the typed map and check the contents type
 
 /**
  * jmap_rawc - unwrap the typed map and check the contents type
@@ -110,7 +116,8 @@ struct jmap {
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
-#define jmap_rawc(map, expr) (&tcon_check((map), ccanary, (expr))->raw)
+#define jmap_rawc(map, expr) \
+       tcon_unwrap(tcon_check(&(map)->jmap_, ccanary, (expr)))
 
 /**
  * jmap_rawci - unwrap the typed map and check the index and contents types
 
 /**
  * jmap_rawci - unwrap the typed map and check the index and contents types
@@ -122,8 +129,9 @@ struct jmap {
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
  * variable is of an unexpected type.  It is used internally where we
  * need to access the raw underlying jmap.
  */
-#define jmap_rawci(map, iexpr, cexpr) \
-       (&tcon_check(tcon_check((map), ccanary, (cexpr)), icanary, (iexpr))->raw)
+#define jmap_rawci(map, iexpr, cexpr)                          \
+       tcon_unwrap(tcon_check(tcon_check(&(map)->jmap_,\
+                                         ccanary, (cexpr)), icanary, (iexpr)))
 
 /**
  * jmap_add - add or replace a value for a given index in the map.
 
 /**
  * jmap_add - add or replace a value for a given index in the map.
@@ -199,7 +207,7 @@ struct jmap {
  *     jmap_getval()
  */
 #define jmap_get(map, index)                                           \
  *     jmap_getval()
  */
 #define jmap_get(map, index)                                           \
-       tcon_cast((map), ccanary,                                       \
+       tcon_cast(&(map)->jmap_, ccanary,                               \
                  jmap_get_(jmap_rawi((map), (index)), (unsigned long)(index)))
 
 /**
                  jmap_get_(jmap_rawi((map), (index)), (unsigned long)(index)))
 
 /**
@@ -210,7 +218,7 @@ struct jmap {
  *     assert(jmap_count(map) < 1000);
  */
 #define jmap_count(map)                                \
  *     assert(jmap_count(map) < 1000);
  */
 #define jmap_count(map)                                \
-       jmap_popcount_(&(map)->raw, 0, -1UL)
+       jmap_popcount_(jmap_raw_(map), 0, -1UL)
 
 /**
  * jmap_popcount - get population of (some part of) the map.
 
 /**
  * jmap_popcount - get population of (some part of) the map.
@@ -249,8 +257,8 @@ struct jmap {
  *     jmap_nthval();
  */
 #define jmap_nth(map, n, invalid)                                      \
  *     jmap_nthval();
  */
 #define jmap_nth(map, n, invalid)                                      \
-       tcon_cast((map), icanary,                                       \
-                 jmap_nth_(jmap_rawi((map), (invalid)),                        \
+       tcon_cast(&(map)->jmap_, icanary,                               \
+                 jmap_nth_(jmap_rawi((map), (invalid)),                \
                            (n), (unsigned long)(invalid)))
 
 /**
                            (n), (unsigned long)(invalid)))
 
 /**
@@ -270,7 +278,7 @@ struct jmap {
  *     jmap_firstval()
  */
 #define jmap_first(map)                                                \
  *     jmap_firstval()
  */
 #define jmap_first(map)                                                \
-       tcon_cast((map), icanary, jmap_first_(&(map)->raw))
+       tcon_cast(&(map)->jmap_, icanary, jmap_first_(jmap_raw_(map)))
 
 /**
  * jmap_next - return the next index in the map.
 
 /**
  * jmap_next - return the next index in the map.
@@ -282,8 +290,8 @@ struct jmap {
  *     jmap_nextval()
  */
 #define jmap_next(map, prev)                                           \
  *     jmap_nextval()
  */
 #define jmap_next(map, prev)                                           \
-       tcon_cast((map), icanary, jmap_next_(jmap_rawi((map), (prev)),  \
-                                            (unsigned long)(prev)))
+       tcon_cast(&(map)->jmap_, icanary, jmap_next_(jmap_rawi((map), (prev)),  \
+                                                    (unsigned long)(prev)))
 
 /**
  * jmap_last - return the last index in the map.
 
 /**
  * jmap_last - return the last index in the map.
@@ -301,7 +309,7 @@ struct jmap {
  *     jmap_lastval()
  */
 #define jmap_last(map)                                         \
  *     jmap_lastval()
  */
 #define jmap_last(map)                                         \
-       tcon_cast((map), icanary, jmap_last_(&(map)->raw))
+       tcon_cast(&(map)->jmap_, icanary, jmap_last_(jmap_raw_(map)))
 
 /**
  * jmap_prev - return the previous index in the map (must not contain 0)
 
 /**
  * jmap_prev - return the previous index in the map (must not contain 0)
@@ -315,7 +323,8 @@ struct jmap {
  *     jmap_prevval()
  */
 #define jmap_prev(map, prev)                                           \
  *     jmap_prevval()
  */
 #define jmap_prev(map, prev)                                           \
-       tcon_cast((map), icanary, jmap_prev_(jmap_rawi((map), (prev)), (prev)))
+       tcon_cast(&(map)->jmap_, icanary,                               \
+                 jmap_prev_(jmap_rawi((map), (prev)), (prev)))
 
 /**
  * jmap_getval - access a value in-place for a given index.
 
 /**
  * jmap_getval - access a value in-place for a given index.
@@ -346,7 +355,7 @@ struct jmap {
  *     jmap_putval(), jmap_firstval()
  */
 #define jmap_getval(map, index)                                                \
  *     jmap_putval(), jmap_firstval()
  */
 #define jmap_getval(map, index)                                                \
-       tcon_cast_ptr((map), ccanary,                                   \
+       tcon_cast_ptr(&(map)->jmap_, ccanary,                           \
                      jmap_getval_(jmap_rawi((map), (index)),           \
                                   (unsigned long)(index)))
 
                      jmap_getval_(jmap_rawi((map), (index)),           \
                                   (unsigned long)(index)))
 
@@ -393,7 +402,7 @@ struct jmap {
  *     jmap_nth();
  */
 #define jmap_nthval(map, n, index)                                     \
  *     jmap_nth();
  */
 #define jmap_nthval(map, n, index)                                     \
-       tcon_cast_ptr((map), ccanary,                                   \
+       tcon_cast_ptr(&(map)->jmap_, ccanary,                           \
                      jmap_nthval_(jmap_rawi((map), *(index)), (n), (index)))
 
 /**
                      jmap_nthval_(jmap_rawi((map), *(index)), (n), (index)))
 
 /**
@@ -415,8 +424,8 @@ struct jmap {
  * See Also:
  *     jmap_first, jmap_nextval()
  */
  * See Also:
  *     jmap_first, jmap_nextval()
  */
-#define jmap_firstval(map, index)            \
-       tcon_cast_ptr((map), ccanary,                           \
+#define jmap_firstval(map, index)                                      \
+       tcon_cast_ptr(&(map)->jmap_, ccanary,                           \
                      jmap_firstval_(jmap_rawi((map), *(index)),        \
                                     (unsigned long *)(index)))
 
                      jmap_firstval_(jmap_rawi((map), *(index)),        \
                                     (unsigned long *)(index)))
 
@@ -432,7 +441,7 @@ struct jmap {
  *     jmap_firstval(), jmap_putval()
  */
 #define jmap_nextval(map, index)                               \
  *     jmap_firstval(), jmap_putval()
  */
 #define jmap_nextval(map, index)                               \
-       tcon_cast_ptr((map), ccanary,                           \
+       tcon_cast_ptr(&(map)->jmap_, ccanary,                   \
                      jmap_nextval_(jmap_rawi((map), *(index)), \
                                    (unsigned long *)(index)))
 
                      jmap_nextval_(jmap_rawi((map), *(index)), \
                                    (unsigned long *)(index)))
 
@@ -445,8 +454,8 @@ struct jmap {
  * See Also:
  *     jmap_last(), jmap_putval()
  */
  * See Also:
  *     jmap_last(), jmap_putval()
  */
-#define jmap_lastval(map, index)             \
-       tcon_cast_ptr((map), ccanary,                           \
+#define jmap_lastval(map, index)                               \
+       tcon_cast_ptr(&(map)->jmap_, ccanary,                   \
                      jmap_lastval_(jmap_rawi((map), *(index)), \
                                    (unsigned long *)(index)))
 
                      jmap_lastval_(jmap_rawi((map), *(index)), \
                                    (unsigned long *)(index)))
 
@@ -463,7 +472,7 @@ struct jmap {
  *     jmap_lastval(), jmap_putval()
  */
 #define jmap_prevval(map, index)                               \
  *     jmap_lastval(), jmap_putval()
  */
 #define jmap_prevval(map, index)                               \
-       tcon_cast_ptr((map), ccanary,                           \
+       tcon_cast_ptr(&(map)->jmap_, ccanary,                   \
                      jmap_prevval_(jmap_rawi((map), *(index)), \
                                    (unsigned long *)(index)))
 
                      jmap_prevval_(jmap_rawi((map), *(index)), \
                                    (unsigned long *)(index)))
 
index 3a4249dc4ae5372327f294b11e916a19391b5f82..490b706fa36845e391a438083201d295f33fcee2 100644 (file)
@@ -38,7 +38,7 @@ int main(int argc, char *argv[])
                jmap_add(map, (i << 4) + 1, (i << 5) + 1);
 
        /* This only take 6.3MB on my 32-bit system. */
                jmap_add(map, (i << 4) + 1, (i << 5) + 1);
 
        /* This only take 6.3MB on my 32-bit system. */
-       diag("%u bytes memory used\n", (unsigned)JudyLMemUsed(map->raw.judy));
+       diag("%u bytes memory used\n", (unsigned)JudyLMemUsed(jmap_raw_(map)));
 
        ok1(jmap_get(map, 1) == 1);
        ok1(jmap_get(map, (999999 << 4) + 1) == (999999 << 5) + 1);
 
        ok1(jmap_get(map, 1) == 1);
        ok1(jmap_get(map, (999999 << 4) + 1) == (999999 << 5) + 1);
@@ -102,13 +102,13 @@ int main(int argc, char *argv[])
        jmap_putval(map, &value);
 
        /* Test error handling */
        jmap_putval(map, &value);
 
        /* Test error handling */
-       JU_ERRNO(&map->raw.err) = 100;
-       JU_ERRID(&map->raw.err) = 991;
+       JU_ERRNO(&jmap_raw_(map)->err) = 100;
+       JU_ERRID(&jmap_raw_(map)->err) = 991;
        err = jmap_error(map);
        ok1(err);
        ok1(strstr(err, "100"));
        ok1(strstr(err, "991"));
        err = jmap_error(map);
        ok1(err);
        ok1(strstr(err, "100"));
        ok1(strstr(err, "991"));
-       ok1(err == map->raw.errstr);
+       ok1(err == jmap_raw_(map)->errstr);
        jmap_free(map);
 
        return exit_status();
        jmap_free(map);
 
        return exit_status();