From: Jaroslav Kysela Date: Tue, 5 Dec 2017 20:06:24 +0000 (+0100) Subject: uuid: add basic uuid set functions X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1e9538574796241de18bec22fea7877a1170d86a;p=thirdparty%2Ftvheadend.git uuid: add basic uuid set functions --- diff --git a/src/uuid.c b/src/uuid.c index 80684151a..bccab1067 100644 --- a/src/uuid.c +++ b/src/uuid.c @@ -155,3 +155,47 @@ uuid_hexvalid ( const char *uuid ) return 0; return 1; } + +/* Init uuid set */ +void +uuid_set_init( tvh_uuid_set_t *us, uint32_t alloc_chunk ) +{ + memset(us, 0, sizeof(*us)); + us->us_alloc_chunk = alloc_chunk ?: 10; +} + +/* Add an uuid to set */ +tvh_uuid_t * +uuid_set_add ( tvh_uuid_set_t *us, const tvh_uuid_t *u ) +{ + tvh_uuid_t *nu; + + if (us->us_count >= us->us_size) { + nu = realloc(us->us_array, sizeof(*u) * (us->us_size + us->us_alloc_chunk)); + if (nu == NULL) + return NULL; + us->us_size += us->us_alloc_chunk; + } + nu = &us->us_array[us->us_count++]; + *nu = *u; + return nu; +} + +/* Free uuid set */ +void +uuid_set_free ( tvh_uuid_set_t *us ) +{ + if (us) { + free(us->us_array); + } +} + +/* Destroy uuid set */ +void +uuid_set_destroy ( tvh_uuid_set_t *us ) +{ + if (us) { + uuid_set_free(us); + free(us); + } +} diff --git a/src/uuid.h b/src/uuid.h index bdd854448..bf174bd6d 100644 --- a/src/uuid.h +++ b/src/uuid.h @@ -32,6 +32,14 @@ typedef struct uuid { uint8_t bin[UUID_BIN_SIZE]; } tvh_uuid_t; +/* Structure for the uuid set */ +typedef struct uuid_set { + tvh_uuid_t *us_array; + uint32_t us_count; + uint32_t us_size; + uint32_t us_alloc_chunk; +} tvh_uuid_set_t; + /* Initialise subsystem */ void uuid_init ( void ); @@ -73,6 +81,40 @@ static inline int uuid_empty ( const tvh_uuid_t *a ) */ int uuid_hexvalid ( const char *uuid ); +/** + * + */ +void uuid_set_init( tvh_uuid_set_t *us, uint32_t alloc_chunk ); + +/** + * + */ +tvh_uuid_t *uuid_set_add( tvh_uuid_set_t *us, const tvh_uuid_t *u ); + +/** + * + */ +void uuid_set_free( tvh_uuid_set_t *us ); + +/** + * + */ +void uuid_set_destroy( tvh_uuid_set_t *us ); + +/** + * + */ +static inline int uuid_set_empty( tvh_uuid_set_t *us ) + { return us->us_count == 0; } + +/** + * + */ +#define UUID_SET_FOREACH(u, us, u32) \ + if ((us)->us_count > 0) \ + for ((u32) = 0, (u) = (us)->us_array; \ + (u32) < (us)->us_count; (u32)++, (u)++) + /** * Hex string to binary */