Commit c688848d by Jonathan Wakely Committed by Jonathan Wakely

Fix indentation in testsuite utility header

	* testsuite/util/testsuite_allocator.h (memory_resource)
	(default_resource_mgr): Fix indentation.

From-SVN: r271161
parent 066f9ea2
2019-05-14 Jonathan Wakely <jwakely@redhat.com> 2019-05-14 Jonathan Wakely <jwakely@redhat.com>
* testsuite/util/testsuite_allocator.h (memory_resource)
(default_resource_mgr): Fix indentation.
* testsuite/20_util/allocator_traits/members/allocate_hint_nonpod.cc: * testsuite/20_util/allocator_traits/members/allocate_hint_nonpod.cc:
Use operator-> to access raw pointer member. Use operator-> to access raw pointer member.
* testsuite/23_containers/vector/59829.cc: Likewise. * testsuite/23_containers/vector/59829.cc: Likewise.
......
...@@ -743,162 +743,162 @@ namespace __gnu_test ...@@ -743,162 +743,162 @@ namespace __gnu_test
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
#if __cpp_aligned_new && __cpp_rtti #if __cpp_aligned_new && __cpp_rtti
// A concrete memory_resource, with error checking. // A concrete memory_resource, with error checking.
class memory_resource : public std::pmr::memory_resource class memory_resource : public std::pmr::memory_resource
{ {
public: public:
memory_resource() memory_resource()
: lists(new allocation_lists) : lists(new allocation_lists)
{ } { }
memory_resource(const memory_resource& r) noexcept memory_resource(const memory_resource& r) noexcept
: lists(r.lists) : lists(r.lists)
{ lists->refcount++; } { lists->refcount++; }
memory_resource& operator=(const memory_resource&) = delete; memory_resource& operator=(const memory_resource&) = delete;
~memory_resource() ~memory_resource()
{ {
if (lists->refcount-- == 1) if (lists->refcount-- == 1)
delete lists; // last one out turns out the lights delete lists; // last one out turns out the lights
} }
struct bad_size { }; struct bad_size { };
struct bad_alignment { }; struct bad_alignment { };
struct bad_address { }; struct bad_address { };
// Deallocate everything (moving the tracking info to the freed list) // Deallocate everything (moving the tracking info to the freed list)
void void
deallocate_everything() deallocate_everything()
{ {
while (lists->active) while (lists->active)
{ {
auto a = lists->active; auto a = lists->active;
// Intentionally virtual dispatch, to inform derived classes: // Intentionally virtual dispatch, to inform derived classes:
this->do_deallocate(a->p, a->bytes, a->alignment); this->do_deallocate(a->p, a->bytes, a->alignment);
} }
} }
// Clear the freed list // Clear the freed list
void void
forget_freed_allocations() forget_freed_allocations()
{ lists->forget_allocations(lists->freed); } { lists->forget_allocations(lists->freed); }
// Count how many allocations have been done and not freed. // Count how many allocations have been done and not freed.
std::size_t std::size_t
number_of_active_allocations() const noexcept number_of_active_allocations() const noexcept
{ {
std::size_t n = 0; std::size_t n = 0;
for (auto a = lists->active; a != nullptr; a = a->next) for (auto a = lists->active; a != nullptr; a = a->next)
++n; ++n;
return n; return n;
} }
protected: protected:
void* void*
do_allocate(std::size_t bytes, std::size_t alignment) override do_allocate(std::size_t bytes, std::size_t alignment) override
{ {
// TODO perform a single allocation and put the allocation struct // TODO perform a single allocation and put the allocation struct
// in the buffer using placement new? It means deallocation won't // in the buffer using placement new? It means deallocation won't
// actually return memory to the OS, as it will stay in lists->freed. // actually return memory to the OS, as it will stay in lists->freed.
// //
// TODO adjust the returned pointer to be minimally aligned? // TODO adjust the returned pointer to be minimally aligned?
// e.g. if alignment==1 don't return something aligned to 2 bytes. // e.g. if alignment==1 don't return something aligned to 2 bytes.
// Maybe not worth it, at least monotonic_buffer_resource will // Maybe not worth it, at least monotonic_buffer_resource will
// never ask upstream for anything with small alignment. // never ask upstream for anything with small alignment.
void* p = ::operator new(bytes, std::align_val_t(alignment)); void* p = ::operator new(bytes, std::align_val_t(alignment));
lists->active = new allocation{p, bytes, alignment, lists->active}; lists->active = new allocation{p, bytes, alignment, lists->active};
return p; return p;
} }
void void
do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override
{ {
allocation** aptr = &lists->active; allocation** aptr = &lists->active;
while (*aptr) while (*aptr)
{ {
allocation* a = *aptr; allocation* a = *aptr;
if (p == a->p) if (p == a->p)
{ {
if (bytes != a->bytes) if (bytes != a->bytes)
throw bad_size(); throw bad_size();
if (alignment != a->alignment) if (alignment != a->alignment)
throw bad_alignment(); throw bad_alignment();
#if __cpp_sized_deallocation #if __cpp_sized_deallocation
::operator delete(p, bytes, std::align_val_t(alignment)); ::operator delete(p, bytes, std::align_val_t(alignment));
#else #else
::operator delete(p, std::align_val_t(alignment)); ::operator delete(p, std::align_val_t(alignment));
#endif #endif
*aptr = a->next; *aptr = a->next;
a->next = lists->freed; a->next = lists->freed;
lists->freed = a; lists->freed = a;
return; return;
}
aptr = &a->next;
}
throw bad_address();
}
bool
do_is_equal(const std::pmr::memory_resource& r) const noexcept override
{
// Equality is determined by sharing the same allocation_lists object.
if (auto p = dynamic_cast<const memory_resource*>(&r))
return p->lists == lists;
return false;
}
private:
struct allocation
{
void* p;
std::size_t bytes;
std::size_t alignment;
allocation* next;
};
// Maintain list of allocated blocks and list of freed blocks.
// Copies of this memory_resource share the same ref-counted lists.
struct allocation_lists
{
unsigned refcount = 1;
allocation* active = nullptr;
allocation* freed = nullptr;
void forget_allocations(allocation*& list)
{
while (list)
{
auto p = list;
list = list->next;
delete p;
} }
aptr = &a->next;
} }
throw bad_address();
}
~allocation_lists() bool
{ do_is_equal(const std::pmr::memory_resource& r) const noexcept override
forget_allocations(active); // Anything in this list is a leak! {
forget_allocations(freed); // Equality is determined by sharing the same allocation_lists object.
} if (auto p = dynamic_cast<const memory_resource*>(&r))
}; return p->lists == lists;
return false;
}
allocation_lists* lists; private:
struct allocation
{
void* p;
std::size_t bytes;
std::size_t alignment;
allocation* next;
}; };
#endif // aligned-new && rtti
// Set the default resource, and restore the previous one on destruction. // Maintain list of allocated blocks and list of freed blocks.
struct default_resource_mgr // Copies of this memory_resource share the same ref-counted lists.
struct allocation_lists
{ {
explicit default_resource_mgr(std::pmr::memory_resource* r) unsigned refcount = 1;
: prev(std::pmr::set_default_resource(r)) allocation* active = nullptr;
{ } allocation* freed = nullptr;
~default_resource_mgr() void forget_allocations(allocation*& list)
{ std::pmr::set_default_resource(prev); } {
while (list)
{
auto p = list;
list = list->next;
delete p;
}
}
std::pmr::memory_resource* prev; ~allocation_lists()
{
forget_allocations(active); // Anything in this list is a leak!
forget_allocations(freed);
}
}; };
allocation_lists* lists;
};
#endif // aligned-new && rtti
// Set the default resource, and restore the previous one on destruction.
struct default_resource_mgr
{
explicit default_resource_mgr(std::pmr::memory_resource* r)
: prev(std::pmr::set_default_resource(r))
{ }
~default_resource_mgr()
{ std::pmr::set_default_resource(prev); }
std::pmr::memory_resource* prev;
};
#endif // C++17 #endif // C++17
} // namespace __gnu_test } // namespace __gnu_test
......
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