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

 
 

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

Submitter:  None
Submitted:  Wed 25 Sep 2013 09:27:14 AM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  1 - Wish Status:  Wont Do
Privacy:  Public Assigned to:  haible
Originator Email:  -email is unavailable- Open/Closed:  Closed
Operating System:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

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
etc.
and have a macro
#define index_of(arg) INDEX_OF_ ## arg

Bruno Haible <haible>
Group administrator
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

Anonymous

 

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

Attach Files:
   
   
Comment:
   

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)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-11-27 haible StatusNone Wont Do
        Assigned toNone haible
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code