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 "SolidSyslogBlockSequencePrivate.h"
9 : : #include "SolidSyslogPoolAllocator.h"
10 : : #include "SolidSyslogTunables.h"
11 : :
12 : : static inline size_t BlockSequence_IndexFromHandle(const struct SolidSyslogBlockSequence* blockSequence);
13 : : static inline void BlockSequence_CleanupAtIndex(size_t index, void* context);
14 : :
15 : : static bool BlockSequence_InUse[SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE];
16 : : static struct SolidSyslogBlockSequence BlockSequence_Pool[SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE];
17 : : static struct SolidSyslogPoolAllocator BlockSequence_Allocator = {
18 : : BlockSequence_InUse,
19 : : SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE
20 : : };
21 : :
22 : 19223 : struct SolidSyslogBlockSequence* SolidSyslogBlockSequence_Create(const struct SolidSyslogBlockSequenceConfig* config)
23 : : {
24 : 19223 : struct SolidSyslogBlockSequence* result = NULL;
25 : 19223 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&BlockSequence_Allocator);
26 [ + + ]: 19223 : if (SolidSyslogPoolAllocator_IndexIsValid(&BlockSequence_Allocator, index))
27 : : {
28 : 19219 : SolidSyslogBlockSequence_Initialise(&BlockSequence_Pool[index], config);
29 : 19219 : result = &BlockSequence_Pool[index];
30 : : }
31 : 19223 : return result;
32 : : }
33 : :
34 : 19235 : void SolidSyslogBlockSequence_Destroy(struct SolidSyslogBlockSequence* blockSequence)
35 : : {
36 [ + + ]: 19235 : if (blockSequence != NULL)
37 : : {
38 : 19220 : size_t index = BlockSequence_IndexFromHandle(blockSequence);
39 [ + + ]: 19220 : if (SolidSyslogPoolAllocator_IndexIsValid(&BlockSequence_Allocator, index))
40 : : {
41 : : (void
42 : 19219 : ) SolidSyslogPoolAllocator_FreeIfInUse(&BlockSequence_Allocator, index, BlockSequence_CleanupAtIndex, NULL);
43 : : }
44 : : }
45 : 19235 : }
46 : :
47 : 19220 : static inline size_t BlockSequence_IndexFromHandle(const struct SolidSyslogBlockSequence* blockSequence)
48 : : {
49 : 19220 : size_t result = SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE;
50 [ + + ]: 19221 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_BLOCK_STORE_POOL_SIZE; poolIndex++)
51 : : {
52 [ + + ]: 19220 : if (blockSequence == &BlockSequence_Pool[poolIndex])
53 : : {
54 : 19219 : result = poolIndex;
55 : 19219 : break;
56 : : }
57 : : }
58 : 19220 : return result;
59 : : }
60 : :
61 : 19219 : static inline void BlockSequence_CleanupAtIndex(size_t index, void* context)
62 : : {
63 : : (void) context;
64 : 19219 : SolidSyslogBlockSequence_Cleanup(&BlockSequence_Pool[index]);
65 : 19219 : }
|