memory/build/replace_malloc.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/build/replace_malloc.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     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 replace_malloc_h
     1.9 +#define replace_malloc_h
    1.10 +
    1.11 +/*
    1.12 + * The replace_malloc facility allows an external library to replace or
    1.13 + * supplement the jemalloc implementation.
    1.14 + *
    1.15 + * The external library may be hooked by setting one of the following
    1.16 + * environment variables to the library path:
    1.17 + *   - LD_PRELOAD on Linux,
    1.18 + *   - DYLD_INSERT_LIBRARIES on OSX,
    1.19 + *   - MOZ_REPLACE_MALLOC_LIB on Windows and Android.
    1.20 + *
    1.21 + * An initialization function is called before any malloc replacement
    1.22 + * function, and has the following declaration:
    1.23 + *
    1.24 + *   void replace_init(const malloc_table_t *)
    1.25 + *
    1.26 + * The const malloc_table_t pointer given to that function is a table
    1.27 + * containing pointers to the original jemalloc implementation, so that
    1.28 + * replacement functions can call them back if they need to. The pointer
    1.29 + * itself can safely be kept around (no need to copy the table itself).
    1.30 + *
    1.31 + * The functions to be implemented in the external library are of the form:
    1.32 + *
    1.33 + *   void *replace_malloc(size_t size)
    1.34 + *   {
    1.35 + *     // Fiddle with the size if necessary.
    1.36 + *     // orig->malloc doesn't have to be called if the external library
    1.37 + *     // provides its own allocator, but in this case it will have to
    1.38 + *     // implement all functions.
    1.39 + *     void *ptr = orig->malloc(size);
    1.40 + *     // Do whatever you want with the ptr.
    1.41 + *     return ptr;
    1.42 + *   }
    1.43 + *
    1.44 + * where "orig" is the pointer obtained from replace_init.
    1.45 + *
    1.46 + * See malloc_decls.h for a list of functions that can be replaced this
    1.47 + * way. The implementations are all in the form:
    1.48 + *   return_type replace_name(arguments [,...])
    1.49 + *
    1.50 + * They don't all need to be provided.
    1.51 + *
    1.52 + * Building a replace-malloc library is like rocket science. It can end up
    1.53 + * with things blowing up, especially when trying to use complex types, and
    1.54 + * even more especially when these types come from XPCOM or other parts of the
    1.55 + * Mozilla codebase.
    1.56 + * It is recommended to add the following to a replace-malloc implementation's
    1.57 + * Makefile.in:
    1.58 + *   MOZ_GLUE_LDFLAGS = # Don't link against mozglue
    1.59 + *   WRAP_LDFLAGS = # Never wrap malloc function calls with -Wl,--wrap
    1.60 + * and the following to the implementation's moz.build:
    1.61 + *   DISABLE_STL_WRAPPING = True # Avoid STL wrapping
    1.62 + *
    1.63 + * If your replace-malloc implementation lives under memory/replace, these
    1.64 + * are taken care of by memory/replace/defs.mk.
    1.65 + */
    1.66 +
    1.67 +/* Implementing a replace-malloc library is incompatible with using mozalloc. */
    1.68 +#define MOZ_NO_MOZALLOC 1
    1.69 +
    1.70 +#include "mozilla/Types.h"
    1.71 +
    1.72 +MOZ_BEGIN_EXTERN_C
    1.73 +
    1.74 +#define MALLOC_DECL(name, return_type, ...) \
    1.75 +  typedef return_type(name ## _impl_t)(__VA_ARGS__);
    1.76 +
    1.77 +#include "malloc_decls.h"
    1.78 +
    1.79 +#define MALLOC_DECL(name, return_type, ...) \
    1.80 +  name ## _impl_t * name;
    1.81 +
    1.82 +typedef struct {
    1.83 +#include "malloc_decls.h"
    1.84 +} malloc_table_t;
    1.85 +
    1.86 +
    1.87 +/* MOZ_NO_REPLACE_FUNC_DECL and MOZ_REPLACE_WEAK are only defined in
    1.88 + * replace_malloc.c. Normally including this header will add function
    1.89 + * definitions. */
    1.90 +#ifndef MOZ_NO_REPLACE_FUNC_DECL
    1.91 +
    1.92 +#  ifndef MOZ_REPLACE_WEAK
    1.93 +#    define MOZ_REPLACE_WEAK
    1.94 +#  endif
    1.95 +
    1.96 +#  define MALLOC_DECL(name, return_type, ...) \
    1.97 +    MOZ_EXPORT return_type replace_ ## name(__VA_ARGS__) MOZ_REPLACE_WEAK;
    1.98 +
    1.99 +#  define MALLOC_FUNCS MALLOC_FUNCS_ALL
   1.100 +#  include "malloc_decls.h"
   1.101 +
   1.102 +#endif /* MOZ_NO_REPLACE_FUNC_DECL */
   1.103 +
   1.104 +/*
   1.105 + * posix_memalign, aligned_alloc, memalign and valloc all implement some
   1.106 + * kind of aligned memory allocation. For convenience, replace_posix_memalign,
   1.107 + * replace_aligned_alloc and replace_valloc can be automatically derived from
   1.108 + * memalign when MOZ_REPLACE_ONLY_MEMALIGN is defined before including this
   1.109 + * header. PAGE_SIZE also needs to be defined to the appropriate expression.
   1.110 + */
   1.111 +#ifdef MOZ_REPLACE_ONLY_MEMALIGN
   1.112 +#include <errno.h>
   1.113 +
   1.114 +int replace_posix_memalign(void **ptr, size_t alignment, size_t size)
   1.115 +{
   1.116 +  if (size == 0) {
   1.117 +    *ptr = NULL;
   1.118 +    return 0;
   1.119 +  }
   1.120 +  /* alignment must be a power of two and a multiple of sizeof(void *) */
   1.121 +  if (((alignment - 1) & alignment) != 0 || (alignment % sizeof(void *)))
   1.122 +    return EINVAL;
   1.123 +  *ptr = replace_memalign(alignment, size);
   1.124 +  return *ptr ? 0 : ENOMEM;
   1.125 +}
   1.126 +
   1.127 +void *replace_aligned_alloc(size_t alignment, size_t size)
   1.128 +{
   1.129 +  /* size should be a multiple of alignment */
   1.130 +  if (size % alignment)
   1.131 +    return NULL;
   1.132 +  return replace_memalign(alignment, size);
   1.133 +}
   1.134 +
   1.135 +void *replace_valloc(size_t size)
   1.136 +{
   1.137 +  return replace_memalign(PAGE_SIZE, size);
   1.138 +}
   1.139 +#endif
   1.140 +
   1.141 +MOZ_END_EXTERN_C
   1.142 +
   1.143 +#endif /* replace_malloc_h */

mercurial