-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose recycling_allocator as part of public interface.
- Loading branch information
1 parent
21ea741
commit 18bf1ca
Showing
9 changed files
with
830 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.