mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
this snowballed into a massive search+destroy of the hodgepodge of mostly equivalent types we had in use (int, uint, unsigned, unsigned int, i32, u32, ulong, uintN). it is more efficient to use 64-bit types in 64-bit mode, so the preferred default is size_t (for anything remotely resembling a size or index). tile coordinates are ssize_t to allow more efficient conversion to/from floating point. flags are int because we almost never need more than 15 distinct bits, bit test/set is not slower and int is fastest to type. finally, some data that is pretty much directly passed to OpenGL is now typed accordingly. after several hours, the code now requires fewer casts and less guesswork. other changes: - unit and player IDs now have an "invalid id" constant in the respective class to avoid casting and -1 - fix some endian/64-bit bugs in the map (un)packing. added a convenience function to write/read a size_t. - ia32: change CPUID interface to allow passing in ecx (required for cache topology detection, which I need at work). remove some unneeded functions from asm, replace with intrinsics where possible. This was SVN commit r5942.
103 lines
2.5 KiB
C++
103 lines
2.5 KiB
C++
#include "precompiled.h"
|
|
#include "write_buffer.h"
|
|
|
|
#include "lib/bits.h" // IsAligned
|
|
#include "lib/sysdep/cpu.h"
|
|
#include "io.h"
|
|
#include "io_align.h"
|
|
|
|
|
|
WriteBuffer::WriteBuffer()
|
|
: m_capacity(4096), m_data(io_Allocate(m_capacity)), m_size(0)
|
|
{
|
|
}
|
|
|
|
|
|
void WriteBuffer::Append(const void* data, size_t size)
|
|
{
|
|
if(m_size + size > m_capacity)
|
|
{
|
|
m_capacity = round_up_to_pow2((size_t)(m_size + size));
|
|
shared_ptr<u8> newData = io_Allocate(m_capacity);
|
|
cpu_memcpy(newData.get(), m_data.get(), m_size);
|
|
m_data = newData;
|
|
}
|
|
|
|
cpu_memcpy(m_data.get() + m_size, data, size);
|
|
m_size += size;
|
|
}
|
|
|
|
|
|
void WriteBuffer::Overwrite(const void* data, size_t size, size_t offset)
|
|
{
|
|
debug_assert(offset+size < m_size);
|
|
cpu_memcpy(m_data.get()+offset, data, size);
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// UnalignedWriter
|
|
//-----------------------------------------------------------------------------
|
|
|
|
UnalignedWriter::UnalignedWriter(PIFile file, off_t ofs)
|
|
: m_file(file), m_alignedBuf(io_Allocate(BLOCK_SIZE))
|
|
{
|
|
m_alignedOfs = AlignedOffset(ofs);
|
|
const size_t misalignment = (size_t)(ofs - m_alignedOfs);
|
|
if(misalignment)
|
|
io_ReadAligned(m_file, m_alignedOfs, m_alignedBuf.get(), BLOCK_SIZE);
|
|
m_bytesUsed = misalignment;
|
|
}
|
|
|
|
|
|
UnalignedWriter::~UnalignedWriter()
|
|
{
|
|
Flush();
|
|
}
|
|
|
|
|
|
LibError UnalignedWriter::Append(const u8* data, size_t size) const
|
|
{
|
|
while(size != 0)
|
|
{
|
|
// optimization: write directly from the input buffer, if possible
|
|
const size_t alignedSize = (size / BLOCK_SIZE) * BLOCK_SIZE;
|
|
if(m_bytesUsed == 0 && IsAligned(data, SECTOR_SIZE) && alignedSize != 0)
|
|
{
|
|
RETURN_ERR(io_WriteAligned(m_file, m_alignedOfs, data, alignedSize));
|
|
m_alignedOfs += (off_t)alignedSize;
|
|
data += alignedSize;
|
|
size -= alignedSize;
|
|
}
|
|
|
|
const size_t chunkSize = std::min(size, BLOCK_SIZE-m_bytesUsed);
|
|
cpu_memcpy(m_alignedBuf.get()+m_bytesUsed, data, chunkSize);
|
|
m_bytesUsed += chunkSize;
|
|
data += chunkSize;
|
|
size -= chunkSize;
|
|
|
|
if(m_bytesUsed == BLOCK_SIZE)
|
|
RETURN_ERR(WriteBlock());
|
|
}
|
|
|
|
return INFO::OK;
|
|
}
|
|
|
|
|
|
void UnalignedWriter::Flush() const
|
|
{
|
|
if(m_bytesUsed)
|
|
{
|
|
memset(m_alignedBuf.get()+m_bytesUsed, 0, BLOCK_SIZE-m_bytesUsed);
|
|
(void)WriteBlock();
|
|
}
|
|
}
|
|
|
|
|
|
LibError UnalignedWriter::WriteBlock() const
|
|
{
|
|
RETURN_ERR(io_WriteAligned(m_file, m_alignedOfs, m_alignedBuf.get(), BLOCK_SIZE));
|
|
m_alignedOfs += BLOCK_SIZE;
|
|
m_bytesUsed = 0;
|
|
return INFO::OK;
|
|
}
|