summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/ioprocs.cpp
blob: 0dcea206571b2f05bf2553e66cf5350e6f73c03e (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

    ioprocs.h

    I/O interfaces

***************************************************************************/

#include "ioprocs.h"

#include "corefile.h"
#include "ioprocsfill.h"

#include "osdfile.h"

#include <algorithm>
#include <cassert>
#include <cerrno>
#include <cstring>
#include <iterator>
#include <limits>
#include <type_traits>


namespace util {

namespace {

// helper for holding a block of memory and deallocating it (or not) as necessary

template <typename T, bool Owned>
class ram_adapter_base : public virtual random_access
{
public:
	virtual ~ram_adapter_base()
	{
		if constexpr (Owned)
			std::free(m_data);
	}

	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:
			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
	{
		if (std::numeric_limits<uint64_t>::max() < m_size)
			return std::errc::file_too_large;

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

protected:
	template <typename U>
	ram_adapter_base(U *data, std::size_t size) noexcept : m_data(reinterpret_cast<T>(data)), m_size(size)
	{
		static_assert(sizeof(*m_data) == 1U, "Element type must be byte-sized");
		assert(m_data || !m_size);
	}

	T m_data;
	std::uint64_t m_pointer = 0U;
	std::size_t m_size;
};


// RAM read implementation

template <typename T, bool Owned>
class ram_read_adapter : public ram_adapter_base<T, Owned>, public virtual random_read
{
public:
	template <typename U>
	ram_read_adapter(U *data, std::size_t size) noexcept : ram_adapter_base<T, Owned>(data, size)
	{
	}

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

	virtual std::error_condition read_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();
	}

private:
	void do_read(std::uint64_t offset, void *buffer, std::size_t length, std::size_t &actual) const noexcept
	{
		if ((offset < this->m_size) && length)
		{
			actual = std::min(std::size_t(this->m_size - offset), length);
			if constexpr (Owned)
				std::memcpy(buffer, this->m_data + offset, actual);
			else
				std::memmove(buffer, this->m_data + offset, actual);
		}
		else
		{
			actual = 0U;
		}
	}
};


// helper for holding a stdio FILE and closing it (or not) as necessary

class stdio_adapter_base : public virtual random_access
{
public:
	virtual ~stdio_adapter_base()
	{
		if (m_close)
			std::fclose(m_file);
	}

	virtual std::error_condition seek(std::int64_t offset, int whence) noexcept override
	{
		if ((std::numeric_limits<long>::max() < offset) || (std::numeric_limits<long>::min() > offset))
			return std::errc::invalid_argument;
		else if (!std::fseek(m_file, long(offset), whence))
			return std::error_condition();
		else
			return std::error_condition(errno, std::generic_category());
	}

	virtual std::error_condition tell(std::uint64_t &result) noexcept override
	{
		long const pos = std::ftell(m_file);
		if (0 > pos)
			return std::error_condition(errno, std::generic_category());
		result = static_cast<unsigned long>(pos);
		return std::error_condition();
	}

	virtual std::error_condition length(std::uint64_t &result) noexcept override
	{
		std::fpos_t oldpos;
		if (std::fgetpos(m_file, &oldpos))
			return std::error_condition(errno, std::generic_category());

		long endpos = -1;
		if (!std::fseek(m_file, 0, SEEK_END))
		{
			m_dangling_read = m_dangling_write = false;
			endpos = std::ftell(m_file);
		}
		std::error_condition err;
		if (0 > endpos)
			err.assign(errno, std::generic_category());
		else if (std::numeric_limits<std::uint64_t>::max() < static_cast<unsigned long>(endpos))
			err = std::errc::file_too_large;
		else
			result = static_cast<unsigned long>(endpos);

		if (!std::fsetpos(m_file, &oldpos))
			m_dangling_read = m_dangling_write = false;
		else if (!err)
			err.assign(errno, std::generic_category());

		return err;
	}

protected:
	stdio_adapter_base(FILE *file, bool close) noexcept : m_file(file), m_close(close)
	{
		assert(m_file);
	}

	FILE *file() noexcept
	{
		return m_file;
	}

private:
	FILE *const m_file;
	bool const m_close;

protected:
	bool m_dangling_read = true, m_dangling_write = true;
};


// stdio read implementation

class stdio_read_adapter : public stdio_adapter_base, public virtual random_read
{
public:
	stdio_read_adapter(FILE *file, bool close) noexcept : stdio_adapter_base(file, close)
	{
	}

	virtual std::error_condition read(void *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		if (m_dangling_write)
		{
			if (std::fflush(file()))
			{
				std::clearerr(file());
				actual = 0U;
				return std::error_condition(errno, std::generic_category());
			}
			m_dangling_write = false;
		}

		actual = std::fread(buffer, sizeof(std::uint8_t), length, file());
		m_dangling_read = true;
		if (length != actual)
		{
			if (std::ferror(file()))
			{
				std::clearerr(file());
				return std::error_condition(errno, std::generic_category());
			}
			else if (std::feof(file()))
			{
				m_dangling_read = false;
			}
		}

		return std::error_condition();
	}

	virtual std::error_condition read_at(std::uint64_t offset, void *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		actual = 0U;

		if (static_cast<unsigned long>(std::numeric_limits<long>::max()) < offset)
			return std::errc::invalid_argument;

		std::fpos_t oldpos;
		if (std::fgetpos(file(), &oldpos))
			return std::error_condition(errno, std::generic_category());

		std::error_condition err;
		if (std::fseek(file(), long(static_cast<unsigned long>(offset)), SEEK_SET))
		{
			err.assign(errno, std::generic_category());
		}
		else
		{
			m_dangling_write = false;
			actual = std::fread(buffer, sizeof(std::uint8_t), length, file());
			m_dangling_read = true;
			if (length != actual)
			{
				if (std::ferror(file()))
				{
					err.assign(errno, std::generic_category());
					std::clearerr(file());
				}
				else if (std::feof(file()))
				{
					m_dangling_read = false;
				}
			}
		}

		if (!std::fsetpos(file(), &oldpos))
			m_dangling_read = m_dangling_write = false;
		else if (!err)
			err.assign(errno, std::generic_category());

		return err;
	}
};


// stdio read/write implementation

class stdio_read_write_adapter : public stdio_read_adapter, public random_read_write
{
public:
	using stdio_read_adapter::stdio_read_adapter;

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

	virtual std::error_condition flush() noexcept override
	{
		if (std::fflush(file()))
		{
			std::clearerr(file());
			return std::error_condition(errno, std::generic_category());
		}
		m_dangling_write = false;
		return std::error_condition();
	}

	virtual std::error_condition write(void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		if (m_dangling_read)
		{
			if (std::fseek(file(), 0, SEEK_CUR))
			{
				actual = 0U;
				return std::error_condition(errno, std::generic_category());
			}
			m_dangling_read = false;
		}

		actual = std::fwrite(buffer, sizeof(std::uint8_t), length, file());
		m_dangling_write = true;
		if (length != actual)
		{
			std::clearerr(file());
			return std::error_condition(errno, std::generic_category());
		}

		return std::error_condition();
	}

	virtual std::error_condition write_at(std::uint64_t offset, void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		actual = 0U;

		if (static_cast<unsigned long>(std::numeric_limits<long>::max()) < offset)
			return std::errc::invalid_argument;

		std::fpos_t oldpos;
		if (std::fgetpos(file(), &oldpos))
			return std::error_condition(errno, std::generic_category());

		std::error_condition err;
		if (std::fseek(file(), long(static_cast<unsigned long>(offset)), SEEK_SET))
		{
			err.assign(errno, std::generic_category());
		}
		else
		{
			m_dangling_read = false;
			actual = std::fwrite(buffer, sizeof(std::uint8_t), length, file());
			m_dangling_write = true;
			if (length != actual)
			{
				err.assign(errno, std::generic_category());
				std::clearerr(file());
			}
		}

		if (!std::fsetpos(file(), &oldpos))
			m_dangling_read = m_dangling_write = false;
		else if (!err)
			err.assign(errno, std::generic_category());

		return err;
	}
};


// stdio helper that fills space when writing past the end-of-file

class stdio_read_write_filler : public random_read_fill_wrapper<stdio_read_write_adapter>
{
public:
	stdio_read_write_filler(FILE *file, bool close, std::uint8_t fill) noexcept : random_read_fill_wrapper<stdio_read_write_adapter>(file, close)
	{
		set_filler(fill);
	}

	virtual std::error_condition write(void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		actual = 0U;

		long const offset = std::ftell(file());
		if (0 > offset)
			return std::error_condition(errno, std::generic_category());

		if (std::fseek(file(), 0, SEEK_END))
		{
			std::error_condition err(errno, std::generic_category());
			if (!std::fseek(file(), offset, SEEK_SET))
				m_dangling_read = m_dangling_write = false;
			return err;
		}
		m_dangling_read = m_dangling_write = false;
		long endpos = std::ftell(file());
		if (0 > endpos)
		{
			std::error_condition err(errno, std::generic_category());
			std::fseek(file(), offset, SEEK_SET);
			return err;
		}

		if (offset > endpos)
		{
			std::uint8_t block[1024];
			std::fill_n(
					block,
					std::min<std::common_type_t<std::size_t, std::uint64_t, long> >(std::size(block), offset - endpos),
					get_filler());
			do
			{
				std::size_t const chunk = std::min<std::common_type_t<std::size_t, std::uint64_t, long> >(std::size(block), offset - endpos);
				std::size_t const filled = std::fwrite(block, sizeof(block[0]), chunk, file());
				endpos += filled;
				m_dangling_write = true;
				if (chunk != filled)
				{
					std::error_condition err(errno, std::generic_category());
					std::clearerr(file());
					if (!std::fseek(file(), offset, SEEK_SET))
						m_dangling_write = false;
					return err;
				}
			}
			while (static_cast<unsigned long>(endpos) < offset);
		}
		else if ((offset < endpos) && std::fseek(file(), offset, SEEK_SET))
		{
			return std::error_condition(errno, std::generic_category());
		}

		actual = std::fwrite(buffer, sizeof(std::uint8_t), length, file());
		m_dangling_write = true;
		if (length != actual)
		{
			std::clearerr(file());
			return std::error_condition(errno, std::generic_category());
		}

		return std::error_condition();
	}

	virtual std::error_condition write_at(std::uint64_t offset, void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		actual = 0U;

		if (static_cast<unsigned long>(std::numeric_limits<long>::max()) < offset)
			return std::errc::invalid_argument;

		std::fpos_t oldpos;
		if (std::fgetpos(file(), &oldpos))
			return std::error_condition(errno, std::generic_category());

		std::error_condition err;
		if (std::fseek(file(), 0, SEEK_END))
		{
			err.assign(errno, std::generic_category());
		}
		else
		{
			m_dangling_read = m_dangling_write = false;
			long endpos = std::ftell(file());
			if (0 > endpos)
			{
				err.assign(errno, std::generic_category());
			}
			else if (static_cast<unsigned long>(endpos) > offset)
			{
				if (std::fseek(file(), long(static_cast<unsigned long>(offset)), SEEK_SET))
					err.assign(errno, std::generic_category());
			}
			else if (static_cast<unsigned long>(endpos) < offset)
			{
				std::uint8_t block[1024];
				std::fill_n(
						block,
						std::min<std::common_type_t<std::size_t, std::uint64_t, long> >(std::size(block), offset - endpos),
						get_filler());
				do
				{
					std::size_t const chunk = std::min<std::common_type_t<std::size_t, std::uint64_t, long> >(std::size(block), offset - endpos);
					std::size_t const filled = std::fwrite(block, sizeof(block[0]), chunk, file());
					endpos += filled;
					m_dangling_write = true;
					if (chunk != filled)
					{
						err.assign(errno, std::generic_category());
						std::clearerr(file());
					}
				}
				while (!err && (static_cast<unsigned long>(endpos) < offset));
			}
		}

		if (!err)
		{
			actual = std::fwrite(buffer, sizeof(std::uint8_t), length, file());
			m_dangling_write = true;
			if (length != actual)
			{
				err.assign(errno, std::generic_category());
				std::clearerr(file());
			}
		}

		if (!std::fsetpos(file(), &oldpos))
			m_dangling_read = m_dangling_write = false;
		else if (!err)
			err.assign(errno, std::generic_category());

		return err;
	}
};


// helper class for holding an osd_file and closing it (or not) as necessary

class osd_file_adapter_base : public virtual random_access
{
public:
	virtual ~osd_file_adapter_base()
	{
		if (m_close)
			delete m_file;
	}

	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();

		// TODO: add SEEK_END when osd_file can support it - should it return a different error?
		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
	{
		// not supported by osd_file
		return std::errc::not_supported; // TODO: revisit this error code
	}

protected:
	osd_file_adapter_base(osd_file::ptr &&file) noexcept : m_file(file.release()), m_close(true)
	{
		assert(m_file);
	}

	osd_file_adapter_base(osd_file &file) noexcept : m_file(&file), m_close(false)
	{
	}

	osd_file &file() noexcept
	{
		return *m_file;
	}

	std::uint64_t m_pointer = 0U;

private:
	osd_file *const m_file;
	bool const m_close;
};


// osd_file read implementation

class osd_file_read_adapter : public osd_file_adapter_base, public virtual random_read
{
public:
	osd_file_read_adapter(osd_file::ptr &&file) noexcept : osd_file_adapter_base(std::move(file))
	{
	}

	osd_file_read_adapter(osd_file &file) noexcept : osd_file_adapter_base(file)
	{
	}

	virtual std::error_condition read(void *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		// TODO: should the client have to deal with reading less than expected even if EOF isn't hit?
		if (std::numeric_limits<std::uint32_t>::max() < length)
		{
			actual = 0U;
			return std::errc::invalid_argument;
		}

		// actual length not valid on error
		std::uint32_t count;
		std::error_condition err = file().read(buffer, m_pointer, std::uint32_t(length), count);
		if (!err)
		{
			m_pointer += count;
			actual = std::size_t(count);
		}
		else
		{
			actual = 0U;
		}
		return err;
	}

	virtual std::error_condition read_at(std::uint64_t offset, void *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		// TODO: should the client have to deal with reading less than expected even if EOF isn't hit?
		if (std::numeric_limits<std::uint32_t>::max() < length)
		{
			actual = 0U;
			return std::errc::invalid_argument;
		}

		// actual length not valid on error
		std::uint32_t count;
		std::error_condition err = file().read(buffer, offset, std::uint32_t(length), count);
		if (!err)
			actual = std::size_t(count);
		else
			actual = 0U;
		return err;
	}
};


// osd_file read/write implementation

class osd_file_read_write_adapter : public osd_file_read_adapter, public random_read_write
{
public:
	using osd_file_read_adapter::osd_file_read_adapter;

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

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

	virtual std::error_condition write(void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		actual = 0U;
		while (length)
		{
			// actual length not valid on error
			std::uint32_t const chunk = std::min<std::common_type_t<std::uint32_t, std::size_t> >(std::numeric_limits<std::uint32_t>::max(), length);
			std::uint32_t written;
			std::error_condition err = file().write(buffer, m_pointer, chunk, written);
			if (err)
				return err;
			m_pointer += written;
			buffer = reinterpret_cast<std::uint8_t const *>(buffer) + written;
			length -= written;
			actual += written;
		}
		return std::error_condition();
	}

	virtual std::error_condition write_at(std::uint64_t offset, void const *buffer, std::size_t length, std::size_t &actual) noexcept override
	{
		actual = 0U;
		while (length)
		{
			// actual length not valid on error
			std::uint32_t const chunk = std::min<std::common_type_t<std::uint32_t, std::size_t> >(std::numeric_limits<std::uint32_t>::max(), length);
			std::uint32_t written;
			std::error_condition err = file().write(buffer, offset, chunk, written);
			if (err)
				return err;
			offset += written;
			buffer = reinterpret_cast<std::uint8_t const *>(buffer) + written;
			length -= written;
			actual += written;
		}
		return std::error_condition();
	}
};

} // anonymous namespace


// creating RAM read adapters

random_read::ptr ram_read(void const *data, std::size_t size) noexcept
{
	random_read::ptr result;
	if (data || !size)
		result.reset(new (std::nothrow) ram_read_adapter<std::uint8_t const *const, false>(data, size));
	return result;
}

random_read::ptr ram_read(void const *data, std::size_t size, std::uint8_t filler) noexcept
{
	std::unique_ptr<random_read_fill_wrapper<ram_read_adapter<std::uint8_t const *const, false> > > result;
	if (data || !size)
		result.reset(new (std::nothrow) decltype(result)::element_type(data, size));
	if (result)
		result->set_filler(filler);
	return result;
}

random_read::ptr ram_read_copy(void const *data, std::size_t size) noexcept
{
	random_read::ptr result;
	void *const copy = size ? std::malloc(size) : nullptr;
	if (copy)
		std::memcpy(copy, data, size);
	if (copy || !size)
		result.reset(new (std::nothrow) ram_read_adapter<std::uint8_t *const, true>(copy, size));
	if (!result)
		std::free(copy);
	return result;
}

random_read::ptr ram_read_copy(void const *data, std::size_t size, std::uint8_t filler) noexcept
{
	std::unique_ptr<random_read_fill_wrapper<ram_read_adapter<std::uint8_t *const, true> > > result;
	void *const copy = size ? std::malloc(size) : nullptr;
	if (copy)
		std::memcpy(copy, data, size);
	if (copy || !size)
		result.reset(new (std::nothrow) decltype(result)::element_type(copy, size));
	if (!result)
		std::free(copy);
	return result;
}


// creating stdio read adapters

random_read::ptr stdio_read(FILE *file) noexcept
{
	random_read::ptr result;
	if (file)
		result.reset(new (std::nothrow) stdio_read_adapter(file, true));
	return result;
}

random_read::ptr stdio_read(FILE *file, std::uint8_t filler) noexcept
{
	std::unique_ptr<random_read_fill_wrapper<stdio_read_adapter> > result;
	if (file)
		result.reset(new (std::nothrow) decltype(result)::element_type(file, true));
	if (result)
		result->set_filler(filler);
	return result;
}

random_read::ptr stdio_read_noclose(FILE *file) noexcept
{
	random_read::ptr result;
	if (file)
		result.reset(new (std::nothrow) stdio_read_adapter(file, false));
	return result;
}

random_read::ptr stdio_read_noclose(FILE *file, std::uint8_t filler) noexcept
{
	std::unique_ptr<random_read_fill_wrapper<stdio_read_adapter> > result;
	if (file)
		result.reset(new (std::nothrow) decltype(result)::element_type(file, false));
	if (result)
		result->set_filler(filler);
	return result;
}


// creating stdio read/write adapters

random_read_write::ptr stdio_read_write(FILE *file) noexcept
{
	random_read_write::ptr result;
	if (file)
		result.reset(new (std::nothrow) stdio_read_write_adapter(file, true));
	return result;
}

random_read_write::ptr stdio_read_write(FILE *file, std::uint8_t filler) noexcept
{
	random_read_write::ptr result;
	if (file)
		result.reset(new (std::nothrow) stdio_read_write_filler(file, true, filler));
	return result;
}

random_read_write::ptr stdio_read_write_noclose(FILE *file) noexcept
{
	random_read_write::ptr result;
	if (file)
		result.reset(new (std::nothrow) stdio_read_write_adapter(file, false));
	return result;
}

random_read_write::ptr stdio_read_write_noclose(FILE *file, std::uint8_t filler) noexcept
{
	random_read_write::ptr result;
	if (file)
		result.reset(new (std::nothrow) stdio_read_write_filler(file, false, filler));
	return result;
}


// creating osd_file read adapters

random_read::ptr osd_file_read(osd_file::ptr &&file) noexcept
{
	random_read::ptr result;
	if (file)
		result.reset(new (std::nothrow) osd_file_read_adapter(std::move(file)));
	return result;
}

random_read::ptr osd_file_read(osd_file &file) noexcept
{
	return random_read::ptr(new (std::nothrow) osd_file_read_adapter(file));
}


// creating osd_file read/write adapters

random_read_write::ptr osd_file_read_write(osd_file::ptr &&file) noexcept
{
	random_read_write::ptr result;
	if (file)
		result.reset(new (std::nothrow) osd_file_read_write_adapter(std::move(file)));
	return result;
}

random_read_write::ptr osd_file_read_write(osd_file &file) noexcept
{
	return random_read_write::ptr(new (std::nothrow) osd_file_read_write_adapter(file));
}

} // namespace util