Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
RamDisk.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_COMMON_RAMDISK_HXX_
35#define _FREERTOS_DRIVERS_COMMON_RAMDISK_HXX_
36
37#include <fcntl.h>
38#include "Devtab.hxx"
39
51class RamDiskBase : public Node
52{
53public:
60 RamDiskBase(const char *path, void *data, unsigned size, bool read_only)
61 : Node(path)
62 , data_((uint8_t *)data)
63 , size_(size)
64 , readOnly_(read_only ? 1 : 0)
65 , owned_(0)
66 {
67 }
68
69private:
70 void enable() OVERRIDE {}
71 void disable() OVERRIDE {}
73
75 int open(File *file, const char * name, int flags, int mode) OVERRIDE
76 {
77 Node::open(file, name, flags, mode);
78 if ((flags & O_TRUNC) && !readOnly_)
79 {
80 actualSize_ = 0;
81 }
82 return 0;
83 }
84
85 ssize_t read(File *file, void *buf, size_t count) OVERRIDE
86 {
87 if (file->offset >= file_size())
88 {
89 return 0;
90 }
91 size_t left = file_size() - file->offset;
92 count = std::min(count, left);
93 memcpy(buf, data_ + file->offset, count);
94 file->offset += count;
95 return count;
96 }
97
98 ssize_t write(File *file, const void *buf, size_t count) OVERRIDE
99 {
100 if (readOnly_)
101 {
102 return -EPERM;
103 }
104 if (file->offset >= size_)
105 {
106 return 0;
107 }
108 size_t left = size_ - file->offset;
109 count = std::min(count, left);
110 memcpy(data_ + file->offset, buf, count);
111 file->offset += count;
112 if (file->offset > (off_t)actualSize_)
113 {
114 actualSize_ = file->offset;
115 }
116 return count;
117 }
118
119protected:
123 off_t file_size()
124 {
125 if (readOnly_)
126 {
127 return size_;
128 }
129 else
130 {
131 return actualSize_;
132 }
133 }
134
136 uint8_t *data_;
138 unsigned actualSize_ = 0;
140 unsigned size_ : 30;
142 unsigned readOnly_ : 1;
144 unsigned owned_ : 1;
145};
146
152class RamDisk : public RamDiskBase
153{
154public:
156 RamDisk(const char *path, size_t size)
157 : RamDiskBase(path, new uint8_t[size], size, false)
158 {
159 memset(data_, 0, size_);
160 owned_ = 1;
161 }
162
169 template<class T>
170 RamDisk(const char* path, T* data, bool read_only = false)
171 : RamDiskBase(path, data, sizeof(T), read_only) {
172 owned_ = 0;
173 actualSize_ = sizeof(T);
174 }
175
176 ~RamDisk()
177 {
178 if (owned_) {
179 delete[] data_;
180 }
181 }
182};
183
184#endif // _FREERTOS_DRIVERS_COMMON_RAMDISK_HXX_
const char * name
device name
Definition Devtab.hxx:266
Node information.
Definition Devtab.hxx:549
int open(File *, const char *, int, int) OVERRIDE
Open method.
A simple device driver that reads/write data from a block of memory in RAM.
Definition RamDisk.hxx:52
RamDiskBase(const char *path, void *data, unsigned size, bool read_only)
Constructor.
Definition RamDisk.hxx:60
unsigned actualSize_
What's the larget file offset that we received an actual write for.
Definition RamDisk.hxx:138
unsigned owned_
1 if we own the data bytes and need to free()them upon exit.
Definition RamDisk.hxx:144
unsigned readOnly_
1 ifreadonly file.
Definition RamDisk.hxx:142
void flush_buffers() OVERRIDE
Instructs the device driver to drop all TX and RX queues.
Definition RamDisk.hxx:72
off_t file_size()
Definition RamDisk.hxx:123
int open(File *file, const char *name, int flags, int mode) OVERRIDE
Open method.
Definition RamDisk.hxx:75
ssize_t write(File *file, const void *buf, size_t count) OVERRIDE
Write to a file or device.
Definition RamDisk.hxx:98
void enable() OVERRIDE
This will be called once when reference-count goes from 0 to positive.
Definition RamDisk.hxx:70
uint8_t * data_
Pointer to data content.
Definition RamDisk.hxx:136
unsigned size_
How many bytes we are exporting.
Definition RamDisk.hxx:140
ssize_t read(File *file, void *buf, size_t count) OVERRIDE
Read from a file or device.
Definition RamDisk.hxx:85
void disable() OVERRIDE
This will be called when reference count goes from non-zero to 0.
Definition RamDisk.hxx:71
A simple device driver that reads/write data from a block of memory in a typed array either staticall...
Definition RamDisk.hxx:153
RamDisk(const char *path, size_t size)
Allocates space on the heap for the ramdisk of size ‘size’.
Definition RamDisk.hxx:156
RamDisk(const char *path, T *data, bool read_only=false)
Uses an existing variable for backing the ramdisk structure.
Definition RamDisk.hxx:170
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
File information.
Definition Devtab.hxx:52