Blame memory/build/malloc_decls.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
// Helper header to declare all the supported malloc functions.
Packit f0b94e
// MALLOC_DECL arguments are:
Packit f0b94e
//   - function name
Packit f0b94e
//   - return type
Packit f0b94e
//   - argument types
Packit f0b94e
Packit f0b94e
#ifndef malloc_decls_h
Packit f0b94e
#define malloc_decls_h
Packit f0b94e
Packit f0b94e
#include "mozjemalloc_types.h"
Packit f0b94e
Packit f0b94e
#define MALLOC_FUNCS_MALLOC_BASE 1
Packit f0b94e
#define MALLOC_FUNCS_MALLOC_EXTRA 2
Packit f0b94e
#define MALLOC_FUNCS_MALLOC \
Packit f0b94e
  (MALLOC_FUNCS_MALLOC_BASE | MALLOC_FUNCS_MALLOC_EXTRA)
Packit f0b94e
#define MALLOC_FUNCS_JEMALLOC 4
Packit f0b94e
#define MALLOC_FUNCS_ARENA_BASE 8
Packit f0b94e
#define MALLOC_FUNCS_ARENA_ALLOC 16
Packit f0b94e
#define MALLOC_FUNCS_ARENA (MALLOC_FUNCS_ARENA_BASE | MALLOC_FUNCS_ARENA_ALLOC)
Packit f0b94e
#define MALLOC_FUNCS_ALL \
Packit f0b94e
  (MALLOC_FUNCS_MALLOC | MALLOC_FUNCS_JEMALLOC | MALLOC_FUNCS_ARENA)
