michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Ideally, this test would be in memory/test/. But I couldn't get it to build michael@0: * there (couldn't find TestHarness.h). I think memory/ is processed too early michael@0: * in the build. So it's here. michael@0: */ michael@0: michael@0: #include "TestHarness.h" michael@0: #include "mozmemory.h" michael@0: michael@0: static inline bool michael@0: TestOne(size_t size) michael@0: { michael@0: size_t req = size; michael@0: size_t adv = malloc_good_size(req); michael@0: char* p = (char*)malloc(req); michael@0: size_t usable = moz_malloc_usable_size(p); michael@0: if (adv != usable) { michael@0: fail("malloc_good_size(%d) --> %d; " michael@0: "malloc_usable_size(%d) --> %d", michael@0: req, adv, req, usable); michael@0: return false; michael@0: } michael@0: free(p); michael@0: return true; michael@0: } michael@0: michael@0: static inline bool michael@0: TestThree(size_t size) michael@0: { michael@0: return TestOne(size - 1) && TestOne(size) && TestOne(size + 1); michael@0: } michael@0: michael@0: static nsresult michael@0: TestJemallocUsableSizeInAdvance() michael@0: { michael@0: #define K * 1024 michael@0: #define M * 1024 * 1024 michael@0: michael@0: /* michael@0: * Test every size up to a certain point, then (N-1, N, N+1) triplets for a michael@0: * various sizes beyond that. michael@0: */ michael@0: michael@0: for (size_t n = 0; n < 16 K; n++) michael@0: if (!TestOne(n)) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: for (size_t n = 16 K; n < 1 M; n += 4 K) michael@0: if (!TestThree(n)) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: for (size_t n = 1 M; n < 8 M; n += 128 K) michael@0: if (!TestThree(n)) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: passed("malloc_good_size"); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: int rv = 0; michael@0: ScopedXPCOM xpcom("jemalloc"); michael@0: if (xpcom.failed()) michael@0: return 1; michael@0: michael@0: if (NS_FAILED(TestJemallocUsableSizeInAdvance())) michael@0: rv = 1; michael@0: michael@0: return rv; michael@0: } michael@0: