I've just read about mode_t that it basically stores the following information:
- 7 boolean values for the file type (S_IFREG, S_IFDIR, S_IFCHR, S_ISBLK, S_ISFIFO, S_ISLINK, S_ISSOCK)
- 3*3 = 9 boolean values for the access permissons (read, write and execute for owner, group and others)
So it needs 16 bit = 2 bytes. I guess you could even have one bit less for the file type, as it has to be either a regular file, a directory, a character or block device, a socket, a symbolic link, or a pipe. Or do other file types exist?
So I've just checked the size of mode_t with
printf("Size: %d byte\n", sizeof(mode_t));
It uses 4 byte. Why does it use 4 byte? Is there any additional information I didn't notice?
edit: I've just found that mode_t is defined in ptypes.inc:
type mode_t = cuint32;
cuint32 is a 32 bits sized, unsigned integer and defined in ctypes.inc:
type cuint32 = LongWord;
Perhaps this helps for the answer.
Let's look at what a "dumb" compiler would do when given the following code:
This seems like a likely use case for values of type
mode_t
, checking if a flag is set. Now we compile it withgcc -O0
and check the generated assembly:See how the special
movzwl
instruction is needed to load the 16-bit value? This is because it needs to be sign-extended to two additional bytes to fit in the register. Obviously this instruction is more complex than a simplemov
. This might have a tiny impact on performance, and it might increase the executable size by some bytes, which by itself wouldn't be too bad.However, if we consider that there would be no advantage in using a 16-bit value, because it would usually take up 32 bits of storage anyway due to alignment, it's clear why the designers choose to use the native word size of the CPU here.