William said:
Hi,
This's a way of defining size_t and ssize_t in Linux:
//"linux/types.h"
typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;
//"asm/posix_types.h"
typedef unsigned int __kernel_size_t;
typedef int __kernel_ssize_t;
It seems so tricky to me. What benefits do we get from such a tricky
typedef ? better name (size_t is shorter than unsigned int) ?
On ssize_t I have no comment, since the C language's definition
does not incorporate such a type.
The size_t type is defined by ISO for use by functions such as
malloc and strlen (among others, of course), for representing
size information. It is required to be an unsigned integral
type, but need not be unsigned int. It could equally be
unsigned long or unsigned long long on some systems. It could
even (perhaps a touch pathologically) be unsigned char!
Consider the following code:
size_t len = strlen(s);
If we didn't have size_t, and wanted our code to run on the Zag,
Zeg, Zig, Zog, and Zug compilers, all of which have different
types to represent the size returned by strlen, we'd have to do
something like:
#if _ZAG
unsigned char len = strlen(s);
#elif _ZEG
unsigned short len = strlen(s);
#elif _ZIG
unsigned int len = strlen(s);
#elif _ZOG
unsigned long len = strlen(s);
#elif _ZUG
unsigned long long len = strlen(s);
#else
#error Unsupported platform.
#endif
The obvious step would be to add a typedef to handle this:
#if _ZAG
typedef unsigned char size_type;
#elif _ZEG
typedef unsigned short size_type;
#elif _ZIG
typedef unsigned int size_type;
#elif _ZOG
typedef unsigned long size_type;
#elif _ZUG
typedef unsigned long long size_type;
#else
#error Unsupported platform.
#endif
size_type len = strlen(s);
This is still a nuisance, although not such a bad nuisance, because
we can hide the cpp nonsense in a header.
If only we could persuade each implementor to include a standard
definition for strlen's return type, and put it in, say, stddef.h.
Then we wouldn't have to put all this nonsense in our own header,
and our code would be simpler as a result (and easier to port).
We're in luck. Fortunately, the C Standard actually /requires/
conforming implementations to do this; the name they settled on
was size_t.
I hope that answers your question.