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 2026 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
26#ifdef FIFTYONE_DEGREES_IPI_SYMBOLS
27#include "ipiSymbols.h"
28#endif
29
239
240 /* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
241#if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
242#define NDEBUG
243#endif
244
245#include "collectionKey.h"
246#include "data.h"
247#include "exceptions.h"
248#include "cache.h"
249#include "file.h"
250#include "memory.h"
251#include "common.h"
252#include <stdio.h>
253#include <string.h>
254
255#include "propertyValueType.h"
256
261#define FIFTYONE_DEGREES_COLLECTION_FREE(c) \
262if (c != NULL) { c->freeCollection(c); }
263
271#ifndef FIFTYONE_DEGREES_MEMORY_ONLY
272#define FIFTYONE_DEGREES_COLLECTION_RELEASE(c, i) { assert(!((i)->collection) || (c == (i)->collection)); c->release(i); };
273#else
274#define FIFTYONE_DEGREES_COLLECTION_RELEASE(c, i)
275#endif
276
281#pragma pack(push, 1)
282typedef struct fiftyone_degrees_collection_header_t {
284 uint32_t length;
287 uint32_t count;
289#pragma pack(pop)
290
295typedef struct fiftyone_degrees_collection_config_t {
296 bool loaded;
297 uint32_t capacity;
299 uint16_t concurrency;
302
304typedef struct fiftyone_degrees_collection_t fiftyoneDegreesCollection;
305typedef struct fiftyone_degrees_collection_item_t fiftyoneDegreesCollectionItem;
306typedef struct fiftyone_degrees_collection_file_t fiftyoneDegreesCollectionFile;
308
315typedef struct fiftyone_degrees_collection_item_t {
317 void *handle;
318
325
337typedef void* (*fiftyoneDegreesCollectionGetMethod)(
338 const fiftyoneDegreesCollection *collection,
341 fiftyoneDegreesException *exception);
342
353typedef void* (*fiftyoneDegreesCollectionFileRead)(
354 const fiftyoneDegreesCollectionFile *collection,
357 fiftyoneDegreesException *exception);
358
371 void *state,
374 fiftyoneDegreesException *exception);
375
383
389 fiftyoneDegreesCollection* collection);
390
399 void *state,
400 uint32_t key,
401 void *data);
402
408typedef struct fiftyone_degrees_collection_t {
419 void *state;
423 uint32_t count;
424 uint32_t elementSize;
425 uint32_t size;
428#ifdef FIFTYONE_DEGREES_LARGE_DATA_FILE_SUPPORT
429 byte offsetShift;
438#endif
439 const char *typeName;
441
445typedef struct fiftyone_degrees_collection_memory_t {
447 byte *firstByte;
448 byte *lastByte;
452
456typedef struct fiftyone_degrees_collection_file_t {
465
471typedef struct fiftyone_degrees_collection_cache_t {
476
486
496 const fiftyoneDegreesCollection *collection,
497 uint32_t indexOrOffset,
498 fiftyoneDegreesException *exception);
499
516 FILE *file,
521
536
537#ifdef FIFTYONE_DEGREES_LARGE_DATA_FILE_SUPPORT
538
563fiftyoneDegreesCollectionCreateFromFileWithOffsetShift(
564 FILE *file,
569 byte offsetShift);
570
586fiftyoneDegreesCollectionCreateFromMemoryWithOffsetShift(
589 byte offsetShift);
590
591#endif
592
606 uint32_t offset,
607 fiftyoneDegreesException *exception);
608
629 fiftyoneDegreesException *exception);
630
645 FILE *file,
646 uint32_t elementSize,
647 bool isCount);
648
666 void *initial,
667 fiftyoneDegreesException *exception);
668
684 uint32_t elementSize,
685 bool isCount);
686
687
709 const fiftyoneDegreesCollection *collection,
714 void *state,
716 fiftyoneDegreesException *exception);
717
731#define fiftyoneDegreesCollectionGetCount(collection) ((collection)->count)
732
736
737#endif
fiftyoneDegreesCollection * fiftyoneDegreesCollectionCreateFromMemory(fiftyoneDegreesMemoryReader *reader, fiftyoneDegreesCollectionHeader header)
Creates the collection from a memory reader where the collection maps to the memory allocated to the ...
bool(* fiftyoneDegreesCollectionIterateMethod)(void *state, uint32_t key, void *data)
Method used to iterate over data held in a collection.
Definition collection.h:398
int(* fiftyoneDegreesCollectionItemComparer)(void *state, fiftyoneDegreesCollectionItem *item, fiftyoneDegreesCollectionKey key, fiftyoneDegreesException *exception)
Compares two items and returns the difference between them for the purposes of a binary search of ord...
Definition collection.h:370
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...
long fiftyoneDegreesCollectionBinarySearch(const fiftyoneDegreesCollection *collection, fiftyoneDegreesCollectionItem *item, fiftyoneDegreesCollectionIndexOrOffset lowerKey, fiftyoneDegreesCollectionIndexOrOffset upperKey, const fiftyoneDegreesCollectionKeyType *keyType, 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...
void * fiftyoneDegreesCollectionReadFileVariable(const fiftyoneDegreesCollectionFile *file, fiftyoneDegreesData *data, const fiftyoneDegreesCollectionKey *key, void *initial, fiftyoneDegreesException *exception)
Reads a variable size item from the file where the initial bytes can be used to calculate the size of...
int32_t fiftyoneDegreesCollectionGetInteger32(const 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.
bool fiftyoneDegreesCollectionGetIsMemoryOnly()
Determines if in memory collection methods have been compiled so they are fully optimized.
void *(* fiftyoneDegreesCollectionFileRead)(const fiftyoneDegreesCollectionFile *collection, const fiftyoneDegreesCollectionKey *key, fiftyoneDegreesData *data, fiftyoneDegreesException *exception)
Reads the item from the underlying data file.
Definition collection.h:353
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.
void(* fiftyoneDegreesCollectionFreeMethod)(fiftyoneDegreesCollection *collection)
Frees all the memory and handles associated with the collection.
Definition collection.h:388
fiftyoneDegreesCollectionHeader fiftyoneDegreesCollectionHeaderFromMemory(fiftyoneDegreesMemoryReader *reader, uint32_t elementSize, bool isCount)
Reads the 4 bytes at the current reader position and configures the collection header.
void *(* fiftyoneDegreesCollectionGetMethod)(const fiftyoneDegreesCollection *collection, const fiftyoneDegreesCollectionKey *key, fiftyoneDegreesCollectionItem *item, fiftyoneDegreesException *exception)
Gets an item from the collection.
Definition collection.h:337
void * fiftyoneDegreesCollectionReadFileFixed(const fiftyoneDegreesCollectionFile *file, const fiftyoneDegreesCollectionKey *key, fiftyoneDegreesData *data, fiftyoneDegreesException *exception)
Used with collections where each item is a fixed number of bytes recorded in elementSize.
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
long fiftyoneDegreesFileOffset
Type for file offset in API.
Definition fileOffset.h:73
uint32_t fiftyoneDegreesFileOffsetUnsigned
Type for collection start offset (in file).
Definition fileOffset.h:65
Cache structure to store the root of the red black tree and a list of allocated cache nodes.
Definition cache.h:191
Type of collection where items are cached as they are used.
Definition collection.h:471
fiftyoneDegreesCollection * source
The source collection used to load items into the cache.
Definition collection.h:472
fiftyoneDegreesCache * cache
Loading cache to use as data source.
Definition collection.h:474
Collection configuration structure which defines how the collection should be created by the create m...
Definition collection.h:295
bool loaded
Collection is loaded entirely into memory.
Definition collection.h:296
uint32_t capacity
Number of items the cache should store, 0 for no cache.
Definition collection.h:297
uint16_t concurrency
Expected number of concurrent requests, 1 or greater.
Definition collection.h:299
Type of collection where the collection is streamed from file.
Definition collection.h:456
fiftyoneDegreesFileOffset offset
Offset to the collection in the source data structure.
Definition collection.h:460
fiftyoneDegreesCollection * collection
The generic collection.
Definition collection.h:457
fiftyoneDegreesFilePool * reader
Reader used to load items into the cache, or NULL if no cache.
Definition collection.h:458
fiftyoneDegreesCollectionFileRead read
Read method used to read an item from file at an offset or index.
Definition collection.h:461
Collection header structure which defines the size and location of the collection data.
Definition collection.h:282
uint32_t length
Length of all the entities, in bytes, or in offset units where the collection is created with a non z...
Definition collection.h:284
uint32_t count
Number of entities in the collection.
Definition collection.h:287
fiftyoneDegreesFileOffsetUnsigned startPosition
Start position in the data file of the entities.
Definition collection.h:283
All the shared methods and fields required by file, memory and cached collections.
Definition collection.h:408
uint32_t count
The number of items, or 0 if not available.
Definition collection.h:423
fiftyoneDegreesCollectionGetMethod get
Gets an entry into the item provided.
Definition collection.h:409
uint32_t size
Number of bytes in the source data structure containing the collection's data, or where offsetShift i...
Definition collection.h:425
uint32_t elementSize
The size of each entry, or 0 if variable length.
Definition collection.h:424
void * state
Pointer to data for memory, cache or file.
Definition collection.h:419
const char * typeName
Name of collection type (vtable).
Definition collection.h:439
fiftyoneDegreesCollectionFreeMethod freeCollection
Frees collection and all items retrieved from the collection will become invalid.
Definition collection.h:415
fiftyoneDegreesCollectionReleaseMethod release
Releases the item handle.
Definition collection.h:413
Used to store a handle to the underlying item that could be used to release the item when it's finish...
Definition collection.h:315
const fiftyoneDegreesCollection * collection
Collection the item came from which may not have been set.
Definition collection.h:323
fiftyoneDegreesData data
Item data including allocated memory.
Definition collection.h:316
void * handle
A handle that relates to the data.
Definition collection.h:317
Explains to a collection (or cache) what the consumer is looking for.
Definition collectionKey.h:76
Explains to a collection how to properly extract the requested value.
Definition collectionKey.h:67
Type of collection where the collection is held in continuous memory.
Definition collection.h:445
void * memoryToFree
Memory to free when freeing the collection, or NULL if no memory to free.
Definition collection.h:449
byte * lastByte
The last byte in memory of the collection.
Definition collection.h:448
fiftyoneDegreesCollection * collection
The generic collection.
Definition collection.h:446
byte * firstByte
The first byte in memory of the collection.
Definition collection.h:447
Data structure used for reusing memory which may have been allocated in a previous operation.
Definition data.h:105
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition exceptions.h:115
Stack of handles used to read data from a single source file.
Definition file.h:233
Used to read data from memory in a similar manner to a file handle.
Definition memory.h:58
Location of the item within the Collection.
Definition collectionKey.h:56
File handle node in the stack of handles.
Definition file.h:225