]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: 51d: Improved string handling for LRU cache
authorJames Rosewell <james@51degrees.com>
Fri, 18 Sep 2015 18:53:05 +0000 (19:53 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 21 Sep 2015 10:55:24 +0000 (12:55 +0200)
Removed use of strlen with the data added to and retrived from the cache
by using chunk structures instead of string pointers.

src/51d.c

index e96e0e436e5928250f0b7ba981c51a01062a2dee..cf32346cea1969df54331d8b7252c49dcd3d1fc7 100644 (file)
--- a/src/51d.c
+++ b/src/51d.c
@@ -128,6 +128,38 @@ static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
        return 0;
 }
 
+#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
+/* Insert the data associated with the sample into the cache as a fresh item.
+ */
+static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru)
+{
+       struct chunk *cache_entry = (struct chunk*)malloc(sizeof(struct chunk));
+
+       if (!cache_entry)
+               return;
+
+       smp->flags |= SMP_F_CONST;
+       cache_entry->str = malloc(smp->data.u.str.len + 1);
+       if (!cache_entry->str)
+               return;
+
+       memcpy(cache_entry->str, smp->data.u.str.str, smp->data.u.str.len);
+       cache_entry->str[smp->data.u.str.len] = 0;
+       cache_entry->len = smp->data.u.str.len;
+       lru64_commit(lru, cache_entry, _51DEGREES_CONV_CACHE_KEY, 0, free);
+}
+
+/* Retrieves the data from the cache and sets the sample data to this string.
+ */
+static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
+{
+       struct chunk *cache_entry = (struct chunk*)lru->data;
+       smp->flags |= SMP_F_CONST;
+       smp->data.u.str.str = cache_entry->str;
+       smp->data.u.str.len = cache_entry->len;
+}
+#endif
+
 #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
 /* Sets the important HTTP headers ahead of the detection
  */
@@ -286,7 +318,7 @@ static void _51d_process_match(const struct arg *args, struct sample *smp)
        }
 
        smp->data.u.str.str = temp->str;
-       smp->data.u.str.len = strlen(temp->str);
+       smp->data.u.str.len = temp->len;
 }
 
 static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
@@ -324,10 +356,7 @@ static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw
                lru = lru64_get(_51d_req_hash(args, ws),
                                _51d_lru_tree, _51DEGREES_FETCH_CACHE_KEY, 0);
                if (lru && lru->domain) {
-                       smp->flags |= SMP_F_CONST;
-                       smp->data.u.str.str = lru->data;
-                       smp->data.u.str.len = strlen(lru->data);
-                       fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws);
+                       _51d_retrieve_cache_entry(smp, lru);
                        return 1;
                }
        }
@@ -351,8 +380,7 @@ static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw
 #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
        fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws);
        if (lru) {
-               smp->flags |= SMP_F_CONST;
-               lru64_commit(lru, strdup(smp->data.u.str.str), _51DEGREES_FETCH_CACHE_KEY, 0, free);
+               _51d_insert_cache_entry(smp, lru);
        }
 #endif
 
@@ -375,9 +403,7 @@ static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
                lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
                                _51d_lru_tree, _51DEGREES_CONV_CACHE_KEY, 0);
                if (lru && lru->domain) {
-                       smp->flags |= SMP_F_CONST;
-                       smp->data.u.str.str = lru->data;
-                       smp->data.u.str.len = strlen(smp->data.u.str.str);
+                       _51d_retrieve_cache_entry(smp, lru);
                        return 1;
                }
        }
@@ -408,8 +434,7 @@ static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
 #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
        fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws);
        if (lru) {
-               smp->flags |= SMP_F_CONST;
-               lru64_commit(lru, strdup(smp->data.u.str.str), _51DEGREES_CONV_CACHE_KEY, 0, free);
+               _51d_insert_cache_entry(smp, lru);
        }
 #endif