\r\n

51Degrees Device Detection C/C++  4.4

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

collection.h

1 /* *********************************************************************
2  * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3  * Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
4  * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5  *
6  * This Original Work is licensed under the European Union Public Licence
7  * (EUPL) 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  * ********************************************************************* */
22 
23 #ifndef FIFTYONE_DEGREES_COLLECTION_H_INCLUDED
24 #define FIFTYONE_DEGREES_COLLECTION_H_INCLUDED
25 
236  /* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
237 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
238 #define NDEBUG
239 #endif
240 
241 #include "data.h"
242 #include "exceptions.h"
243 #include "cache.h"
244 #include "file.h"
245 #include "memory.h"
246 #include "common.h"
247 #include <stdio.h>
248 #include <string.h>
249 #include <assert.h>
250 
255 #define FIFTYONE_DEGREES_COLLECTION_FREE(c) \
256 if (c != NULL) { c->freeCollection(c); }
257 
265 #ifndef FIFTYONE_DEGREES_MEMORY_ONLY
266 #define FIFTYONE_DEGREES_COLLECTION_RELEASE(c, i) c->release(i)
267 #else
268 #define FIFTYONE_DEGREES_COLLECTION_RELEASE(c, i)
269 #endif
270 
275 #pragma pack(push, 4)
276 typedef struct fiftyone_degrees_collection_header_t {
277  uint32_t startPosition;
278  uint32_t length;
279  uint32_t count;
281 #pragma pack(pop)
282 
287 typedef struct fiftyone_degrees_collection_config_t {
288  uint32_t loaded;
290  uint32_t capacity;
292  uint16_t concurrency;
295 
297 typedef struct fiftyone_degrees_collection_t fiftyoneDegreesCollection;
298 typedef struct fiftyone_degrees_collection_item_t fiftyoneDegreesCollectionItem;
299 typedef struct fiftyone_degrees_collection_file_t fiftyoneDegreesCollectionFile;
308 typedef struct fiftyone_degrees_collection_item_t {
310  void *handle;
316 
328 typedef void* (*fiftyoneDegreesCollectionGetMethod)(
329  fiftyoneDegreesCollection *collection,
330  uint32_t indexOrOffset,
332  fiftyoneDegreesException *exception);
333 
341  void *initial);
342 
353 typedef void* (*fiftyoneDegreesCollectionFileRead)(
354  const fiftyoneDegreesCollectionFile *collection,
355  uint32_t offsetOrIndex,
356  fiftyoneDegreesData *data,
357  fiftyoneDegreesException *exception);
358 
371  void *state,
373  long curIndex,
374  fiftyoneDegreesException *exception);
375 
383 
389  fiftyoneDegreesCollection* collection);
390 
399  void *state,
400  uint32_t key,
401  void *data);
402 
408 typedef struct fiftyone_degrees_collection_t {
419  void *state;
425  uint32_t count;
426  uint32_t elementSize;
427  uint32_t size;
430 
434 typedef struct fiftyone_degrees_collection_memory_t {
436  byte *firstByte;
437  byte *lastByte;
438  void *memoryToFree;
441 
445 typedef struct fiftyone_degrees_collection_file_t {
449  long offset;
454 
460 typedef struct fiftyone_degrees_collection_cache_t {
465 
475 
485  fiftyoneDegreesCollection *collection,
486  uint32_t indexOrOffset,
487  fiftyoneDegreesException *exception);
488 
505  FILE *file,
506  fiftyoneDegreesFilePool *reader,
507  const fiftyoneDegreesCollectionConfig *config,
510 
525 
538  const fiftyoneDegreesCollectionFile *file,
539  uint32_t offset,
540  fiftyoneDegreesException *exception);
541 
559  const fiftyoneDegreesCollectionFile *file,
560  uint32_t index,
561  fiftyoneDegreesData *data,
562  fiftyoneDegreesException *exception);
563 
578  FILE *file,
579  uint32_t elementSize,
580  bool isCount);
581 
598  const fiftyoneDegreesCollectionFile *file,
599  fiftyoneDegreesData *data,
600  uint32_t offset,
601  void *initial,
602  size_t initialSize,
604  fiftyoneDegreesException *exception);
605 
621  uint32_t elementSize,
622  bool isCount);
623 
624 
645  fiftyoneDegreesCollection *collection,
647  uint32_t lowerIndex,
648  uint32_t upperIndex,
649  void *state,
651  fiftyoneDegreesException *exception);
652 
666 EXTERNAL uint32_t fiftyoneDegreesCollectionGetCount(
667  fiftyoneDegreesCollection *collection);
668 
673 #endif
bool fiftyoneDegreesCollectionGetIsMemoryOnly()
Determines if in memory collection methods have been compiled so they are fully optimized.
fiftyoneDegreesCollection * source
The source collection used to load items into the cache.
Definition: collection.h:461
Type of collection where items are cached as they are used.
Definition: collection.h:460
fiftyoneDegreesCollectionFileRead read
Read method used to read an item from file at an offset or index.
Definition: collection.h:450
Collection header structure which defines the size and location of the collection data.
Definition: collection.h:276
void * fiftyoneDegreesCollectionReadFileFixed(const fiftyoneDegreesCollectionFile *file, uint32_t index, fiftyoneDegreesData *data, fiftyoneDegreesException *exception)
Used with collections where each item is a fixed number of bytes recorded in elementSize.
fiftyoneDegreesCache * cache
Loading cache to use as data source.
Definition: collection.h:463
int32_t fiftyoneDegreesCollectionGetInteger32(fiftyoneDegreesCollection *collection, uint32_t indexOrOffset, fiftyoneDegreesException *exception)
Returns a 32 bit integer from collections that provide such values.
fiftyoneDegreesCollectionHeader fiftyoneDegreesCollectionHeaderFromFile(FILE *file, uint32_t elementSize, bool isCount)
Reads the 4 bytes at the current reader position and configures the collection header.
long fiftyoneDegreesCollectionBinarySearch(fiftyoneDegreesCollection *collection, fiftyoneDegreesCollectionItem *item, uint32_t lowerIndex, uint32_t upperIndex, void *state, fiftyoneDegreesCollectionItemComparer comparer, fiftyoneDegreesException *exception)
Where a collection is fixed width and contains an ordered list of items this method is used to perfor...
fiftyoneDegreesCollection * collection
The generic collection.
Definition: collection.h:446
bool(* fiftyoneDegreesCollectionIterateMethod)(void *state, uint32_t key, void *data)
Method used to iterate over data held in a collection.
Definition: collection.h:398
Used to store a handle to the underlying item that could be used to release the item when it's finish...
Definition: collection.h:308
void(* fiftyoneDegreesCollectionReleaseMethod)(fiftyoneDegreesCollectionItem *item)
Releases the item so that the collection can free the memory or take other actions when the caller no...
Definition: collection.h:381
fiftyoneDegreesFileHandle * fiftyoneDegreesCollectionReadFilePosition(const fiftyoneDegreesCollectionFile *file, uint32_t offset, fiftyoneDegreesException *exception)
Get a handle from the file pool associated with the collection and position the file handle at the of...
Used to read data from memory in a similar manner to a file handle.
Definition: memory.h:53
void *(* fiftyoneDegreesCollectionFileRead)(const fiftyoneDegreesCollectionFile *collection, uint32_t offsetOrIndex, fiftyoneDegreesData *data, fiftyoneDegreesException *exception)
Reads the item from the underlying data file.
Definition: collection.h:353
Cache structure to store the root of the red black tree and a list of allocated cache nodes.
Definition: cache.h:187
fiftyoneDegreesCollectionFreeMethod freeCollection
Frees collection and all items retrieved from the collection will become invalid.
Definition: collection.h:415
void *(* fiftyoneDegreesCollectionGetMethod)(fiftyoneDegreesCollection *collection, uint32_t indexOrOffset, fiftyoneDegreesCollectionItem *item, fiftyoneDegreesException *exception)
Gets an item from the collection.
Definition: collection.h:328
fiftyoneDegreesCollection * fiftyoneDegreesCollectionCreateFromFile(FILE *file, fiftyoneDegreesFilePool *reader, const fiftyoneDegreesCollectionConfig *config, fiftyoneDegreesCollectionHeader header, fiftyoneDegreesCollectionFileRead read)
Creates a collection from the file handle at the current position in the file.
uint32_t count
The number of items, or 0 if not available.
Definition: collection.h:425
Type of collection where the collection is streamed from file.
Definition: collection.h:445
fiftyoneDegreesCollection * collection
Collection the item came from which may not have been set.
Definition: collection.h:311
int(* fiftyoneDegreesCollectionItemComparer)(void *state, fiftyoneDegreesCollectionItem *item, long curIndex, fiftyoneDegreesException *exception)
Compares two items and returns the difference between them for the purposes of a binary search of ord...
Definition: collection.h:370
uint32_t capacity
Number of items the cache should store, 0 for no cache.
Definition: collection.h:290
byte * firstByte
The first byte in memory of the collection.
Definition: collection.h:436
uint32_t fiftyoneDegreesCollectionGetCount(fiftyoneDegreesCollection *collection)
Gets the actual number of items in the collection by iterating through to the base collection.
Data structure used for reusing memory which may have been allocated in a previous operation.
Definition: data.h:101
uint32_t count
Number of entities in the collection.
Definition: collection.h:279
fiftyoneDegreesCollectionHeader fiftyoneDegreesCollectionHeaderFromMemory(fiftyoneDegreesMemoryReader *reader, uint32_t elementSize, bool isCount)
Reads the 4 bytes at the current reader position and configures the collection header.
void * handle
A handle that relates to the data.
Definition: collection.h:310
uint32_t startPosition
Start position in the data file of the entities.
Definition: collection.h:277
long offset
Offset to the collection in the source data structure.
Definition: collection.h:449
void * memoryToFree
Memory to free when freeing the collection, or NULL if no memory to free.
Definition: collection.h:438
fiftyoneDegreesCollection * next
The next collection implementation or NULL.
Definition: collection.h:423
fiftyoneDegreesFilePool * reader
Reader used to load items into the cache, or NULL if no cache.
Definition: collection.h:447
uint16_t concurrency
Expected number of concurrent requests, 1 or greater.
Definition: collection.h:292
File handle node in the stack of handles.
Definition: file.h:219
uint32_t elementSize
The size of each entry, or 0 if variable length.
Definition: collection.h:426
fiftyoneDegreesCollectionGetMethod get
Gets an entry into the item provided.
Definition: collection.h:409
void * state
Pointer to data for memory, cache or file.
Definition: collection.h:419
uint32_t loaded
Number of items to load into memory from the start of the collection.
Definition: collection.h:288
Collection configuration structure which defines how the collection should be created by the create m...
Definition: collection.h:287
void * fiftyoneDegreesCollectionReadFileVariable(const fiftyoneDegreesCollectionFile *file, fiftyoneDegreesData *data, uint32_t offset, void *initial, size_t initialSize, fiftyoneDegreesCollectionGetFileVariableSizeMethod getFinalSize, fiftyoneDegreesException *exception)
Reads a variable size item from the file where the initial bytes can be used to calculate the size of...
void(* fiftyoneDegreesCollectionFreeMethod)(fiftyoneDegreesCollection *collection)
Frees all the memory and handles associated with the collection.
Definition: collection.h:388
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:111
Type of collection where the collection is held in continuous memory.
Definition: collection.h:434
fiftyoneDegreesCollection * fiftyoneDegreesCollectionCreateFromMemory(fiftyoneDegreesMemoryReader *reader, fiftyoneDegreesCollectionHeader header)
Creates the collection from a memory reader where the collection maps to the memory allocated to the ...
Stack of handles used to read data from a single source file.
Definition: file.h:227
uint32_t length
Length in bytes of all the entities.
Definition: collection.h:278
All the shared methods and fields required by file, memory and cached collections.
Definition: collection.h:408
fiftyoneDegreesData data
Item data including allocated memory.
Definition: collection.h:309
fiftyoneDegreesCollectionReleaseMethod release
Releases the item handle.
Definition: collection.h:413
byte * lastByte
The last byte in memory of the collection.
Definition: collection.h:437
fiftyoneDegreesCollection * collection
The generic collection.
Definition: collection.h:435
uint32_t size
Number of bytes in the source data structure containing the collection's data.
Definition: collection.h:427
uint32_t(* fiftyoneDegreesCollectionGetFileVariableSizeMethod)(void *initial)
Passed a pointer to the first part of a variable size item and returns the size of the entire item.
Definition: collection.h:340