• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #define lwsl_cache lwsl_debug
26 #define lwsl_hexdump_cache lwsl_hexdump_debug
27 
28 #define LWS_CACHE_MAX_LEVELS 3
29 
30 /*
31  * If we need structure inside the cache tag names, use this character as a
32  * separator
33  */
34 #define LWSCTAG_SEP '|'
35 
36 /*
37  * Our synthetic cache result items all have tags starting with this char
38  */
39 #define META_ITEM_LEADING '!'
40 
41 typedef struct lws_cache_ttl_item_heap {
42 	lws_dll2_t			list_expiry;
43 	lws_dll2_t			list_lru;
44 
45 	lws_usec_t			expiry;
46 	size_t				key_len;
47 	size_t				size;
48 
49 	/*
50 	 * len + key_len + 1 bytes of data overcommitted, user object first
51 	 * so it is well-aligned, then the NUL-terminated key name
52 	 */
53 } lws_cache_ttl_item_heap_t;
54 
55 /* this is a "base class", all cache implementations have one at the start */
56 
57 typedef struct lws_cache_ttl_lru {
58 	struct lws_cache_creation_info	info;
59 	lws_sorted_usec_list_t		sul;
60 	struct lws_cache_ttl_lru	*child;
61 	uint64_t			current_footprint;
62 } lws_cache_ttl_lru_t;
63 
64 /*
65  * The heap-backed cache uses lws_dll2 linked-lists to track items that are
66  * in it.
67  */
68 
69 typedef struct lws_cache_ttl_lru_heap {
70 	lws_cache_ttl_lru_t		cache;
71 
72 	lws_dll2_owner_t		items_expiry;
73 	lws_dll2_owner_t		items_lru;
74 } lws_cache_ttl_lru_t_heap_t;
75 
76 /*
77  * We want to be able to work with a large file-backed implementation even on
78  * devices that don't have heap to track what is in it.  It means if lookups
79  * reach this cache layer, we will be scanning a potentially large file.
80  *
81  * L1 caching of lookups (including null result list) reduces the expense of
82  * this on average.  We keep a copy of the last computed earliest expiry.
83  *
84  * We can't keep an open file handle here.  Because other processes may change
85  * the cookie file by deleting and replacing it, we have to open it fresh each
86  * time.
87  */
88 typedef struct lws_cache_nscookiejar {
89 	lws_cache_ttl_lru_t		cache;
90 
91 	lws_usec_t			earliest_expiry;
92 } lws_cache_nscookiejar_t;
93 
94 void
95 lws_cache_clear_matches(lws_dll2_owner_t *results_owner);
96 
97 void
98 lws_cache_schedule(struct lws_cache_ttl_lru *cache, sul_cb_t cb, lws_usec_t e);
99