odyssey/sources/id.h

58 lines
996 B
C
Raw Normal View History

#ifndef ODYSSEY_ID_H
#define ODYSSEY_ID_H
2017-06-20 13:33:20 +00:00
/*
2018-03-12 14:03:15 +00:00
* Odyssey.
2017-06-20 13:33:20 +00:00
*
2018-04-04 13:19:58 +00:00
* Scalable PostgreSQL connection pooler.
*/
2017-06-20 13:33:20 +00:00
2019-01-30 09:30:31 +00:00
#include "machinarium.h"
typedef struct od_id od_id_t;
typedef struct od_id_mgr od_id_mgr_t;
2017-06-20 13:33:20 +00:00
#define OD_ID_SEEDMAX 6
2017-06-20 13:33:20 +00:00
struct od_id
{
char *id_prefix;
char id[OD_ID_SEEDMAX * 2];
uint64_t id_a;
uint64_t id_b;
2017-06-20 13:33:20 +00:00
};
2019-01-30 09:30:31 +00:00
static inline int
od_id_cmp(od_id_t *a, od_id_t *b)
2017-06-20 13:33:20 +00:00
{
2019-01-30 09:30:31 +00:00
return memcmp(a->id, b->id, sizeof(a->id)) == 0;
}
2017-06-20 13:33:20 +00:00
2019-01-30 09:30:31 +00:00
static inline void
od_id_generate(od_id_t *id, char *prefix)
{
2019-01-30 09:30:31 +00:00
long int a = machine_lrand48();
long int b = machine_lrand48();
char seed[OD_ID_SEEDMAX];
memcpy(seed + 0, &a, 4);
memcpy(seed + 4, &b, 2);
id->id_prefix = prefix;
id->id_a = a;
id->id_b = b;
2019-01-30 09:30:31 +00:00
static const char *hex = "0123456789abcdef";
int q, w;
for (q = 0, w = 0; q < OD_ID_SEEDMAX; q++) {
id->id[w++] = hex[(seed[q] >> 4) & 0x0F];
id->id[w++] = hex[(seed[q]) & 0x0F];
2019-01-30 09:30:31 +00:00
}
assert(w == (OD_ID_SEEDMAX * 2));
}
2019-01-30 09:30:31 +00:00
void
od_id_generator_seed(void);
2019-01-30 09:30:31 +00:00
#endif /* ODYSSEY_ID_H */