Skip to content

Commit

Permalink
Expose recycling_allocator as part of public interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskohlhoff committed Mar 3, 2022
1 parent 21ea741 commit 18bf1ca
Show file tree
Hide file tree
Showing 9 changed files with 830 additions and 0 deletions.
1 change: 1 addition & 0 deletions asio/include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ nobase_include_HEADERS = \
asio/read.hpp \
asio/read_until.hpp \
asio/readable_pipe.hpp \
asio/recycling_allocator.hpp \
asio/redirect_error.hpp \
asio/registered_buffer.hpp \
asio/require.hpp \
Expand Down
1 change: 1 addition & 0 deletions asio/include/asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
#include "asio/read_at.hpp"
#include "asio/read_until.hpp"
#include "asio/readable_pipe.hpp"
#include "asio/recycling_allocator.hpp"
#include "asio/redirect_error.hpp"
#include "asio/registered_buffer.hpp"
#include "asio/require.hpp"
Expand Down
138 changes: 138 additions & 0 deletions asio/include/asio/recycling_allocator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//
// recycling_allocator.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef ASIO_RECYCLING_ALLOCATOR_HPP
#define ASIO_RECYCLING_ALLOCATOR_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include "asio/detail/config.hpp"
#include "asio/detail/recycling_allocator.hpp"

#include "asio/detail/push_options.hpp"

namespace asio {

/// An allocator that caches memory blocks in thread-local storage for reuse.
/**
* The @recycling_allocator uses a simple strategy where a limited number of
* small memory blocks are cached in thread-local storage, if the current
* thread is running an @c io_context or is part of a @c thread_pool.
*/
template <typename T>
class recycling_allocator
{
public:
/// The type of object allocated by the recycling allocator.
typedef T value_type;

/// Rebind the allocator to another value_type.
template <typename U>
struct rebind
{
/// The rebound @c allocator type.
typedef recycling_allocator<U> other;
};

/// Default constructor.
ASIO_CONSTEXPR recycling_allocator() ASIO_NOEXCEPT
{
}

/// Converting constructor.
template <typename U>
ASIO_CONSTEXPR recycling_allocator(
const recycling_allocator<U>&) ASIO_NOEXCEPT
{
}

/// Equality operator. Always returns true.
ASIO_CONSTEXPR bool operator==(
const recycling_allocator&) const ASIO_NOEXCEPT
{
return true;
}

/// Inequality operator. Always returns false.
ASIO_CONSTEXPR bool operator!=(
const recycling_allocator&) const ASIO_NOEXCEPT
{
return false;
}

/// Allocate memory for the specified number of values.
T* allocate(std::size_t n)
{
return detail::recycling_allocator<T>().allocate(n);
}

/// Deallocate memory for the specified number of values.
void deallocate(T* p, std::size_t n)
{
detail::recycling_allocator<T>().deallocate(p, n);
}
};

/// A proto-allocator that caches memory blocks in thread-local storage for
/// reuse.
/**
* The @recycling_allocator uses a simple strategy where a limited number of
* small memory blocks are cached in thread-local storage, if the current
* thread is running an @c io_context or is part of a @c thread_pool.
*/
template <>
class recycling_allocator<void>
{
public:
/// No values are allocated by a proto-allocator.
typedef void value_type;

/// Rebind the allocator to another value_type.
template <typename U>
struct rebind
{
/// The rebound @c allocator type.
typedef recycling_allocator<U> other;
};

/// Default constructor.
ASIO_CONSTEXPR recycling_allocator() ASIO_NOEXCEPT
{
}

/// Converting constructor.
template <typename U>
ASIO_CONSTEXPR recycling_allocator(
const recycling_allocator<U>&) ASIO_NOEXCEPT
{
}

/// Equality operator. Always returns true.
ASIO_CONSTEXPR bool operator==(
const recycling_allocator&) const ASIO_NOEXCEPT
{
return true;
}

/// Inequality operator. Always returns false.
ASIO_CONSTEXPR bool operator!=(
const recycling_allocator&) const ASIO_NOEXCEPT
{
return false;
}
};

} // namespace asio

#include "asio/detail/pop_options.hpp"

#endif // ASIO_RECYCLING_ALLOCATOR_HPP
1 change: 1 addition & 0 deletions asio/src/Makefile.msc
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ UNIT_TEST_EXES = \
tests\unit\read_at.exe \
tests\unit\read_until.exe \
tests\unit\readable_pipe.exe \
tests\unit\recycling_allocator.exe \
tests\unit\redirect_error.exe \
tests\unit\registered_buffer.exe \
tests\unit\serial_port.exe \
Expand Down
1 change: 1 addition & 0 deletions asio/src/doc/quickref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
<member><link linkend="asio.reference.experimental__wait_for_one_error">experimental::wait_for_one_error</link></member>
<member><link linkend="asio.reference.experimental__wait_for_one_success">experimental::wait_for_one_success</link></member>
<member><link linkend="asio.reference.io_context__basic_executor_type">io_context::basic_executor_type</link></member>
<member><link linkend="asio.reference.recycling_allocator">recycling_allocator</link></member>
<member><link linkend="asio.reference.redirect_error_t">redirect_error_t</link></member>
<member><link linkend="asio.reference.strand">strand</link></member>
<member><link linkend="asio.reference.thread_pool__basic_executor_type">thread_pool::basic_executor_type</link></member>
Expand Down
Loading

0 comments on commit 18bf1ca

Please sign in to comment.