std C/C++ patch #4
David Scott Page
page at cs.utexas.edu
Thu Apr 25 17:21:36 EDT 2002
Hi,
This patch makes two changes. The first assumes the "pad" comment
implies 8-byte alignment (strictest host alignment?) is desired for
the data area (_x[]), and attempts to achieve this via the usual union
with an appropriate type. I believe double will force _x to an 8-byte
alignment anywhere it matters (until long double comes into vogue).
The second change is from _x[0], which I believe is forbidden in any
std C/C++ prior to C99 (and should be expressed _x[] in C99), to
_x[1], and replacing the sizeof with the appropriate offsetof.
--
Scott Page
--------------------------------- 8< ---------------------------------
diff -Naur click-stdC++.3a/include/click/bighashmap_arena.hh click-stdC++.4/include/click/bighashmap_arena.hh
--- click-stdC++.3a/include/click/bighashmap_arena.hh Thu Apr 25 20:59:13 2002
+++ click-stdC++.4/include/click/bighashmap_arena.hh Thu Apr 25 20:57:17 2002
@@ -3,14 +3,16 @@
struct BigHashMap_Arena {
- enum { SIZE = 128 };
- int _first;
- int _padding; // pad to 8-byte boundary
- unsigned char _x[0];
+ enum { SIZE = 128 } ;
+ union {
+ int _first;
+ double _padding; // align _x to 8-byte boundary
+ } _un ;
+ unsigned char _x[1];
static BigHashMap_Arena *new_arena(unsigned esize);
static void delete_arena(BigHashMap_Arena *);
- void clear() { _first = 0; }
+ void clear() { _un._first = 0; }
void *alloc(unsigned esize);
void *elt(unsigned esize, int i) { return (_x + esize*i); }
@@ -19,8 +21,8 @@
inline void *
BigHashMap_Arena::alloc(unsigned esize)
{
- if (_first < SIZE)
- return elt(esize, _first++);
+ if (_un._first < SIZE)
+ return elt(esize, _un._first++);
else
return 0;
}
diff -Naur click-stdC++.3a/lib/bighashmap_arena.cc click-stdC++.4/lib/bighashmap_arena.cc
--- click-stdC++.3a/lib/bighashmap_arena.cc Fri May 25 00:04:04 2001
+++ click-stdC++.4/lib/bighashmap_arena.cc Thu Apr 25 20:57:17 2002
@@ -23,9 +23,9 @@
BigHashMap_Arena::new_arena(unsigned esize)
{
BigHashMap_Arena *arena =
- reinterpret_cast<BigHashMap_Arena *>(new unsigned char[sizeof(BigHashMap_Arena) + esize*SIZE]);
+ reinterpret_cast<BigHashMap_Arena *>(new unsigned char[offsetof(BigHashMap_Arena,_x) + esize*SIZE]);
if (arena)
- arena->_first = 0;
+ arena->_un._first = 0;
return arena;
}
More information about the click
mailing list