4 ms·
After reading the source, I realized that I have no idea what modern, idiomatic C looks like. There are tons of uses of #define, some even in the middle of type
by jzelinskie 11y ago
After reading the source, I realized that I have no idea what modern, idiomatic C looks like. There are tons of uses of #define, some even in the middle of typedefs. I'm pretty sure a whole queue data structure is defined in macros in vqueue.h. Is this normal?
- vbezhenar 11y agoC does not have generics or templates so macroses are the only way to define data structures and algorithms over arbitrary types without repetition. Linux kernel uses macroses to define some data structures. Many other projects do the same. Whether that is normal or not, it's up to developer. It's possible to use as small subset of C++ as developer wants. E.g. use only templates. But staying in C realm might be better for portability.
- JoshTriplett 11y ago> C does not have generics or templates so macroses When I read this, I couldn't help but think of "macroses" as pronounced like "neuroses", which seems appropriate.
- lordlarm 11y agoFrom vqueue.h header: * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. It's very much based on this: http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/queue.h?annotate=1.68 http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/queue.h?anno... Not very modern, in other words.
- talideon 11y agoPretty normal for the BSD kernels. A lot of the very basic datastructures are defined as macros. For instance, here's queue.h: https://svnweb.freebsd.org/base/head/sys/sys/queue.h?revision=279242&view=markup https://svnweb.freebsd.org/base/head/sys/sys/queue.h?revisio...
- pyritschard 11y agoWhile not 100% necessary, header macros to implement lists, queues (and even trees, see: http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/sys/tree.h?rev=1.14&content-type=text/x-cvsweb-markup http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/sys/tree.h?...) are common in C. They first appear for kernel usage where the fact that they expanded to inline code in functions avoided creating too many stack-frames and provided optimization. They do have the advantage of not relying on casting everything to "void *" or resort to callbacks for walking (see: TAILQ_FOREACH for instance).