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 "SolidSyslogPassthroughBuffer.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 "SolidSyslogNullBuffer.h"
14 : : #include "SolidSyslogPassthroughBufferErrors.h"
15 : : #include "SolidSyslogPassthroughBufferPrivate.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogBuffer;
21 : : struct SolidSyslogSender;
22 : :
23 : : static inline size_t PassthroughBuffer_IndexFromHandle(const struct SolidSyslogBuffer* base);
24 : : static inline void PassthroughBuffer_CleanupAtIndex(size_t index, void* context);
25 : :
26 : : static bool PassthroughBuffer_InUse[SOLIDSYSLOG_PASSTHROUGH_BUFFER_POOL_SIZE];
27 : : static struct SolidSyslogPassthroughBuffer PassthroughBuffer_Pool[SOLIDSYSLOG_PASSTHROUGH_BUFFER_POOL_SIZE];
28 : : static struct SolidSyslogPoolAllocator PassthroughBuffer_Allocator = {
29 : : PassthroughBuffer_InUse,
30 : : SOLIDSYSLOG_PASSTHROUGH_BUFFER_POOL_SIZE
31 : : };
32 : :
33 : 139 : struct SolidSyslogBuffer* SolidSyslogPassthroughBuffer_Create(struct SolidSyslogSender* sender)
34 : : {
35 : 139 : struct SolidSyslogBuffer* handle = SolidSyslogNullBuffer_Get();
36 [ + + ]: 139 : if (sender == NULL)
37 : : {
38 : 3 : PassthroughBuffer_Report(
39 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
40 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
41 : : SOLIDSYSLOG_PASSTHROUGH_BUFFER_ERROR_NULL_SENDER
42 : : );
43 : : }
44 : : else
45 : : {
46 : 136 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&PassthroughBuffer_Allocator);
47 [ + + ]: 136 : if (SolidSyslogPoolAllocator_IndexIsValid(&PassthroughBuffer_Allocator, index))
48 : : {
49 : 133 : SolidSyslogPassthroughBuffer_Initialise(&PassthroughBuffer_Pool[index].Base, sender);
50 : 133 : handle = &PassthroughBuffer_Pool[index].Base;
51 : : }
52 : : else
53 : : {
54 : 3 : PassthroughBuffer_Report(
55 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
56 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
57 : : SOLIDSYSLOG_PASSTHROUGH_BUFFER_ERROR_POOL_EXHAUSTED
58 : : );
59 : : }
60 : : }
61 : 139 : return handle;
62 : : }
63 : :
64 : 141 : void SolidSyslogPassthroughBuffer_Destroy(struct SolidSyslogBuffer* base)
65 : : {
66 : 141 : size_t index = PassthroughBuffer_IndexFromHandle(base);
67 [ + + + + ]: 275 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&PassthroughBuffer_Allocator, index) &&
68 : 134 : SolidSyslogPoolAllocator_FreeIfInUse(
69 : : &PassthroughBuffer_Allocator,
70 : : index,
71 : : PassthroughBuffer_CleanupAtIndex,
72 : : NULL
73 : : );
74 [ + + ]: 141 : if (!released)
75 : : {
76 : 8 : PassthroughBuffer_Report(
77 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
78 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
79 : : SOLIDSYSLOG_PASSTHROUGH_BUFFER_ERROR_UNKNOWN_DESTROY
80 : : );
81 : : }
82 : 141 : }
83 : :
84 : 141 : static inline size_t PassthroughBuffer_IndexFromHandle(const struct SolidSyslogBuffer* base)
85 : : {
86 : 141 : size_t result = SOLIDSYSLOG_PASSTHROUGH_BUFFER_POOL_SIZE;
87 [ + + ]: 148 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_PASSTHROUGH_BUFFER_POOL_SIZE; poolIndex++)
88 : : {
89 [ + + ]: 141 : if (base == &PassthroughBuffer_Pool[poolIndex].Base)
90 : : {
91 : 134 : result = poolIndex;
92 : 134 : break;
93 : : }
94 : : }
95 : 141 : return result;
96 : : }
97 : :
98 : 133 : static inline void PassthroughBuffer_CleanupAtIndex(size_t index, void* context)
99 : : {
100 : : (void) context;
101 : 133 : SolidSyslogPassthroughBuffer_Cleanup(&PassthroughBuffer_Pool[index].Base);
102 : 133 : }
|