summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/ioprocsvec.h
blob: e66000b57292d823d8c64b3d7db853286e153af3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

    ioprocsvec.h

    Helper for using a vector as an in-memory file

***************************************************************************/
#ifndef MAME_LIB_UTIL_IOPROCSVEC_H
#define MAME_LIB_UTIL_IOPROCSVEC_H

#include "ioprocs.h"

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <system_error>
#include <type_traits>
#include <vector>


namespace util {

template <typename T = std::uint8_t>
class vector_read_write_adapter : public random_read_write
{
public:
	vector_read_write_adapter(std::vector<T> &storage) noexcept : m_storage(storage)
	{
		check_resized();
	}

	virtual std::error_condition seek(std::int64_t offset, int whence) noexcept override
	{
		switch (whence)
		{
		case SEEK_SET:
			if (0 > offset)
				return std::errc::invalid_argument;
			m_pointer = std::uint64_t(offset);
			return std::error_condition();

		case SEEK_CUR:
			if (0 > offset)
			{
				if (std::uint64_t(-offset) > m_pointer)
					return std::errc::invalid_argument;
			}
			else if ((std::numeric_limits<std::uint64_t>::max() - offset) < m_pointer)
			{
				return std::errc::invalid_argument;
			}
			m_pointer += offset;
			return std::error_condition();

		case SEEK_END:
			check_resized();
			if (0 > offset)
			{
				if (std::uint64_t(-offset) > m_size)
					return std::errc::invalid_argument;
			}
			else if ((std::numeric_limits<std::uint64_t>::max() - offset) < m_size)
			{
				return std::errc::invalid_argument;
			}
			m_pointer = std::uint64_t(m_size) + offset;
			return std::error_condition();

		default:
			return std::errc::invalid_argument;
		}
	}

	virtual std::error_condition tell(std::uint64_t &result) noexcept override
	{
		result = m_pointer;
		return std::error_condition();
	}

	virtual std::error_condition length(std::uint64_t &result) noexcept override
	{
		check_resized();
		if (std::numeric_limits<uint64_t>::max() < m_size)
			return std::errc::file_too_large;

		result = m_size;
		return std::error_condition();
	}

	virtual std::error_condition read_some(void *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		do_read(m_pointer, buffer, length, actual);
		m_pointer += actual;
		return std::error_condition();
	}

	virtual std::error_condition read_some_at(std::uint64_t offset, void *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		do_read(offset, buffer, length, actual);
		return std::error_condition();
	}

	virtual std::error_condition finalize() noexcept override
	{
		return std::error_condition();
	}

	virtual std::error_condition flush() noexcept override
	{
		return std::error_condition();
	}

	virtual std::error_condition write_some(void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		std::error_condition result = do_write(m_pointer, buffer, length, actual);
		m_pointer += actual;
		return result;
	}

	virtual std::error_condition write_some_at(std::uint64_t offset, void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		return do_write(offset, buffer, length, actual);
	}

private:
	using elements_type = typename std::vector<T>::size_type;
	using common_size_type = std::common_type_t<std::uint64_t, std::size_t>;

	void check_resized() noexcept
	{
		if (m_storage.size() != m_elements)
		{
			m_elements = m_storage.size();
			m_size = m_elements * sizeof(T);
		}
	}

	void do_read(std::uint64_t offset, void *buffer, std::size_t length, std::size_t &actual) noexcept
	{
		check_resized();
		if ((offset < m_size) && length)
		{
			actual = std::min(std::size_t(m_size - offset), length);
			std::memmove(buffer, reinterpret_cast<std::uint8_t *>(m_storage.data()) + offset, actual);
		}
		else
		{
			actual = 0U;
		}
	}

	std::error_condition do_write(std::uint64_t offset, void const *buffer, std::size_t length, std::size_t &actual) noexcept
	{
		check_resized();
		std::size_t allocated = m_elements * sizeof(T);
		std::uint64_t const target = ((std::numeric_limits<std::uint64_t>::max() - offset) >= length)
				? (offset + length)
				: std::numeric_limits<std::uint64_t>::max();

		std::error_condition result;
		if (target > allocated)
		{
			elements_type const required = std::min<std::common_type_t<elements_type, std::uint64_t> >(
					std::numeric_limits<elements_type>::max(),
					(target + sizeof(T) - 1) / sizeof(T)); // technically can overflow but unlikely
			try { m_storage.resize(required); } // unpredictable if constructing/assigning T can throw
			catch (...) { result = std::errc::not_enough_memory; }
			m_elements = m_storage.size();
			allocated = m_elements * sizeof(T);
			assert(allocated >= m_size);

			if (offset > m_size)
			{
				std::size_t const new_size = std::size_t(std::min<common_size_type>(allocated, offset));
				std::memset(
						reinterpret_cast<std::uint8_t *>(m_storage.data()) + m_size,
						0x00, // POSIX says unwritten areas of files should read as zero
						new_size - m_size);
				m_size = new_size;
			}
		}

		std::size_t const limit = std::min<common_size_type>(target, allocated);
		if (limit > offset)
		{
			actual = std::size_t(limit - offset);
			std::memmove(reinterpret_cast<std::uint8_t *>(m_storage.data()) + offset, buffer, actual);
			if (limit > m_size)
				m_size = limit;
		}
		else
		{
			actual = 0U;
		}
		if (!result && (actual < length))
			result = std::errc::no_space_on_device;

		return result;
	}

	std::vector<T> &m_storage;
	std::uint64_t m_pointer = 0U;
	elements_type m_elements = 0U;
	std::size_t m_size = 0U;
};

} // namespace util

#endif // MAME_LIB_UTIL_IOPROCSVEC_H