src/util/vfs/vfs-fifo.c (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba-util/vfs.h>
7#include <mgba-util/circle-buffer.h>
8
9struct VFileFIFO {
10 struct VFile d;
11 struct CircleBuffer* backing;
12};
13
14static bool _vffClose(struct VFile* vf);
15static off_t _vffSeek(struct VFile* vf, off_t offset, int whence);
16static ssize_t _vffRead(struct VFile* vf, void* buffer, size_t size);
17static ssize_t _vffWrite(struct VFile* vf, const void* buffer, size_t size);
18static void* _vffMap(struct VFile* vf, size_t size, int flags);
19static void _vffUnmap(struct VFile* vf, void* memory, size_t size);
20static void _vffTruncate(struct VFile* vf, size_t size);
21static ssize_t _vffSize(struct VFile* vf);
22static bool _vffSync(struct VFile* vf, const void* buffer, size_t size);
23
24struct VFile* VFileFIFO(struct CircleBuffer* backing) {
25 if (!backing) {
26 return NULL;
27 }
28
29 struct VFileFIFO* vff = malloc(sizeof(*vff));
30 if (!vff) {
31 return NULL;
32 }
33
34 vff->backing = backing;
35 vff->d.close = _vffClose;
36 vff->d.seek = _vffSeek;
37 vff->d.read = _vffRead;
38 vff->d.readline = VFileReadline;
39 vff->d.write = _vffWrite;
40 vff->d.map = _vffMap;
41 vff->d.unmap = _vffUnmap;
42 vff->d.truncate = _vffTruncate;
43 vff->d.size = _vffSize;
44 vff->d.sync = _vffSync;
45
46 return &vff->d;
47}
48
49
50static bool _vffClose(struct VFile* vf) {
51 free(vf);
52 return true;
53}
54
55static off_t _vffSeek(struct VFile* vf, off_t offset, int whence) {
56 UNUSED(vf);
57 UNUSED(offset);
58 UNUSED(whence);
59 return 0;
60}
61
62static ssize_t _vffRead(struct VFile* vf, void* buffer, size_t size) {
63 struct VFileFIFO* vff = (struct VFileFIFO*) vf;
64 return CircleBufferRead(vff->backing, buffer, size);
65}
66
67static ssize_t _vffWrite(struct VFile* vf, const void* buffer, size_t size) {
68 struct VFileFIFO* vff = (struct VFileFIFO*) vf;
69 return CircleBufferWrite(vff->backing, buffer, size);
70}
71
72static void* _vffMap(struct VFile* vf, size_t size, int flags) {
73 UNUSED(vf);
74 UNUSED(size);
75 UNUSED(flags);
76 return NULL;
77}
78
79static void _vffUnmap(struct VFile* vf, void* memory, size_t size) {
80 UNUSED(vf);
81 UNUSED(memory);
82 UNUSED(size);
83}
84
85static void _vffTruncate(struct VFile* vf, size_t size) {
86 struct VFileFIFO* vff = (struct VFileFIFO*) vf;
87 if (!size) {
88 CircleBufferClear(vff->backing);
89 }
90}
91
92static ssize_t _vffSize(struct VFile* vf) {
93 struct VFileFIFO* vff = (struct VFileFIFO*) vf;
94 return CircleBufferSize(vff->backing);
95}
96
97static bool _vffSync(struct VFile* vf, const void* buffer, size_t size) {
98 UNUSED(vf);
99 UNUSED(buffer);
100 UNUSED(size);
101 return true;
102}