51Degrees Device Detection C/C++  4.2

A device detection library that is used natively or by 51Degrees products


1 /* *********************************************************************
2  * This Original Work 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 Original Work is licensed under the European Union Public Licence (EUPL)
7  * v.1.2 and is subject to its terms as set out below.
8  *
9  * If a copy of the EUPL was not distributed with this file, You can obtain
10  * one at https://opensource.org/licenses/EUPL-1.2.
11  *
12  * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13  * amended by the European Commission) shall be deemed incompatible for
14  * the purposes of the Work and the provisions of the compatibility
15  * clause in Article 5 of the EUPL shall not apply.
16  *
17  * If using the Work as, or as part of, a network application, by
18  * including the attribution notice(s) required under Article 5 of the EUPL
19  * in the end user terms of the application under an appropriate heading,
20  * such notice(s) shall fulfill the requirements of that article.
21  * ********************************************************************* */
95 /* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
96 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
97 #define NDEBUG
98 #endif
100 #include <stdint.h>
101 #include <stdbool.h>
102 #ifdef _MSC_VER
103 #include <windows.h>
104 #endif
105 #include <assert.h>
106 #include "data.h"
107 #include "exceptions.h"
108 #include "tree.h"
110 #include "threading.h"
111 #endif
113 #ifdef __cplusplus
114 #define EXTERNAL extern "C"
115 #else
116 #define EXTERNAL
117 #endif
120 typedef struct fiftyone_degrees_cache_node_t fiftyoneDegreesCacheNode;
121 typedef struct fiftyone_degrees_cache_shard_t fiftyoneDegreesCacheShard;
122 typedef struct fiftyone_degrees_cache_t fiftyoneDegreesCache;
129 typedef struct fiftyone_degrees_cache_node_t {
141 typedef struct fiftyone_degrees_cache_shard_t {
145  uint32_t capacity;
146  uint32_t allocated;
155 #endif
167  const void *state,
168  fiftyoneDegreesData *data,
169  const void *key,
170  fiftyoneDegreesException *exception);
177 typedef int64_t(*fiftyoneDegreesCacheHashCodeMethod)(const void* key);
184 typedef struct fiftyone_degrees_cache_t {
187  uint16_t concurrency;
188  int32_t capacity;
189  unsigned long hits;
190  unsigned long misses;
194  const void* loaderState;
208  uint32_t capacity,
209  uint16_t concurrency,
212  const void *state);
218 EXTERNAL void fiftyoneDegreesCacheFree(fiftyoneDegreesCache *cache);
243  fiftyoneDegreesCache *cache,
244  const void *key,
245  fiftyoneDegreesException *exception);
261 EXTERNAL int64_t fiftyoneDegreesCacheHash32(const void *key);
270 EXTERNAL int64_t fiftyoneDegreesCacheHash64(const void *key);
275 #endif
uint32_t capacity
Capacity of the shard.
Definition: cache.h:145
uint16_t concurrency
Expected concurrency and number of shards.
Definition: cache.h:187
int32_t capacity
Capacity of the cache.
Definition: cache.h:188
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 ...
const void * loaderState
Cache loader specific state.
Definition: cache.h:194
Cache structure to store the root of the red black tree and a list of allocated cache nodes.
Definition: cache.h:184
pthread_mutex_t fiftyoneDegreesMutex
Definition: threading.h:90
fiftyoneDegreesCacheShard * shards
Array of shards / concurrency.
Definition: cache.h:185
fiftyoneDegreesCacheNode * nodes
Array of nodes / capacity.
Definition: cache.h:186
Tree root structure defining the beginning of the tree.
Definition: tree.h:87
fiftyoneDegreesCacheNode * last
Pointer to the last node in the linked list.
Definition: cache.h:150
fiftyoneDegreesCacheShard * shard
Shard the node is associated with.
Definition: cache.h:132
Node structure defining a single node in the tree.
Definition: tree.h:77
Cache node structure used for storing data in the cache along with its key.
Definition: cache.h:129
unsigned long hits
The requests served from the cache.
Definition: cache.h:189
void fiftyoneDegreesCacheFree(fiftyoneDegreesCache *cache)
Frees the cache structure, all allocated nodes and their data.
Data structure used for reusing memory which may have been allocated in a previous operation.
Definition: data.h:106
int64_t(* fiftyoneDegreesCacheHashCodeMethod)(const void *key)
Method used to calculate a hash code from the key.
Definition: cache.h:177
uint32_t allocated
Number of nodes currently used in the shard.
Definition: cache.h:146
int activeCount
Number of external references to the node data.
Definition: cache.h:135
fiftyoneDegreesTreeRoot root
Root node of the red black tree.
Definition: cache.h:144
fiftyoneDegreesCacheNode * first
Pointer to the first node in the linked list.
Definition: cache.h:148
fiftyoneDegreesCacheNode * nodes
Pointer to the array of all nodes.
Definition: cache.h:147
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.
fiftyoneDegreesMutex lock
Used to ensure exclusive access to the shard for get and release operations.
Definition: cache.h:153
fiftyoneDegreesCacheNode * fiftyoneDegreesCacheGet(fiftyoneDegreesCache *cache, const void *key, fiftyoneDegreesException *exception)
Gets an item from the cache.
Cache shard structure used to enable concurrent access to the cache.
Definition: cache.h:141
fiftyoneDegreesCacheNode * listPrevious
Previous node or NULL if first.
Definition: cache.h:133
fiftyoneDegreesCache * cache
Pointer to the cache to which the node belongs.
Definition: cache.h:142
fiftyoneDegreesTreeNode tree
Tree node for this cache node.
Definition: cache.h:130
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:116
void fiftyoneDegreesCacheRelease(fiftyoneDegreesCacheNode *node)
Releases the cache node previous obtained via fiftyoneDegreesCacheGet so that it can be evicted from ...
fiftyoneDegreesCacheHashCodeMethod hash
Used to hash a key pointer.
Definition: cache.h:193
unsigned long misses
The requests NOT served from the cache.
Definition: cache.h:190
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 ...
fiftyoneDegreesCacheLoadMethod load
Used by the cache to load an item into the cache.
Definition: cache.h:191
void(* fiftyoneDegreesCacheLoadMethod)(const void *state, fiftyoneDegreesData *data, const void *key, fiftyoneDegreesException *exception)
Method used to load data into the cache.
Definition: cache.h:166
fiftyoneDegreesCacheNode * listNext
Next node or NULL if last.
Definition: cache.h:134
fiftyoneDegreesData data
Data contained in the node.
Definition: cache.h:131