Commit 2760a60f authored by David Gibson's avatar David Gibson

jmap: Use TCON_WRAP instead of TCON

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: default avatarDavid Gibson <david@gibson.dropbear.id.au>
parent f44e77a4
...@@ -41,8 +41,7 @@ struct jmap { ...@@ -41,8 +41,7 @@ struct jmap {
* }; * };
*/ */
#define JMAP_MEMBERS(itype, ctype) \ #define JMAP_MEMBERS(itype, ctype) \
struct jmap raw; \ TCON_WRAP(struct jmap, itype icanary; ctype ccanary) jmap_
TCON(itype icanary; ctype ccanary)
/** /**
* jmap_new - create a new, empty jmap. * jmap_new - create a new, empty jmap.
...@@ -61,6 +60,12 @@ struct 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. * jmap_free - destroy a jmap.
* @map: the map returned from jmap_new. * @map: the map returned from jmap_new.
...@@ -68,7 +73,7 @@ struct jmap { ...@@ -68,7 +73,7 @@ struct jmap {
* Example: * Example:
* jmap_free(map); * 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 { ...@@ -88,7 +93,7 @@ struct jmap {
* if (errstr) * if (errstr)
* errx(1, "Woah, error on newly created map?! %s", 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 { ...@@ -99,7 +104,8 @@ struct jmap {
* variable is of an unexpected type. It is used internally where we * variable is of an unexpected type. It is used internally where we
* need to access the raw underlying jmap. * 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 { ...@@ -110,7 +116,8 @@ struct jmap {
* variable is of an unexpected type. It is used internally where we * variable is of an unexpected type. It is used internally where we
* need to access the raw underlying jmap. * 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
...@@ -123,7 +130,8 @@ struct jmap { ...@@ -123,7 +130,8 @@ struct jmap {
* need to access the raw underlying jmap. * need to access the raw underlying jmap.
*/ */
#define jmap_rawci(map, iexpr, cexpr) \ #define jmap_rawci(map, iexpr, cexpr) \
(&tcon_check(tcon_check((map), ccanary, (cexpr)), icanary, (iexpr))->raw) 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 { ...@@ -199,7 +207,7 @@ struct jmap {
* jmap_getval() * jmap_getval()
*/ */
#define jmap_get(map, index) \ #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 { ...@@ -210,7 +218,7 @@ struct jmap {
* assert(jmap_count(map) < 1000); * assert(jmap_count(map) < 1000);
*/ */
#define jmap_count(map) \ #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,7 +257,7 @@ struct jmap { ...@@ -249,7 +257,7 @@ struct jmap {
* jmap_nthval(); * jmap_nthval();
*/ */
#define jmap_nth(map, n, invalid) \ #define jmap_nth(map, n, invalid) \
tcon_cast((map), icanary, \ tcon_cast(&(map)->jmap_, icanary, \
jmap_nth_(jmap_rawi((map), (invalid)), \ jmap_nth_(jmap_rawi((map), (invalid)), \
(n), (unsigned long)(invalid))) (n), (unsigned long)(invalid)))
...@@ -270,7 +278,7 @@ struct jmap { ...@@ -270,7 +278,7 @@ struct jmap {
* jmap_firstval() * jmap_firstval()
*/ */
#define jmap_first(map) \ #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,7 +290,7 @@ struct jmap { ...@@ -282,7 +290,7 @@ struct jmap {
* jmap_nextval() * jmap_nextval()
*/ */
#define jmap_next(map, prev) \ #define jmap_next(map, prev) \
tcon_cast((map), icanary, jmap_next_(jmap_rawi((map), (prev)), \ tcon_cast(&(map)->jmap_, icanary, jmap_next_(jmap_rawi((map), (prev)), \
(unsigned long)(prev))) (unsigned long)(prev)))
/** /**
...@@ -301,7 +309,7 @@ struct jmap { ...@@ -301,7 +309,7 @@ struct jmap {
* jmap_lastval() * jmap_lastval()
*/ */
#define jmap_last(map) \ #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 { ...@@ -315,7 +323,8 @@ struct jmap {
* jmap_prevval() * jmap_prevval()
*/ */
#define jmap_prev(map, prev) \ #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 { ...@@ -346,7 +355,7 @@ struct jmap {
* jmap_putval(), jmap_firstval() * jmap_putval(), jmap_firstval()
*/ */
#define jmap_getval(map, index) \ #define jmap_getval(map, index) \
tcon_cast_ptr((map), ccanary, \ tcon_cast_ptr(&(map)->jmap_, ccanary, \
jmap_getval_(jmap_rawi((map), (index)), \ jmap_getval_(jmap_rawi((map), (index)), \
(unsigned long)(index))) (unsigned long)(index)))
...@@ -393,7 +402,7 @@ struct jmap { ...@@ -393,7 +402,7 @@ struct jmap {
* jmap_nth(); * jmap_nth();
*/ */
#define jmap_nthval(map, n, index) \ #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)))
/** /**
...@@ -416,7 +425,7 @@ struct jmap { ...@@ -416,7 +425,7 @@ struct jmap {
* jmap_first, jmap_nextval() * jmap_first, jmap_nextval()
*/ */
#define jmap_firstval(map, index) \ #define jmap_firstval(map, index) \
tcon_cast_ptr((map), ccanary, \ tcon_cast_ptr(&(map)->jmap_, ccanary, \
jmap_firstval_(jmap_rawi((map), *(index)), \ jmap_firstval_(jmap_rawi((map), *(index)), \
(unsigned long *)(index))) (unsigned long *)(index)))
...@@ -432,7 +441,7 @@ struct jmap { ...@@ -432,7 +441,7 @@ struct jmap {
* jmap_firstval(), jmap_putval() * jmap_firstval(), jmap_putval()
*/ */
#define jmap_nextval(map, index) \ #define jmap_nextval(map, index) \
tcon_cast_ptr((map), ccanary, \ tcon_cast_ptr(&(map)->jmap_, ccanary, \
jmap_nextval_(jmap_rawi((map), *(index)), \ jmap_nextval_(jmap_rawi((map), *(index)), \
(unsigned long *)(index))) (unsigned long *)(index)))
...@@ -446,7 +455,7 @@ struct jmap { ...@@ -446,7 +455,7 @@ struct jmap {
* jmap_last(), jmap_putval() * jmap_last(), jmap_putval()
*/ */
#define jmap_lastval(map, index) \ #define jmap_lastval(map, index) \
tcon_cast_ptr((map), ccanary, \ tcon_cast_ptr(&(map)->jmap_, ccanary, \
jmap_lastval_(jmap_rawi((map), *(index)), \ jmap_lastval_(jmap_rawi((map), *(index)), \
(unsigned long *)(index))) (unsigned long *)(index)))
...@@ -463,7 +472,7 @@ struct jmap { ...@@ -463,7 +472,7 @@ struct jmap {
* jmap_lastval(), jmap_putval() * jmap_lastval(), jmap_putval()
*/ */
#define jmap_prevval(map, index) \ #define jmap_prevval(map, index) \
tcon_cast_ptr((map), ccanary, \ tcon_cast_ptr(&(map)->jmap_, ccanary, \
jmap_prevval_(jmap_rawi((map), *(index)), \ jmap_prevval_(jmap_rawi((map), *(index)), \
(unsigned long *)(index))) (unsigned long *)(index)))
......
...@@ -38,7 +38,7 @@ int main(int argc, char *argv[]) ...@@ -38,7 +38,7 @@ int main(int argc, char *argv[])
jmap_add(map, (i << 4) + 1, (i << 5) + 1); jmap_add(map, (i << 4) + 1, (i << 5) + 1);
/* This only take 6.3MB on my 32-bit system. */ /* 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, 1) == 1);
ok1(jmap_get(map, (999999 << 4) + 1) == (999999 << 5) + 1); ok1(jmap_get(map, (999999 << 4) + 1) == (999999 << 5) + 1);
...@@ -102,13 +102,13 @@ int main(int argc, char *argv[]) ...@@ -102,13 +102,13 @@ int main(int argc, char *argv[])
jmap_putval(map, &value); jmap_putval(map, &value);
/* Test error handling */ /* Test error handling */
JU_ERRNO(&map->raw.err) = 100; JU_ERRNO(&jmap_raw_(map)->err) = 100;
JU_ERRID(&map->raw.err) = 991; JU_ERRID(&jmap_raw_(map)->err) = 991;
err = jmap_error(map); err = jmap_error(map);
ok1(err); ok1(err);
ok1(strstr(err, "100")); ok1(strstr(err, "100"));
ok1(strstr(err, "991")); ok1(strstr(err, "991"));
ok1(err == map->raw.errstr); ok1(err == jmap_raw_(map)->errstr);
jmap_free(map); jmap_free(map);
return exit_status(); return exit_status();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment