@ -21,6 +21,14 @@ class ArenaTest : public testing::Test {};
TEST_F ( ArenaTest , Empty ) { Arena arena0 ; }
TEST_F ( ArenaTest , Empty ) { Arena arena0 ; }
namespace {
namespace {
bool CheckMemoryAllocated ( size_t allocated , size_t expected ) {
// The value returned by Arena::MemoryAllocatedBytes() may be greater than
// the requested memory. We choose a somewhat arbitrary upper bound of
// max_expected = expected * 1.1 to detect critical overallocation.
size_t max_expected = expected * 1.1 ;
return allocated > = expected & & allocated < = max_expected ;
}
void MemoryAllocatedBytesTest ( size_t huge_page_size ) {
void MemoryAllocatedBytesTest ( size_t huge_page_size ) {
const int N = 17 ;
const int N = 17 ;
size_t req_sz ; // requested size
size_t req_sz ; // requested size
@ -36,7 +44,8 @@ void MemoryAllocatedBytesTest(size_t huge_page_size) {
arena . Allocate ( req_sz ) ;
arena . Allocate ( req_sz ) ;
}
}
expected_memory_allocated = req_sz * N + Arena : : kInlineSize ;
expected_memory_allocated = req_sz * N + Arena : : kInlineSize ;
ASSERT_GE ( arena . MemoryAllocatedBytes ( ) , expected_memory_allocated ) ;
ASSERT_PRED2 ( CheckMemoryAllocated , arena . MemoryAllocatedBytes ( ) ,
expected_memory_allocated ) ;
arena . Allocate ( Arena : : kInlineSize - 1 ) ;
arena . Allocate ( Arena : : kInlineSize - 1 ) ;
@ -49,13 +58,15 @@ void MemoryAllocatedBytesTest(size_t huge_page_size) {
arena . Allocate ( req_sz ) ;
arena . Allocate ( req_sz ) ;
}
}
if ( huge_page_size ) {
if ( huge_page_size ) {
ASSERT_TRUE ( arena . MemoryAllocatedBytes ( ) > =
ASSERT_TRUE (
expected_memory_allocated + bsz | |
CheckMemoryAllocated ( arena . MemoryAllocatedBytes ( ) ,
arena . MemoryAllocatedBytes ( ) > =
expected_memory_allocated + bsz ) | |
expected_memory_allocated + huge_page_size ) ;
CheckMemoryAllocated ( arena . MemoryAllocatedBytes ( ) ,
expected_memory_allocated + huge_page_size ) ) ;
} else {
} else {
expected_memory_allocated + = bsz ;
expected_memory_allocated + = bsz ;
ASSERT_GE ( arena . MemoryAllocatedBytes ( ) , expected_memory_allocated ) ;
ASSERT_PRED2 ( CheckMemoryAllocated , arena . MemoryAllocatedBytes ( ) ,
expected_memory_allocated ) ;
}
}
// requested size > size of a block:
// requested size > size of a block:
@ -66,7 +77,8 @@ void MemoryAllocatedBytesTest(size_t huge_page_size) {
arena . Allocate ( req_sz ) ;
arena . Allocate ( req_sz ) ;
}
}
expected_memory_allocated + = req_sz * N ;
expected_memory_allocated + = req_sz * N ;
ASSERT_GE ( arena . MemoryAllocatedBytes ( ) , expected_memory_allocated ) ;
ASSERT_PRED2 ( CheckMemoryAllocated , arena . MemoryAllocatedBytes ( ) ,
expected_memory_allocated ) ;
}
}
// Make sure we didn't count the allocate but not used memory space in
// Make sure we didn't count the allocate but not used memory space in
@ -83,7 +95,8 @@ static void ApproximateMemoryUsageTest(size_t huge_page_size) {
arena . AllocateAligned ( Arena : : kInlineSize / 2 - 16 ) ;
arena . AllocateAligned ( Arena : : kInlineSize / 2 - 16 ) ;
arena . AllocateAligned ( Arena : : kInlineSize / 2 ) ;
arena . AllocateAligned ( Arena : : kInlineSize / 2 ) ;
ASSERT_EQ ( arena . ApproximateMemoryUsage ( ) , Arena : : kInlineSize - 8 ) ;
ASSERT_EQ ( arena . ApproximateMemoryUsage ( ) , Arena : : kInlineSize - 8 ) ;
ASSERT_EQ ( arena . MemoryAllocatedBytes ( ) , Arena : : kInlineSize ) ;
ASSERT_PRED2 ( CheckMemoryAllocated , arena . MemoryAllocatedBytes ( ) ,
Arena : : kInlineSize ) ;
auto num_blocks = kBlockSize / kEntrySize ;
auto num_blocks = kBlockSize / kEntrySize ;
@ -91,10 +104,12 @@ static void ApproximateMemoryUsageTest(size_t huge_page_size) {
arena . AllocateAligned ( kEntrySize ) ;
arena . AllocateAligned ( kEntrySize ) ;
auto mem_usage = arena . MemoryAllocatedBytes ( ) ;
auto mem_usage = arena . MemoryAllocatedBytes ( ) ;
if ( huge_page_size ) {
if ( huge_page_size ) {
ASSERT_TRUE ( mem_usage = = kBlockSize + Arena : : kInlineSize | |
ASSERT_TRUE (
mem_usage = = huge_page_size + Arena : : kInlineSize ) ;
CheckMemoryAllocated ( mem_usage , kBlockSize + Arena : : kInlineSize ) | |
CheckMemoryAllocated ( mem_usage , huge_page_size + Arena : : kInlineSize ) ) ;
} else {
} else {
ASSERT_EQ ( mem_usage , kBlockSize + Arena : : kInlineSize ) ;
ASSERT_PRED2 ( CheckMemoryAllocated , mem_usage ,
kBlockSize + Arena : : kInlineSize ) ;
}
}
auto usage = arena . ApproximateMemoryUsage ( ) ;
auto usage = arena . ApproximateMemoryUsage ( ) ;
ASSERT_LT ( usage , mem_usage ) ;
ASSERT_LT ( usage , mem_usage ) ;