samedi 21 février 2015

How to calculate string checksum using C preprocessor only?

I need to get a string checksum or hash (or something equivalent) using just the C preprocessor, if possible.


The use case is as follows: I'm doing error logging on an embedded device with very limited memory and cpu. I would like to define a LogError() macro which inserts hash(__FILE__) and __LINE__ in a circular buffer (as 16bit numbers). But hash(__FILE__) needs to be compiled to a constant; if the actual filenames are stored as strings in the program that would use too much memory. The hash can be calculated using any method.


It is possible to #define FILE_ID with some unique number at the top of every file, and use that when logging, but that is not the preferred solution, it has a bit of a maintenance cost. Is there a better method?


Aucun commentaire:

Enregistrer un commentaire