]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add API for RB trees with expiry timers
authorAlan T. DeKok <aland@freeradius.org>
Thu, 26 Dec 2024 17:07:33 +0000 (12:07 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 26 Dec 2024 20:27:07 +0000 (15:27 -0500)
src/lib/util/cbor.c
src/lib/util/libfreeradius-util.mk
src/lib/util/rb_expire.c [new file with mode: 0644]
src/lib/util/rb_expire.h [new file with mode: 0644]

index f0e109e068ad4c2a6a33231de62fbd525581df4a..7fba7673eee7d145b742110df7058c472e9dabd1 100644 (file)
@@ -1828,4 +1828,6 @@ done:
  *     digits for integer
  *     'string' for string
  *     h'HHHH' for octets
+ *
+ *     https://datatracker.ietf.org/doc/html/draft-ietf-cbor-edn-literals
  */
index b2c8cf127ada65ad864db55c93a8bbdae6f49511..180d47ba0da4aea5a67cb0428c3574501223bf98 100644 (file)
@@ -72,6 +72,7 @@ SOURCES               := \
                   proto.c \
                   rand.c \
                   rb.c \
+                  rb_expire.c \
                   regex.c \
                   retry.c \
                   sbuff.c \
diff --git a/src/lib/util/rb_expire.c b/src/lib/util/rb_expire.c
new file mode 100644 (file)
index 0000000..5058a45
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ *   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 St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/** Red/black expiry tree implementation
+ *
+ * @file src/lib/util/rb_expire.c
+ *
+ * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
+ */
+RCSID("$Id$")
+
+#include <freeradius-devel/util/rb_expire.h>
+
+/** Attempt to find current data in the tree, if it does not exist insert it
+ *
+ *  Any used node will be inserted into the tail of the expire list,
+ *  and will expire at "now + expire->lifetime".
+ *
+ * @param[in] expire   to search/insert into.
+ * @param[in] data     to find.
+ * @param[in] now      the current time
+ * @return
+ *     - true if data was inserted.
+ *     - false if data already existed and was not inserted.
+ */
+bool fr_rb_expire_insert(fr_rb_expire_t *expire, void const *data, fr_time_t now)
+{
+       fr_dlist_t *entry = fr_dlist_item_to_entry(expire->head.offset, data);
+       fr_rb_expire_node_t *re = (fr_rb_expire_node_t *) (((uintptr_t) entry) - offsetof(fr_rb_expire_node_t, entry));
+
+       if (!fr_rb_insert(&expire->tree, data)) {
+               fr_dlist_remove(&expire->head, entry);
+       }
+
+       fr_dlist_insert_tail(&expire->head, entry);
+
+       re->when = fr_time_add_time_delta(now, expire->lifetime);       
+
+       return true;
+}
+
+void fr_rb_expire_update(fr_rb_expire_t *expire, void const *data, fr_time_t now)
+{
+       fr_dlist_t *entry = fr_dlist_item_to_entry(expire->head.offset, data);
+       fr_rb_expire_node_t *re = (fr_rb_expire_node_t *) (((uintptr_t) entry) - offsetof(fr_rb_expire_node_t, entry));
+
+       fr_dlist_remove(&expire->head, entry);
+
+       fr_dlist_insert_tail(&expire->head, entry);
+       re->when = fr_time_add_time_delta(now, expire->lifetime);       
+
+#if 0
+       /*
+        *      Expire old entries.
+        */
+       fr_dlist_foreach_safe(&expire->head, fr_rb_expire_node_t, old) {{
+               re = (fr_rb_expire_node_t *) (((uintptr_t) old) - offsetof(fr_rb_expire_node_t, entry));
+
+               if (old->when > now) break;
+
+               fr_dlist_remove(&expire->head, &old->entry);
+
+               fr_rb_delete(&expire->tree, re);
+       }}
+#endif
+}
diff --git a/src/lib/util/rb_expire.h b/src/lib/util/rb_expire.h
new file mode 100644 (file)
index 0000000..10be839
--- /dev/null
@@ -0,0 +1,64 @@
+#pragma once
+/*
+ *   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 St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/** RB trees with expiry timers
+ *
+ * @file src/lib/util/rb_expire.h
+ *
+ * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
+ */
+RCSIDH(fr_rb_expire_h, "$Id$")
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <freeradius-devel/util/rb.h>
+#include <freeradius-devel/util/dlist.h>
+
+typedef struct {
+       fr_rb_tree_t    tree;
+       fr_dlist_head_t head;
+       fr_time_delta_t lifetime;
+       fr_time_t       last_expiry;
+} fr_rb_expire_t;
+
+/** dlist for expiring old entries
+ *     
+ *  This structure should be inside of the 
+ */
+typedef struct {
+       fr_rb_node_t    node;
+       fr_dlist_t      entry;
+       fr_time_t       when;
+} fr_rb_expire_node_t;
+
+#define fr_rb_expire_inline_talloc_init(_expire, _type, _field, _data_cmp, _data_free, _lifetime) \
+       do { \
+               fr_rb_inline_talloc_init(&(_expire)->tree, _type, _field.node, _data_cmp, _data_free); \
+               fr_dlist_init(&(_expire)->head, _type, _field.entry); \
+               (_expire)->lifetime = _lifetime; \
+               (_expire)->last_expiry = fr_time(); \
+       } while (0)
+
+bool           fr_rb_expire_insert(fr_rb_expire_t *expire, void const *data, fr_time_t now) CC_HINT(nonnull);
+
+void           fr_rb_expire_update(fr_rb_expire_t *expire, void const *data, fr_time_t now) CC_HINT(nonnull);
+
+#ifdef __cplusplus
+}
+#endif