Branch data Line data Source code
1 : : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
2 : : * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
3 : : */
4 : :
5 : : #include <stdbool.h>
6 : : #include <stddef.h>
7 : :
8 : : #include "SolidSyslogRecordStorePrivate.h"
9 : : #include "SolidSyslogPoolAllocator.h"
10 : : #include "SolidSyslogTunables.h"
11 : :
12 : : struct SolidSyslogSecurityPolicy;
13 : :
14 : : static inline size_t RecordStore_IndexFromHandle(const struct SolidSyslogRecordStore* recordStore);
15 : : static inline void RecordStore_CleanupAtIndex(size_t index, void* context);
16 : :
17 : : static bool RecordStore_InUse[SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE];
18 : : static struct SolidSyslogRecordStore RecordStore_Pool[SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE];
19 : : static struct SolidSyslogPoolAllocator RecordStore_Allocator = {RecordStore_InUse, SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE};
20 : :
21 : 19194 : struct SolidSyslogRecordStore* SolidSyslogRecordStore_Create(struct SolidSyslogSecurityPolicy* securityPolicy)
22 : : {
23 : 19194 : struct SolidSyslogRecordStore* result = NULL;
24 : 19194 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&RecordStore_Allocator);
25 [ + + ]: 19194 : if (SolidSyslogPoolAllocator_IndexIsValid(&RecordStore_Allocator, index))
26 : : {
27 : 19191 : SolidSyslogRecordStore_Initialise(&RecordStore_Pool[index], securityPolicy);
28 : 19191 : result = &RecordStore_Pool[index];
29 : : }
30 : 19194 : return result;
31 : : }
32 : :
33 : 19208 : void SolidSyslogRecordStore_Destroy(struct SolidSyslogRecordStore* recordStore)
34 : : {
35 [ + + ]: 19208 : if (recordStore != NULL)
36 : : {
37 : 19192 : size_t index = RecordStore_IndexFromHandle(recordStore);
38 [ + + ]: 19192 : if (SolidSyslogPoolAllocator_IndexIsValid(&RecordStore_Allocator, index))
39 : : {
40 : : (void
41 : 19191 : ) SolidSyslogPoolAllocator_FreeIfInUse(&RecordStore_Allocator, index, RecordStore_CleanupAtIndex, NULL);
42 : : }
43 : : }
44 : 19208 : }
45 : :
46 : 19192 : static inline size_t RecordStore_IndexFromHandle(const struct SolidSyslogRecordStore* recordStore)
47 : : {
48 : 19192 : size_t result = SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE;
49 [ + + ]: 19193 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE; poolIndex++)
50 : : {
51 [ + + ]: 19192 : if (recordStore == &RecordStore_Pool[poolIndex])
52 : : {
53 : 19191 : result = poolIndex;
54 : 19191 : break;
55 : : }
56 : : }
57 : 19192 : return result;
58 : : }
59 : :
60 : 19191 : static inline void RecordStore_CleanupAtIndex(size_t index, void* context)
61 : : {
62 : : (void) context;
63 : 19191 : SolidSyslogRecordStore_Cleanup(&RecordStore_Pool[index]);
64 : 19191 : }
|