51Degrees Common C/C++  4.1

A shared functionality library that is used by 51Degrees products

cache.h

1 /* *********************************************************************
2  * This Source Code Form is copyright of 51 Degrees Mobile Experts Limited.
3  * Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4  * Caversham, Reading, Berkshire, United Kingdom RG4 7BY
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0.
8  *
9  * If a copy of the MPL was not distributed with this file, You can obtain
10  * one at http://mozilla.org/MPL/2.0/.
11  *
12  * This Source Code Form is "Incompatible With Secondary Licenses", as
13  * defined by the Mozilla Public License, v. 2.0.
14  * ********************************************************************* */
15 
85 #ifndef FIFTYONE_DEGREES_CACHE_H_INCLUDED
86 #define FIFTYONE_DEGREES_CACHE_H_INCLUDED
87 
88 /* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
89 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
90 #define NDEBUG
91 #endif
92 
93 #include <stdint.h>
94 #include <stdbool.h>
95 #ifdef _MSC_VER
96 #include <windows.h>
97 #endif
98 #include <assert.h>
99 #include "data.h"
100 #include "exceptions.h"
101 #include "tree.h"
102 #ifndef FIFTYONE_DEGREES_NO_THREADING
103 #include "threading.h"
104 #endif
105 
106 #ifdef __cplusplus
107 #define EXTERNAL extern "C"
108 #else
109 #define EXTERNAL
110 #endif
111 
113 typedef struct fiftyone_degrees_cache_node_t fiftyoneDegreesCacheNode;
114 typedef struct fiftyone_degrees_cache_shard_t fiftyoneDegreesCacheShard;
115 typedef struct fiftyone_degrees_cache_t fiftyoneDegreesCache;
122 typedef struct fiftyone_degrees_cache_node_t {
130 
134 typedef struct fiftyone_degrees_cache_shard_t {
138  uint32_t capacity;
139  uint32_t allocated;
145 #ifndef FIFTYONE_DEGREES_NO_THREADING
148 #endif
150 
160  const void *state,
161  fiftyoneDegreesData *data,
162  const void *key,
163  fiftyoneDegreesException *exception);
164 
170 typedef int64_t(*fiftyoneDegreesCacheHashCodeMethod)(const void* key);
171 
177 typedef struct fiftyone_degrees_cache_t {
180  uint16_t concurrency;
181  int32_t capacity;
182  unsigned long hits;
183  unsigned long misses;
187  const void* loaderState;
189 
201  uint32_t capacity,
202  uint16_t concurrency,
205  const void *state);
206 
211 EXTERNAL void fiftyoneDegreesCacheFree(fiftyoneDegreesCache *cache);
212 
236  fiftyoneDegreesCache *cache,
237  const void *key,
238  fiftyoneDegreesException *exception);
239 
246 
254 EXTERNAL int64_t fiftyoneDegreesCacheHash32(const void *key);
255 
263 EXTERNAL int64_t fiftyoneDegreesCacheHash64(const void *key);
264 
268 #endif
uint32_t capacity
Capacity of the shard.
Definition: cache.h:138
uint16_t concurrency
Expected concurrency and number of shards.
Definition: cache.h:180
int32_t capacity
Capacity of the cache.
Definition: cache.h:181
EXTERNAL void fiftyoneDegreesCacheRelease(fiftyoneDegreesCacheNode *node)
Releases the cache node previous obtained via fiftyoneDegreesCacheGet so that it can be evicted from ...
Definition: cache.c:412
const void * loaderState
Cache loader specific state.
Definition: cache.h:187
Cache structure to store the root of the red black tree and a list of allocated cache nodes.
Definition: cache.h:177
pthread_mutex_t fiftyoneDegreesMutex
MUTEX AND THREADING MACROS.
Definition: threading.h:82
fiftyoneDegreesCacheShard * shards
Array of shards / concurrency.
Definition: cache.h:178
fiftyoneDegreesCacheNode * nodes
Array of nodes / capacity.
Definition: cache.h:179
Tree root structure defining the beginning of the tree.
Definition: tree.h:80
fiftyoneDegreesCacheNode * last
Pointer to the last node in the linked list.
Definition: cache.h:143
fiftyoneDegreesCacheShard * shard
Shard the node is associated with.
Definition: cache.h:125
Node structure defining a single node in the tree.
Definition: tree.h:70
Cache node structure used for storing data in the cache along with its key.
Definition: cache.h:122
unsigned long hits
The requests served from the cache.
Definition: cache.h:182
Data structure used for reusing memory which may have been allocated in a previous operation.
Definition: data.h:99
int64_t(* fiftyoneDegreesCacheHashCodeMethod)(const void *key)
Method used to calculate a hash code from the key.
Definition: cache.h:170
uint32_t allocated
Number of nodes currently used in the shard.
Definition: cache.h:139
int activeCount
Number of external references to the node data.
Definition: cache.h:128
fiftyoneDegreesTreeRoot root
Root node of the red black tree.
Definition: cache.h:137
EXTERNAL fiftyoneDegreesCacheNode * fiftyoneDegreesCacheGet(fiftyoneDegreesCache *cache, const void *key, fiftyoneDegreesException *exception)
Gets an item from the cache.
Definition: cache.c:375
fiftyoneDegreesCacheNode * first
Pointer to the first node in the linked list.
Definition: cache.h:141
fiftyoneDegreesCacheNode * nodes
Pointer to the array of all nodes.
Definition: cache.h:140
fiftyoneDegreesMutex lock
Used to ensure exclusive access to the shard for get and release operations.
Definition: cache.h:146
EXTERNAL fiftyoneDegreesCache * fiftyoneDegreesCacheCreate(uint32_t capacity, uint16_t concurrency, fiftyoneDegreesCacheLoadMethod load, fiftyoneDegreesCacheHashCodeMethod hash, const void *state)
Creates a new cache.The cache must be destroyed with the fiftyoneDegreesCacheFree method.
Definition: cache.c:309
EXTERNAL int64_t fiftyoneDegreesCacheHash32(const void *key)
Passed a pointer to a 32 bit / 4 byte data structure and returns the data as a 64 bit / 8 byte value ...
Definition: cache.c:429
Cache shard structure used to enable concurrent access to the cache.
Definition: cache.h:134
fiftyoneDegreesCacheNode * listPrevious
Previous node or NULL if first.
Definition: cache.h:126
fiftyoneDegreesCache * cache
Pointer to the cache to which the node belongs.
Definition: cache.h:135
fiftyoneDegreesTreeNode tree
Tree node for this cache node.
Definition: cache.h:123
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:109
fiftyoneDegreesCacheHashCodeMethod hash
Used to hash a key pointer.
Definition: cache.h:186
EXTERNAL int64_t fiftyoneDegreesCacheHash64(const void *key)
Passed a pointer to a 64 bit / 8 byte data structure and returns the data as a 64 bit / 8 byte value ...
Definition: cache.c:433
unsigned long misses
The requests NOT served from the cache.
Definition: cache.h:183
fiftyoneDegreesCacheLoadMethod load
Used by the cache to load an item into the cache.
Definition: cache.h:184
void(* fiftyoneDegreesCacheLoadMethod)(const void *state, fiftyoneDegreesData *data, const void *key, fiftyoneDegreesException *exception)
Method used to load data into the cache.
Definition: cache.h:159
fiftyoneDegreesCacheNode * listNext
Next node or NULL if last.
Definition: cache.h:127
fiftyoneDegreesData data
Data contained in the node.
Definition: cache.h:124
EXTERNAL void fiftyoneDegreesCacheFree(fiftyoneDegreesCache *cache)
Frees the cache structure, all allocated nodes and their data.
Definition: cache.c:362