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 "SolidSyslogPosixTcpStream.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 "SolidSyslogNullStream.h"
14 : : #include "SolidSyslogPoolAllocator.h"
15 : : #include "SolidSyslogPosixTcpStreamErrors.h"
16 : : #include "SolidSyslogPosixTcpStreamPrivate.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogStream;
21 : :
22 : : static inline size_t PosixTcpStream_IndexFromHandle(const struct SolidSyslogStream* base);
23 : : static inline void PosixTcpStream_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool PosixTcpStream_InUse[SOLIDSYSLOG_TCP_STREAM_POOL_SIZE];
26 : : static struct SolidSyslogPosixTcpStream PosixTcpStream_Pool[SOLIDSYSLOG_TCP_STREAM_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator PosixTcpStream_Allocator = {
28 : : PosixTcpStream_InUse,
29 : : SOLIDSYSLOG_TCP_STREAM_POOL_SIZE
30 : : };
31 : :
32 : 171 : struct SolidSyslogStream* SolidSyslogPosixTcpStream_Create(const struct SolidSyslogPosixTcpStreamConfig* config)
33 : : {
34 : 171 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&PosixTcpStream_Allocator);
35 : 171 : struct SolidSyslogStream* handle = SolidSyslogNullStream_Get();
36 [ + + ]: 171 : if (SolidSyslogPoolAllocator_IndexIsValid(&PosixTcpStream_Allocator, index) == true)
37 : : {
38 : 167 : SolidSyslogPosixTcpStream_Initialise(&PosixTcpStream_Pool[index].Base, config);
39 : 167 : handle = &PosixTcpStream_Pool[index].Base;
40 : : }
41 : : else
42 : : {
43 : 4 : PosixTcpStream_Report(
44 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
45 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
46 : : SOLIDSYSLOG_TCP_STREAM_ERROR_POOL_EXHAUSTED
47 : : );
48 : : }
49 : 171 : return handle;
50 : : }
51 : :
52 : 176 : void SolidSyslogPosixTcpStream_Destroy(struct SolidSyslogStream* base)
53 : : {
54 : 176 : size_t index = PosixTcpStream_IndexFromHandle(base);
55 : 176 : bool released =
56 [ + + + + ]: 346 : SolidSyslogPoolAllocator_IndexIsValid(&PosixTcpStream_Allocator, index) &&
57 : 170 : SolidSyslogPoolAllocator_FreeIfInUse(&PosixTcpStream_Allocator, index, PosixTcpStream_CleanupAtIndex, NULL);
58 [ + + ]: 176 : if (!released)
59 : : {
60 : 9 : PosixTcpStream_Report(
61 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
62 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
63 : : SOLIDSYSLOG_TCP_STREAM_ERROR_UNKNOWN_DESTROY
64 : : );
65 : : }
66 : 176 : }
67 : :
68 : 176 : static inline size_t PosixTcpStream_IndexFromHandle(const struct SolidSyslogStream* base)
69 : : {
70 : 176 : size_t result = SOLIDSYSLOG_TCP_STREAM_POOL_SIZE;
71 [ + + ]: 192 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_TCP_STREAM_POOL_SIZE; poolIndex++)
72 : : {
73 [ + + ]: 186 : if (base == &PosixTcpStream_Pool[poolIndex].Base)
74 : : {
75 : 170 : result = poolIndex;
76 : 170 : break;
77 : : }
78 : : }
79 : 176 : return result;
80 : : }
81 : :
82 : 167 : static inline void PosixTcpStream_CleanupAtIndex(size_t index, void* context)
83 : : {
84 : : (void) context;
85 : 167 : SolidSyslogPosixTcpStream_Cleanup(&PosixTcpStream_Pool[index].Base);
86 : 167 : }
|