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 "SolidSyslogLittleFsFile.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 "SolidSyslogLittleFsFileErrors.h"
14 : : #include "SolidSyslogLittleFsFilePrivate.h"
15 : : #include "SolidSyslogNullFile.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : : #include "lfs.h"
20 : :
21 : : struct SolidSyslogFile;
22 : :
23 : : static inline size_t LittleFsFile_IndexFromHandle(const struct SolidSyslogFile* base);
24 : : static inline void LittleFsFile_CleanupAtIndex(size_t index, void* context);
25 : :
26 : : static bool LittleFsFile_InUse[SOLIDSYSLOG_FILE_POOL_SIZE];
27 : : static struct SolidSyslogLittleFsFile LittleFsFile_Pool[SOLIDSYSLOG_FILE_POOL_SIZE];
28 : : static struct SolidSyslogPoolAllocator LittleFsFile_Allocator = {LittleFsFile_InUse, SOLIDSYSLOG_FILE_POOL_SIZE};
29 : :
30 : 59 : struct SolidSyslogFile* SolidSyslogLittleFsFile_Create(lfs_t* filesystem, void* fileBuffer, uint32_t fileBufferBytes)
31 : : {
32 : 59 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&LittleFsFile_Allocator);
33 : 59 : struct SolidSyslogFile* handle = SolidSyslogNullFile_Get();
34 [ + + ]: 59 : if (SolidSyslogPoolAllocator_IndexIsValid(&LittleFsFile_Allocator, index) == true)
35 : : {
36 [ + + ]: 55 : if (SolidSyslogLittleFsFile_Initialise(
37 : : &LittleFsFile_Pool[index].Base,
38 : : filesystem,
39 : : fileBuffer,
40 : : fileBufferBytes
41 : : ) == true)
42 : : {
43 : 51 : handle = &LittleFsFile_Pool[index].Base;
44 : : }
45 : : else
46 : : {
47 : : /* Give the slot straight back. Refused wiring is a build fault the
48 : : * integrator will fix, and holding the slot would turn one of those
49 : : * into pool exhaustion for every later Create. Initialise has
50 : : * already reported why. */
51 : : (void
52 : 4 : ) SolidSyslogPoolAllocator_FreeIfInUse(&LittleFsFile_Allocator, index, LittleFsFile_CleanupAtIndex, NULL);
53 : : }
54 : : }
55 : : else
56 : : {
57 : 4 : LittleFsFile_Report(
58 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
59 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
60 : : SOLIDSYSLOG_FILE_ERROR_POOL_EXHAUSTED
61 : : );
62 : : }
63 : 59 : return handle;
64 : : }
65 : :
66 : 57 : void SolidSyslogLittleFsFile_Destroy(struct SolidSyslogFile* base)
67 : : {
68 : 57 : size_t index = LittleFsFile_IndexFromHandle(base);
69 : 57 : bool released =
70 [ + + + + ]: 109 : SolidSyslogPoolAllocator_IndexIsValid(&LittleFsFile_Allocator, index) &&
71 : 52 : SolidSyslogPoolAllocator_FreeIfInUse(&LittleFsFile_Allocator, index, LittleFsFile_CleanupAtIndex, NULL);
72 [ + + ]: 57 : if (!released)
73 : : {
74 : 6 : LittleFsFile_Report(
75 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
76 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
77 : : SOLIDSYSLOG_FILE_ERROR_UNKNOWN_DESTROY
78 : : );
79 : : }
80 : 57 : }
81 : :
82 : 57 : static inline size_t LittleFsFile_IndexFromHandle(const struct SolidSyslogFile* base)
83 : : {
84 : 57 : size_t result = SOLIDSYSLOG_FILE_POOL_SIZE;
85 [ + + ]: 62 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_FILE_POOL_SIZE; poolIndex++)
86 : : {
87 [ + + ]: 57 : if (base == &LittleFsFile_Pool[poolIndex].Base)
88 : : {
89 : 52 : result = poolIndex;
90 : 52 : break;
91 : : }
92 : : }
93 : 57 : return result;
94 : : }
95 : :
96 : 55 : static inline void LittleFsFile_CleanupAtIndex(size_t index, void* context)
97 : : {
98 : : (void) context;
99 : 55 : SolidSyslogLittleFsFile_Cleanup(&LittleFsFile_Pool[index].Base);
100 : 55 : }
|