Packit f0b94e
Packit f0b94e
#endif  // malloc_decls_h
Packit f0b94e
Packit f0b94e
#ifndef MALLOC_FUNCS
Packit f0b94e
#define MALLOC_FUNCS MALLOC_FUNCS_ALL
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
#ifdef MALLOC_DECL
Packit f0b94e
#if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_BASE
Packit f0b94e
MALLOC_DECL(malloc, void*, size_t)
Packit f0b94e
MALLOC_DECL(calloc, void*, size_t, size_t)
Packit f0b94e
MALLOC_DECL(realloc, void*, void*, size_t)
Packit f0b94e
MALLOC_DECL(free, void, void*)
Packit f0b94e
MALLOC_DECL(memalign, void*, size_t, size_t)
Packit f0b94e
#endif
Packit f0b94e
#if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_EXTRA
Packit f0b94e
MALLOC_DECL(posix_memalign, int, void**, size_t, size_t)
Packit f0b94e
MALLOC_DECL(aligned_alloc, void*, size_t, size_t)
Packit f0b94e
MALLOC_DECL(valloc, void*, size_t)
Packit f0b94e
MALLOC_DECL(malloc_usable_size, size_t, usable_ptr_t)
Packit f0b94e
MALLOC_DECL(malloc_good_size, size_t, size_t)
Packit f0b94e
#endif
Packit f0b94e
#if MALLOC_FUNCS & MALLOC_FUNCS_JEMALLOC
Packit f0b94e
MALLOC_DECL(jemalloc_stats, void, jemalloc_stats_t*)
Packit f0b94e
Packit f0b94e
// On some operating systems (Mac), we use madvise(MADV_FREE) to hand pages
Packit f0b94e
// back to the operating system.  On Mac, the operating system doesn't take
Packit f0b94e
// this memory back immediately; instead, the OS takes it back only when the
Packit f0b94e
// machine is running out of physical memory.
Packit f0b94e
//
Packit f0b94e
// This is great from the standpoint of efficiency, but it makes measuring our
Packit f0b94e
// actual RSS difficult, because pages which we've MADV_FREE'd shouldn't count
Packit f0b94e
// against our RSS.
Packit f0b94e
//
Packit f0b94e
// This function explicitly purges any MADV_FREE'd pages from physical memory,
Packit f0b94e
// causing our reported RSS match the amount of memory we're actually using.
Packit f0b94e
//
Packit f0b94e
// Note that this call is expensive in two ways.  First, it may be slow to
Packit f0b94e
// execute, because it may make a number of slow syscalls to free memory.  This
Packit f0b94e
// function holds the big jemalloc locks, so basically all threads are blocked
Packit f0b94e
// while this function runs.
Packit f0b94e
//
Packit f0b94e
// This function is also expensive in that the next time we go to access a page
Packit f0b94e
// which we've just explicitly decommitted, the operating system has to attach
Packit f0b94e
// to it a physical page!  If we hadn't run this function, the OS would have
Packit f0b94e
// less work to do.
Packit f0b94e
//
Packit f0b94e
// If MALLOC_DOUBLE_PURGE is not defined, this function does nothing.
Packit f0b94e
MALLOC_DECL(jemalloc_purge_freed_pages, void)
Packit f0b94e
Packit f0b94e
// Free all unused dirty pages in all arenas. Calling this function will slow
Packit f0b94e
// down subsequent allocations so it is recommended to use it only when
Packit f0b94e
// memory needs to be reclaimed at all costs (see bug 805855). This function
Packit f0b94e
// provides functionality similar to mallctl("arenas.purge") in jemalloc 3.
Packit f0b94e
MALLOC_DECL(jemalloc_free_dirty_pages, void)
Packit f0b94e
Packit f0b94e
// Opt in or out of a thread local arena (bool argument is whether to opt-in
Packit f0b94e
// (true) or out (false)).
Packit f0b94e
MALLOC_DECL(jemalloc_thread_local_arena, void, bool)
Packit f0b94e
Packit f0b94e
// Provide information about any allocation enclosing the given address.
Packit f0b94e
MALLOC_DECL(jemalloc_ptr_info, void, const void*, jemalloc_ptr_info_t*)
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
#if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_BASE
Packit f0b94e
Packit f0b94e
// Creates a separate arena, and returns its id, valid to use with moz_arena_*
Packit f0b94e
// functions. A helper is provided in mozmemory.h that doesn't take any
Packit f0b94e
// arena_params_t: moz_create_arena.
Packit f0b94e
MALLOC_DECL(moz_create_arena_with_params, arena_id_t, arena_params_t*)
Packit f0b94e
Packit f0b94e
// Dispose of the given arena. Subsequent uses of the arena will crash.
Packit f0b94e
// Passing an invalid id (inexistent or already disposed) to this function
Packit f0b94e
// will crash.
Packit f0b94e
MALLOC_DECL(moz_dispose_arena, void, arena_id_t)
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
#if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_ALLOC
Packit f0b94e
// Same as the functions without the moz_arena_ prefix, but using arenas
Packit f0b94e
// created with moz_create_arena.
Packit f0b94e
// The contract, even if not enforced at runtime in some configurations,
Packit f0b94e
// is that moz_arena_realloc and moz_arena_free will crash if the given
Packit f0b94e
// arena doesn't own the given pointer. All functions will crash if the
Packit f0b94e
// arena id is invalid.
Packit f0b94e
// Although discouraged, plain realloc and free can still be used on
Packit f0b94e
// pointers allocated with these functions. Realloc will properly keep
Packit f0b94e
// new pointers in the same arena as the original.
Packit f0b94e
MALLOC_DECL(moz_arena_malloc, void*, arena_id_t, size_t)
Packit f0b94e
MALLOC_DECL(moz_arena_calloc, void*, arena_id_t, size_t, size_t)
Packit f0b94e
MALLOC_DECL(moz_arena_realloc, void*, arena_id_t, void*, size_t)
Packit f0b94e
MALLOC_DECL(moz_arena_free, void, arena_id_t, void*)
Packit f0b94e
MALLOC_DECL(moz_arena_memalign, void*, arena_id_t, size_t, size_t)
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
#endif  // MALLOC_DECL
Packit f0b94e
Packit f0b94e
#undef MALLOC_DECL
Packit f0b94e
#undef MALLOC_FUNCS