Combine templates in fx_memory_wrappers.
Templating on the address of a function is allowed in C++.
Change-Id: Ie4f7777f458e6d2066ced321140a32933254c809
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/89190
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/fx_memory_wrappers.h b/core/fxcrt/fx_memory_wrappers.h
index d56b9de..9cc4713 100644
--- a/core/fxcrt/fx_memory_wrappers.h
+++ b/core/fxcrt/fx_memory_wrappers.h
@@ -16,12 +16,11 @@
inline void operator()(void* ptr) const { FX_Free(ptr); }
};
-// Used with std::vector<> to put purely numeric vectors into the same
-// "general" partition used by FX_AllocUninit().
-// Otherwise, replacing the FX_AllocUninit/FX_Free pairs with std::vector<> may
+// Allocators for mapping STL containers onto Partition Alloc.
+// Otherwise, replacing e.g. the FX_AllocUninit/FX_Free pairs with STL may
// undo some of the nice segregation that we get from PartitionAlloc.
-template <class T>
-struct FxAllocAllocator {
+template <class T, void* F(size_t, size_t)>
+struct FxPartitionAllocAllocator {
public:
#if !defined(COMPILER_MSVC) || defined(NDEBUG)
static_assert(std::is_arithmetic<T>::value,
@@ -38,20 +37,22 @@
template <class U>
struct rebind {
- using other = FxAllocAllocator<U>;
+ using other = FxPartitionAllocAllocator<U, F>;
};
- FxAllocAllocator() noexcept = default;
- FxAllocAllocator(const FxAllocAllocator& other) noexcept = default;
- ~FxAllocAllocator() = default;
+ FxPartitionAllocAllocator() noexcept = default;
+ FxPartitionAllocAllocator(const FxPartitionAllocAllocator& other) noexcept =
+ default;
+ ~FxPartitionAllocAllocator() = default;
template <typename U>
- FxAllocAllocator(const FxAllocAllocator<U>& other) noexcept {}
+ FxPartitionAllocAllocator(
+ const FxPartitionAllocAllocator<U, F>& other) noexcept {}
pointer address(reference x) const noexcept { return &x; }
const_pointer address(const_reference x) const noexcept { return &x; }
pointer allocate(size_type n, const void* hint = 0) {
- return FX_AllocUninit(value_type, n);
+ return static_cast<pointer>(F(n, sizeof(value_type)));
}
void deallocate(pointer p, size_type n) { FX_Free(p); }
size_type max_size() const noexcept {
@@ -69,59 +70,20 @@
}
// There's no state, so they are all the same,
- bool operator==(const FxAllocAllocator& that) { return true; }
- bool operator!=(const FxAllocAllocator& that) { return false; }
+ bool operator==(const FxPartitionAllocAllocator& that) { return true; }
+ bool operator!=(const FxPartitionAllocAllocator& that) { return false; }
};
-// Used to put backing store for std::string and std::ostringstream
+// Used to put backing store for std::vector<> and such
+// into the general partition.
+template <typename T>
+using FxAllocAllocator =
+ FxPartitionAllocAllocator<T, pdfium::internal::AllocOrDie>;
+
+// Used to put backing store for std::string<> and std::ostringstream<>
// into the string partition.
-// TODO(tsepez): de-duplicate with above if decide to keep this.
-template <class T>
-struct FxStringAllocator {
- public:
- using value_type = T;
- using pointer = T*;
- using const_pointer = const T*;
- using reference = T&;
- using const_reference = const T&;
- using size_type = size_t;
- using difference_type = ptrdiff_t;
-
- template <class U>
- struct rebind {
- using other = FxStringAllocator<U>;
- };
-
- FxStringAllocator() noexcept = default;
- FxStringAllocator(const FxStringAllocator& other) noexcept = default;
- ~FxStringAllocator() = default;
-
- template <typename U>
- FxStringAllocator(const FxStringAllocator<U>& other) noexcept {}
-
- pointer address(reference x) const noexcept { return &x; }
- const_pointer address(const_reference x) const noexcept { return &x; }
- pointer allocate(size_type n, const void* hint = 0) {
- return FX_StringAlloc(value_type, n);
- }
- void deallocate(pointer p, size_type n) { FX_Free(p); }
- size_type max_size() const noexcept {
- return std::numeric_limits<size_type>::max() / sizeof(value_type);
- }
-
- template <class U, class... Args>
- void construct(U* p, Args&&... args) {
- new (reinterpret_cast<void*>(p)) U(std::forward<Args>(args)...);
- }
-
- template <class U>
- void destroy(U* p) {
- p->~U();
- }
-
- // There's no state, so they are all the same,
- bool operator==(const FxStringAllocator& that) { return true; }
- bool operator!=(const FxStringAllocator& that) { return false; }
-};
+template <typename T>
+using FxStringAllocator =
+ FxPartitionAllocAllocator<T, pdfium::internal::StringAllocOrDie>;
#endif // CORE_FXCRT_FX_MEMORY_WRAPPERS_H_