Skip to content

Commit

Permalink
MDEV-15914: Make mtr_buf_t a plain class
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-m committed Apr 26, 2018
1 parent 362151e commit 2898c7e
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions storage/innobase/include/dyn0buf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*****************************************************************************
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
Expand Down Expand Up @@ -32,32 +33,33 @@ 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;

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 */
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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];

Expand All @@ -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()
Expand All @@ -187,7 +180,7 @@ class dyn_buf_t {
}

/** Destructor */
~dyn_buf_t()
~mtr_buf_t()
{
erase();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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));
}
Expand Down Expand Up @@ -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*/
Expand All @@ -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));
}
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 2898c7e

Please sign in to comment.