summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-05-24 16:28:33 +0000
committersmatz <smatz@openttd.org>2009-05-24 16:28:33 +0000
commitb7d065ce99886e2cf86883469a077f653428e5b8 (patch)
treea762bfd595e7d4830500efdefb0285ddc14b0d2b
parent8109a74c95c0b6882c681ee012c57bf503c7b602 (diff)
downloadopenttd-b7d065ce99886e2cf86883469a077f653428e5b8.tar.xz
(svn r16415) -Fix (r13008): some 64bit architectures require size_t to be aligned at 8-byte boundary, ensure it for MemBlock
-rw-r--r--src/spritecache.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/spritecache.cpp b/src/spritecache.cpp
index 990f40726..1470522d2 100644
--- a/src/spritecache.cpp
+++ b/src/spritecache.cpp
@@ -306,8 +306,18 @@ void DupSprite(SpriteID old_spr, SpriteID new_spr)
scnew->warned = false;
}
+/**
+ * S_FREE_MASK is used to mask-out lower bits of MemBlock::size
+ * If they are non-zero, the block is free.
+ * S_FREE_MASK has to ensure MemBlock is correctly aligned -
+ * it means 8B (S_FREE_MASK == 7) on 64bit systems!
+ */
+static const size_t S_FREE_MASK = sizeof(size_t) - 1;
-#define S_FREE_MASK 1
+/* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
+assert_compile(sizeof(MemBlock) == sizeof(size_t));
+/* make sure it's a power of two */
+assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
static inline MemBlock *NextBlock(MemBlock *block)
{
@@ -439,9 +449,9 @@ void *AllocSprite(size_t mem_req)
{
mem_req += sizeof(MemBlock);
- /* Align this to an uint32 boundary. This also makes sure that the 2 least
- * bits are not used, so we could use those for other things. */
- mem_req = Align(mem_req, sizeof(uint32));
+ /* Align this to correct boundary. This also makes sure at least one
+ * bit is not used, so we can use it for other things. */
+ mem_req = Align(mem_req, S_FREE_MASK + 1);
for (;;) {
MemBlock *s;