runtime: move name hasing to common/hash
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
#include <cassert>
|
||||
|
||||
#include "Names.h"
|
||||
|
||||
#include "City.h"
|
||||
#include "Sha1.h"
|
||||
|
||||
namespace {
|
||||
|
||||
void Printable(const uint8_t* data, uint32_t data_length, char* hex) {
|
||||
static const char* hex_digits = "0123456789ABCDEF";
|
||||
int i = 0;
|
||||
for(int i = 0; i < data_length; ++i) {
|
||||
*hex++ = hex_digits[(*data >> 4) & 0xf];
|
||||
*hex++ = hex_digits[(*data++) & 0xf];
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
// Make local hash out of arbitrary data.
|
||||
void MakeLocalHash(const void* data, uint32_t size, LocalHash* hash) {
|
||||
*hash = CityHash64(data, size);
|
||||
}
|
||||
|
||||
// Make global hash out of arbitrary data.
|
||||
void MakeGlobalHash(const void* data, uint32_t size, GlobalHash* hash) {
|
||||
SHA1_CTX ctx;
|
||||
SHA1Init(&ctx);
|
||||
SHA1Update(&ctx, reinterpret_cast<const unsigned char *>(data), size);
|
||||
SHA1Final(&hash->bits[0], &ctx);
|
||||
}
|
||||
|
||||
// Make printable C string out of local hash.
|
||||
void PrintableLocalHash(const LocalHash* hash, char* buffer, uint32_t size) {
|
||||
if (size < sizeof(*hash) * 2) {
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
Printable(reinterpret_cast<const uint8_t*>(&hash), sizeof(*hash), buffer);
|
||||
}
|
||||
|
||||
// Make printable C string out of global hash.
|
||||
void PrintableGlobalHash(const GlobalHash* hash, char* buffer, uint32_t size) {
|
||||
if (size < sizeof(*hash) * 2) {
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
Printable(hash->bits, sizeof(*hash), buffer);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -1,43 +0,0 @@
|
||||
#ifndef RUNTIME_NAMES_H
|
||||
#define RUNTIME_NAMES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// All names in system are stored as hashes (or maybe, for debug builds,
|
||||
// as pointers to uniqued C strings containing names?).
|
||||
// There are two types of hashes:
|
||||
// - local hash, must be unique per class/scope (CityHash64 is being used)
|
||||
// - global hash, must be unique globally (SHA1 is being used)
|
||||
// Generic guideline is that global hash is being used in global persistent
|
||||
// context, while local hashes are more local in scope.
|
||||
// Local hash.
|
||||
typedef uint64_t LocalHash;
|
||||
// Hash of field name.
|
||||
typedef LocalHash FieldNameHash;
|
||||
// Hash of open method name.
|
||||
typedef LocalHash MethodNameHash;
|
||||
// Global hash.
|
||||
typedef struct {
|
||||
uint8_t bits[20];
|
||||
} GlobalHash;
|
||||
// Hash of function name.
|
||||
typedef GlobalHash FunctionNameHash;
|
||||
// Hash of class name.
|
||||
typedef GlobalHash ClassNameHash;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
// Make local hash out of arbitrary data.
|
||||
void MakeLocalHash(const void* data, uint32_t size, LocalHash* hash);
|
||||
// Make global hash out of arbitrary data.
|
||||
void MakeGlobalHash(const void* data, uint32_t size, GlobalHash* hash);
|
||||
// Make printable C string out of local hash.
|
||||
void PrintableLocalHash(const LocalHash* hash, char* buffer, uint32_t size);
|
||||
// Make printable C string out of global hash.
|
||||
void PrintableGlobalHash(const GlobalHash* hash, char* buffer, uint32_t size);
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // RUNTIME_NAMES_H
|
||||
Reference in New Issue
Block a user