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 "SolidSyslogPosixFile.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 "SolidSyslogNullFile.h"
14 : : #include "SolidSyslogPoolAllocator.h"
15 : : #include "SolidSyslogPosixFileErrors.h"
16 : : #include "SolidSyslogPosixFilePrivate.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogFile;
21 : :
22 : : static inline size_t PosixFile_IndexFromHandle(const struct SolidSyslogFile* base);
23 : : static inline void PosixFile_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool PosixFile_InUse[SOLIDSYSLOG_FILE_POOL_SIZE];
26 : : static struct SolidSyslogPosixFile PosixFile_Pool[SOLIDSYSLOG_FILE_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator PosixFile_Allocator = {PosixFile_InUse, SOLIDSYSLOG_FILE_POOL_SIZE};
28 : :
29 : 38 : struct SolidSyslogFile* SolidSyslogPosixFile_Create(void)
30 : : {
31 : 38 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&PosixFile_Allocator);
32 : 38 : struct SolidSyslogFile* handle = SolidSyslogNullFile_Get();
33 [ + + ]: 38 : if (SolidSyslogPoolAllocator_IndexIsValid(&PosixFile_Allocator, index) == true)
34 : : {
35 : 34 : SolidSyslogPosixFile_Initialise(&PosixFile_Pool[index].Base);
36 : 34 : handle = &PosixFile_Pool[index].Base;
37 : : }
38 : : else
39 : : {
40 : 4 : PosixFile_Report(
41 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
42 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
43 : : SOLIDSYSLOG_FILE_ERROR_POOL_EXHAUSTED
44 : : );
45 : : }
46 : 38 : return handle;
47 : : }
48 : :
49 : 41 : void SolidSyslogPosixFile_Destroy(struct SolidSyslogFile* base)
50 : : {
51 : 41 : size_t index = PosixFile_IndexFromHandle(base);
52 [ + + + + ]: 76 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&PosixFile_Allocator, index) &&
53 : 35 : SolidSyslogPoolAllocator_FreeIfInUse(&PosixFile_Allocator, index, PosixFile_CleanupAtIndex, NULL);
54 [ + + ]: 41 : if (!released)
55 : : {
56 : 7 : PosixFile_Report(
57 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
58 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
59 : : SOLIDSYSLOG_FILE_ERROR_UNKNOWN_DESTROY
60 : : );
61 : : }
62 : 41 : }
63 : :
64 : 41 : static inline size_t PosixFile_IndexFromHandle(const struct SolidSyslogFile* base)
65 : : {
66 : 41 : size_t result = SOLIDSYSLOG_FILE_POOL_SIZE;
67 [ + + ]: 47 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_FILE_POOL_SIZE; poolIndex++)
68 : : {
69 [ + + ]: 41 : if (base == &PosixFile_Pool[poolIndex].Base)
70 : : {
71 : 35 : result = poolIndex;
72 : 35 : break;
73 : : }
74 : : }
75 : 41 : return result;
76 : : }
77 : :
78 : 34 : static inline void PosixFile_CleanupAtIndex(size_t index, void* context)
79 : : {
80 : : (void) context;
81 : 34 : SolidSyslogPosixFile_Cleanup(&PosixFile_Pool[index].Base);
82 : 34 : }
|