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 "SolidSyslogPlusTcpAddress.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 "SolidSyslogPlusTcpAddressErrors.h"
14 : : #include "SolidSyslogPlusTcpAddressPrivate.h"
15 : : #include "SolidSyslogPoolAllocator.h"
16 : : #include "SolidSyslogPrival.h"
17 : : #include "SolidSyslogTunables.h"
18 : :
19 : : struct SolidSyslogAddress;
20 : :
21 : : static inline struct SolidSyslogAddress* PlusTcpAddress_HandleFromIndex(size_t index);
22 : : static inline size_t PlusTcpAddress_IndexFromHandle(const struct SolidSyslogAddress* base);
23 : : static inline void PlusTcpAddress_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool PlusTcpAddress_InUse[SOLIDSYSLOG_ADDRESS_POOL_SIZE];
26 : : static struct SolidSyslogPoolAllocator PlusTcpAddress_Allocator = {PlusTcpAddress_InUse, SOLIDSYSLOG_ADDRESS_POOL_SIZE};
27 : :
28 : 127 : struct SolidSyslogAddress* SolidSyslogPlusTcpAddress_Create(void)
29 : : {
30 : : /* TU-private fallback returned when the pool is exhausted. Sized as
31 : : * a real SolidSyslogPlusTcpAddress so a Resolver overwrite at the
32 : : * exhausted-fallback call site is bounded - same freertos_sockaddr
33 : : * storage as any pooled slot. */
34 : : static struct SolidSyslogPlusTcpAddress fallback;
35 : :
36 : 127 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&PlusTcpAddress_Allocator);
37 : 127 : struct SolidSyslogAddress* handle = (struct SolidSyslogAddress*) &fallback;
38 [ + + ]: 127 : if (SolidSyslogPoolAllocator_IndexIsValid(&PlusTcpAddress_Allocator, index) == true)
39 : : {
40 : 124 : handle = PlusTcpAddress_HandleFromIndex(index);
41 : 124 : SolidSyslogPlusTcpAddress_Initialise(handle);
42 : : }
43 : : else
44 : : {
45 : 3 : PlusTcpAddress_Report(
46 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
47 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
48 : : SOLIDSYSLOG_ADDRESS_ERROR_POOL_EXHAUSTED
49 : : );
50 : : }
51 : 127 : return handle;
52 : : }
53 : :
54 : 397 : static inline struct SolidSyslogAddress* PlusTcpAddress_HandleFromIndex(size_t index)
55 : : {
56 : : static struct SolidSyslogPlusTcpAddress pool[SOLIDSYSLOG_ADDRESS_POOL_SIZE];
57 : 397 : return (struct SolidSyslogAddress*) &pool[index];
58 : : }
59 : :
60 : 130 : void SolidSyslogPlusTcpAddress_Destroy(struct SolidSyslogAddress* base)
61 : : {
62 : 130 : size_t index = PlusTcpAddress_IndexFromHandle(base);
63 : 130 : bool released =
64 [ + + + + ]: 255 : SolidSyslogPoolAllocator_IndexIsValid(&PlusTcpAddress_Allocator, index) &&
65 : 125 : SolidSyslogPoolAllocator_FreeIfInUse(&PlusTcpAddress_Allocator, index, PlusTcpAddress_CleanupAtIndex, NULL);
66 [ + + ]: 130 : if (!released)
67 : : {
68 : 6 : PlusTcpAddress_Report(
69 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
70 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
71 : : SOLIDSYSLOG_ADDRESS_ERROR_UNKNOWN_DESTROY
72 : : );
73 : : }
74 : 130 : }
75 : :
76 : 130 : static inline size_t PlusTcpAddress_IndexFromHandle(const struct SolidSyslogAddress* base)
77 : : {
78 : 130 : size_t result = SOLIDSYSLOG_ADDRESS_POOL_SIZE;
79 [ + + ]: 154 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_ADDRESS_POOL_SIZE; poolIndex++)
80 : : {
81 [ + + ]: 149 : if (base == PlusTcpAddress_HandleFromIndex(poolIndex))
82 : : {
83 : 125 : result = poolIndex;
84 : 125 : break;
85 : : }
86 : : }
87 : 130 : return result;
88 : : }
89 : :
90 : 124 : static inline void PlusTcpAddress_CleanupAtIndex(size_t index, void* context)
91 : : {
92 : : (void) context;
93 : 124 : SolidSyslogPlusTcpAddress_Cleanup(PlusTcpAddress_HandleFromIndex(index));
94 : 124 : }
|