]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: guid: add guid_count() function
authorAurelien DARRAGON <adarragon@haproxy.com>
Tue, 15 Apr 2025 10:40:17 +0000 (12:40 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Thu, 7 Aug 2025 20:26:58 +0000 (22:26 +0200)
returns the total amount of registered GUIDs in the guid_tree

include/haproxy/guid.h
src/guid.c

index 42f758ba6ed45c03531e6aee4cb3137a8865d94f..f57a21d76c5ee7807fb033ad0aefc77039628c74 100644 (file)
@@ -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 */
index 01f52c16958d7b13141fb8ecb927237e7da79b73..9f29cfec0e865150bebdce205f8c34f48046d141 100644 (file)
@@ -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 <guid> 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;
+}