Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : #include "precompiled.h"
24 :
25 : #include "write_buffer.h"
26 :
27 : #include "lib/alignment.h"
28 : #include "lib/allocators/shared_ptr.h"
29 : #include "lib/bits.h" // IsAligned
30 : #include "lib/debug.h"
31 : #include "lib/file/io/io.h"
32 : #include "lib/sysdep/rtl.h"
33 :
34 : #include <algorithm>
35 : #include <bit>
36 : #include <cstring>
37 :
38 : static const size_t BLOCK_SIZE = 512*KiB;
39 :
40 :
41 7813 : WriteBuffer::WriteBuffer() :
42 7813 : m_capacity{g_PageSize},
43 7813 : m_data{static_cast<std::uint8_t*>(rtl_AllocateAligned(m_capacity, maxSectorSize)), AlignedDeleter()},
44 7813 : m_size{0}
45 : {
46 7813 : }
47 :
48 :
49 1006868 : void WriteBuffer::EnsureSufficientCapacity(size_t size)
50 : {
51 1006868 : if(m_size + size > m_capacity)
52 : {
53 0 : m_capacity = std::bit_ceil(m_size + size);
54 0 : std::shared_ptr<std::uint8_t> newData;
55 0 : AllocateAligned(newData, m_capacity, maxSectorSize);
56 0 : memcpy(newData.get(), m_data.get(), m_size);
57 0 : m_data = newData;
58 0 : }
59 1006868 : }
60 :
61 :
62 1006868 : void WriteBuffer::Append(const void* data, size_t size)
63 : {
64 1006868 : EnsureSufficientCapacity(size);
65 1006868 : memcpy(m_data.get() + m_size, data, size);
66 1006868 : m_size += size;
67 1006868 : }
68 :
69 :
70 0 : void WriteBuffer::Reserve(size_t size)
71 : {
72 0 : EnsureSufficientCapacity(size);
73 0 : memset(m_data.get() + m_size, 0, size);
74 0 : m_size += size;
75 0 : }
76 :
77 :
78 249683 : void WriteBuffer::Overwrite(const void* data, size_t size, size_t offset)
79 : {
80 249683 : ENSURE(offset+size < m_size);
81 249683 : memcpy(m_data.get()+offset, data, size);
82 249683 : }
83 :
84 :
85 : //-----------------------------------------------------------------------------
86 : // UnalignedWriter
87 : //-----------------------------------------------------------------------------
88 :
89 0 : UnalignedWriter::UnalignedWriter(const PFile& file, off_t ofs) :
90 0 : m_file{file},
91 0 : m_alignedBuf{static_cast<std::uint8_t*>(rtl_AllocateAligned(BLOCK_SIZE, maxSectorSize)),
92 : AlignedDeleter()}
93 : {
94 0 : m_alignedOfs = round_down(ofs, (off_t)BLOCK_SIZE);
95 0 : const size_t misalignment = (size_t)(ofs - m_alignedOfs);
96 0 : if(misalignment)
97 : {
98 0 : io::Operation op(*m_file.get(), m_alignedBuf.get(), BLOCK_SIZE, m_alignedOfs);
99 0 : THROW_STATUS_IF_ERR(io::Run(op));
100 : }
101 0 : m_bytesUsed = misalignment;
102 0 : }
103 :
104 :
105 0 : UnalignedWriter::~UnalignedWriter()
106 : {
107 0 : Flush();
108 0 : }
109 :
110 :
111 0 : Status UnalignedWriter::Append(const std::uint8_t* data, size_t size) const
112 : {
113 0 : while(size != 0)
114 : {
115 : // optimization: write directly from the input buffer, if possible
116 0 : const size_t alignedSize = (size / BLOCK_SIZE) * BLOCK_SIZE;
117 0 : if(m_bytesUsed == 0 && IsAligned(data, maxSectorSize) && alignedSize != 0)
118 : {
119 0 : io::Operation op(*m_file.get(), (void*)data, alignedSize, m_alignedOfs);
120 0 : RETURN_STATUS_IF_ERR(io::Run(op));
121 0 : m_alignedOfs += (off_t)alignedSize;
122 0 : data += alignedSize;
123 0 : size -= alignedSize;
124 : }
125 :
126 0 : const size_t chunkSize = std::min(size, BLOCK_SIZE-m_bytesUsed);
127 0 : memcpy(m_alignedBuf.get()+m_bytesUsed, data, chunkSize);
128 0 : m_bytesUsed += chunkSize;
129 0 : data += chunkSize;
130 0 : size -= chunkSize;
131 :
132 0 : if(m_bytesUsed == BLOCK_SIZE)
133 0 : RETURN_STATUS_IF_ERR(WriteBlock());
134 : }
135 :
136 : return INFO::OK;
137 : }
138 :
139 :
140 0 : void UnalignedWriter::Flush() const
141 : {
142 0 : if(m_bytesUsed)
143 : {
144 0 : memset(m_alignedBuf.get()+m_bytesUsed, 0, BLOCK_SIZE-m_bytesUsed);
145 0 : (void)WriteBlock();
146 : }
147 0 : }
148 :
149 :
150 0 : Status UnalignedWriter::WriteBlock() const
151 : {
152 0 : io::Operation op(*m_file.get(), m_alignedBuf.get(), BLOCK_SIZE, m_alignedOfs);
153 0 : RETURN_STATUS_IF_ERR(io::Run(op));
154 0 : m_alignedOfs += BLOCK_SIZE;
155 0 : m_bytesUsed = 0;
156 0 : return INFO::OK;
157 : }
|