Skip to content

Commit

Permalink
Merge 9059aed into 49aa0fc
Browse files Browse the repository at this point in the history
  • Loading branch information
ylavic committed Apr 4, 2021
2 parents 49aa0fc + 9059aed commit 5ed5a15
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Expand Up @@ -40,10 +40,10 @@ matrix:
- env: CONF=debug ARCH=x86_64 CXX11=OFF CXX17=OFF
compiler: gcc
arch: amd64
- env: CONF=debug ARCH=x86 CXX11=OFF CXX17=ON
- env: CONF=debug ARCH=x86 CXX11=OFF CXX17=ON CXX_FLAGS='-D_GLIBCXX_DEBUG'
compiler: gcc
arch: amd64
- env: CONF=debug ARCH=x86_64 CXX11=OFF CXX17=ON
arch: amd64/
- env: CONF=debug ARCH=x86_64 CXX11=OFF CXX17=ON CXX_FLAGS='-D_GLIBCXX_DEBUG'
compiler: gcc
arch: amd64
- env: CONF=release ARCH=aarch64 CXX11=ON CXX17=OFF
Expand Down Expand Up @@ -84,7 +84,7 @@ matrix:
compiler: clang
arch: arm64
# coverage report
- env: CONF=debug ARCH=x86 CXX11=ON CXX17=OFF GCOV_FLAGS='--coverage'
- env: CONF=debug ARCH=x86 GCOV_FLAGS='--coverage' CXX_FLAGS='-O0' CXX11=OFF CXX17=OFF
compiler: gcc
arch: amd64
cache:
Expand All @@ -93,7 +93,7 @@ matrix:
after_success:
- pip install --user cpp-coveralls
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
- env: CONF=debug ARCH=x86_64 GCOV_FLAGS='--coverage'
- env: CONF=debug ARCH=x86_64 GCOV_FLAGS='--coverage' CXX_FLAGS='-O0' CXX11=ON CXX17=OFF
compiler: gcc
arch: amd64
cache:
Expand All @@ -102,7 +102,7 @@ matrix:
after_success:
- pip install --user cpp-coveralls
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
- env: CONF=debug ARCH=aarch64 GCOV_FLAGS='--coverage'
- env: CONF=debug ARCH=aarch64 GCOV_FLAGS='--coverage' CXX_FLAGS='-O0' CXX11=OFF CXX17=ON
compiler: gcc
arch: arm64
cache:
Expand Down Expand Up @@ -150,7 +150,7 @@ script:
-DRAPIDJSON_BUILD_CXX17=$CXX17
-DCMAKE_VERBOSE_MAKEFILE=ON
-DCMAKE_BUILD_TYPE=$CONF
-DCMAKE_CXX_FLAGS="$ARCH_FLAGS $GCOV_FLAGS"
-DCMAKE_CXX_FLAGS="$ARCH_FLAGS $GCOV_FLAGS $CXX_FLAGS"
-DCMAKE_EXE_LINKER_FLAGS=$GCOV_FLAGS
..)
- cd build
Expand Down
42 changes: 41 additions & 1 deletion include/rapidjson/allocators.h
Expand Up @@ -222,18 +222,42 @@ class MemoryPoolAllocator {
{
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
++rhs.shared_->refcount;

this->~MemoryPoolAllocator();
baseAllocator_ = rhs.baseAllocator_;
chunk_capacity_ = rhs.chunk_capacity_;
shared_ = rhs.shared_;
return *this;
}

#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
MemoryPoolAllocator(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT :
chunk_capacity_(rhs.chunk_capacity_),
baseAllocator_(std::move(rhs.baseAllocator_)),
shared_(rhs.shared_)
{
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
rhs.shared_ = 0;
}
MemoryPoolAllocator& operator=(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT
{
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
this->~MemoryPoolAllocator();
baseAllocator_ = std::move(rhs.baseAllocator_);
chunk_capacity_ = rhs.chunk_capacity_;
shared_ = rhs.shared_;
rhs.shared_ = 0;
return *this;
}
#endif

//! Destructor.
/*! This deallocates all memory chunks, excluding the user-supplied buffer.
*/
~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT {
if (!shared_) {
// do nothing if moved
return;
}
if (shared_->refcount > 1) {
--shared_->refcount;
return;
Expand Down Expand Up @@ -449,6 +473,17 @@ class StdAllocator :
baseAllocator_(rhs.baseAllocator_)
{ }

#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
StdAllocator(StdAllocator&& rhs) RAPIDJSON_NOEXCEPT :
allocator_type(std::move(rhs)),
baseAllocator_(std::move(rhs.baseAllocator_))
{ }
#endif
#if RAPIDJSON_HAS_CXX11
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
#endif

/* implicit */
StdAllocator(const BaseAllocator& allocator) RAPIDJSON_NOEXCEPT :
allocator_type(),
Expand Down Expand Up @@ -549,6 +584,10 @@ class StdAllocator :
deallocate<value_type>(p, n);
}

#if RAPIDJSON_HAS_CXX11
using is_always_equal = std::is_empty<BaseAllocator>;
#endif

template<typename U>
bool operator==(const StdAllocator<U, BaseAllocator>& rhs) const RAPIDJSON_NOEXCEPT
{
Expand All @@ -561,6 +600,7 @@ class StdAllocator :
}

//! rapidjson Allocator concept
static const bool kNeedFree = BaseAllocator::kNeedFree;
void* Malloc(size_t size)
{
return baseAllocator_.Malloc(size);
Expand Down
26 changes: 25 additions & 1 deletion test/unittest/allocatorstest.cpp
Expand Up @@ -16,8 +16,10 @@

#include "rapidjson/allocators.h"

#include <map>
#include <string>
#include <cstring>
#include <utility>
#include <functional>

using namespace rapidjson;

Expand Down Expand Up @@ -141,12 +143,34 @@ void TestStdAllocator(const Allocator& a) {

typedef StdAllocator<char, Allocator> CharAllocator;
typedef std::basic_string<char, std::char_traits<char>, CharAllocator> String;
#if RAPIDJSON_HAS_CXX11
String s(CharAllocator{a});
#else
CharAllocator ca(a);
String s(ca);
#endif
for (int i = 0; i < 26; i++) {
s.push_back(static_cast<char>('A' + i));
}
EXPECT_TRUE(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

typedef StdAllocator<std::pair<const int, bool>, Allocator> MapAllocator;
typedef std::map<int, bool, std::less<int>, MapAllocator> Map;
#if RAPIDJSON_HAS_CXX11
Map map(std::less<int>(), MapAllocator{a});
#else
MapAllocator ma(a);
Map map(std::less<int>(), ma);
#endif
for (int i = 0; i < 10; i++) {
map.insert(std::make_pair(i, (i % 2) == 0));
}
EXPECT_TRUE(map.size() == 10);
for (int i = 0; i < 10; i++) {
typename Map::iterator it = map.find(i);
EXPECT_TRUE(it != map.end());
EXPECT_TRUE(it->second == ((i % 2) == 0));
}
}

TEST(Allocator, CrtAllocator) {
Expand Down

0 comments on commit 5ed5a15

Please sign in to comment.