helpgperf - Support: sr #108399, gperf should also generate a macro...


sr #108399: gperf should also generate a macro to calculate the hash value

Submitted by:  None
Submitted on:  Wed 25 Sep 2013 09:27:14 AM UTC  
Category:  None Priority:  5 - Normal
Severity:  1 - Wish Status:  Wont Do
Privacy:  Public Assigned to:  Bruno Haible <haible>
Originator Email:  -email is unavailable- Open/Closed:  Closed
Operating System:  None

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Sun 27 Nov 2016 05:50:45 PM UTC, comment #1:

It is an interesting idea.

Unfortunately, you cannot do much string processing in C. The only string operation you have in the C preprocessor is the concatenation.

You could try to work on a list of characters, i.e. lookup3('f', 'o', 'o') instead of lookup("foo"), but this soon gets very ugly.

So, the best approach I can see is that at build time you generate a file with preprocessor defines such as
#define INDEX_OF_foo 5
#define INDEX_OF_bar 2
and have a macro
#define index_of(arg) INDEX_OF_ ## arg

Bruno Haible <haible>
Project AdministratorIn charge of this item.
Wed 25 Sep 2013 09:27:14 AM UTC, original submission:

background: I use gperf for embedded development to maintain a key-value database in flash for a fixed set of variables. When I get a string from the outside world I use the 'lookup' function. A subset of the variables are never exposed to the outside world, counters, statistics and are manipulated internally. For these variables it would be very convenient to have a macro which would calculate the hash value of the known string (lookup("foo")): this would give me the index at compile time, which could be used to give (1) a compile time guarantee that I only try to look up existing strings and (2) would avoid the runtime overhead of calculating the hash for known strings.

(1) would be a pleasant property, (2) would be extremely useful to avoid the runtime overhead for a code base with hundreds of internal variables.

If there are no takers, I will probably end up doing this, in which case I'll be glad to submit a patch if there is interest.

Thanks, Laszlo Nemeth



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by haible (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    Sun 27 Nov 2016 05:50:45 PM UTC haible StatusNone => Wont Do
        Assigned toNone => haible
        Open/ClosedOpen => Closed

    Back to the top

    Powered by Savane 3.3