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) 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 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 the terms of the GNU General Public License as published by the Free Software
...@@ -32,14 +33,13 @@ Created 2013-03-16 Sunny Bains ...@@ -32,14 +33,13 @@ Created 2013-03-16 Sunny Bains
#include "dyn0types.h" #include "dyn0types.h"
/** Class that manages dynamic buffers. It uses a UT_LIST of /** 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 order to avoid the overhead of heap calls. Using a custom memory
allocator doesn't solve the problem either because we have to get 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 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 backend for the custom allocator because we would like the data in
the blocks to be contiguous. */ the blocks to be contiguous. */
template <size_t SIZE = DYN_ARRAY_DATA_SIZE> class mtr_buf_t {
class dyn_buf_t {
public: public:
class block_t; class block_t;
...@@ -47,17 +47,19 @@ class dyn_buf_t { ...@@ -47,17 +47,19 @@ class dyn_buf_t {
typedef UT_LIST_NODE_T(block_t) block_node_t; typedef UT_LIST_NODE_T(block_t) block_node_t;
typedef UT_LIST_BASE_NODE_T(block_t) block_list_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 { class block_t {
public: public:
block_t() block_t()
{ {
ut_ad(MAX_DATA_SIZE <= (2 << 15)); compile_time_assert(MAX_DATA_SIZE <= (2 << 15));
init(); init();
} }
~block_t() { }
/** /**
Gets the number of used bytes in a block. Gets the number of used bytes in a block.
@return number of bytes used */ @return number of bytes used */
...@@ -112,12 +114,12 @@ class dyn_buf_t { ...@@ -112,12 +114,12 @@ class dyn_buf_t {
/** /**
@return pointer to start of reserved space */ @return pointer to start of reserved space */
template <typename Type> template <typename Type>
Type push(ib_uint32_t size) Type push(uint32_t size)
{ {
Type ptr = reinterpret_cast<Type>(end()); Type ptr = reinterpret_cast<Type>(end());
m_used += size; 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); return(ptr);
} }
...@@ -131,7 +133,7 @@ class dyn_buf_t { ...@@ -131,7 +133,7 @@ class dyn_buf_t {
ut_ad(ptr <= begin() + m_buf_end); ut_ad(ptr <= begin() + m_buf_end);
/* We have done the boundary check above */ /* 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_ad(m_used <= MAX_DATA_SIZE);
ut_d(m_buf_end = 0); ut_d(m_buf_end = 0);
...@@ -154,13 +156,6 @@ class dyn_buf_t { ...@@ -154,13 +156,6 @@ class dyn_buf_t {
ulint m_magic_n; ulint m_magic_n;
#endif /* UNIV_DEBUG */ #endif /* UNIV_DEBUG */
/** SIZE - sizeof(m_node) + sizeof(m_used) */
enum {
MAX_DATA_SIZE = SIZE
- sizeof(block_node_t)
+ sizeof(ib_uint32_t)
};
/** Storage */ /** Storage */
byte m_data[MAX_DATA_SIZE]; byte m_data[MAX_DATA_SIZE];
...@@ -169,15 +164,13 @@ class dyn_buf_t { ...@@ -169,15 +164,13 @@ class dyn_buf_t {
/** number of data bytes used in this block; /** number of data bytes used in this block;
DYN_BLOCK_FULL_FLAG is set when the block becomes full */ 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 */ /** Default constructor */
dyn_buf_t() mtr_buf_t()
: :
m_heap(), m_heap(),
m_size() m_size()
...@@ -187,7 +180,7 @@ class dyn_buf_t { ...@@ -187,7 +180,7 @@ class dyn_buf_t {
} }
/** Destructor */ /** Destructor */
~dyn_buf_t() ~mtr_buf_t()
{ {
erase(); erase();
} }
...@@ -252,7 +245,7 @@ class dyn_buf_t { ...@@ -252,7 +245,7 @@ class dyn_buf_t {
@param size in bytes of the element @param size in bytes of the element
@return pointer to the element */ @return pointer to the element */
template <typename Type> template <typename Type>
Type push(ib_uint32_t size) Type push(uint32_t size)
{ {
ut_ad(size > 0); ut_ad(size > 0);
ut_ad(size <= MAX_DATA_SIZE); ut_ad(size <= MAX_DATA_SIZE);
...@@ -272,17 +265,11 @@ class dyn_buf_t { ...@@ -272,17 +265,11 @@ class dyn_buf_t {
Pushes n bytes. Pushes n bytes.
@param str string to write @param str string to write
@param len string length */ @param len string length */
void push(const byte* ptr, ib_uint32_t len) void push(const byte* ptr, uint32_t len)
{ {
while (len > 0) { while (len > 0) {
ib_uint32_t n_copied; uint32_t n_copied = std::min(len,
uint32_t(MAX_DATA_SIZE));
if (len >= MAX_DATA_SIZE) {
n_copied = MAX_DATA_SIZE;
} else {
n_copied = len;
}
::memmove(push<byte*>(n_copied), ptr, n_copied); ::memmove(push<byte*>(n_copied), ptr, n_copied);
ptr += n_copied; ptr += n_copied;
...@@ -298,7 +285,7 @@ class dyn_buf_t { ...@@ -298,7 +285,7 @@ class dyn_buf_t {
const Type at(ulint pos) const const Type at(ulint pos) const
{ {
block_t* block = const_cast<block_t*>( 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)); return(reinterpret_cast<Type>(block->begin() + pos));
} }
...@@ -391,8 +378,8 @@ class dyn_buf_t { ...@@ -391,8 +378,8 @@ class dyn_buf_t {
private: private:
// Disable copying // Disable copying
dyn_buf_t(const dyn_buf_t&); mtr_buf_t(const mtr_buf_t&);
dyn_buf_t& operator=(const dyn_buf_t&); mtr_buf_t& operator=(const mtr_buf_t&);
/** /**
Add the block to the end of the list*/ Add the block to the end of the list*/
...@@ -404,7 +391,7 @@ class dyn_buf_t { ...@@ -404,7 +391,7 @@ class dyn_buf_t {
} }
/** @return the last block in the list */ /** @return the last block in the list */
block_t* back() block_t* back() const
{ {
return(UT_LIST_GET_LAST(m_list)); return(UT_LIST_GET_LAST(m_list));
} }
...@@ -484,8 +471,6 @@ class dyn_buf_t { ...@@ -484,8 +471,6 @@ class dyn_buf_t {
block_t m_first_block; block_t m_first_block;
}; };
typedef dyn_buf_t<DYN_ARRAY_DATA_SIZE> mtr_buf_t;
/** mtr_buf_t copier */ /** mtr_buf_t copier */
struct mtr_buf_copy_t { struct mtr_buf_copy_t {
/** The copied buffer */ /** 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