MCPcopy Create free account
hub / github.com/cuberite/cuberite / Write

Method Write

src/ByteBuffer.cpp:174–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

172
173
174bool cByteBuffer::Write(const char * a_Bytes, size_t a_Count)
175{
176 CHECK_THREAD;
177 CheckValid();
178
179 // Store the current free space for a check after writing:
180 size_t CurFreeSpace = GetFreeSpace();
181 size_t CurReadableSpace = GetReadableSpace();
182 size_t WrittenBytes = 0;
183
184 if (CurFreeSpace < a_Count)
185 {
186 return false;
187 }
188 ASSERT(m_BufferSize >= m_WritePos);
189 size_t TillEnd = m_BufferSize - m_WritePos;
190 if (TillEnd <= a_Count)
191 {
192 // Need to wrap around the ringbuffer end
193 if (TillEnd > 0)
194 {
195 memcpy(m_Buffer + m_WritePos, a_Bytes, TillEnd);
196 a_Bytes += TillEnd;
197 a_Count -= TillEnd;
198 WrittenBytes = TillEnd;
199 }
200 m_WritePos = 0;
201 }
202
203 // We're guaranteed that we'll fit in a single write op
204 if (a_Count > 0)
205 {
206 memcpy(m_Buffer + m_WritePos, a_Bytes, a_Count);
207 m_WritePos += a_Count;
208 WrittenBytes += a_Count;
209 }
210
211 ASSERT(GetFreeSpace() == CurFreeSpace - WrittenBytes);
212 ASSERT(GetReadableSpace() == CurReadableSpace + WrittenBytes);
213 return true;
214}
215
216
217

Callers 3

TestReadMethod · 0.45
TestWrapMethod · 0.45
ReadToByteBufferMethod · 0.45

Calls

no outgoing calls

Tested by 2

TestReadMethod · 0.36
TestWrapMethod · 0.36