Commit 2898c7ec authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-15914: Make mtr_buf_t a plain class

parent 362151e8
/*****************************************************************************
Copyright (c) 2013, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2018, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
......@@ -32,14 +33,13 @@ Created 2013-03-16 Sunny Bains
#include "dyn0types.h"
/** Class that manages dynamic buffers. It uses a UT_LIST of
dyn_buf_t::block_t instances. We don't use STL containers in
mtr_buf_t::block_t instances. We don't use STL containers in
order to avoid the overhead of heap calls. Using a custom memory
allocator doesn't solve the problem either because we have to get
the memory from somewhere. We can't use the block_t::m_data as the
backend for the custom allocator because we would like the data in
the blocks to be contiguous. */
template <size_t SIZE = DYN_ARRAY_DATA_SIZE>
class dyn_buf_t {
class mtr_buf_t {
public:
class block_t;
......@@ -47,17 +47,19 @@ class dyn_buf_t {
typedef UT_LIST_NODE_T(block_t) block_node_t;
typedef UT_LIST_BASE_NODE_T(block_t) block_list_t;
/** SIZE - sizeof(m_node) + sizeof(m_used) */
enum { MAX_DATA_SIZE = DYN_ARRAY_DATA_SIZE
- sizeof(block_node_t) + sizeof(ib_uint32_t) };
class block_t {
public:
block_t()
{
ut_ad(MAX_DATA_SIZE <= (2 << 15));
compile_time_assert(MAX_DATA_SIZE <= (2 << 15));
init();
}
~block_t() { }
/**
Gets the number of used bytes in a block.
@return number of bytes used */
......@@ -112,12 +114,12 @@ class dyn_buf_t {
/**
@return pointer to start of reserved space */
template <typename Type>
Type push(ib_uint32_t size)
Type push(uint32_t size)
{
Type ptr = reinterpret_cast<Type>(end());
m_used += size;
ut_ad(m_used <= static_cast<ib_uint32_t>(MAX_DATA_SIZE));
ut_ad(m_used <= uint32_t(MAX_DATA_SIZE));
return(ptr);
}
......@@ -131,7 +133,7 @@ class dyn_buf_t {
ut_ad(ptr <= begin() + m_buf_end);
/* We have done the boundary check above */
m_used = static_cast<ib_uint32_t>(ptr - begin());
m_used = uint32_t(ptr - begin());
ut_ad(m_used <= MAX_DATA_SIZE);
ut_d(m_buf_end = 0);
......@@ -154,13 +156,6 @@ class dyn_buf_t {
ulint m_magic_n;
#endif /* UNIV_DEBUG */
/** SIZE - sizeof(m_node) + sizeof(m_used) */
enum {
MAX_DATA_SIZE = SIZE
- sizeof(block_node_t)
+ sizeof(ib_uint32_t)
};
/** Storage */
byte m_data[MAX_DATA_SIZE];
......@@ -169,15 +164,13 @@ class dyn_buf_t {
/** number of data bytes used in this block;
DYN_BLOCK_FULL_FLAG is set when the block becomes full */
ib_uint32_t m_used;
uint32_t m_used;
friend class dyn_buf_t;
friend class mtr_buf_t;
};
enum { MAX_DATA_SIZE = block_t::MAX_DATA_SIZE};
/** Default constructor */
dyn_buf_t()
mtr_buf_t()
:
m_heap(),
m_size()
......@@ -187,7 +180,7 @@ class dyn_buf_t {
}
/** Destructor */
~dyn_buf_t()
~mtr_buf_t()
{
erase();
}
......@@ -252,7 +245,7 @@ class dyn_buf_t {
@param size in bytes of the element
@return pointer to the element */
template <typename Type>
Type push(ib_uint32_t size)
Type push(uint32_t size)
{
ut_ad(size > 0);
ut_ad(size <= MAX_DATA_SIZE);
......@@ -272,17 +265,11 @@ class dyn_buf_t {
Pushes n bytes.
@param str string to write
@param len string length */
void push(const byte* ptr, ib_uint32_t len)
void push(const byte* ptr, uint32_t len)
{
while (len > 0) {
ib_uint32_t n_copied;
if (len >= MAX_DATA_SIZE) {
n_copied = MAX_DATA_SIZE;
} else {
n_copied = len;
}
uint32_t n_copied = std::min(len,
uint32_t(MAX_DATA_SIZE));
::memmove(push<byte*>(n_copied), ptr, n_copied);
ptr += n_copied;
......@@ -298,7 +285,7 @@ class dyn_buf_t {
const Type at(ulint pos) const
{
block_t* block = const_cast<block_t*>(
const_cast<dyn_buf_t*>(this)->find(pos));
const_cast<mtr_buf_t*>(this)->find(pos));
return(reinterpret_cast<Type>(block->begin() + pos));
}
......@@ -391,8 +378,8 @@ class dyn_buf_t {
private:
// Disable copying
dyn_buf_t(const dyn_buf_t&);
dyn_buf_t& operator=(const dyn_buf_t&);
mtr_buf_t(const mtr_buf_t&);
mtr_buf_t& operator=(const mtr_buf_t&);
/**
Add the block to the end of the list*/
......@@ -404,7 +391,7 @@ class dyn_buf_t {
}
/** @return the last block in the list */
block_t* back()
block_t* back() const
{
return(UT_LIST_GET_LAST(m_list));
}
......@@ -484,8 +471,6 @@ class dyn_buf_t {
block_t m_first_block;
};
typedef dyn_buf_t<DYN_ARRAY_DATA_SIZE> mtr_buf_t;
/** mtr_buf_t copier */
struct mtr_buf_copy_t {
/** The copied buffer */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment