Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/MemoryStream.h
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*/
17
18
/*
19
Notes:
20
For performance reasons(like in the state rewinding code), we should try to make sure map()
21
returns a pointer that is aligned to at least what malloc()/realloc() provides.
22
(And maybe forcefully align it to at least 16 bytes in the future)
23
*/
24
25
#ifndef __MDFN_MEMORYSTREAM_H
26
#define __MDFN_MEMORYSTREAM_H
27
28
#include "Stream.h"
29
30
class MemoryStream : public Stream
31
{
32
public:
33
34
MemoryStream();
35
MemoryStream(uint64 alloc_hint, int alloc_hint_is_size = false); // Pass -1 instead of 1 for alloc_hint_is_size to skip initialization of the memory.
36
MemoryStream(Stream *stream, uint64 size_limit = ~(uint64)0);
37
// Will create a MemoryStream equivalent of the contents of "stream", and then "delete stream".
38
// Will only work if stream->tell() == 0, or if "stream" is seekable.
39
// stream will be deleted even if this constructor throws.
40
//
41
// Will throw an exception if the initial size() of the MemoryStream would be greater than size_limit(useful for when passing
42
// in GZFileStream streams).
43
44
MemoryStream(const MemoryStream &zs);
45
MemoryStream & operator=(const MemoryStream &zs);
46
47
virtual ~MemoryStream() override;
48
49
virtual uint64 attributes(void) override;
50
51
virtual uint8 *map(void) noexcept override;
52
virtual uint64 map_size(void) noexcept override;
53
virtual void unmap(void) noexcept override;
54
55
virtual uint64 read(void *data, uint64 count, bool error_on_eos = true) override;
56
virtual void write(const void *data, uint64 count) override;
57
virtual void truncate(uint64 length) override;
58
virtual void seek(int64 offset, int whence) override;
59
virtual uint64 tell(void) override;
60
virtual uint64 size(void) override;
61
virtual void flush(void) override;
62
virtual void close(void) override;
63
64
virtual int get_line(std::string &str) override;
65
66
void shrink_to_fit(void) noexcept; // Minimizes alloced memory.
67
68
private:
69
uint8 *data_buffer;
70
uint64 data_buffer_size;
71
uint64 data_buffer_alloced;
72
73
uint64 position;
74
75
void grow_if_necessary(uint64 new_required_size, uint64 hole_end);
76
};
77
#endif
78
79