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 "SolidSyslogMetaSd.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 "SolidSyslogMetaSdErrors.h"
14 : : #include "SolidSyslogMetaSdPrivate.h"
15 : : #include "SolidSyslogNullSd.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogStructuredData;
21 : :
22 : : static bool MetaSd_IsValidConfig(const struct SolidSyslogMetaSdConfig* config);
23 : : static inline size_t MetaSd_IndexFromHandle(const struct SolidSyslogStructuredData* base);
24 : : static inline void MetaSd_CleanupAtIndex(size_t index, void* context);
25 : :
26 : : static bool MetaSd_InUse[SOLIDSYSLOG_META_SD_POOL_SIZE];
27 : : static struct SolidSyslogMetaSd MetaSd_Pool[SOLIDSYSLOG_META_SD_POOL_SIZE];
28 : : static struct SolidSyslogPoolAllocator MetaSd_Allocator = {MetaSd_InUse, SOLIDSYSLOG_META_SD_POOL_SIZE};
29 : :
30 : 192 : struct SolidSyslogStructuredData* SolidSyslogMetaSd_Create(const struct SolidSyslogMetaSdConfig* config)
31 : : {
32 : 192 : struct SolidSyslogStructuredData* result = SolidSyslogNullSd_Get();
33 [ + + ]: 192 : if (MetaSd_IsValidConfig(config))
34 : : {
35 : 187 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&MetaSd_Allocator);
36 [ + + ]: 187 : if (SolidSyslogPoolAllocator_IndexIsValid(&MetaSd_Allocator, index))
37 : : {
38 : 185 : SolidSyslogMetaSd_Initialise(&MetaSd_Pool[index].Base, config);
39 : 185 : result = &MetaSd_Pool[index].Base;
40 : : }
41 : : else
42 : : {
43 : 2 : MetaSd_Report(
44 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
45 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
46 : : SOLIDSYSLOG_META_SD_ERROR_POOL_EXHAUSTED
47 : : );
48 : : }
49 : : }
50 : 192 : return result;
51 : : }
52 : :
53 : 192 : static bool MetaSd_IsValidConfig(const struct SolidSyslogMetaSdConfig* config)
54 : : {
55 : 192 : bool valid = false;
56 [ + + ]: 192 : if (config == NULL)
57 : : {
58 : 2 : MetaSd_Report(
59 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
60 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
61 : : SOLIDSYSLOG_META_SD_ERROR_NULL_CONFIG
62 : : );
63 : : }
64 [ + + ]: 190 : else if (config->Counter == NULL)
65 : : {
66 : 3 : MetaSd_Report(
67 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
68 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
69 : : SOLIDSYSLOG_META_SD_ERROR_NULL_COUNTER
70 : : );
71 : : }
72 : : else
73 : : {
74 : 187 : valid = true;
75 : : }
76 : 192 : return valid;
77 : : }
78 : :
79 : 193 : void SolidSyslogMetaSd_Destroy(struct SolidSyslogStructuredData* base)
80 : : {
81 : 193 : size_t index = MetaSd_IndexFromHandle(base);
82 [ + + + + ]: 379 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&MetaSd_Allocator, index) &&
83 : 186 : SolidSyslogPoolAllocator_FreeIfInUse(&MetaSd_Allocator, index, MetaSd_CleanupAtIndex, NULL);
84 [ + + ]: 193 : if (!released)
85 : : {
86 : 8 : MetaSd_Report(
87 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
88 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
89 : : SOLIDSYSLOG_META_SD_ERROR_UNKNOWN_DESTROY
90 : : );
91 : : }
92 : 193 : }
93 : :
94 : 193 : static inline size_t MetaSd_IndexFromHandle(const struct SolidSyslogStructuredData* base)
95 : : {
96 : 193 : size_t result = SOLIDSYSLOG_META_SD_POOL_SIZE;
97 [ + + ]: 200 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_META_SD_POOL_SIZE; poolIndex++)
98 : : {
99 [ + + ]: 193 : if (base == &MetaSd_Pool[poolIndex].Base)
100 : : {
101 : 186 : result = poolIndex;
102 : 186 : break;
103 : : }
104 : : }
105 : 193 : return result;
106 : : }
107 : :
108 : 185 : static inline void MetaSd_CleanupAtIndex(size_t index, void* context)
109 : : {
110 : : (void) context;
111 : 185 : SolidSyslogMetaSd_Cleanup(&MetaSd_Pool[index].Base);
112 : 185 : }
|