From: Aurelien DARRAGON Date: Tue, 15 Apr 2025 10:40:17 +0000 (+0200) Subject: MINOR: guid: add guid_count() function X-Git-Tag: v3.3-dev7~57 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4c3a36c60998c9784b06d258419cd7209c2ba07a;p=thirdparty%2Fhaproxy.git MINOR: guid: add guid_count() function returns the total amount of registered GUIDs in the guid_tree --- diff --git a/include/haproxy/guid.h b/include/haproxy/guid.h index 42f758ba6..f57a21d76 100644 --- a/include/haproxy/guid.h +++ b/include/haproxy/guid.h @@ -22,5 +22,6 @@ static inline const char *guid_get(const struct guid_node *guid) int guid_is_valid_fmt(const char *uid, char **errmsg); char *guid_name(const struct guid_node *guid); +int guid_count(void); #endif /* _HAPROXY_GUID_H */ diff --git a/src/guid.c b/src/guid.c index 01f52c169..9f29cfec0 100644 --- a/src/guid.c +++ b/src/guid.c @@ -11,6 +11,7 @@ /* GUID global tree */ struct eb_root guid_tree = EB_ROOT_UNIQUE; __decl_thread(HA_RWLOCK_T guid_lock); +static int _guid_count = 0; /* Initialize members. */ void guid_init(struct guid_node *guid) @@ -69,15 +70,19 @@ int guid_insert(enum obj_type *objt, const char *uid, char **errmsg) memprintf(errmsg, "duplicate entry with %s", dup_name); goto err; } + _guid_count += 1; HA_RWLOCK_WRUNLOCK(GUID_LOCK, &guid_lock); guid->obj_type = objt; + return 0; err: if (guid) ha_free(&guid->node.key); ha_free(&dup_name); + if (guid) + guid->node.key = NULL; /* so that we can check that guid is not in a tree */ return 1; } @@ -88,6 +93,8 @@ void guid_remove(struct guid_node *guid) { HA_RWLOCK_WRLOCK(GUID_LOCK, &guid_lock); ebpt_delete(&guid->node); + if (guid->node.key) + _guid_count--; ha_free(&guid->node.key); HA_RWLOCK_WRUNLOCK(GUID_LOCK, &guid_lock); } @@ -171,3 +178,14 @@ char *guid_name(const struct guid_node *guid) return NULL; } + +/* returns the number of guid inserted in guid_tree */ +int guid_count(void) +{ + int count; + + HA_RWLOCK_WRLOCK(GUID_LOCK, &guid_lock); + count = _guid_count; + HA_RWLOCK_WRUNLOCK(GUID_LOCK, &guid_lock); + return count; +}