xpcom/tests/TestJemalloc.cpp

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

mercurial