lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Coroutines once stated:
> On Sat, Apr 19, 2014 at 3:10 AM, Sean Conner <sean@conman.org> wrote:
> 
> >   Hmm ... I'm rereading the man page, and I may not be doing it correctly.
> > I may be able to use luaL_Buffer and a fixed-length output buffer, but I'm
> > still not going to be hostage to Microsoft (so I'll still use C99 features).
> 
> I'm kind of curious to know what you require from C99. :>

  In general or for this module in particular?

  In general, I tend to use inline (imagine that!  the equivalent of
typesafe macros but portable!), restrict (still working out if that's a win
or not---at the very least it documents intent), and declaring local
variables in for loops, like:

		for (int i = 0 ; i < SOME_MAX; i++)
		{
			/* ... */
		}

  I still prefer to declare my variables at the top of a block but there are
the rare exceptions (usually if I have to declare a variable length array
based on some calculation).  The named field initialization feature of C99
is also nice:

	struct itimerval x = 
	{
	  .it_value = 
	  {
	    .tv_sec = 5,
	    .tv_usec = 500000
	  },

	  .it_interval = 
	  {
	    .tv_sec = 3,
	    .tv_usec = 250000
	  }
	};

which leads to the other feature I have used (although I can't think of the
name for it):

	setitimer(
		TIMER_REAL,
		& (struct itimerval) {
			.it_interval = { .tv_sec = alarm , .tv_usec = 0 }
			.it_value    = { .tv_sec = alarm , .tv_usec = 0 },

		},
		NULL
	);

It's also useful for something like setsockopt():

	if (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&(int){1},sizeof(int)) < 0)
	  error();

otherwise, I would have to declare an int just so I could pass in a constant
value to setsockopt.  This saves the cognitive load of coming up with some
name for the variable when all I really want to pass in is a constant 1.  

  I also love <stdint.h> and <stdbool.h>.  The former replaces a large block
of preprocessor code I used to use to get known sized integers, and the
later gives me booleans, true and false (okay, so booleans are a bit larger
than 1 bit, but hey, I can live with that).

  There's not much else I use from C99, but what I do use, I like.

  As for the program in question---I could make it use C89, but I'm being a
bastard on this because of Microsoft's stance.

> The main benefit of using variable-length arrays is allocating on the
> stack, which can also be done with alloca() -- both avenues not
> possible with C89.  

  alloca() is not standard.  It's a compiler hack that is found on several
copilers (you can't write the function at all---it *has* to be a compiler
hack).

> Going the route of luaL_Buffer would still
> allocate on the heap (internally using Lua's set allocator function --
> which looks like realloc).  I'm continually mad at Microsoft for not
> better supporting C99 but in the grand scheme of things I'd rather
> skip C99 entirely.  It doesn't add enough to be worth jumping ship.  

  Have you really looked at it?

> ((Yes I do curse the people who only have C89 runtimes at their
> disposal, but nobody completely supports C11 yet... stupid glibc))

  It's been fifteen years since C99 and how many people use it?  At this
stage in the game, I'd be surprised if anyone picks up C11 ...

  -spc