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 "SolidSyslogOriginSd.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 "SolidSyslogNullSd.h"
14 : : #include "SolidSyslogOriginSdErrors.h"
15 : : #include "SolidSyslogOriginSdPrivate.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogStructuredData;
21 : :
22 : : static inline size_t OriginSd_IndexFromHandle(const struct SolidSyslogStructuredData* base);
23 : : static inline void OriginSd_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool OriginSd_InUse[SOLIDSYSLOG_ORIGIN_SD_POOL_SIZE];
26 : : static struct SolidSyslogOriginSd OriginSd_Pool[SOLIDSYSLOG_ORIGIN_SD_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator OriginSd_Allocator = {OriginSd_InUse, SOLIDSYSLOG_ORIGIN_SD_POOL_SIZE};
28 : :
29 : 132 : struct SolidSyslogStructuredData* SolidSyslogOriginSd_Create(const struct SolidSyslogOriginSdConfig* config)
30 : : {
31 : 132 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&OriginSd_Allocator);
32 : 132 : struct SolidSyslogStructuredData* handle = SolidSyslogNullSd_Get();
33 [ + + ]: 132 : if (SolidSyslogPoolAllocator_IndexIsValid(&OriginSd_Allocator, index))
34 : : {
35 : 130 : SolidSyslogOriginSd_Initialise(&OriginSd_Pool[index].Base, config);
36 : 130 : handle = &OriginSd_Pool[index].Base;
37 : : }
38 : : else
39 : : {
40 : 2 : OriginSd_Report(
41 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
42 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
43 : : SOLIDSYSLOG_ORIGIN_SD_ERROR_POOL_EXHAUSTED
44 : : );
45 : : }
46 : 132 : return handle;
47 : : }
48 : :
49 : 133 : void SolidSyslogOriginSd_Destroy(struct SolidSyslogStructuredData* base)
50 : : {
51 : 133 : size_t index = OriginSd_IndexFromHandle(base);
52 [ + + + + ]: 264 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&OriginSd_Allocator, index) &&
53 : 131 : SolidSyslogPoolAllocator_FreeIfInUse(&OriginSd_Allocator, index, OriginSd_CleanupAtIndex, NULL);
54 [ + + ]: 133 : if (!released)
55 : : {
56 : 3 : OriginSd_Report(
57 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
58 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
59 : : SOLIDSYSLOG_ORIGIN_SD_ERROR_UNKNOWN_DESTROY
60 : : );
61 : : }
62 : 133 : }
63 : :
64 : 133 : static inline size_t OriginSd_IndexFromHandle(const struct SolidSyslogStructuredData* base)
65 : : {
66 : 133 : size_t result = SOLIDSYSLOG_ORIGIN_SD_POOL_SIZE;
67 [ + + ]: 135 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_ORIGIN_SD_POOL_SIZE; poolIndex++)
68 : : {
69 [ + + ]: 133 : if (base == &OriginSd_Pool[poolIndex].Base)
70 : : {
71 : 131 : result = poolIndex;
72 : 131 : break;
73 : : }
74 : : }
75 : 133 : return result;
76 : : }
77 : :
78 : 130 : static inline void OriginSd_CleanupAtIndex(size_t index, void* context)
79 : : {
80 : : (void) context;
81 : 130 : SolidSyslogOriginSd_Cleanup(&OriginSd_Pool[index].Base);
82 : 130 : }
|