xpcom/tests/TestJemalloc.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestJemalloc.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * Ideally, this test would be in memory/test/.  But I couldn't get it to build
    1.11 + * there (couldn't find TestHarness.h).  I think memory/ is processed too early
    1.12 + * in the build.  So it's here.
    1.13 + */
    1.14 +
    1.15 +#include "TestHarness.h"
    1.16 +#include "mozmemory.h"
    1.17 +
    1.18 +static inline bool
    1.19 +TestOne(size_t size)
    1.20 +{
    1.21 +    size_t req = size;
    1.22 +    size_t adv = malloc_good_size(req);
    1.23 +    char* p = (char*)malloc(req);
    1.24 +    size_t usable = moz_malloc_usable_size(p);
    1.25 +    if (adv != usable) {
    1.26 +      fail("malloc_good_size(%d) --> %d; "
    1.27 +           "malloc_usable_size(%d) --> %d",
    1.28 +           req, adv, req, usable);
    1.29 +      return false;
    1.30 +    }
    1.31 +    free(p);
    1.32 +    return true;
    1.33 +}
    1.34 +
    1.35 +static inline bool
    1.36 +TestThree(size_t size)
    1.37 +{
    1.38 +    return TestOne(size - 1) && TestOne(size) && TestOne(size + 1);
    1.39 +}
    1.40 +
    1.41 +static nsresult
    1.42 +TestJemallocUsableSizeInAdvance()
    1.43 +{
    1.44 +  #define K   * 1024
    1.45 +  #define M   * 1024 * 1024
    1.46 +
    1.47 +  /*
    1.48 +   * Test every size up to a certain point, then (N-1, N, N+1) triplets for a
    1.49 +   * various sizes beyond that.
    1.50 +   */
    1.51 +
    1.52 +  for (size_t n = 0; n < 16 K; n++)
    1.53 +    if (!TestOne(n))
    1.54 +      return NS_ERROR_UNEXPECTED;
    1.55 +
    1.56 +  for (size_t n = 16 K; n < 1 M; n += 4 K)
    1.57 +    if (!TestThree(n))
    1.58 +      return NS_ERROR_UNEXPECTED;
    1.59 +
    1.60 +  for (size_t n = 1 M; n < 8 M; n += 128 K)
    1.61 +    if (!TestThree(n))
    1.62 +      return NS_ERROR_UNEXPECTED;
    1.63 +
    1.64 +  passed("malloc_good_size");
    1.65 +
    1.66 +  return NS_OK;
    1.67 +}
    1.68 +
    1.69 +int main(int argc, char** argv)
    1.70 +{
    1.71 +  int rv = 0;
    1.72 +  ScopedXPCOM xpcom("jemalloc");
    1.73 +  if (xpcom.failed())
    1.74 +      return 1;
    1.75 +
    1.76 +  if (NS_FAILED(TestJemallocUsableSizeInAdvance()))
    1.77 +    rv = 1;
    1.78 +
    1.79 +  return rv;
    1.80 +}
    1.81 +

mercurial