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 "SolidSyslogLwipRawAddress.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 "SolidSyslogLwipRawAddressErrors.h"
14 : : #include "SolidSyslogLwipRawAddressPrivate.h"
15 : : #include "SolidSyslogPoolAllocator.h"
16 : : #include "SolidSyslogPrival.h"
17 : : #include "SolidSyslogTunables.h"
18 : :
19 : : struct SolidSyslogAddress;
20 : :
21 : : static inline struct SolidSyslogAddress* LwipRawAddress_HandleFromIndex(size_t index);
22 : : static inline size_t LwipRawAddress_IndexFromHandle(const struct SolidSyslogAddress* base);
23 : : static inline void LwipRawAddress_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool LwipRawAddress_InUse[SOLIDSYSLOG_ADDRESS_POOL_SIZE];
26 : : static struct SolidSyslogPoolAllocator LwipRawAddress_Allocator = {LwipRawAddress_InUse, SOLIDSYSLOG_ADDRESS_POOL_SIZE};
27 : :
28 : 167 : struct SolidSyslogAddress* SolidSyslogLwipRawAddress_Create(void)
29 : : {
30 : : /* TU-private fallback returned when the pool is exhausted. Sized as
31 : : * a real SolidSyslogLwipRawAddress so subsequent writes (e.g. a Resolver
32 : : * overwrite at the exhausted-fallback call site) are bounded - same
33 : : * ip_addr_t + u16_t storage as any pooled slot. */
34 : : static struct SolidSyslogLwipRawAddress fallback;
35 : :
36 : 167 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&LwipRawAddress_Allocator);
37 : 167 : struct SolidSyslogAddress* handle = (struct SolidSyslogAddress*) &fallback;
38 [ + + ]: 167 : if (SolidSyslogPoolAllocator_IndexIsValid(&LwipRawAddress_Allocator, index) == true)
39 : : {
40 : 164 : handle = LwipRawAddress_HandleFromIndex(index);
41 : 164 : SolidSyslogLwipRawAddress_Initialise(handle);
42 : : }
43 : : else
44 : : {
45 : 3 : LwipRawAddress_Report(
46 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
47 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
48 : : SOLIDSYSLOG_ADDRESS_ERROR_POOL_EXHAUSTED
49 : : );
50 : : }
51 : 167 : return handle;
52 : : }
53 : :
54 : 517 : static inline struct SolidSyslogAddress* LwipRawAddress_HandleFromIndex(size_t index)
55 : : {
56 : : static struct SolidSyslogLwipRawAddress pool[SOLIDSYSLOG_ADDRESS_POOL_SIZE];
57 : 517 : return (struct SolidSyslogAddress*) &pool[index];
58 : : }
59 : :
60 : 170 : void SolidSyslogLwipRawAddress_Destroy(struct SolidSyslogAddress* base)
61 : : {
62 : 170 : size_t index = LwipRawAddress_IndexFromHandle(base);
63 : 170 : bool released =
64 [ + + + + ]: 335 : SolidSyslogPoolAllocator_IndexIsValid(&LwipRawAddress_Allocator, index) &&
65 : 165 : SolidSyslogPoolAllocator_FreeIfInUse(&LwipRawAddress_Allocator, index, LwipRawAddress_CleanupAtIndex, NULL);
66 [ + + ]: 170 : if (!released)
67 : : {
68 : 6 : LwipRawAddress_Report(
69 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
70 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
71 : : SOLIDSYSLOG_ADDRESS_ERROR_UNKNOWN_DESTROY
72 : : );
73 : : }
74 : 170 : }
75 : :
76 : 170 : static inline size_t LwipRawAddress_IndexFromHandle(const struct SolidSyslogAddress* base)
77 : : {
78 : 170 : size_t result = SOLIDSYSLOG_ADDRESS_POOL_SIZE;
79 [ + + ]: 194 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_ADDRESS_POOL_SIZE; poolIndex++)
80 : : {
81 [ + + ]: 189 : if (base == LwipRawAddress_HandleFromIndex(poolIndex))
82 : : {
83 : 165 : result = poolIndex;
84 : 165 : break;
85 : : }
86 : : }
87 : 170 : return result;
88 : : }
89 : :
90 : 164 : static inline void LwipRawAddress_CleanupAtIndex(size_t index, void* context)
91 : : {
92 : : (void) context;
93 : 164 : SolidSyslogLwipRawAddress_Cleanup(LwipRawAddress_HandleFromIndex(index));
94 : 164 : }
|