]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Minor reorg of the async session
authorVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 7 Sep 2023 12:39:25 +0000 (13:39 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 7 Sep 2023 12:49:56 +0000 (13:49 +0100)
src/libserver/async_session.c

index 66552418af98d54c2e03ce59d108f142a7218a4f..962d5136803bddc9b3ee91c6b7ba2f60e14350d5 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -55,43 +55,15 @@ struct rspamd_async_event {
        void *user_data;
 };
 
-static guint rspamd_event_hash(gconstpointer a);
-static gboolean rspamd_event_equal(gconstpointer a, gconstpointer b);
-
-/* Define **SET** of events */
-KHASH_INIT(rspamd_events_hash,
-                  struct rspamd_async_event *,
-                  char,
-                  false,
-                  rspamd_event_hash,
-                  rspamd_event_equal);
-
-struct rspamd_async_session {
-       session_finalizer_t fin;
-       event_finalizer_t restore;
-       event_finalizer_t cleanup;
-       khash_t(rspamd_events_hash) * events;
-       void *user_data;
-       rspamd_mempool_t *pool;
-       guint flags;
-};
-
-static gboolean
-rspamd_event_equal(gconstpointer a, gconstpointer b)
+static inline bool
+rspamd_event_equal(const struct rspamd_async_event *ev1, const struct rspamd_async_event *ev2)
 {
-       const struct rspamd_async_event *ev1 = a, *ev2 = b;
-
-       if (ev1->fin == ev2->fin) {
-               return ev1->user_data == ev2->user_data;
-       }
-
-       return FALSE;
+       return ev1->fin == ev2->fin && ev1->user_data == ev2->user_data;
 }
 
-static guint
-rspamd_event_hash(gconstpointer a)
+static inline guint64
+rspamd_event_hash(const struct rspamd_async_event *ev)
 {
-       const struct rspamd_async_event *ev = a;
        union _pointer_fp_thunk {
                event_finalizer_t f;
                gpointer p;
@@ -107,6 +79,24 @@ rspamd_event_hash(gconstpointer a)
        return rspamd_cryptobox_fast_hash(&st, sizeof(st), rspamd_hash_seed());
 }
 
+/* Define **SET** of events */
+KHASH_INIT(rspamd_events_hash,
+                  struct rspamd_async_event *,
+                  char,
+                  false,
+                  rspamd_event_hash,
+                  rspamd_event_equal);
+
+struct rspamd_async_session {
+       session_finalizer_t fin;
+       event_finalizer_t restore;
+       event_finalizer_t cleanup;
+       khash_t(rspamd_events_hash) * events;
+       void *user_data;
+       rspamd_mempool_t *pool;
+       guint flags;
+};
+
 static void
 rspamd_session_dtor(gpointer d)
 {
@@ -134,13 +124,7 @@ rspamd_session_create(rspamd_mempool_t *pool,
        s->user_data = user_data;
        s->events = kh_init(rspamd_events_hash);
 
-       if (events_count.mean > 4) {
-               kh_resize(rspamd_events_hash, s->events, events_count.mean);
-       }
-       else {
-               kh_resize(rspamd_events_hash, s->events, 4);
-       }
-
+       kh_resize(rspamd_events_hash, s->events, MAX(4, events_count.mean));
        rspamd_mempool_add_destructor(pool, rspamd_session_dtor, s);
 
        return s;