diff options
author | Nick Mathewson <nickm@torproject.org> | 2011-01-06 15:59:05 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2011-01-06 15:59:05 -0500 |
commit | d4165ef8b4c2ea697a0b73d80efc6575c0f2279a (patch) | |
tree | 62bd907a4f382ceb3111273ac0dc187088a63ae5 /src/common/mempool.c | |
parent | fd8f7991e47d02cc345af6f190a7b480703822df (diff) | |
download | tor-d4165ef8b4c2ea697a0b73d80efc6575c0f2279a.tar.gz tor-d4165ef8b4c2ea697a0b73d80efc6575c0f2279a.zip |
Use autoconf's FLEXIBLE_ARRAY_MEMBER for unspecified-length arrays
C99 allows a syntax for structures whose last element is of
unspecified length:
struct s {
int elt1;
...
char last_element[];
};
Recent (last-5-years) autoconf versions provide an
AC_C_FLEXIBLE_ARRAY_MEMBER test that defines FLEXIBLE_ARRAY_MEMBER
to either no tokens (if you have c99 flexible array support) or to 1
(if you don't). At that point you just use offsetof
[STRUCT_OFFSET() for us] to see where last_element begins, and
allocate your structures like:
struct s {
int elt1;
...
char last_element[FLEXIBLE_ARRAY_MEMBER];
};
tor_malloc(STRUCT_OFFSET(struct s, last_element) +
n_elements*sizeof(char));
The advantages are:
1) It's easier to see which structures and elements are of
unspecified length.
2) The compiler and related checking tools can also see which
structures and elements are of unspecified length, in case they
wants to try weird bounds-checking tricks or something.
3) The compiler can warn us if we do something dumb, like try
to stack-allocate a flexible-length structure.
Diffstat (limited to 'src/common/mempool.c')
-rw-r--r-- | src/common/mempool.c | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/src/common/mempool.c b/src/common/mempool.c index c444923189..520e470c7f 100644 --- a/src/common/mempool.c +++ b/src/common/mempool.c @@ -137,7 +137,8 @@ struct mp_chunk_t { int capacity; /**< Number of items that can be fit into this chunk. */ size_t mem_size; /**< Number of usable bytes in mem. */ char *next_mem; /**< Pointer into part of <b>mem</b> not yet carved up. */ - char mem[1]; /**< Storage for this chunk. (Not actual size.) */ + /** Storage for this chunk */ + char mem[FLEXIBLE_ARRAY_MEMBER]; }; /** Number of extra bytes needed beyond mem_size to allocate a chunk. */ |