memory/build/mozjemalloc_compat.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/build/mozjemalloc_compat.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef MOZ_JEMALLOC3
     1.9 +#  error Should only compile this file when building with jemalloc 3
    1.10 +#endif
    1.11 +
    1.12 +#define MOZ_JEMALLOC_IMPL
    1.13 +
    1.14 +#include "mozmemory_wrap.h"
    1.15 +#include "jemalloc_types.h"
    1.16 +#include "mozilla/Types.h"
    1.17 +
    1.18 +#if defined(MOZ_NATIVE_JEMALLOC)
    1.19 +
    1.20 +MOZ_IMPORT_API int
    1.21 +je_(mallctl)(const char*, void*, size_t*, void*, size_t);
    1.22 +MOZ_IMPORT_API int
    1.23 +je_(mallctlnametomib)(const char *name, size_t *mibp, size_t *miblenp);
    1.24 +MOZ_IMPORT_API int
    1.25 +je_(mallctlbymib)(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
    1.26 +MOZ_IMPORT_API int
    1.27 +je_(nallocm)(size_t *rsize, size_t size, int flags);
    1.28 +
    1.29 +#else
    1.30 +#  include "jemalloc/jemalloc.h"
    1.31 +#endif
    1.32 +
    1.33 +/*
    1.34 + *  CTL_* macros are from memory/jemalloc/src/src/stats.c with changes:
    1.35 + *  - drop `t' argument to avoid redundancy in calculating type size
    1.36 + *  - require `i' argument for arena number explicitly
    1.37 + */
    1.38 +
    1.39 +#define	CTL_GET(n, v) do {						\
    1.40 +	size_t sz = sizeof(v);						\
    1.41 +	je_(mallctl)(n, &v, &sz, NULL, 0);				\
    1.42 +} while (0)
    1.43 +
    1.44 +#define	CTL_I_GET(n, v, i) do {						\
    1.45 +	size_t mib[6];							\
    1.46 +	size_t miblen = sizeof(mib) / sizeof(mib[0]);			\
    1.47 +	size_t sz = sizeof(v);						\
    1.48 +	je_(mallctlnametomib)(n, mib, &miblen);			\
    1.49 +	mib[2] = i;							\
    1.50 +	je_(mallctlbymib)(mib, miblen, &v, &sz, NULL, 0);		\
    1.51 +} while (0)
    1.52 +
    1.53 +MOZ_MEMORY_API size_t
    1.54 +malloc_good_size_impl(size_t size)
    1.55 +{
    1.56 +  size_t ret;
    1.57 +  /* je_nallocm crashes when given a size of 0. As
    1.58 +   * malloc_usable_size(malloc(0)) and malloc_usable_size(malloc(1))
    1.59 +   * return the same value, use a size of 1. */
    1.60 +  if (size == 0)
    1.61 +    size = 1;
    1.62 +  if (!je_(nallocm)(&ret, size, 0))
    1.63 +    return ret;
    1.64 +  return size;
    1.65 +}
    1.66 +
    1.67 +MOZ_JEMALLOC_API void
    1.68 +jemalloc_stats_impl(jemalloc_stats_t *stats)
    1.69 +{
    1.70 +  unsigned narenas;
    1.71 +  size_t active, allocated, mapped, page, pdirty;
    1.72 +
    1.73 +  CTL_GET("arenas.narenas", narenas);
    1.74 +  CTL_GET("arenas.page", page);
    1.75 +  CTL_GET("stats.active", active);
    1.76 +  CTL_GET("stats.allocated", allocated);
    1.77 +  CTL_GET("stats.mapped", mapped);
    1.78 +
    1.79 +  /* get the summation for all arenas, i == narenas */
    1.80 +  CTL_I_GET("stats.arenas.0.pdirty", pdirty, narenas);
    1.81 +
    1.82 +  stats->mapped = mapped;
    1.83 +  stats->allocated = allocated;
    1.84 +  stats->waste = active - allocated;
    1.85 +  stats->page_cache = pdirty * page;
    1.86 +
    1.87 +  // We could get this value out of base.c::base_pages, but that really should
    1.88 +  // be an upstream change, so don't worry about it for now.
    1.89 +  stats->bookkeeping = 0;
    1.90 +}
    1.91 +
    1.92 +MOZ_JEMALLOC_API void
    1.93 +jemalloc_purge_freed_pages_impl()
    1.94 +{
    1.95 +}
    1.96 +
    1.97 +MOZ_JEMALLOC_API void
    1.98 +jemalloc_free_dirty_pages_impl()
    1.99 +{
   1.100 +  je_(mallctl)("arenas.purge", NULL, 0, NULL, 0);
   1.101 +}

mercurial