1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is c++ array template tests.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The Mozilla Foundation <http://www.mozilla.org/>.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Nicholas Nethercote <nnethercote@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /*
40 : * Ideally, this test would be in memory/test/. But I couldn't get it to build
41 : * there (couldn't find TestHarness.h). I think memory/ is processed too early
42 : * in the build. So it's here.
43 : */
44 :
45 : #include "TestHarness.h"
46 : #include "jemalloc.h"
47 :
48 : static inline bool
49 17308 : TestOne(size_t size)
50 : {
51 17308 : size_t req = size;
52 17308 : size_t adv = je_malloc_usable_size_in_advance(req);
53 17308 : char* p = (char*)malloc(req);
54 17308 : size_t usable = moz_malloc_usable_size(p);
55 17308 : if (adv != usable) {
56 : fail("je_malloc_usable_size_in_advance(%d) --> %d; "
57 : "malloc_usable_size(%d) --> %d",
58 0 : req, adv, req, usable);
59 0 : return false;
60 : }
61 17308 : free(p);
62 17308 : return true;
63 : }
64 :
65 : static inline bool
66 308 : TestThree(size_t size)
67 : {
68 308 : return TestOne(size - 1) && TestOne(size) && TestOne(size + 1);
69 : }
70 :
71 : static nsresult
72 1 : TestJemallocUsableSizeInAdvance()
73 : {
74 : #define K * 1024
75 : #define M * 1024 * 1024
76 :
77 : /*
78 : * Test every size up to a certain point, then (N-1, N, N+1) triplets for a
79 : * various sizes beyond that.
80 : */
81 :
82 16385 : for (size_t n = 0; n < 16 K; n++)
83 16384 : if (!TestOne(n))
84 0 : return NS_ERROR_UNEXPECTED;
85 :
86 253 : for (size_t n = 16 K; n < 1 M; n += 4 K)
87 252 : if (!TestThree(n))
88 0 : return NS_ERROR_UNEXPECTED;
89 :
90 57 : for (size_t n = 1 M; n < 8 M; n += 128 K)
91 56 : if (!TestThree(n))
92 0 : return NS_ERROR_UNEXPECTED;
93 :
94 1 : passed("je_malloc_usable_size_in_advance");
95 :
96 1 : return NS_OK;
97 : }
98 :
99 1 : int main(int argc, char** argv)
100 : {
101 1 : int rv = 0;
102 2 : ScopedXPCOM xpcom("jemalloc");
103 1 : if (xpcom.failed())
104 0 : return 1;
105 :
106 1 : if (NS_FAILED(TestJemallocUsableSizeInAdvance()))
107 0 : rv = 1;
108 :
109 1 : return rv;
110 : }
111 :
|