michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZ_JEMALLOC3 michael@0: # error Should only compile this file when building with jemalloc 3 michael@0: #endif michael@0: michael@0: #define MOZ_JEMALLOC_IMPL michael@0: michael@0: #include "mozmemory_wrap.h" michael@0: #include "jemalloc_types.h" michael@0: #include "mozilla/Types.h" michael@0: michael@0: #if defined(MOZ_NATIVE_JEMALLOC) michael@0: michael@0: MOZ_IMPORT_API int michael@0: je_(mallctl)(const char*, void*, size_t*, void*, size_t); michael@0: MOZ_IMPORT_API int michael@0: je_(mallctlnametomib)(const char *name, size_t *mibp, size_t *miblenp); michael@0: MOZ_IMPORT_API int michael@0: je_(mallctlbymib)(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); michael@0: MOZ_IMPORT_API int michael@0: je_(nallocm)(size_t *rsize, size_t size, int flags); michael@0: michael@0: #else michael@0: # include "jemalloc/jemalloc.h" michael@0: #endif michael@0: michael@0: /* michael@0: * CTL_* macros are from memory/jemalloc/src/src/stats.c with changes: michael@0: * - drop `t' argument to avoid redundancy in calculating type size michael@0: * - require `i' argument for arena number explicitly michael@0: */ michael@0: michael@0: #define CTL_GET(n, v) do { \ michael@0: size_t sz = sizeof(v); \ michael@0: je_(mallctl)(n, &v, &sz, NULL, 0); \ michael@0: } while (0) michael@0: michael@0: #define CTL_I_GET(n, v, i) do { \ michael@0: size_t mib[6]; \ michael@0: size_t miblen = sizeof(mib) / sizeof(mib[0]); \ michael@0: size_t sz = sizeof(v); \ michael@0: je_(mallctlnametomib)(n, mib, &miblen); \ michael@0: mib[2] = i; \ michael@0: je_(mallctlbymib)(mib, miblen, &v, &sz, NULL, 0); \ michael@0: } while (0) michael@0: michael@0: MOZ_MEMORY_API size_t michael@0: malloc_good_size_impl(size_t size) michael@0: { michael@0: size_t ret; michael@0: /* je_nallocm crashes when given a size of 0. As michael@0: * malloc_usable_size(malloc(0)) and malloc_usable_size(malloc(1)) michael@0: * return the same value, use a size of 1. */ michael@0: if (size == 0) michael@0: size = 1; michael@0: if (!je_(nallocm)(&ret, size, 0)) michael@0: return ret; michael@0: return size; michael@0: } michael@0: michael@0: MOZ_JEMALLOC_API void michael@0: jemalloc_stats_impl(jemalloc_stats_t *stats) michael@0: { michael@0: unsigned narenas; michael@0: size_t active, allocated, mapped, page, pdirty; michael@0: michael@0: CTL_GET("arenas.narenas", narenas); michael@0: CTL_GET("arenas.page", page); michael@0: CTL_GET("stats.active", active); michael@0: CTL_GET("stats.allocated", allocated); michael@0: CTL_GET("stats.mapped", mapped); michael@0: michael@0: /* get the summation for all arenas, i == narenas */ michael@0: CTL_I_GET("stats.arenas.0.pdirty", pdirty, narenas); michael@0: michael@0: stats->mapped = mapped; michael@0: stats->allocated = allocated; michael@0: stats->waste = active - allocated; michael@0: stats->page_cache = pdirty * page; michael@0: michael@0: // We could get this value out of base.c::base_pages, but that really should michael@0: // be an upstream change, so don't worry about it for now. michael@0: stats->bookkeeping = 0; michael@0: } michael@0: michael@0: MOZ_JEMALLOC_API void michael@0: jemalloc_purge_freed_pages_impl() michael@0: { michael@0: } michael@0: michael@0: MOZ_JEMALLOC_API void michael@0: jemalloc_free_dirty_pages_impl() michael@0: { michael@0: je_(mallctl)("arenas.purge", NULL, 0, NULL, 0); michael@0: }