summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-08-28 09:22:46 +0200
committerYorhel <git@yorhel.nl>2012-08-28 09:22:46 +0200
commit2e1838d8904d91faf45700b06d6d14fd831b3a08 (patch)
treef0c781446dc945ea779b75d6950c7e79e2ab02c5 /src
parentdfbeed9daf6cf04fccfdfcb34693a3a8739b6fa4 (diff)
Get rid of padding in dir struct + use offsetof() to calc needed size
There used to be four bytes of padding in the struct on systems with 32bit pointers. Moving the pointers down so that they are in between the 64bit and 32bit fields means that there'll never be any padding. There may, however, be some extra padding at the end of the struct to make the size a multiple of 8. Since we use the name field as a sort of "flexible array member", we don't particularly care about that padding and just want to allocate enough memory to hold the struct and the name field. offsetof() allows us to do that without relying on compiler support for flexible array members.
Diffstat (limited to 'src')
-rw-r--r--src/global.h7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/global.h b/src/global.h
index ace92cb..7249bcd 100644
--- a/src/global.h
+++ b/src/global.h
@@ -28,6 +28,7 @@
#include "config.h"
#include <stdio.h>
+#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -57,15 +58,15 @@
/* structure representing a file or directory */
struct dir {
- struct dir *parent, *next, *prev, *sub, *hlnk;
int64_t size, asize;
uint64_t ino, dev;
+ struct dir *parent, *next, *prev, *sub, *hlnk;
int items;
unsigned char flags;
char name[3]; /* must be large enough to hold ".." */
};
-/* sizeof(total dir) = SDIRSIZE + strlen(name) = sizeof(struct dir) - 3 + strlen(name) + 1 */
-#define SDIRSIZE (sizeof(struct dir)-2)
+/* sizeof(total dir) = SDIRSIZE + strlen(name) = offsetof(struct dir, name) + strlen(name) + 1 */
+#define SDIRSIZE (offsetof(struct dir, name)+1)
/* A note on the ino and dev fields above: ino is usually represented as ino_t,
* which POSIX specifies to be an unsigned integer. dev is usually represented