Skip to content

Commit

Permalink
Add test coverage for AllocateAtLeast()
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 504893114
  • Loading branch information
martijnvels authored and copybara-github committed Jan 26, 2023
1 parent bbe2e68 commit 8e022f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/google/protobuf/arena_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
#include "google/protobuf/arena.h"

#include <algorithm>
#include <array>
#include <atomic>
#include <cstddef>
#include <cstring>
#include <memory>
#include <string>
#include <thread>
#include <type_traits>
#include <typeinfo>
#include <vector>
Expand All @@ -51,6 +54,7 @@
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/message.h"
#include "google/protobuf/message_lite.h"
#include "google/protobuf/port.h"
#include "google/protobuf/repeated_field.h"
#include "google/protobuf/test_util.h"
#include "google/protobuf/unittest.pb.h"
Expand Down
34 changes: 34 additions & 0 deletions src/google/protobuf/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,45 @@ struct SizedPtr {
size_t n;
};

// Debug hook allowing setting up test scenarios for AllocateAtLeast usage.
using AllocateAtLeastHookFn = SizedPtr (*)(size_t, void*);

// `AllocAtLeastHook` API
constexpr bool HaveAllocateAtLeastHook();
void SetAllocateAtLeastHook(AllocateAtLeastHookFn fn, void* context = nullptr);

#if !defined(NDEBUG) && defined(ABSL_HAVE_THREAD_LOCAL) && \
defined(__cpp_inline_variables)

// Hook data for current thread. These vars must not be accessed directly, use
// the 'HaveAllocateAtLeastHook()` and `SetAllocateAtLeastHook()` API instead.
inline thread_local AllocateAtLeastHookFn allocate_at_least_hook = nullptr;
inline thread_local void* allocate_at_least_hook_context = nullptr;

constexpr bool HaveAllocateAtLeastHook() { return true; }
inline void SetAllocateAtLeastHook(AllocateAtLeastHookFn fn, void* context) {
allocate_at_least_hook = fn;
allocate_at_least_hook_context = context;
}

#else // !NDEBUG && ABSL_HAVE_THREAD_LOCAL && __cpp_inline_variables

constexpr bool HaveAllocateAtLeastHook() { return false; }
inline void SetAllocateAtLeastHook(AllocateAtLeastHookFn fn, void* context) {}

#endif // !NDEBUG && ABSL_HAVE_THREAD_LOCAL && __cpp_inline_variables

// Allocates at least `size` bytes. This function follows the c++ language
// proposal from D0901R10 (http://wg21.link/D0901R10) and will be implemented
// in terms of the new operator new semantics when available. The allocated
// memory should be released by a call to `SizedDelete` or `::operator delete`.
inline SizedPtr AllocateAtLeast(size_t size) {
#if !defined(NDEBUG) && defined(ABSL_HAVE_THREAD_LOCAL) && \
defined(__cpp_inline_variables)
if (allocate_at_least_hook != nullptr) {
return allocate_at_least_hook(size, allocate_at_least_hook_context);
}
#endif // !NDEBUG && ABSL_HAVE_THREAD_LOCAL && __cpp_inline_variables
return {::operator new(size), size};
}

Expand Down

0 comments on commit 8e022f8

Please sign in to comment.