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 "SolidSyslogPlusTcpResolver.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 "SolidSyslogNullResolver.h"
14 : : #include "SolidSyslogPlusTcpResolverErrors.h"
15 : : #include "SolidSyslogPlusTcpResolverPrivate.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogResolver;
21 : :
22 : : static inline size_t PlusTcpResolver_IndexFromHandle(const struct SolidSyslogResolver* base);
23 : : static inline void PlusTcpResolver_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool PlusTcpResolver_InUse[SOLIDSYSLOG_RESOLVER_POOL_SIZE];
26 : : static struct SolidSyslogPlusTcpResolver PlusTcpResolver_Pool[SOLIDSYSLOG_RESOLVER_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator PlusTcpResolver_Allocator = {
28 : : PlusTcpResolver_InUse,
29 : : SOLIDSYSLOG_RESOLVER_POOL_SIZE
30 : : };
31 : :
32 : 23 : struct SolidSyslogResolver* SolidSyslogPlusTcpResolver_Create(void)
33 : : {
34 : 23 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&PlusTcpResolver_Allocator);
35 : 23 : struct SolidSyslogResolver* handle = SolidSyslogNullResolver_Get();
36 [ + + ]: 23 : if (SolidSyslogPoolAllocator_IndexIsValid(&PlusTcpResolver_Allocator, index) == true)
37 : : {
38 : 19 : SolidSyslogPlusTcpResolver_Initialise(&PlusTcpResolver_Pool[index].Base);
39 : 19 : handle = &PlusTcpResolver_Pool[index].Base;
40 : : }
41 : : else
42 : : {
43 : 4 : PlusTcpResolver_Report(
44 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
45 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
46 : : SOLIDSYSLOG_RESOLVER_ERROR_POOL_EXHAUSTED
47 : : );
48 : : }
49 : 23 : return handle;
50 : : }
51 : :
52 : 26 : void SolidSyslogPlusTcpResolver_Destroy(struct SolidSyslogResolver* base)
53 : : {
54 : 26 : size_t index = PlusTcpResolver_IndexFromHandle(base);
55 : 26 : bool released =
56 [ + + + + ]: 46 : SolidSyslogPoolAllocator_IndexIsValid(&PlusTcpResolver_Allocator, index) &&
57 : 20 : SolidSyslogPoolAllocator_FreeIfInUse(&PlusTcpResolver_Allocator, index, PlusTcpResolver_CleanupAtIndex, NULL);
58 [ + + ]: 26 : if (!released)
59 : : {
60 : 7 : PlusTcpResolver_Report(
61 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
62 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
63 : : SOLIDSYSLOG_RESOLVER_ERROR_UNKNOWN_DESTROY
64 : : );
65 : : }
66 : 26 : }
67 : :
68 : 26 : static inline size_t PlusTcpResolver_IndexFromHandle(const struct SolidSyslogResolver* base)
69 : : {
70 : 26 : size_t result = SOLIDSYSLOG_RESOLVER_POOL_SIZE;
71 [ + + ]: 32 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_RESOLVER_POOL_SIZE; poolIndex++)
72 : : {
73 [ + + ]: 26 : if (base == &PlusTcpResolver_Pool[poolIndex].Base)
74 : : {
75 : 20 : result = poolIndex;
76 : 20 : break;
77 : : }
78 : : }
79 : 26 : return result;
80 : : }
81 : :
82 : 19 : static inline void PlusTcpResolver_CleanupAtIndex(size_t index, void* context)
83 : : {
84 : : (void) context;
85 : 19 : SolidSyslogPlusTcpResolver_Cleanup(&PlusTcpResolver_Pool[index].Base);
86 : 19 : }
|