2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
#include "Core/Buffer.hpp"
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
#include <cstring>
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Compute the next power of two for the specified number.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD inline unsigned int NextPow2(unsigned int num)
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
--num;
|
2020-03-22 08:16:40 +01:00
|
|
|
num |= num >> 1u;
|
|
|
|
num |= num >> 2u;
|
|
|
|
num |= num >> 4u;
|
|
|
|
num |= num >> 8u;
|
|
|
|
num |= num >> 16u;
|
2016-02-20 23:25:00 +01:00
|
|
|
return ++num;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer::Buffer(const Buffer & o)
|
2021-01-30 07:51:39 +01:00
|
|
|
: m_Ptr(nullptr), m_Cap(o.m_Cap), m_Cur(o.m_Cur)
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
if (m_Cap)
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
Request(o.m_Cap);
|
2016-05-22 05:20:38 +02:00
|
|
|
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer::~Buffer()
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Do we have a buffer?
|
2016-02-20 23:25:00 +01:00
|
|
|
if (m_Ptr)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
Release(); // Release it!
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-22 08:16:40 +01:00
|
|
|
Buffer & Buffer::operator = (const Buffer & o) // NOLINT(cert-oop54-cpp)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
if (m_Ptr != o.m_Ptr)
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Can we work in the current buffer?
|
2016-02-20 23:25:00 +01:00
|
|
|
if (m_Cap && o.m_Cap <= m_Cap)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// It's safe to copy the data
|
2016-05-22 05:20:38 +02:00
|
|
|
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Do we even have data to copy?
|
2016-02-20 23:25:00 +01:00
|
|
|
else if (!o.m_Cap)
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Do we have a buffer?
|
2016-02-20 23:25:00 +01:00
|
|
|
if (m_Ptr)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
Release(); // Release it!
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Do we have a buffer?
|
2016-02-20 23:25:00 +01:00
|
|
|
if (m_Ptr)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
Release(); // Release it!
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Request a larger buffer
|
2016-02-20 23:25:00 +01:00
|
|
|
Request(o.m_Cap);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Now it's safe to copy the data
|
2016-05-22 05:20:38 +02:00
|
|
|
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
2016-03-11 03:14:28 +01:00
|
|
|
// Also copy the edit cursor
|
|
|
|
m_Cur = o.m_Cur;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
return *this;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
2016-03-11 03:14:28 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Buffer::Grow(SzType n)
|
|
|
|
{
|
|
|
|
// Backup the current memory
|
2021-01-28 23:20:09 +01:00
|
|
|
Buffer bkp(m_Ptr, m_Cap, m_Cur);
|
2016-03-11 03:14:28 +01:00
|
|
|
// Acquire a bigger buffer
|
|
|
|
Request(bkp.m_Cap + n);
|
|
|
|
// Copy the data from the old buffer
|
2016-05-22 05:20:38 +02:00
|
|
|
std::memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
|
2016-03-11 03:14:28 +01:00
|
|
|
// Copy the previous edit cursor
|
|
|
|
m_Cur = bkp.m_Cur;
|
|
|
|
}
|
|
|
|
|
2015-11-07 11:17:39 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
void Buffer::Request(SzType n)
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
// NOTE: Function assumes (n > 0)
|
2021-01-28 23:20:09 +01:00
|
|
|
assert(n > 0);
|
|
|
|
// Round up the size to a power of two number
|
|
|
|
n = (n & (n - 1)) ? NextPow2(n) : n;
|
|
|
|
// Release previous memory if any
|
|
|
|
delete[] m_Ptr; // Implicitly handles null!
|
|
|
|
// Attempt to allocate memory
|
|
|
|
m_Ptr = new Value[n];
|
2016-03-10 04:57:13 +01:00
|
|
|
// If no errors occurred then we can set the size
|
|
|
|
m_Cap = n;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
void Buffer::Release()
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2021-01-28 23:20:09 +01:00
|
|
|
// Deallocate the memory
|
|
|
|
delete[] m_Ptr; // Implicitly handles null!
|
2016-02-20 23:25:00 +01:00
|
|
|
// Explicitly reset the buffer
|
2016-03-10 04:57:13 +01:00
|
|
|
m_Ptr = nullptr;
|
2016-02-20 23:25:00 +01:00
|
|
|
m_Cap = 0;
|
2016-03-11 03:14:28 +01:00
|
|
|
m_Cur = 0;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2016-03-11 03:14:28 +01:00
|
|
|
// Do we have what to write?
|
|
|
|
if (!data || !size)
|
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
return 0;
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
// See if the buffer size must be adjusted
|
|
|
|
else if ((pos + size) >= m_Cap)
|
|
|
|
{
|
2016-03-11 03:14:28 +01:00
|
|
|
// Acquire a larger buffer
|
|
|
|
Grow((pos + size) - m_Cap + 32);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// Copy the data into the internal buffer
|
2016-05-22 05:20:38 +02:00
|
|
|
std::memcpy(m_Ptr + pos, data, size);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the amount of data written to the buffer
|
|
|
|
return size;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Initialize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Call the function that takes the variable argument list
|
2016-03-11 03:14:28 +01:00
|
|
|
const SzType ret = WriteF(pos, fmt, args);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Finalize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_end(args);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Return the result
|
|
|
|
return ret;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
|
|
|
|
{
|
2016-03-11 03:14:28 +01:00
|
|
|
// Is the specified position within range?
|
|
|
|
if (pos >= m_Cap)
|
|
|
|
{
|
|
|
|
// Acquire a larger buffer
|
|
|
|
Grow(pos - m_Cap + 32);
|
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Backup the variable argument list
|
|
|
|
va_list args_cpy;
|
|
|
|
va_copy(args_cpy, args);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Attempt to write to the current buffer
|
2016-03-10 04:57:13 +01:00
|
|
|
// (if empty, it should tell us the necessary size)
|
2016-05-22 05:20:38 +02:00
|
|
|
int ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Do we need a bigger buffer?
|
|
|
|
if ((pos + ret) >= m_Cap)
|
|
|
|
{
|
2016-03-11 03:14:28 +01:00
|
|
|
// Acquire a larger buffer
|
|
|
|
Grow((pos + ret) - m_Cap + 32);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Retry writing the requested information
|
2016-05-22 05:20:38 +02:00
|
|
|
ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args_cpy);
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
|
|
|
// Return the value 0 if data could not be written
|
|
|
|
if (ret < 0)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
return 0;
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Return the number of written characters
|
|
|
|
return static_cast< SzType >(ret);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-11 03:14:28 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer::SzType Buffer::WriteS(SzType pos, ConstPtr str)
|
|
|
|
{
|
|
|
|
// Is there any string to write?
|
|
|
|
if (str && *str != '\0')
|
|
|
|
{
|
|
|
|
// Forward this to the regular write function
|
2021-01-30 07:51:39 +01:00
|
|
|
return Write(pos, str, static_cast< SzType >(std::strlen(str)));
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
// Nothing to write
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Buffer::AppendF(const char * fmt, ...)
|
|
|
|
{
|
|
|
|
// Initialize the variable argument list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
// Forward this to the regular write function
|
|
|
|
m_Cur += WriteF(m_Cur, fmt, args);
|
|
|
|
// Finalize the variable argument list
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Buffer::AppendS(const char * str)
|
|
|
|
{
|
|
|
|
// Is there any string to write?
|
|
|
|
if (str)
|
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
m_Cur += Write(m_Cur, str, static_cast< SzType >(std::strlen(str)));
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
} // Namespace:: SqMod
|