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 "SolidSyslogStdAtomicCounter.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : : #include <stdint.h>
10 : :
11 : : #include "SolidSyslogError.h"
12 : : #include "SolidSyslogErrorCategory.h"
13 : : #include "SolidSyslogNullAtomicCounter.h"
14 : : #include "SolidSyslogPoolAllocator.h"
15 : : #include "SolidSyslogPrival.h"
16 : : #include "SolidSyslogStdAtomicCounterErrors.h"
17 : : #include "SolidSyslogStdAtomicCounterPrivate.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogAtomicCounter;
21 : :
22 : : static inline size_t StdAtomicCounter_IndexFromHandle(const struct SolidSyslogAtomicCounter* base);
23 : : static inline void StdAtomicCounter_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool StdAtomicCounter_InUse[SOLIDSYSLOG_ATOMIC_COUNTER_POOL_SIZE];
26 : : static struct SolidSyslogStdAtomicCounter StdAtomicCounter_Pool[SOLIDSYSLOG_ATOMIC_COUNTER_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator StdAtomicCounter_Allocator = {
28 : : StdAtomicCounter_InUse,
29 : : SOLIDSYSLOG_ATOMIC_COUNTER_POOL_SIZE
30 : : };
31 : :
32 : 49 : struct SolidSyslogAtomicCounter* SolidSyslogStdAtomicCounter_Create(void)
33 : : {
34 : 49 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&StdAtomicCounter_Allocator);
35 : 49 : struct SolidSyslogAtomicCounter* handle = SolidSyslogNullAtomicCounter_Get();
36 [ + + ]: 49 : if (SolidSyslogPoolAllocator_IndexIsValid(&StdAtomicCounter_Allocator, index) == true)
37 : : {
38 : 45 : SolidSyslogStdAtomicCounter_Initialise(&StdAtomicCounter_Pool[index].Base);
39 : 45 : handle = &StdAtomicCounter_Pool[index].Base;
40 : : }
41 : : else
42 : : {
43 : 4 : StdAtomicCounter_Report(
44 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
45 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
46 : : SOLIDSYSLOG_ATOMIC_COUNTER_ERROR_POOL_EXHAUSTED
47 : : );
48 : : }
49 : 49 : return handle;
50 : : }
51 : :
52 : 52 : void SolidSyslogStdAtomicCounter_Destroy(struct SolidSyslogAtomicCounter* base)
53 : : {
54 : 52 : size_t index = StdAtomicCounter_IndexFromHandle(base);
55 : 52 : bool released =
56 [ + + + + ]: 98 : SolidSyslogPoolAllocator_IndexIsValid(&StdAtomicCounter_Allocator, index) &&
57 : 46 : SolidSyslogPoolAllocator_FreeIfInUse(&StdAtomicCounter_Allocator, index, StdAtomicCounter_CleanupAtIndex, NULL);
58 [ + + ]: 52 : if (!released)
59 : : {
60 : 7 : StdAtomicCounter_Report(
61 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
62 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
63 : : SOLIDSYSLOG_ATOMIC_COUNTER_ERROR_UNKNOWN_DESTROY
64 : : );
65 : : }
66 : 52 : }
67 : :
68 : 52 : static inline size_t StdAtomicCounter_IndexFromHandle(const struct SolidSyslogAtomicCounter* base)
69 : : {
70 : 52 : size_t result = SOLIDSYSLOG_ATOMIC_COUNTER_POOL_SIZE;
71 [ + + ]: 58 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_ATOMIC_COUNTER_POOL_SIZE; poolIndex++)
72 : : {
73 [ + + ]: 52 : if (base == &StdAtomicCounter_Pool[poolIndex].Base)
74 : : {
75 : 46 : result = poolIndex;
76 : 46 : break;
77 : : }
78 : : }
79 : 52 : return result;
80 : : }
81 : :
82 : 45 : static inline void StdAtomicCounter_CleanupAtIndex(size_t index, void* context)
83 : : {
84 : : (void) context;
85 : 45 : SolidSyslogStdAtomicCounter_Cleanup(&StdAtomicCounter_Pool[index].Base);
86 : 45 : }
|