memory/build/mozjemalloc_compat.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef MOZ_JEMALLOC3
     6 #  error Should only compile this file when building with jemalloc 3
     7 #endif
     9 #define MOZ_JEMALLOC_IMPL
    11 #include "mozmemory_wrap.h"
    12 #include "jemalloc_types.h"
    13 #include "mozilla/Types.h"
    15 #if defined(MOZ_NATIVE_JEMALLOC)
    17 MOZ_IMPORT_API int
    18 je_(mallctl)(const char*, void*, size_t*, void*, size_t);
    19 MOZ_IMPORT_API int
    20 je_(mallctlnametomib)(const char *name, size_t *mibp, size_t *miblenp);
    21 MOZ_IMPORT_API int
    22 je_(mallctlbymib)(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
    23 MOZ_IMPORT_API int
    24 je_(nallocm)(size_t *rsize, size_t size, int flags);
    26 #else
    27 #  include "jemalloc/jemalloc.h"
    28 #endif
    30 /*
    31  *  CTL_* macros are from memory/jemalloc/src/src/stats.c with changes:
    32  *  - drop `t' argument to avoid redundancy in calculating type size
    33  *  - require `i' argument for arena number explicitly
    34  */
    36 #define	CTL_GET(n, v) do {						\
    37 	size_t sz = sizeof(v);						\
    38 	je_(mallctl)(n, &v, &sz, NULL, 0);				\
    39 } while (0)
    41 #define	CTL_I_GET(n, v, i) do {						\
    42 	size_t mib[6];							\
    43 	size_t miblen = sizeof(mib) / sizeof(mib[0]);			\
    44 	size_t sz = sizeof(v);						\
    45 	je_(mallctlnametomib)(n, mib, &miblen);			\
    46 	mib[2] = i;							\
    47 	je_(mallctlbymib)(mib, miblen, &v, &sz, NULL, 0);		\
    48 } while (0)
    50 MOZ_MEMORY_API size_t
    51 malloc_good_size_impl(size_t size)
    52 {
    53   size_t ret;
    54   /* je_nallocm crashes when given a size of 0. As
    55    * malloc_usable_size(malloc(0)) and malloc_usable_size(malloc(1))
    56    * return the same value, use a size of 1. */
    57   if (size == 0)
    58     size = 1;
    59   if (!je_(nallocm)(&ret, size, 0))
    60     return ret;
    61   return size;
    62 }
    64 MOZ_JEMALLOC_API void
    65 jemalloc_stats_impl(jemalloc_stats_t *stats)
    66 {
    67   unsigned narenas;
    68   size_t active, allocated, mapped, page, pdirty;
    70   CTL_GET("arenas.narenas", narenas);
    71   CTL_GET("arenas.page", page);
    72   CTL_GET("stats.active", active);
    73   CTL_GET("stats.allocated", allocated);
    74   CTL_GET("stats.mapped", mapped);
    76   /* get the summation for all arenas, i == narenas */
    77   CTL_I_GET("stats.arenas.0.pdirty", pdirty, narenas);
    79   stats->mapped = mapped;
    80   stats->allocated = allocated;
    81   stats->waste = active - allocated;
    82   stats->page_cache = pdirty * page;
    84   // We could get this value out of base.c::base_pages, but that really should
    85   // be an upstream change, so don't worry about it for now.
    86   stats->bookkeeping = 0;
    87 }
    89 MOZ_JEMALLOC_API void
    90 jemalloc_purge_freed_pages_impl()
    91 {
    92 }
    94 MOZ_JEMALLOC_API void
    95 jemalloc_free_dirty_pages_impl()
    96 {
    97   je_(mallctl)("arenas.purge", NULL, 0, NULL, 0);
    98 }

mercurial