|
#define | SCRU128_LEN (16) |
| The size in bytes of a SCRU128 ID in the binary representation (16 bytes).
|
|
#define | SCRU128_STR_LEN (26) |
| The size in bytes of a SCRU128 ID in the string representation (25 digits + NUL).
|
|
|
#define | SCRU128_GENERATOR_STATUS_NEW_TIMESTAMP (1) |
| Indicates that the latest timestamp was used because it was greater than the previous one.
|
|
#define | SCRU128_GENERATOR_STATUS_COUNTER_LO_INC (2) |
| Indicates that counter_lo was incremented because the latest timestamp was no greater than the previous one.
|
|
#define | SCRU128_GENERATOR_STATUS_COUNTER_HI_INC (3) |
| Indicates that counter_hi was incremented because counter_lo reached its maximum value.
|
|
#define | SCRU128_GENERATOR_STATUS_TIMESTAMP_INC (4) |
| Indicates that the previous timestamp was incremented because counter_hi reached its maximum value.
|
|
#define | SCRU128_GENERATOR_STATUS_ROLLBACK_RESET (5) |
| Indicates that the generator was reinitialized and the monotonic order of generated IDs was broken because the latest timestamp was significantly smaller than the previous one.
|
|
#define | SCRU128_GENERATOR_STATUS_ERROR (-1) |
| Indicates that the previous generation failed.
|
|
#define | SCRU128_GENERATOR_STATUS_ROLLBACK_ABORT (-2) |
| Indicates that the previous generation was aborted because the latest timestamp was significantly smaller than the previous one.
|
|
|
|
This library represents a binary SCRU128 ID as a 16-byte uint8_t array containing a 128-bit unsigned integer in the big-endian (network) byte order.
|
static int | scru128_from_fields (uint8_t *id_out, uint64_t timestamp, uint32_t counter_hi, uint32_t counter_lo, uint32_t entropy) |
| Creates a SCRU128 ID from field values.
|
|
static void | scru128_copy (uint8_t *id_dst, const uint8_t *id_src) |
| Copies a SCRU128 ID from id_src to id_dst .
|
|
static int | scru128_from_str (uint8_t *id_out, const char *str) |
| Creates a SCRU128 ID from a 25-digit string representation.
|
|
static uint64_t | scru128_timestamp (const uint8_t *id) |
| Returns the 48-bit timestamp field value of a SCRU128 ID.
|
|
static uint32_t | scru128_counter_hi (const uint8_t *id) |
| Returns the 24-bit counter_hi field value of a SCRU128 ID.
|
|
static uint32_t | scru128_counter_lo (const uint8_t *id) |
| Returns the 24-bit counter_lo field value of a SCRU128 ID.
|
|
static uint32_t | scru128_entropy (const uint8_t *id) |
| Returns the 32-bit entropy field value of a SCRU128 ID.
|
|
static void | scru128_to_str (const uint8_t *id, char *str_out) |
| Returns the 25-digit canonical string representation of a SCRU128 ID.
|
|
static int | scru128_compare (const uint8_t *id_lft, const uint8_t *id_rgt) |
| Returns a negative integer, zero, or positive integer if id_lft is less than, equal to, or greater than id_rgt , respectively.
|
|
|
static void | scru128_generator_init (Scru128Generator *g) |
| Initializes a generator struct g .
|
|
static int8_t | scru128_generate_or_abort_core (Scru128Generator *g, uint8_t *id_out, uint64_t timestamp, uint32_t(*arc4random)(void), uint64_t rollback_allowance) |
| Generates a new SCRU128 ID with the given timestamp and random number generator, or returns an error upon significant timestamp rollback.
|
|
static int8_t | scru128_generate_or_reset_core (Scru128Generator *g, uint8_t *id_out, uint64_t timestamp, uint32_t(*arc4random)(void), uint64_t rollback_allowance) |
| Generates a new SCRU128 ID with the given timestamp and random number generator, or resets the generator upon significant timestamp rollback.
|
|
|
int | scru128_generate (Scru128Generator *g, uint8_t *id_out) |
| Generates a new SCRU128 ID from the current timestamp .
|
|
static int | scru128_generate_string (Scru128Generator *g, char *str_out) |
| Generates a new SCRU128 ID encoded in the 25-digit canonical string representation.
|
|
SCRU128: Sortable, Clock and Random number-based Unique identifier.
- Version
- v0.4.3
- Copyright
- Licensed under the Apache License, Version 2.0
- See also
- https://github.com/scru128/c
static int8_t scru128_generate_or_abort_core |
( |
Scru128Generator * |
g, |
|
|
uint8_t * |
id_out, |
|
|
uint64_t |
timestamp, |
|
|
uint32_t(*)(void) |
arc4random, |
|
|
uint64_t |
rollback_allowance |
|
) |
| |
|
inlinestatic |
Generates a new SCRU128 ID with the given timestamp
and random number generator, or returns an error upon significant timestamp rollback.
This function returns a monotonically increasing ID by reusing the previous timestamp
even if the one provided is smaller than the immediately preceding ID's. However, when such a clock rollback is considered significant (by more than rollback_allowance
milliseconds), this function aborts and returns SCRU128_GENERATOR_STATUS_ROLLBACK_ABORT
immediately.
See scru128_generate_or_reset_core()
for the other mode of generation.
- Parameters
-
g | A generator state object used to generate an ID. |
id_out | A 16-byte byte array where the generated SCRU128 ID is stored. |
timestamp | A 48-bit timestamp field value. |
arc4random | A function pointer to arc4random() or a compatible function that returns a (cryptographically strong) random number in the range of 32-bit unsigned integer. |
rollback_allowance | The amount of timestamp rollback that is considered significant. A suggested value is 10000 (milliseconds). |
- Returns
- One of
SCRU128_GENERATOR_STATUS_*
codes that describes the characteristics of generated ID. A negative return code reports an error.
- Attention
- This function is NOT thread-safe. The generator
g
should be protected from concurrent accesses using a mutex or other synchronization mechanism to avoid race conditions.
static int8_t scru128_generate_or_reset_core |
( |
Scru128Generator * |
g, |
|
|
uint8_t * |
id_out, |
|
|
uint64_t |
timestamp, |
|
|
uint32_t(*)(void) |
arc4random, |
|
|
uint64_t |
rollback_allowance |
|
) |
| |
|
inlinestatic |
Generates a new SCRU128 ID with the given timestamp
and random number generator, or resets the generator upon significant timestamp rollback.
This function returns a monotonically increasing ID by reusing the previous timestamp
even if the one provided is smaller than the immediately preceding ID's. However, when such a clock rollback is considered significant (by more than rollback_allowance
milliseconds), this function resets the generator and returns a new ID based on the given timestamp
, breaking the increasing order of IDs.
See scru128_generate_or_abort_core()
for the other mode of generation.
- Parameters
-
g | A generator state object used to generate an ID. |
id_out | A 16-byte byte array where the generated SCRU128 ID is stored. |
timestamp | A 48-bit timestamp field value. |
arc4random | A function pointer to arc4random() or a compatible function that returns a (cryptographically strong) random number in the range of 32-bit unsigned integer. |
rollback_allowance | The amount of timestamp rollback that is considered significant. A suggested value is 10000 (milliseconds). |
- Returns
- One of
SCRU128_GENERATOR_STATUS_*
codes that describes the characteristics of generated ID. A negative return code reports an error.
- Attention
- This function is NOT thread-safe. The generator
g
should be protected from concurrent accesses using a mutex or other synchronization mechanism to avoid race conditions.