xpcom/tests/TestJemalloc.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * Ideally, this test would be in memory/test/. But I couldn't get it to build
michael@0 8 * there (couldn't find TestHarness.h). I think memory/ is processed too early
michael@0 9 * in the build. So it's here.
michael@0 10 */
michael@0 11
michael@0 12 #include "TestHarness.h"
michael@0 13 #include "mozmemory.h"
michael@0 14
michael@0 15 static inline bool
michael@0 16 TestOne(size_t size)
michael@0 17 {
michael@0 18 size_t req = size;
michael@0 19 size_t adv = malloc_good_size(req);
michael@0 20 char* p = (char*)malloc(req);
michael@0 21 size_t usable = moz_malloc_usable_size(p);
michael@0 22 if (adv != usable) {
michael@0 23 fail("malloc_good_size(%d) --> %d; "
michael@0 24 "malloc_usable_size(%d) --> %d",
michael@0 25 req, adv, req, usable);
michael@0 26 return false;
michael@0 27 }
michael@0 28 free(p);
michael@0 29 return true;
michael@0 30 }
michael@0 31
michael@0 32 static inline bool
michael@0 33 TestThree(size_t size)
michael@0 34 {
michael@0 35 return TestOne(size - 1) && TestOne(size) && TestOne(size + 1);
michael@0 36 }
michael@0 37
michael@0 38 static nsresult
michael@0 39 TestJemallocUsableSizeInAdvance()
michael@0 40 {
michael@0 41 #define K * 1024
michael@0 42 #define M * 1024 * 1024
michael@0 43
michael@0 44 /*
michael@0 45 * Test every size up to a certain point, then (N-1, N, N+1) triplets for a
michael@0 46 * various sizes beyond that.
michael@0 47 */
michael@0 48
michael@0 49 for (size_t n = 0; n < 16 K; n++)
michael@0 50 if (!TestOne(n))
michael@0 51 return NS_ERROR_UNEXPECTED;
michael@0 52
michael@0 53 for (size_t n = 16 K; n < 1 M; n += 4 K)
michael@0 54 if (!TestThree(n))
michael@0 55 return NS_ERROR_UNEXPECTED;
michael@0 56
michael@0 57 for (size_t n = 1 M; n < 8 M; n += 128 K)
michael@0 58 if (!TestThree(n))
michael@0 59 return NS_ERROR_UNEXPECTED;
michael@0 60
michael@0 61 passed("malloc_good_size");
michael@0 62
michael@0 63 return NS_OK;
michael@0 64 }
michael@0 65
michael@0 66 int main(int argc, char** argv)
michael@0 67 {
michael@0 68 int rv = 0;
michael@0 69 ScopedXPCOM xpcom("jemalloc");
michael@0 70 if (xpcom.failed())
michael@0 71 return 1;
michael@0 72
michael@0 73 if (NS_FAILED(TestJemallocUsableSizeInAdvance()))
michael@0 74 rv = 1;
michael@0 75
michael@0 76 return rv;
michael@0 77 }
michael@0 78

mercurial