bug-gnulib
[Top][All Lists]
Advanced

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

Re: stack module


From: Bruno Haible
Subject: Re: stack module
Date: Sat, 23 May 2020 14:30:57 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-177-generic; KDE/5.18.0; x86_64; ; )

Hi Marc,

> Alternatively, one could implement a universally usable stack through
> the following header file (mimicking somewhat C++ templates). What do
> you think? It will be a lot faster than using the general list modules
> of Gnulib.

I agree that a generic 'stack' module is useful. I also agree that a single
implementation, based on an array, is the way to go. Then it is already
faster than the generic list module.

In Gnulib, we usually avoid extensive use of multi-line macros, because
it hampers debuggability. Here, however, one needs macros, in order to
accommodate the TYPE argument, which is not necessarily compatible to 'void *'.
Nevertheless, we would try to put as much code as possible into functions.
The STACK_INIT macro, for example, could be implemented as a function.

> #define STACK_CLEAR(stack)            \
>   free ((stack).base)

Shouldn't this one also set .size and .allocated to 0 ?

> #define STACK_POP(stack)            \
>   (stack).base [--(stack).size]
> 
> #define STACK_DISCARD(stack)            \
>   (--(stack).size)
> 
> #define STACK_TOP(stack)            \
>   (stack).base[(stack).size - 1]

In these three macros, I would consider to abort() when (stack).size == 0.

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]