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 "SolidSyslogFileBlockDevice.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 "SolidSyslogFileBlockDeviceErrors.h"
14 : : #include "SolidSyslogFileBlockDevicePrivate.h"
15 : : #include "SolidSyslogNullBlockDevice.h"
16 : : #include "SolidSyslogPoolAllocator.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogBlockDevice;
21 : : struct SolidSyslogFile;
22 : :
23 : : static inline size_t FileBlockDevice_IndexFromHandle(const struct SolidSyslogBlockDevice* base);
24 : : static inline void FileBlockDevice_CleanupAtIndex(size_t index, void* context);
25 : :
26 : : static bool FileBlockDevice_InUse[SOLIDSYSLOG_FILE_BLOCK_DEVICE_POOL_SIZE];
27 : : static struct SolidSyslogFileBlockDevice FileBlockDevice_Pool[SOLIDSYSLOG_FILE_BLOCK_DEVICE_POOL_SIZE];
28 : : static struct SolidSyslogPoolAllocator FileBlockDevice_Allocator = {
29 : : FileBlockDevice_InUse,
30 : : SOLIDSYSLOG_FILE_BLOCK_DEVICE_POOL_SIZE
31 : : };
32 : :
33 : 354 : struct SolidSyslogBlockDevice* SolidSyslogFileBlockDevice_Create(
34 : : struct SolidSyslogFile* file,
35 : : const char* pathPrefix,
36 : : size_t blockSize
37 : : )
38 : : {
39 : 354 : struct SolidSyslogBlockDevice* result = SolidSyslogNullBlockDevice_Get();
40 [ + + ]: 354 : size_t resolvedBlockSize = (blockSize == 0U) ? (size_t) SOLIDSYSLOG_FILE_DEFAULT_BLOCK_SIZE : blockSize;
41 : 354 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&FileBlockDevice_Allocator);
42 [ + + ]: 354 : if (SolidSyslogPoolAllocator_IndexIsValid(&FileBlockDevice_Allocator, index))
43 : : {
44 : 352 : SolidSyslogFileBlockDevice_Initialise(&FileBlockDevice_Pool[index].Base, file, pathPrefix, resolvedBlockSize);
45 : 352 : result = &FileBlockDevice_Pool[index].Base;
46 : : }
47 : : else
48 : : {
49 : 2 : FileBlockDevice_Report(
50 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
51 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
52 : : SOLIDSYSLOG_FILE_BLOCK_DEVICE_ERROR_POOL_EXHAUSTED
53 : : );
54 : : }
55 : 354 : return result;
56 : : }
57 : :
58 : 355 : void SolidSyslogFileBlockDevice_Destroy(struct SolidSyslogBlockDevice* base)
59 : : {
60 : 355 : size_t index = FileBlockDevice_IndexFromHandle(base);
61 : 355 : bool released =
62 [ + + + + ]: 708 : SolidSyslogPoolAllocator_IndexIsValid(&FileBlockDevice_Allocator, index) &&
63 : 353 : SolidSyslogPoolAllocator_FreeIfInUse(&FileBlockDevice_Allocator, index, FileBlockDevice_CleanupAtIndex, NULL);
64 [ + + ]: 355 : if (!released)
65 : : {
66 : 3 : FileBlockDevice_Report(
67 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
68 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
69 : : SOLIDSYSLOG_FILE_BLOCK_DEVICE_ERROR_UNKNOWN_DESTROY
70 : : );
71 : : }
72 : 355 : }
73 : :
74 : 355 : static inline size_t FileBlockDevice_IndexFromHandle(const struct SolidSyslogBlockDevice* base)
75 : : {
76 : 355 : size_t result = SOLIDSYSLOG_FILE_BLOCK_DEVICE_POOL_SIZE;
77 [ + + ]: 357 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_FILE_BLOCK_DEVICE_POOL_SIZE; poolIndex++)
78 : : {
79 [ + + ]: 355 : if (base == &FileBlockDevice_Pool[poolIndex].Base)
80 : : {
81 : 353 : result = poolIndex;
82 : 353 : break;
83 : : }
84 : : }
85 : 355 : return result;
86 : : }
87 : :
88 : 352 : static inline void FileBlockDevice_CleanupAtIndex(size_t index, void* context)
89 : : {
90 : : (void) context;
91 : 352 : SolidSyslogFileBlockDevice_Cleanup(&FileBlockDevice_Pool[index].Base);
92 : 352 : }
|