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 "SolidSyslogPosixResolver.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 "SolidSyslogPosixResolverErrors.h"
14 : : #include "SolidSyslogPosixResolverPrivate.h"
15 : : #include "SolidSyslogNullResolver.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogResolver;
21 : :
22 : : static inline size_t PosixResolver_IndexFromHandle(const struct SolidSyslogResolver* base);
23 : : static inline void PosixResolver_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool PosixResolver_InUse[SOLIDSYSLOG_RESOLVER_POOL_SIZE];
26 : : static struct SolidSyslogPosixResolver PosixResolver_Pool[SOLIDSYSLOG_RESOLVER_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator PosixResolver_Allocator = {PosixResolver_InUse, SOLIDSYSLOG_RESOLVER_POOL_SIZE};
28 : :
29 : 175 : struct SolidSyslogResolver* SolidSyslogPosixResolver_Create(void)
30 : : {
31 : 175 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&PosixResolver_Allocator);
32 : 175 : struct SolidSyslogResolver* handle = SolidSyslogNullResolver_Get();
33 [ + + ]: 175 : if (SolidSyslogPoolAllocator_IndexIsValid(&PosixResolver_Allocator, index) == true)
34 : : {
35 : 171 : SolidSyslogPosixResolver_Initialise(&PosixResolver_Pool[index].Base);
36 : 171 : handle = &PosixResolver_Pool[index].Base;
37 : : }
38 : : else
39 : : {
40 : 4 : PosixResolver_Report(
41 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
42 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
43 : : SOLIDSYSLOG_RESOLVER_ERROR_POOL_EXHAUSTED
44 : : );
45 : : }
46 : 175 : return handle;
47 : : }
48 : :
49 : 178 : void SolidSyslogPosixResolver_Destroy(struct SolidSyslogResolver* base)
50 : : {
51 : 178 : size_t index = PosixResolver_IndexFromHandle(base);
52 : 178 : bool released =
53 [ + + + + ]: 350 : SolidSyslogPoolAllocator_IndexIsValid(&PosixResolver_Allocator, index) &&
54 : 172 : SolidSyslogPoolAllocator_FreeIfInUse(&PosixResolver_Allocator, index, PosixResolver_CleanupAtIndex, NULL);
55 [ + + ]: 178 : if (!released)
56 : : {
57 : 7 : PosixResolver_Report(
58 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
59 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
60 : : SOLIDSYSLOG_RESOLVER_ERROR_UNKNOWN_DESTROY
61 : : );
62 : : }
63 : 178 : }
64 : :
65 : 178 : static inline size_t PosixResolver_IndexFromHandle(const struct SolidSyslogResolver* base)
66 : : {
67 : 178 : size_t result = SOLIDSYSLOG_RESOLVER_POOL_SIZE;
68 [ + + ]: 184 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_RESOLVER_POOL_SIZE; poolIndex++)
69 : : {
70 [ + + ]: 178 : if (base == &PosixResolver_Pool[poolIndex].Base)
71 : : {
72 : 172 : result = poolIndex;
73 : 172 : break;
74 : : }
75 : : }
76 : 178 : return result;
77 : : }
78 : :
79 : 171 : static inline void PosixResolver_CleanupAtIndex(size_t index, void* context)
80 : : {
81 : : (void) context;
82 : 171 : SolidSyslogPosixResolver_Cleanup(&PosixResolver_Pool[index].Base);
83 : 171 : }
|