Standardize behavior of CrtAllocator::Malloc()

This commit is contained in:
Milo Yip 2015-04-17 13:01:14 +08:00
parent 4cd14b7c0e
commit 0e8bbe5e3e
2 changed files with 8 additions and 1 deletions

View File

@ -62,7 +62,12 @@ concept Allocator {
class CrtAllocator { class CrtAllocator {
public: public:
static const bool kNeedFree = true; static const bool kNeedFree = true;
void* Malloc(size_t size) { return std::malloc(size); } void* Malloc(size_t size) {
if (size) // behavior of malloc(0) is implementation defined.
return std::malloc(size);
else
return NULL; // standardize to returning NULL.
}
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { (void)originalSize; return std::realloc(originalPtr, newSize); } void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { (void)originalSize; return std::realloc(originalPtr, newSize); }
static void Free(void *ptr) { std::free(ptr); } static void Free(void *ptr) { std::free(ptr); }
}; };

View File

@ -26,6 +26,8 @@ using namespace rapidjson;
template <typename Allocator> template <typename Allocator>
void TestAllocator(Allocator& a) { void TestAllocator(Allocator& a) {
EXPECT_TRUE(a.Malloc(0) == 0);
uint8_t* p = (uint8_t*)a.Malloc(100); uint8_t* p = (uint8_t*)a.Malloc(100);
EXPECT_TRUE(p != 0); EXPECT_TRUE(p != 0);
for (size_t i = 0; i < 100; i++) for (size_t i = 0; i < 100; i++)