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