I'm reading C on disk hash table code without knowing much about C or mmap
but i know Golang.
This piece of code confuses me. There are two structs like this.
typedef struct HashTbl
{
void *data;
...
} HashTbl;
typedef struct Header
{
char magic[16];
size_t total;
size_t used;
} Header;
It uses mmap
to map HashTbl
data
property
ht->data = mmap(NULL, data_size, prot, MAP_SHARED, file, 0);
ht
type is HashTbl
, ht->data
would be cast to Header
to set property value like this:
Header *h = (Header *)ht->data;
strcpy(h->magic, MAGIC_STR);
h->total = 12;
h->used = 0;
Then this function:
void *hashtable_of(HashTbl *ht)
{
return (unsigned char *)ht->data + sizeof(Header);
}
usage of this function:
uint64_t *table = (uint64_t *)hashtable_of(ht);
I don't understand what's the purpose of this function, is that to calculate the length of void pointer (Header::data)
value?
void pointer
in C seems like interface{}
in Go, which could be cast to any type.
but Go has error handling while doing type casting, if we cast interface{}
type to wrong type, it would return the error
But in this C code, it casts a Struct
-> unsigned char pointer
and combine it to sizeof
other struct, which means unsigned char pointer
is an integer?!
How is that even possible?