Debian Codegraph @

mednafen/MemoryStream.cpp

raw symbols SCIP
1 #include <stdint.h>
2 #include "MemoryStream.h"
3
4 #ifdef _XBOX
5 #ifndef SIZE_MAX
6 #define SIZE_MAX _UI32_MAX
7 #endif
8 #endif
9
10 /*
11 TODO:
12 Write and Seek expansion that fail should not corrupt the state.
13
14 Copy and assignment constructor fixes.
15 */
16
17 // TODO 128-bit integers for range checking?
18
19
20 MemoryStream::MemoryStream() : data_buffer(NULL), data_buffer_size(0), data_buffer_alloced(0), position(0)
21 {
22 data_buffer_size = 0;
23 data_buffer_alloced = 64;
24 if(!(data_buffer = (uint8*)realloc(data_buffer, data_buffer_alloced)))
25 throw MDFN_Error(ErrnoHolder(errno));
26 }
27
28 MemoryStream::MemoryStream(uint64 size_hint) : data_buffer(NULL), data_buffer_size(0), data_buffer_alloced(0), position(0)
29 {
30 data_buffer_size = 0;
31 data_buffer_alloced = (size_hint > SIZE_MAX) ? SIZE_MAX : size_hint;
32
33 if(!(data_buffer = (uint8*)realloc(data_buffer, data_buffer_alloced)))
34 throw MDFN_Error(ErrnoHolder(errno));
35 }
36
37 MemoryStream::MemoryStream(Stream *stream) : data_buffer(NULL), data_buffer_size(0), data_buffer_alloced(0), position(0)
38 {
39 if((position = stream->tell()) != 0)
40 stream->seek(0, SEEK_SET);
41
42 data_buffer_size = stream->size();
43 data_buffer_alloced = data_buffer_size;
44 if(!(data_buffer = (uint8*)realloc(data_buffer, data_buffer_alloced)))
45 throw MDFN_Error(ErrnoHolder(errno));
46
47 stream->read(data_buffer, data_buffer_size);
48
49 stream->close();
50 delete stream;
51 }
52
53 MemoryStream::MemoryStream(const MemoryStream &zs)
54 {
55 data_buffer_size = zs.data_buffer_size;
56 data_buffer_alloced = zs.data_buffer_alloced;
57 if(!(data_buffer = (uint8*)malloc(data_buffer_alloced)))
58 throw MDFN_Error(ErrnoHolder(errno));
59
60 memcpy(data_buffer, zs.data_buffer, data_buffer_size);
61
62 position = zs.position;
63 }
64
65 MemoryStream::~MemoryStream()
66 {
67 if(data_buffer)
68 {
69 free(data_buffer);
70 data_buffer = NULL;
71 }
72 }
73
74 uint64 MemoryStream::attributes(void)
75 {
76 return (ATTRIBUTE_READABLE | ATTRIBUTE_WRITEABLE | ATTRIBUTE_SEEKABLE);
77 }
78
79
80 uint8 *MemoryStream::map(void)
81 {
82 return data_buffer;
83 }
84
85 void MemoryStream::unmap(void)
86 {
87
88 }
89
90
91 INLINE void MemoryStream::grow_if_necessary(uint64 new_required_size)
92 {
93 if(new_required_size > data_buffer_size)
94 {
95 if(new_required_size > data_buffer_alloced)
96 {
97 uint64 new_required_alloced = round_up_pow2(new_required_size);
98 uint8 *new_data_buffer;
99
100 // first condition will happen at new_required_size > (1ULL << 63) due to round_up_pow2() "wrapping".
101 // second condition can occur when running on a 32-bit system.
102 if(new_required_alloced < new_required_size || new_required_alloced > SIZE_MAX)
103 new_required_alloced = SIZE_MAX;
104
105 // If constrained alloc size isn't enough, throw an out-of-memory/address-space type error.
106 if(new_required_alloced < new_required_size)
107 throw MDFN_Error(ErrnoHolder(ENOMEM));
108
109 if(!(new_data_buffer = (uint8*)realloc(data_buffer, new_required_alloced)))
110 throw MDFN_Error(ErrnoHolder(errno));
111
112 //
113 // Assign all in one go after the realloc() so we don't leave our object in an inconsistent state if the realloc() fails.
114 //
115 data_buffer = new_data_buffer;
116 data_buffer_size = new_required_size;
117 data_buffer_alloced = new_required_alloced;
118 }
119 else
120 data_buffer_size = new_required_size;
121 }
122 }
123
124 uint64 MemoryStream::read(void *data, uint64 count, bool error_on_eos)
125 {
126 if(count > data_buffer_size)
127 {
128 count = data_buffer_size;
129 }
130
131 if((uint64)position > (data_buffer_size - count))
132 {
133 count = data_buffer_size - position;
134 }
135
136 memmove(data, &data_buffer[position], count);
137 position += count;
138
139 return count;
140 }
141
142 void MemoryStream::write(const void *data, uint64 count)
143 {
144 uint64 nrs = position + count;
145
146 if(nrs < position)
147 throw MDFN_Error(ErrnoHolder(EFBIG));
148
149 grow_if_necessary(nrs);
150
151 memmove(&data_buffer[position], data, count);
152 position += count;
153 }
154
155 void MemoryStream::seek(int64 offset, int whence)
156 {
157 int64 new_position;
158
159 switch(whence)
160 {
161 default:
162 throw MDFN_Error(ErrnoHolder(EINVAL));
163 break;
164
165 case SEEK_SET:
166 new_position = offset;
167 break;
168
169 case SEEK_CUR:
170 new_position = position + offset;
171 break;
172
173 case SEEK_END:
174 new_position = data_buffer_size + offset;
175 break;
176 }
177
178 if(new_position < 0)
179 throw MDFN_Error(ErrnoHolder(EINVAL));
180 else
181 grow_if_necessary(new_position);
182
183 position = new_position;
184 }
185
186 int64 MemoryStream::tell(void)
187 {
188 return position;
189 }
190
191 int64 MemoryStream::size(void)
192 {
193 return data_buffer_size;
194 }
195
196 void MemoryStream::close(void)
197 {
198
199 }
200
201
202 int MemoryStream::get_line(std::string &str)
203 {
204 str.clear(); // or str.resize(0)??
205
206 while(position < data_buffer_size)
207 {
208 uint8 c = data_buffer[position++];
209
210 if(c == '\r' || c == '\n' || c == 0)
211 return(c);
212
213 str.push_back(c); // Should be faster than str.append(1, c)
214 }
215
216 return(-1);
217 }
218