Purpose
I am writing a small library for a larger project which supplies malloc/realloc/free wrapper-functions as well as a function which can tell you whether or not its parameter (of type void *
) corresponds to live (not yet freed) memory allocated and managed by the library's wrapper-functions. Let's refer to this function as isgood_memory
.
Internally, the library maintains a hash-table to ensure that the search performed by isgood_memory
is reasonably fast. The hash-table maintains pointer values (elements of type void *
) to make the search possible. Clearly, values are added and removed from the hash-table to keep it up-to-date with what has been allocated and what has been freed, respectively.
The portability of the library is my biggest concern. It has been designed to assume only a mostly-compliant C90 (ISO/IEC 9899:1990) environment... nothing more.
Question
Since portability is my biggest concern, I couldn't assume that sizeof(void *) == sizeof(X)
for the hash-function. Therefore, I have resorted to treating the value byte-by-byte as if it were a string. To accomplish this, the hash function looks a little like:
static size_t hashit(void *ptrval)
{
size_t i = 0, h = 0;
union {
void *ptrval;
unsigned char string[sizeof(void *)];
} ptrstr;
ptrstr.ptrval = ptrval;
for (; i < sizeof(void *); ++i) {
size_t byte = ptrstr.string[i];
/* Crazy operations here... */
}
return (h);
}
What portability concerns do any of you have with this particular fragment? Will I encounter any funky alignment issues by accessing ptrval
byte-by-byte?