rename from memory/mozjemalloc/jemalloc.c
rename to memory/mozjemalloc/mozjemalloc.cpp
--- a/memory/mozjemalloc/jemalloc.c
+++ b/memory/mozjemalloc/mozjemalloc.cpp
@@ -95,16 +95,19 @@
* in the associated arena chunk header maps.
*
* Huge : Each allocation is backed by a dedicated contiguous set of chunks.
* Metadata are stored in a separate red-black tree.
*
*******************************************************************************
*/
+#include "mozmemory_wrap.h"
+#include "mozilla/Sprintf.h"
+
#ifdef MOZ_MEMORY_ANDROID
#define NO_TLS
#endif
/*
* On Linux, we use madvise(MADV_DONTNEED) to release memory back to the
* operating system. If we release 1MB of live pages with MADV_DONTNEED, our
* RSS will decrease by 1MB (almost) immediately.
@@ -159,20 +162,16 @@
/* Support optional abort() on OOM. */
# define MALLOC_XMALLOC
/* Support SYSV semantics. */
# define MALLOC_SYSV
#endif
-#if defined(MOZ_MEMORY_LINUX) && !defined(MOZ_MEMORY_ANDROID)
-#define _GNU_SOURCE /* For mremap(2). */
-#endif
-
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
@@ -180,27 +179,23 @@
#ifdef MOZ_MEMORY_WINDOWS
/* Some defines from the CRT internal headers that we need here. */
#define _CRT_SPINCOUNT 5000
#define __crtInitCritSecAndSpinCount InitializeCriticalSectionAndSpinCount
#include <io.h>
#include <windows.h>
#include <intrin.h>
+#include <algorithm>
#pragma warning( disable: 4267 4996 4146 )
-#define bool BOOL
-#define false FALSE
-#define true TRUE
-#define inline __inline
#define SIZE_T_MAX SIZE_MAX
#define STDERR_FILENO 2
#define PATH_MAX MAX_PATH
-#define vsnprintf _vsnprintf
#ifndef NO_TLS
static unsigned long tlsIndex = 0xffffffff;
#endif
/* use MSVC intrinsics */
#pragma intrinsic(_BitScanForward)
static __forceinline int
@@ -301,19 +296,18 @@ typedef long ssize_t;
#include <mach/vm_map.h>
#include <malloc/malloc.h>
#endif
#endif
#include "jemalloc_types.h"
#include "linkedlist.h"
-#include "mozmemory_wrap.h"
-
-extern void moz_abort();
+
+extern "C" void moz_abort();
/* Some tools, such as /dev/dsp wrappers, LD_PRELOAD libraries that
* happen to override mmap() and call dlsym() from their overridden
* mmap(). The problem is that dlsym() calls malloc(), and this ends
* up in a dead lock in jemalloc.
* On these systems, we prefer to directly use the system call.
* We do that for Linux systems and kfreebsd with GNU userland.
* Note sanity checks are not done (alignment of offset, ...) because
@@ -1170,33 +1164,31 @@ static bool arena_ralloc_large(void *ptr
static void *arena_ralloc(void *ptr, size_t size, size_t oldsize);
static bool arena_new(arena_t *arena);
static arena_t *arenas_extend();
static void *huge_malloc(size_t size, bool zero);
static void *huge_palloc(size_t size, size_t alignment, bool zero);
static void *huge_ralloc(void *ptr, size_t size, size_t oldsize);
static void huge_dalloc(void *ptr);
static void malloc_print_stats(void);
-#ifndef MOZ_MEMORY_WINDOWS
+#ifdef MOZ_MEMORY_WINDOWS
+extern "C"
+#else
static
#endif
bool malloc_init_hard(void);
-#ifndef MOZ_MEMORY_DARWIN
-static
+#ifdef MOZ_MEMORY_DARWIN
+#define FORK_HOOK extern "C"
+#else
+#define FORK_HOOK static
#endif
-void _malloc_prefork(void);
-#ifndef MOZ_MEMORY_DARWIN
-static
-#endif
-void _malloc_postfork_parent(void);
-#ifndef MOZ_MEMORY_DARWIN
-static
-#endif
-void _malloc_postfork_child(void);
+FORK_HOOK void _malloc_prefork(void);
+FORK_HOOK void _malloc_postfork_parent(void);
+FORK_HOOK void _malloc_postfork_child(void);
/*
* End function prototypes.
*/
/******************************************************************************/
static inline size_t
load_acquire_z(size_t *p)
@@ -1285,17 +1277,17 @@ void (*_malloc_message)(const char *p1,
#ifdef MALLOC_DEBUG
# define assert(e) MOZ_ASSERT(e)
#else
# define assert(e)
#endif
#ifdef MOZ_MEMORY_ANDROID
// Android's pthread.h does not declare pthread_atfork() until SDK 21.
-extern MOZ_EXPORT
+extern "C" MOZ_EXPORT
int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));
#endif
#if defined(MOZ_JEMALLOC_HARD_ASSERTS)
# define RELEASE_ASSERT(assertion) do { \
if (!(assertion)) { \
MOZ_CRASH_UNSAFE_OOL(#assertion); \
} \
@@ -1493,17 +1485,17 @@ static inline const char *
*/
static void
malloc_printf(const char *format, ...)
{
char buf[4096];
va_list ap;
va_start(ap, format);
- vsnprintf(buf, sizeof(buf), format, ap);
+ VsprintfLiteral(buf, format, ap);
va_end(ap);
_malloc_message(buf, "", "", "");
}
/******************************************************************************/
static inline void
pages_decommit(void *addr, size_t size)
@@ -1511,24 +1503,24 @@ pages_decommit(void *addr, size_t size)
#ifdef MOZ_MEMORY_WINDOWS
/*
* The region starting at addr may have been allocated in multiple calls
* to VirtualAlloc and recycled, so decommitting the entire region in one
* go may not be valid. However, since we allocate at least a chunk at a
* time, we may touch any region in chunksized increments.
*/
- size_t pages_size = min(size, chunksize -
+ size_t pages_size = std::min(size, chunksize -
CHUNK_ADDR2OFFSET((uintptr_t)addr));
while (size > 0) {
if (!VirtualFree(addr, pages_size, MEM_DECOMMIT))
moz_abort();
addr = (void *)((uintptr_t)addr + pages_size);
size -= pages_size;
- pages_size = min(size, chunksize);
+ pages_size = std::min(size, chunksize);
}
#else
if (mmap(addr, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1,
0) == MAP_FAILED)
moz_abort();
MozTagAnonymousMemory(addr, size, "jemalloc-decommitted");
#endif
}
@@ -1539,24 +1531,24 @@ pages_commit(void *addr, size_t size)
# ifdef MOZ_MEMORY_WINDOWS
/*
* The region starting at addr may have been allocated in multiple calls
* to VirtualAlloc and recycled, so committing the entire region in one
* go may not be valid. However, since we allocate at least a chunk at a
* time, we may touch any region in chunksized increments.
*/
- size_t pages_size = min(size, chunksize -
+ size_t pages_size = std::min(size, chunksize -
CHUNK_ADDR2OFFSET((uintptr_t)addr));
while (size > 0) {
if (!VirtualAlloc(addr, pages_size, MEM_COMMIT, PAGE_READWRITE))
moz_abort();
addr = (void *)((uintptr_t)addr + pages_size);
size -= pages_size;
- pages_size = min(size, chunksize);
+ pages_size = std::min(size, chunksize);
}
# else
if (mmap(addr, size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE |
MAP_ANON, -1, 0) == MAP_FAILED)
moz_abort();
MozTagAnonymousMemory(addr, size, "jemalloc");
# endif
}
@@ -2261,23 +2253,23 @@ pages_purge(void *addr, size_t length)
#else
# ifdef MOZ_MEMORY_WINDOWS
/*
* The region starting at addr may have been allocated in multiple calls
* to VirtualAlloc and recycled, so resetting the entire region in one
* go may not be valid. However, since we allocate at least a chunk at a
* time, we may touch any region in chunksized increments.
*/
- size_t pages_size = min(length, chunksize -
+ size_t pages_size = std::min(length, chunksize -
CHUNK_ADDR2OFFSET((uintptr_t)addr));
while (length > 0) {
VirtualAlloc(addr, pages_size, MEM_RESET, PAGE_READWRITE);
addr = (void *)((uintptr_t)addr + pages_size);
length -= pages_size;
- pages_size = min(length, chunksize);
+ pages_size = std::min(length, chunksize);
}
unzeroed = true;
# else
# ifdef MOZ_MEMORY_LINUX
# define JEMALLOC_MADV_PURGE MADV_DONTNEED
# define JEMALLOC_MADV_ZEROS true
# else /* FreeBSD and Darwin. */
# define JEMALLOC_MADV_PURGE MADV_FREE
@@ -4695,17 +4687,17 @@ malloc_init(void)
if (malloc_initialized == false)
return (malloc_init_hard());
return (false);
}
#endif
#if defined(MOZ_MEMORY_DARWIN)
-extern void register_zone(void);
+extern "C" void register_zone(void);
#endif
#if !defined(MOZ_MEMORY_WINDOWS)
static
#endif
bool
malloc_init_hard(void)
{
@@ -5159,21 +5151,19 @@ RETURN:
#ifndef MOZ_REPLACE_MALLOC
#if defined(__GNUC__) && !defined(MOZ_MEMORY_DARWIN)
#define MOZ_MEMORY_ELF
#endif
#endif /* MOZ_REPLACE_MALLOC */
#ifdef MOZ_MEMORY_ELF
#define MEMALIGN memalign_internal
+extern "C"
#else
#define MEMALIGN memalign_impl
-#endif
-
-#ifndef MOZ_MEMORY_ELF
MOZ_MEMORY_API
#endif
void *
MEMALIGN(size_t alignment, size_t size)
{
void *ret;
assert(((alignment - 1) & alignment) == 0);
@@ -5841,20 +5831,22 @@ jemalloc_darwin_init(void)
* glibc provides the RTLD_DEEPBIND flag for dlopen which can make it possible
* to inconsistently reference libc's malloc(3)-compatible functions
* (bug 493541).
*
* These definitions interpose hooks in glibc. The functions are actually
* passed an extra argument for the caller return address, which will be
* ignored.
*/
-MOZ_MEMORY_API void (*__free_hook)(void *ptr) = free_impl;
-MOZ_MEMORY_API void *(*__malloc_hook)(size_t size) = malloc_impl;
-MOZ_MEMORY_API void *(*__realloc_hook)(void *ptr, size_t size) = realloc_impl;
-MOZ_MEMORY_API void *(*__memalign_hook)(size_t alignment, size_t size) = MEMALIGN;
+extern "C" {
+MOZ_EXPORT void (*__free_hook)(void *ptr) = free_impl;
+MOZ_EXPORT void *(*__malloc_hook)(size_t size) = malloc_impl;
+MOZ_EXPORT void *(*__realloc_hook)(void *ptr, size_t size) = realloc_impl;
+MOZ_EXPORT void *(*__memalign_hook)(size_t alignment, size_t size) = MEMALIGN;
+}
# elif defined(RTLD_DEEPBIND)
/*
* XXX On systems that support RTLD_GROUP or DF_1_GROUP, do their
* implementations permit similar inconsistencies? Should STV_SINGLETON
* visibility be used for interposition where available?
*/
# error "Interposing malloc is unsafe on this system without libc malloc hooks."