-
Notifications
You must be signed in to change notification settings - Fork 6
/
collection.c
1236 lines (1058 loc) · 33.3 KB
/
collection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
#include "collection.h"
#include "fiftyone.h"
MAP_TYPE(Collection)
#ifndef FIFTYONE_DEGREES_MEMORY_ONLY
MAP_TYPE(CollectionConfig)
#endif
/**
* Used by methods which retrieve values from a collection to set an exception.
*/
#ifndef FIFTYONE_DEGREES_EXCEPTIONS_DISABLED
#define GET_EXCEPTION_SET(s) \
if (item->data.allocated > 0) { \
Free(item->data.ptr); \
} \
item->data.ptr = NULL; \
item->data.used = 0; \
item->data.allocated = 0; \
item->handle = NULL; \
item->collection = NULL; \
if (exception->status == NOT_SET) { \
EXCEPTION_SET(s); \
}
#else
#define GET_EXCEPTION_SET(s)
#endif
/**
* Used to work out the number of variable width items can be loaded
* into a fixed amount of memory.
*/
typedef struct size_counter_t {
uint32_t count; /* The number of entries read so far */
uint32_t size; /* The total number of bytes read so far */
uint32_t max; /* The maximum number of entries to read */
} sizeCounter;
typedef struct in_memory_key_value_t inMemoryKeyValue;
typedef struct in_memory_key_value_t {
uint32_t key; /* Value of the key */
void *data; /* Pointer to the data */
inMemoryKeyValue *next; /* Pointer to the next item or NULL if end */
} inMemoryKeyValue;
#ifndef FIFTYONE_DEGREES_MEMORY_ONLY
bool fiftyoneDegreesCollectionGetIsMemoryOnly() { return false; }
#else
bool fiftyoneDegreesCollectionGetIsMemoryOnly() { return true; }
#endif
/**
* The following methods are used to release an item when it has been finished
* with. Each release method differs depending on the implementation of the
* collection. For example; releasing an item from a cache collection will
* make it eligible for eviction or releasing an item from a file collection
* will simply free the memory it used.
*/
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4100)
#endif
/**
* Used by memory collection where there is nothing to be done when the release
* occurs because the memory it uses is only freed when the collection is
* freed.
*/
static void releaseNothing(Item *item) {
assert(item != NULL);
}
#ifdef _MSC_VER
#pragma warning (default: 4100)
#pragma warning (pop)
#endif
/**
* Releases an item held in a cache collection so that it is eligible for
* eviction from the cache.
* @param item to be released and eligible for eviction from the cache. The
* handle must reference a cache node.
*/
static void releaseCache(Item *item) {
if (item->handle != NULL) {
CacheRelease((CacheNode*)item->handle);
item->handle = NULL;
}
}
/**
* Frees the memory used by the file item and resets the item ready to be used
* in a subsequent request.
* @param item to be released with a handle set to the memory to be freed.
*/
static void releaseFile(Item *item) {
if (item->handle != NULL) {
Free(item->handle);
DataReset(&item->data);
item->handle = NULL;
item->collection = NULL;
}
}
/**
* Calls the collection the item is assigned to's release method. The item may
* be of any type and must have the collection reference set when it is
* initialised.
* @param item to be released with it's collection reference set.
*/
static void releasePartial(Item *item) {
if (item->handle != NULL &&
item->collection != NULL) {
COLLECTION_RELEASE(item->collection, item);
}
}
static void freeCollection(Collection *collection) {
Free(collection->state);
Free(collection);
}
static void freeMemoryCollection(Collection *collection) {
CollectionMemory *memory = (CollectionMemory*)collection->state;
if (collection->next != NULL) {
collection->next->freeCollection(collection->next);
}
if (memory->memoryToFree != NULL) {
Free(memory->memoryToFree);
}
freeCollection(collection);
}
#ifndef FIFTYONE_DEGREES_MEMORY_ONLY
static void freeFileCollection(Collection *collection) {
freeCollection(collection);
}
static void freeCacheCollection(Collection *collection) {
Collection *loader;
CollectionCache *cache = (CollectionCache*)collection->state;
if (cache->cache != NULL) {
// Free the loader collection used by the cache.
loader = (Collection*)cache->cache->loaderState;
loader->freeCollection(loader);
// Free the cache itself.
CacheFree(cache->cache);
}
freeCollection(collection);
}
#endif
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4100)
#endif
static void* getMemoryVariable(
Collection *collection,
uint32_t offset,
Item *item,
Exception *exception) {
CollectionMemory *memory = (CollectionMemory*)collection->state;
if (offset < collection->size) {
item->data.ptr = memory->firstByte + offset;
assert(item->data.ptr < memory->lastByte);
item->collection = collection;
}
else {
GET_EXCEPTION_SET(COLLECTION_OFFSET_OUT_OF_RANGE);
}
return item->data.ptr;
}
static void* getMemoryFixed(
Collection *collection,
uint32_t index,
Item *item,
Exception *exception) {
CollectionMemory *memory = (CollectionMemory*)collection->state;
if (index < collection->count) {
item->data.ptr = memory->firstByte + ((uint64_t)index * collection->elementSize);
assert(item->data.ptr < memory->lastByte);
item->collection = collection;
}
else {
GET_EXCEPTION_SET(COLLECTION_INDEX_OUT_OF_RANGE);
}
return item->data.ptr;
}
#ifdef _MSC_VER
#pragma warning (default: 4100)
#pragma warning (pop)
#endif
#ifndef FIFTYONE_DEGREES_MEMORY_ONLY
static void* getPartialVariable(
Collection *collection,
uint32_t offset,
Item *item,
Exception *exception) {
CollectionMemory *memory = (CollectionMemory*)collection->state;
if (offset < collection->size) {
item->data.ptr = memory->firstByte + offset;
assert(item->data.ptr < memory->lastByte);
item->data.allocated = 0;
item->data.used = 0;
item->handle = NULL;
item->collection = collection;
}
else if (collection->next != NULL) {
collection->next->get(collection->next, offset, item, exception);
}
else {
GET_EXCEPTION_SET(COLLECTION_OFFSET_OUT_OF_RANGE);
}
return item->data.ptr;
}
static void* getPartialFixed(
Collection *collection,
uint32_t index,
Item *item,
Exception *exception) {
CollectionMemory *memory = (CollectionMemory*)collection->state;
if (index < collection->count) {
item->data.ptr = memory->firstByte + ((uint64_t)index * collection->elementSize);
assert(item->data.ptr < memory->lastByte);
item->data.allocated = 0;
item->data.used = collection->elementSize;
item->handle = NULL;
item->collection = collection;
}
else if (collection->next != NULL) {
collection->next->get(collection->next, index, item, exception);
}
else {
GET_EXCEPTION_SET(COLLECTION_INDEX_OUT_OF_RANGE);
}
return item->data.ptr;
}
static void* getFile(
Collection *collection,
uint32_t indexOrOffset,
Item *item,
Exception *exception) {
CollectionFile *file = (CollectionFile*)collection->state;
void *ptr = NULL;
// Set the item's handle to the pointer at the start of the data item's
// data structure following the read operation.
item->handle = file->read(file, indexOrOffset, &item->data, exception);
// If the read operation returned a pointer to the item's data then set
// the collection for the item to the collection used so that it is
// available when the memory used by the item is released. If the pointer
// could not be retrieved then set the collection to NULL indicating that
// no memory free operation is needed.
if (EXCEPTION_OKAY && item->handle != NULL) {
item->collection = collection;
ptr = item->data.ptr;
}
return ptr;
}
/**
* Gets an item from the cache pointed to by the collection's state. If the
* cache get method returns null or the item fetched is invalid, then the
* item's data is unset and null returned.
* @param collection to use to retrieve the item. Must be of type cache.
* @param key of the item to be retrieved.
* @param item data structure to place the value in.
* @param exception pointer to an exception data structure to be used if an
* exception occurs. See exceptions.h.
* @return a pointer to the data retrieved, or NULL if no data retrieved.
*/
static void* getFromCache(
Collection *collection,
uint32_t key,
Item *item,
Exception *exception) {
void *ptr = NULL;
// Set the collection in the item passed to ensure it can be released when
// the caller finishes with it.
item->collection = collection;
// Get the node from the cache or the loader. This method doesn't need
// to know which.
CollectionCache *cache = (CollectionCache*)collection->state;
CacheNode *node = CacheGet(cache->cache, &key, exception);
if (EXCEPTION_OKAY && node != NULL) {
// Set the handle in the item passed to ensure it can be released when
// the caller finishes with it.
item->handle = node;
// If the node was loaded correctly then set the item data to the
// pointer in the node's data structure.
if (node->data.ptr != NULL &&
node->data.used > 0) {
item->data = node->data;
ptr = item->data.ptr;
}
}
else {
item->data.used = 0;
item->handle = NULL;
}
return ptr;
}
/**
* Loads the data for the key into the data structure passed to the method.
* @param state information used for the load operation.
* @param data structure to be used to store the data loaded.
* @param key for the item in the collection to be loaded.
* @param exception pointer to an exception data structure to be used if an
* exception occurs. See exceptions.h.
*/
static void loaderCache(
const void *state,
Data *data,
const void *key,
Exception *exception) {
Item item;
Collection *collection = (Collection*)state;
// Set the data used to 0 in case the read operation fails for any reason.
data->used = 0;
// Get the item from the source collection.
DataReset(&item.data);
if (collection->get(
collection,
*(uint32_t*)key,
&item,
exception) != NULL &&
EXCEPTION_OKAY) {
// If the item from the source collection has bytes then copy them into
// the cache node item ensuring sufficient memory is allocated first.
if (item.data.used > 0 &&
DataMalloc(data, item.data.allocated) != NULL) {
// Copy the data from the collection into the cache.
if (memcpy(
data->ptr,
item.data.ptr,
item.data.used) == data->ptr) {
// Set the number of used bytes to match the loaded item.
data->used = item.data.used;
}
}
// Release the item from the source collection.
COLLECTION_RELEASE(collection, &item);
}
}
#endif
static void iterateCollection(
Collection *collection,
void *state,
CollectionIterateMethod callback,
Exception *exception) {
Item item;
uint32_t nextIndexOrOffset = 0;
DataReset(&item.data);
while (nextIndexOrOffset < collection->size &&
collection->get(
collection,
nextIndexOrOffset,
&item,
exception) != NULL &&
EXCEPTION_OKAY &&
// There is valid data for this iteration. Call the callback method.
callback(state, nextIndexOrOffset, &item.data)) {
// Set the next index or offset.
if (collection->elementSize != 0) {
nextIndexOrOffset++;
}
else {
nextIndexOrOffset += item.data.used;
}
// Release the item just retrieved.
COLLECTION_RELEASE(collection, &item);
}
// Release the final item that wasn't released in the while loop.
// This uses the actual method pointer instead of the macro and is the only
// place this is necessary. This is the case because even when MEMORY_ONLY
// is specified, a file collection can still exist internally while
// creating a memory collection, so the real method must be called here to
// ensure any allocated memory is freed.
if (collection->release != NULL) {
collection->release(&item);
}
}
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4100)
#endif
static bool callbackLoadedSize(
void *state,
uint32_t key,
void *data) {
sizeCounter *tracker = (sizeCounter*)state;
tracker->size += ((Data*)data)->used;
tracker->count++;
return tracker->count < tracker->max;
}
#ifdef _MSC_VER
#pragma warning (default: 4100)
#pragma warning (pop)
#endif
static sizeCounter calculateLoadedSize(
Collection *collection,
const uint32_t count,
Exception *exception) {
sizeCounter counter;
counter.max = count;
// Can the size be worked out from the element size and the count?
if (collection->elementSize != 0) {
counter.count = count > collection->count ? collection->count : count;
counter.size = counter.count * collection->elementSize;
}
// Can the size be worked out from the collection itself?
else if (collection->size < count) {
counter.count = 0;
counter.size = collection->size;
}
// If none of the previous options can work then iterate the collection
// reading all the values to work out it's size.
else {
counter.count = 0;
counter.size = 0;
iterateCollection(collection, &counter, callbackLoadedSize, exception);
}
return counter;
}
static Collection* createCollection(
size_t sizeOfState,
CollectionHeader *header) {
Collection *collection = (Collection*)Malloc(sizeof(Collection));
if (collection != NULL) {
collection->state = Malloc(sizeOfState);
if (collection->state != NULL) {
collection->next = NULL;
collection->elementSize = header->count == 0 ?
0 : header->length / header->count;
collection->size = header->length;
collection->count = header->count;
}
else {
Free(collection);
collection = NULL;
}
}
return collection;
}
#ifndef FIFTYONE_DEGREES_MEMORY_ONLY
static CollectionFile* readFile(CollectionFile *fileCollection, FILE *file) {
// Set the count of items if not already set and the elements are of a
// fixed size.
if (fileCollection->collection->count == 0 &&
fileCollection->collection->elementSize > 0) {
fileCollection->collection->count = fileCollection->collection->size /
fileCollection->collection->elementSize;
}
// Record the offset in the source file to the collection.
fileCollection->offset = ftell(file);
// Move the file handle past the collection.
if (fseek(file, fileCollection->collection->size, SEEK_CUR) != 0) {
return NULL;
}
return fileCollection;
}
static Collection* createFromFile(
FILE *file,
FilePool *reader,
CollectionHeader *header,
CollectionFileRead read) {
// Allocate the memory for the collection and file implementation.
Collection *collection = createCollection(
sizeof(CollectionFile),
header);
CollectionFile *fileCollection = (CollectionFile*)collection->state;
fileCollection->collection = collection;
fileCollection->reader = reader;
// Use the read method provided to get records from the file.
fileCollection->read = read;
// Read the file data into the structure.
if (readFile(fileCollection, file) == NULL) {
freeFileCollection(collection);
return NULL;
}
// Set the get and release functions for the collection.
collection->get = getFile;
collection->release = releaseFile;
collection->freeCollection = freeFileCollection;
return collection;
}
static Collection* createFromFilePartial(
FILE *file,
FilePool *reader,
CollectionHeader *header,
int count,
CollectionFileRead read) {
EXCEPTION_CREATE;
sizeCounter counter;
// Create a file collection to populate the memory collection.
Collection *source = createFromFile(file, reader, header, read);
// Allocate the memory for the collection and implementation.
Collection *collection = createCollection(sizeof(CollectionFile), header);
CollectionMemory *memory = (CollectionMemory*)collection->state;
memory->memoryToFree = NULL;
memory->collection = collection;
// Get the number of bytes that need to be loaded into memory.
counter = calculateLoadedSize(source, count, exception);
if (EXCEPTION_FAILED) {
freeMemoryCollection(collection);
source->freeCollection(source);
return NULL;
}
memory->collection->count = counter.count;
memory->collection->size = counter.size;
// Allocate sufficient memory for the data to be stored in.
memory->firstByte = (byte*)Malloc(memory->collection->size);
if (memory->firstByte == NULL) {
freeMemoryCollection(collection);
source->freeCollection(source);
return NULL;
}
memory->memoryToFree = memory->firstByte;
// Position the file reader at the start of the collection.
if (fseek(file, header->startPosition, SEEK_SET) != 0) {
freeMemoryCollection(collection);
source->freeCollection(source);
return NULL;
}
// Read the portion of the file into memory.
if (fread(memory->firstByte, 1, memory->collection->size, file) !=
memory->collection->size) {
freeMemoryCollection(collection);
source->freeCollection(source);
return NULL;
}
// Move the file position to the byte after the collection.
if (fseek(file, source->size - memory->collection->size, SEEK_CUR) != 0) {
freeMemoryCollection(collection);
source->freeCollection(source);
return NULL;
}
// Set the last byte to enable checking for invalid requests.
memory->lastByte = memory->firstByte + memory->collection->size;
// Set the getter to a method that will check for another collection
// if the memory collection does not contain the entry.
if (memory->collection->elementSize != 0) {
collection->get = getPartialFixed;
}
else {
collection->get = getPartialVariable;
}
if (fiftyoneDegreesCollectionGetIsMemoryOnly()) {
collection->release = NULL;
}
else {
collection->release = releasePartial;
}
collection->freeCollection = freeMemoryCollection;
// Finally free the file collection which is no longer needed.
source->freeCollection(source);
return collection;
}
static Collection* createFromFileCached(
FILE *file,
FilePool *reader,
CollectionHeader *header,
uint32_t capacity,
uint16_t concurrency,
CollectionFileRead read) {
// Allocate the memory for the collection and implementation.
Collection *collection = createCollection(sizeof(CollectionFile), header);
CollectionCache *cache = (CollectionCache*)collection->state;
cache->cache = NULL;
// Create the file collection to be used with the cache.
cache->source = createFromFile(file, reader, header, read);
if (cache->source == NULL) {
freeCacheCollection(collection);
return NULL;
}
// Create the cache to be used with the collection.
cache->cache = CacheCreate(
capacity,
concurrency,
loaderCache,
fiftyoneDegreesCacheHash32,
cache->source);
if (cache->cache == NULL) {
freeCacheCollection(collection);
return NULL;
}
// Copy the source information to the cache collection.
collection->count = cache->source->count;
collection->size = cache->source->size;
// Set the get method for the collection.
collection->get = getFromCache;
collection->release = releaseCache;
collection->freeCollection = freeCacheCollection;
return collection;
}
/**
* Either the first collection does not contain any in memory items, or there
* is a need for a secondary collection to be used if the first does not
* contain any items. Returns the second collection, or NULL if there is no
* need for one.
*/
static Collection* createFromFileSecond(
FILE *file,
FilePool *reader,
const CollectionConfig *config,
CollectionHeader header,
CollectionFileRead read) {
// Return the file position to the start of the collection ready to
// read the next collection.
if (fseek(file, header.startPosition, SEEK_SET) == 0) {
// Choose between the cached or file based collection.
if (config->capacity > 0 && config->concurrency > 0) {
// If the collection should have a cache then set the next
// collection to be cache based.
return createFromFileCached(
file,
reader,
&header,
config->capacity,
config->concurrency,
read);
}
else {
// If there is no cache then the entries will be fetched
// directly from the source file.
return createFromFile(file, reader, &header, read);
}
}
return NULL;
}
#endif
fiftyoneDegreesCollectionHeader fiftyoneDegreesCollectionHeaderFromMemory(
fiftyoneDegreesMemoryReader *reader,
uint32_t elementSize,
bool isCount) {
CollectionHeader header;
if (isCount) {
// The next integer is the count of items in the data structure.
header.count = *(uint32_t*)reader->current;
header.length = header.count * elementSize;
}
else {
// The next integer is the size of the data structure.
header.length = *(const uint32_t*)reader->current;
header.count = elementSize > 0 ? header.length / elementSize : 0;
}
// Advance the memory reader and record the start of the collection.
if (MemoryAdvance(reader, sizeof(uint32_t))) {
header.startPosition = (uint32_t)(reader->current - reader->startByte);
}
else {
header.startPosition = 0;
}
return header;
}
fiftyoneDegreesCollection* fiftyoneDegreesCollectionCreateFromMemory(
fiftyoneDegreesMemoryReader *reader,
fiftyoneDegreesCollectionHeader header) {
// Validate the header and the reader are in sync at the correct position.
if ((uint32_t)(reader->current - reader->startByte) !=
header.startPosition) {
return NULL;
}
// Allocate the memory for the collection and implementation.
Collection *collection = createCollection(
sizeof(CollectionMemory),
&header);
CollectionMemory *memory = (CollectionMemory*)collection->state;
// Configure the fields for the collection.
memory->collection = collection;
memory->memoryToFree = NULL;
memory->collection->elementSize = header.count == 0 ?
0 : header.length / header.count;
memory->firstByte = reader->current;
memory->lastByte = memory->firstByte + memory->collection->size;
// Assign the get and release functions for the collection.
if (memory->collection->elementSize != 0) {
collection->get = getMemoryFixed;
memory->collection->count = memory->collection->size /
memory->collection->elementSize;
}
else {
collection->get = getMemoryVariable;
}
if (fiftyoneDegreesCollectionGetIsMemoryOnly()) {
collection->release = NULL;
}
else {
collection->release = releaseNothing;
}
collection->freeCollection = freeMemoryCollection;
// Move over the structure and the size integer checking the move
// operation was successful.
if (MemoryAdvance(
reader,
memory->collection->size) == false) {
collection->freeCollection(collection);
collection = NULL;
}
return collection;
}
fiftyoneDegreesCollectionHeader fiftyoneDegreesCollectionHeaderFromFile(
FILE *file,
uint32_t elementSize,
bool isCount) {
fiftyoneDegreesCollectionHeader header;
uint32_t sizeOrCount;
// Get the size or count of the data structure in bytes.
if (fread((void*)&sizeOrCount, sizeof(uint32_t), 1, file) == 1) {
if (isCount) {
// The integer is the count of items in the data structure.
header.count = sizeOrCount;
header.length = header.count * elementSize;
}
else {
// The integer is the size of the data structure.
header.length = sizeOrCount;
header.count = elementSize > 0 ? header.length / elementSize : 0;
}
header.startPosition = (uint32_t)ftell(file);
}
else {
header.startPosition = 0;
}
return header;
}
#if defined(_MSC_VER) && defined(FIFTYONE_DEGREES_MEMORY_ONLY)
#pragma warning (disable: 4100)
#endif
fiftyoneDegreesCollection* fiftyoneDegreesCollectionCreateFromFile(
FILE *file,
fiftyoneDegreesFilePool *reader,
const fiftyoneDegreesCollectionConfig *config,
fiftyoneDegreesCollectionHeader header,
fiftyoneDegreesCollectionFileRead read) {
Collection *result = NULL;
#ifndef FIFTYONE_DEGREES_MEMORY_ONLY
Collection **next = &result;
if (config->loaded > 0) {
// If the collection should be partially loaded into memory set the
// first collection to be a memory collection with the relevant number
// of entries loaded.
result = createFromFilePartial(
file,
reader,
&header,
config->loaded,
read);
if (result == NULL) {
// The collection could not be created from file.
return NULL;
}
// Point to the next collection to create.
next = &result->next;
}
if (result == NULL || (
result->count == config->loaded &&
(long)result->size < (long)(ftell(file) - header.startPosition))) {
// Create the next collection if one is needed.
*next = createFromFileSecond(file, reader, config, header, read);
if (*next == NULL) {
// If the secondary collection could not be generated then free
// the primary one and return NULL to indicate that the collection
// could not be created.
if (result != NULL) {
result->freeCollection(result);
}
result = NULL;
}
}
else {
// The partial collection supports all items so no need for secondary
// collections.
*next = NULL;
}
#else
byte *data = (byte*)Malloc(header.length * sizeof(byte));
MemoryReader memory;
memory.current = data;
if (memory.current == NULL) {
Free(data);
return NULL;
}
memory.startByte = memory.current;
memory.length = (long)header.length;
memory.lastByte = memory.current + memory.length;
// Position the file reader at the start of the collection.
if (fseek(file, header.startPosition, SEEK_SET) != 0) {
free(data);
return NULL;
}
// Read the portion of the file into memory.
if (fread(memory.startByte, 1, header.length, file) != header.length) {
free(data);
return NULL;
}
header.startPosition = 0;
result = CollectionCreateFromMemory(&memory, header);
if (result == NULL) {
free(data);
return NULL;
}
((CollectionMemory*)result->state)->memoryToFree = data;
#endif
return result;
}
#if defined(_MSC_VER) && defined(FIFTYONE_DEGREES_MEMORY_ONLY)
#pragma warning (default: 4100)
#endif
fiftyoneDegreesFileHandle* fiftyoneDegreesCollectionReadFilePosition(
const fiftyoneDegreesCollectionFile *file,
uint32_t offset,
fiftyoneDegreesException *exception) {
FileHandle *handle = NULL;
// If the offset is outside the size of the collection then return NULL.
if (offset < file->collection->size) {
// Get the next free handle from the list of readers.
handle = FileHandleGet(file->reader, exception);
// The concurrency setting does not allow for another file handle, so
// return NULL.
if (handle != NULL && EXCEPTION_OKAY) {
// Move to the start of the record in the file handling success or
// failure of the operation via the status code.
if (fseek(handle->file, file->offset + offset, SEEK_SET) != 0) {
// Release the handle as the operation failed.
FileHandleRelease(handle);
EXCEPTION_SET(COLLECTION_FILE_SEEK_FAIL);
handle = NULL;
}
}
}
else {
EXCEPTION_SET(COLLECTION_OFFSET_OUT_OF_RANGE);
}
return handle;
}
void* fiftyoneDegreesCollectionReadFileFixed(
const fiftyoneDegreesCollectionFile *file,
uint32_t index,
fiftyoneDegreesData *data,
fiftyoneDegreesException *exception) {
void *ptr = NULL;
FileHandle *handle = NULL;
uint32_t offset = index * file->collection->elementSize;
// Indicate that no data is being used at the start of the operation.
data->used = 0;
// If the index is outside the range of the collection then return NULL.
if (index < file->collection->count) {
// Get the handle positioned at the start of the item to be read.
handle = CollectionReadFilePosition(file, offset, exception);
if (handle != NULL && EXCEPTION_OKAY) {
// Ensure sufficient memory is allocated for the item being read.
if (DataMalloc(data, file->collection->elementSize) != NULL) {