]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: counters: add shared counters helpers to get and drop shared pointers
authorAurelien DARRAGON <adarragon@haproxy.com>
Tue, 6 May 2025 18:45:40 +0000 (20:45 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Thu, 5 Jun 2025 07:59:04 +0000 (09:59 +0200)
create include/haproxy/counters.h and src/counters.c files to anticipate
for further helpers as some counters specific tasks needs to be carried
out and since counters are shared between multiple object types (ie:
listener, proxy, server..) we need generic helpers.

Add some shared counters helper which are not yet used but will be updated
in upcoming commits.

Makefile
include/haproxy/counters.h [new file with mode: 0644]
src/counters.c [new file with mode: 0644]

index 1959ac47921f344e452be5142222e45ff69a3d21..9f5d3ffed57471d3119e2abbaaf64bdf0d39b5d3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -984,7 +984,7 @@ OBJS += src/mux_h2.o src/mux_h1.o src/mux_fcgi.o src/log.o          \
         src/lb_fas.o src/clock.o src/sock_inet.o src/ev_select.o       \
         src/lb_map.o src/shctx.o src/mworker-prog.o src/hpack-dec.o    \
         src/arg.o src/signal.o src/fix.o src/dynbuf.o src/guid.o       \
-        src/cfgparse-tcp.o src/lb_ss.o src/chunk.o                     \
+        src/cfgparse-tcp.o src/lb_ss.o src/chunk.o src/counters.o      \
         src/cfgparse-unix.o src/regex.o src/fcgi.o src/uri_auth.o      \
         src/eb64tree.o src/eb32tree.o src/eb32sctree.o src/lru.o       \
         src/limits.o src/ebimtree.o src/wdt.o src/hpack-tbl.o          \
diff --git a/include/haproxy/counters.h b/include/haproxy/counters.h
new file mode 100644 (file)
index 0000000..5260427
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * include/haproxy/counters.h
+ * objects counters management
+ *
+ * Copyright 2025 HAProxy Technologies
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, version 2.1
+ * exclusively.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _HAPROXY_COUNTERS_H
+# define _HAPROXY_COUNTERS_H
+
+#include <haproxy/counters-t.h>
+#include <haproxy/guid-t.h>
+
+struct fe_counters_shared *counters_fe_shared_get(const struct guid_node *guid);
+struct be_counters_shared *counters_be_shared_get(const struct guid_node *guid);
+
+void counters_fe_shared_drop(struct fe_counters_shared *counters);
+void counters_be_shared_drop(struct be_counters_shared *counters);
+
+#endif /* _HAPROXY_COUNTERS_H */
diff --git a/src/counters.c b/src/counters.c
new file mode 100644 (file)
index 0000000..41d0293
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * objects counters management
+ *
+ * Copyright 2025 HAProxy Technologies
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <stdlib.h>
+#include <haproxy/counters.h>
+
+/* retrieved shared counters pointer for a given <guid> object
+ * <size> hint is expected to reflect the actual type size (fe/be)
+ * Returns the pointer on success or NULL on failure
+ */
+static void *_counters_shared_get(const struct guid_node *guid, size_t size)
+{
+       /* no shared memory for now, simply allocate a memory block
+        * for the counters (zero-initialized), ignore guid
+        */
+       return calloc(1, size);
+}
+
+/* retrieve shared fe counters pointer for a given <guid> object */
+struct fe_counters_shared *counters_fe_shared_get(const struct guid_node *guid)
+{
+       return _counters_shared_get(guid, sizeof(struct fe_counters_shared));
+}
+
+/* retrieve shared be counters pointer for a given <guid> object */
+struct be_counters_shared *counters_be_shared_get(const struct guid_node *guid)
+{
+       return _counters_shared_get(guid, sizeof(struct be_counters_shared));
+}
+
+static void _counters_shared_drop(void *counters)
+{
+       /* memory was allocated using calloc(), simply free it */
+       free(counters);
+}
+
+/* release a shared fe counters struct */
+void counters_fe_shared_drop(struct fe_counters_shared *counters)
+{
+       _counters_shared_drop(counters);
+}
+
+/* release a shared be counters struct */
+void counters_be_shared_drop(struct be_counters_shared *counters)
+{
+       _counters_shared_drop(counters);
+}