2017-11-01 10:56:27 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-12-07 12:32:24 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2017-11-01 10:56:27 +03:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-12-07 12:32:24 +04:00
|
|
|
|
|
|
|
#ifndef replace_malloc_h
|
|
|
|
#define replace_malloc_h
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// The replace_malloc facility allows an external library to replace or
|
|
|
|
// supplement the jemalloc implementation.
|
|
|
|
//
|
|
|
|
// The external library may be hooked by setting one of the following
|
|
|
|
// environment variables to the library path:
|
|
|
|
// - LD_PRELOAD on Linux,
|
|
|
|
// - DYLD_INSERT_LIBRARIES on OSX,
|
|
|
|
// - MOZ_REPLACE_MALLOC_LIB on Windows and Android.
|
|
|
|
//
|
|
|
|
// An initialization function is called before any malloc replacement
|
|
|
|
// function, and has the following declaration:
|
|
|
|
//
|
2017-11-24 10:02:05 +03:00
|
|
|
// void replace_init(malloc_table_t*, ReplaceMallocBridge**)
|
2017-11-01 11:15:12 +03:00
|
|
|
//
|
Bug 1420353 - Change how replace-malloc initializes, part 1. r=njn
The allocator API is a moving target, and every time we change it, the
surface for replace-malloc libraries grows. This causes some build
system problems, because of the tricks in replace_malloc.mk, which
require the full list of symbols.
Considering the above and the goal of moving some of the replace-malloc
libraries into mozglue, it becomes simpler to reduce the replace-malloc
exposure to the initialization functions.
So instead of the allocator poking into replace-malloc libraries for all
the functions, we expect their replace_init function to alter the table
of allocator functions it's passed to register its own functions.
This means replace-malloc implementations now need to copy the original
table, which is not a bad thing, as it allows function calls with one
level of indirection less. It also replace_init functions to not
actually register the replace-malloc functions in some cases, which will
be useful when linking some replace-malloc libraries into mozglue.
Note this is binary compatible with previously built replace-malloc
libraries, but because those libraries wouldn't update the function
table, they would stay disabled.
--HG--
extra : rebase_source : 2518f6ebe76b4c82359e98369de6a5a8c3ca9967
2017-11-22 11:24:29 +03:00
|
|
|
// The malloc_table_t pointer given to that function is a table containing
|
|
|
|
// pointers to the original allocator implementation, so that replacement
|
|
|
|
// functions can call them back if they need to. The initialization function
|
|
|
|
// needs to alter that table to replace the function it wants to replace.
|
|
|
|
// If it needs the original implementation, it thus needs a copy of the
|
|
|
|
// original table.
|
2017-11-01 11:15:12 +03:00
|
|
|
//
|
2017-11-24 10:02:05 +03:00
|
|
|
// The ReplaceMallocBridge* pointer is an outparam that allows the
|
|
|
|
// replace_init function to return a pointer to its ReplaceMallocBridge
|
|
|
|
// (see replace_malloc_bridge.h).
|
|
|
|
//
|
2017-11-01 11:15:12 +03:00
|
|
|
// The functions to be implemented in the external library are of the form:
|
|
|
|
//
|
2017-11-24 10:02:05 +03:00
|
|
|
// void* replace_malloc(size_t size)
|
2017-11-01 11:15:12 +03:00
|
|
|
// {
|
|
|
|
// // Fiddle with the size if necessary.
|
|
|
|
// // orig->malloc doesn't have to be called if the external library
|
|
|
|
// // provides its own allocator, but in this case it will have to
|
|
|
|
// // implement all functions.
|
|
|
|
// void *ptr = orig->malloc(size);
|
|
|
|
// // Do whatever you want with the ptr.
|
|
|
|
// return ptr;
|
|
|
|
// }
|
|
|
|
//
|
Bug 1420353 - Change how replace-malloc initializes, part 1. r=njn
The allocator API is a moving target, and every time we change it, the
surface for replace-malloc libraries grows. This causes some build
system problems, because of the tricks in replace_malloc.mk, which
require the full list of symbols.
Considering the above and the goal of moving some of the replace-malloc
libraries into mozglue, it becomes simpler to reduce the replace-malloc
exposure to the initialization functions.
So instead of the allocator poking into replace-malloc libraries for all
the functions, we expect their replace_init function to alter the table
of allocator functions it's passed to register its own functions.
This means replace-malloc implementations now need to copy the original
table, which is not a bad thing, as it allows function calls with one
level of indirection less. It also replace_init functions to not
actually register the replace-malloc functions in some cases, which will
be useful when linking some replace-malloc libraries into mozglue.
Note this is binary compatible with previously built replace-malloc
libraries, but because those libraries wouldn't update the function
table, they would stay disabled.
--HG--
extra : rebase_source : 2518f6ebe76b4c82359e98369de6a5a8c3ca9967
2017-11-22 11:24:29 +03:00
|
|
|
// where "orig" is a pointer to a copy of the table replace_init got.
|
2017-11-01 11:15:12 +03:00
|
|
|
//
|
|
|
|
// See malloc_decls.h for a list of functions that can be replaced this
|
|
|
|
// way. The implementations are all in the form:
|
|
|
|
// return_type replace_name(arguments [,...])
|
|
|
|
//
|
|
|
|
// They don't all need to be provided.
|
|
|
|
//
|
|
|
|
// Building a replace-malloc library is like rocket science. It can end up
|
|
|
|
// with things blowing up, especially when trying to use complex types, and
|
|
|
|
// even more especially when these types come from XPCOM or other parts of the
|
|
|
|
// Mozilla codebase.
|
|
|
|
// It is recommended to add the following to a replace-malloc implementation's
|
|
|
|
// moz.build:
|
|
|
|
// DISABLE_STL_WRAPPING = True # Avoid STL wrapping
|
|
|
|
//
|
|
|
|
// If your replace-malloc implementation lives under memory/replace, these
|
|
|
|
// are taken care of by memory/replace/defs.mk.
|
2012-12-07 12:32:24 +04:00
|
|
|
|
2014-11-18 13:21:06 +03:00
|
|
|
#ifdef replace_malloc_bridge_h
|
|
|
|
# error Do not include replace_malloc_bridge.h before replace_malloc.h. \
|
|
|
|
In fact, you only need the latter.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define REPLACE_MALLOC_IMPL
|
|
|
|
|
|
|
|
#include "replace_malloc_bridge.h"
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Implementing a replace-malloc library is incompatible with using mozalloc.
|
2012-12-07 12:32:24 +04:00
|
|
|
#define MOZ_NO_MOZALLOC 1
|
|
|
|
|
2017-11-23 11:24:19 +03:00
|
|
|
#include "mozilla/MacroArgs.h"
|
2012-12-07 12:32:24 +04:00
|
|
|
#include "mozilla/Types.h"
|
|
|
|
|
|
|
|
MOZ_BEGIN_EXTERN_C
|
|
|
|
|
2017-11-24 10:36:29 +03:00
|
|
|
// MOZ_REPLACE_WEAK is only defined in mozjemalloc.cpp. Normally including
|
|
|
|
// this header will add function definitions.
|
2017-11-01 11:20:54 +03:00
|
|
|
#ifndef MOZ_REPLACE_WEAK
|
|
|
|
# define MOZ_REPLACE_WEAK
|
|
|
|
#endif
|
2012-12-07 12:32:24 +04:00
|
|
|
|
2017-11-23 11:24:19 +03:00
|
|
|
// When building a replace-malloc library for static linking, we want
|
|
|
|
// each to have a different name for their "public" functions.
|
|
|
|
// The build system defines MOZ_REPLACE_MALLOC_PREFIX in that case.
|
|
|
|
#ifdef MOZ_REPLACE_MALLOC_PREFIX
|
|
|
|
# define replace_init MOZ_CONCAT(MOZ_REPLACE_MALLOC_PREFIX, _init)
|
|
|
|
# define MOZ_REPLACE_PUBLIC
|
|
|
|
#else
|
|
|
|
# define MOZ_REPLACE_PUBLIC MOZ_EXPORT
|
|
|
|
#endif
|
|
|
|
|
2018-10-10 05:28:37 +03:00
|
|
|
struct ReplaceMallocBridge;
|
|
|
|
typedef void (*jemalloc_init_func)(malloc_table_t*,
|
|
|
|
struct ReplaceMallocBridge**);
|
|
|
|
|
2017-11-24 10:36:29 +03:00
|
|
|
// Replace-malloc library initialization function. See top of this file
|
2017-11-23 11:24:19 +03:00
|
|
|
MOZ_REPLACE_PUBLIC void replace_init(
|
2017-11-24 10:36:29 +03:00
|
|
|
malloc_table_t*, struct ReplaceMallocBridge**) MOZ_REPLACE_WEAK;
|
2012-12-07 12:32:24 +04:00
|
|
|
|
2018-10-10 05:28:37 +03:00
|
|
|
// ensure this is visible and libxul/etc reference it with a weak ref
|
|
|
|
MFBT_API void jemalloc_replace_dynamic(jemalloc_init_func);
|
|
|
|
|
2012-12-07 12:32:24 +04:00
|
|
|
MOZ_END_EXTERN_C
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
#endif // replace_malloc_h
|