From: Alan T. DeKok Date: Thu, 26 Dec 2024 17:07:33 +0000 (-0500) Subject: add API for RB trees with expiry timers X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=42e49cafc730e116808f45cb6656c68114de3d03;p=thirdparty%2Ffreeradius-server.git add API for RB trees with expiry timers --- diff --git a/src/lib/util/cbor.c b/src/lib/util/cbor.c index f0e109e068a..7fba7673eee 100644 --- a/src/lib/util/cbor.c +++ b/src/lib/util/cbor.c @@ -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 */ diff --git a/src/lib/util/libfreeradius-util.mk b/src/lib/util/libfreeradius-util.mk index b2c8cf127ad..180d47ba0da 100644 --- a/src/lib/util/libfreeradius-util.mk +++ b/src/lib/util/libfreeradius-util.mk @@ -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 index 00000000000..5058a455cea --- /dev/null +++ b/src/lib/util/rb_expire.c @@ -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 + +/** 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 index 00000000000..10be8392971 --- /dev/null +++ b/src/lib/util/rb_expire.h @@ -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 +#include + +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