Skip to content

Commit

Permalink
bug 441324: implement infallible ::operator new(), malloc() and frien…
Browse files Browse the repository at this point in the history
…ds. make |new Foo()| infallible, but leave |malloc()| fallible for the time being. r=blassey sr=bsmedberg,vlad
  • Loading branch information
joneschrisg committed Oct 2, 2009
1 parent 99f0737 commit 50b7b03
Show file tree
Hide file tree
Showing 39 changed files with 1,052 additions and 104 deletions.
5 changes: 1 addition & 4 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,10 @@ TIERS += base
tier_base_dirs = \
config \
build \
memory \
probes \
$(NULL)

ifdef MOZ_MEMORY
tier_base_dirs += memory/jemalloc
endif

ifdef COMPILE_ENVIRONMENT
include $(topsrcdir)/$(MOZ_BUILD_APP)/build.mk
endif
Expand Down
1 change: 1 addition & 0 deletions browser/installer/package-manifest.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
@BINPATH@/@DLL_PREFIX@plds4@DLL_SUFFIX@
@BINPATH@/@DLL_PREFIX@xpcom@DLL_SUFFIX@
@BINPATH@/@DLL_PREFIX@nspr4@DLL_SUFFIX@
@BINPATH@/@DLL_PREFIX@mozalloc@DLL_SUFFIX@
#ifdef XP_MACOSX
@BINPATH@/XUL
#else
Expand Down
4 changes: 2 additions & 2 deletions build/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ endif
ifdef WINCE
# We need jemalloc built before the shunt
ifdef MOZ_MEMORY
DIRS += wince/tools $(DEPTH)/memory/jemalloc wince/shunt
DIRS += wince/tools $(DEPTH)/memory/jemalloc $(DEPTH)/memory/mozalloc wince/shunt
else
DIRS += wince/tools wince/shunt
DIRS += wince/tools $(DEPTH)/memory/mozalloc wince/shunt
endif
endif

Expand Down
2 changes: 2 additions & 0 deletions build/wince/shunt/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ EXPORTS_mozce_shunt += $(topsrcdir)/memory/jemalloc/jemalloc.h
EXTRA_LIBS += $(OBJDIR)/memory/jemalloc/jemalloc.obj
endif

EXTRA_DSO_LDOPTS += $(MOZALLOC_LIB)

DEFFILE = mozce_shunt.def

OS_LIBS += $(call EXPAND_LIBNAME, libcmt)
Expand Down
56 changes: 39 additions & 17 deletions build/wince/shunt/include/mozce_shunt.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,27 @@ typedef unsigned short wchar_t;
namespace std {
struct nothrow_t {};
extern const nothrow_t nothrow;
struct bad_alloc {};
};
#endif

// grab malloc and free prototypes
#include "jemalloc.h"

// Normal and nothrow versions of operator new, none of which
// actually throw for us. These are both inline and exported
// from the shunt.
inline void *operator new(size_t size) throw() {
return (void*) malloc(size);
}
// mozalloc.h defines "normal" infallible operator new, but not
// std::nothrow operator new, so we define that below. This is inline
// and exported from the shunt.
inline void *operator new(size_t size, const std::nothrow_t&) throw() {
return (void*) malloc(size);
}
inline void operator delete(void *ptr) throw() {
free(ptr);
}
inline void *operator new[](size_t size) throw() {
return (void*) malloc(size);
return malloc(size);
}
inline void *operator new[](size_t size, const std::nothrow_t&) throw() {
return (void*) malloc(size);
return malloc(size);
}
inline void operator delete(void* ptr, const std::nothrow_t&) throw() {
free(ptr);
}
inline void operator delete[](void *ptr) throw() {
return free(ptr);
inline void operator delete[](void* ptr, const std::nothrow_t&) throw() {
free(ptr);
}

// Placement new. Just inline, not exported (which doesn't work for
Expand All @@ -104,6 +99,30 @@ inline void *operator new[](size_t, void *p) {
return p;
}


// for Gecko, include infallible mozalloc allocators. elsewhere, define
// operator new() normally.
// NB: this include guard needs to be kept in sync with the one in nscore.h
#if defined(_MOZILLA_CONFIG_H_) && !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
# include "mozilla/mozalloc.h"
#else

inline void* operator new(size_t size) throw() {
return malloc(size);
}
inline void* operator new[](size_t size) throw() {
return malloc(size);
}
inline void operator delete(void* ptr) throw() {
free(ptr);
}
inline void operator delete[](void* ptr) throw() {
free(ptr);
}

#endif // if defined(_MOZILLA_CONFIG_H_)


extern "C" {
#endif

Expand All @@ -116,6 +135,9 @@ extern "C" {
#undef _wcsndup
#undef wcsndup

// _strdup() and _wcsdup() are both infallible, i.e., will never return
// NULL

char * __cdecl
_strdup(const char*);

Expand All @@ -132,7 +154,7 @@ _wcsndup(const wchar_t *, unsigned int);
} //extern "C"
#endif

#endif
#endif // ifdef MOZ_MEMORY

#define strdup _strdup
#define strndup _strndup
Expand Down
8 changes: 4 additions & 4 deletions build/wince/shunt/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "include/mozce_shunt.h"
#include <stdlib.h>

#include "mozilla/mozalloc_macro_wrappers.h" /* infallible malloc */

#ifdef MOZ_MEMORY

// declare the nothrow object
Expand All @@ -47,8 +49,7 @@ const std::nothrow_t std::nothrow;
char*
_strndup(const char *src, size_t len) {
char* dst = (char*)malloc(len + 1);
if(dst)
strncpy(dst, src, len + 1);
strncpy(dst, src, len + 1);
return dst;
}

Expand All @@ -62,8 +63,7 @@ _strdup(const char *src) {
wchar_t *
_wcsndup(const wchar_t *src, size_t len) {
wchar_t* dst = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1));
if(dst)
wcsncpy(dst, src, len + 1);
wcsncpy(dst, src, len + 1);
return dst;
}

Expand Down
1 change: 1 addition & 0 deletions config/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ else
endif
endif

MOZALLOC_LIB = $(call EXPAND_MOZLIBNAME,mozalloc)

# append debug flags
# (these might have been above when processing MOZ_DBGRINFO_MODULES)
Expand Down
110 changes: 98 additions & 12 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -1055,9 +1055,9 @@ MOZ_PNG_CFLAGS=
MOZ_PNG_LIBS='$(call EXPAND_LIBNAME_PATH,mozpng,$(DEPTH)/modules/libimg/png)'

MOZ_JS_LIBS='-L$(LIBXUL_DIST)/bin -lmozjs'
DYNAMIC_XPCOM_LIBS='-L$(LIBXUL_DIST)/bin -lxpcom -lxpcom_core'
DYNAMIC_XPCOM_LIBS='-L$(LIBXUL_DIST)/bin -lxpcom -lxpcom_core -lmozalloc'
MOZ_FIX_LINK_PATHS='-Wl,-rpath-link,$(LIBXUL_DIST)/bin -Wl,-rpath-link,$(prefix)/lib'
XPCOM_FROZEN_LDOPTS='-L$(LIBXUL_DIST)/bin -lxpcom'
XPCOM_FROZEN_LDOPTS='-L$(LIBXUL_DIST)/bin -lxpcom -lmozalloc'
LIBXUL_LIBS='$(XPCOM_FROZEN_LDOPTS) -lxul'
XPCOM_GLUE_LDOPTS='$(LIBXUL_DIST)/lib/$(LIB_PREFIX)xpcomglue_s.$(LIB_SUFFIX) $(XPCOM_FROZEN_LDOPTS)'
XPCOM_STANDALONE_GLUE_LDOPTS='$(LIBXUL_DIST)/lib/$(LIB_PREFIX)xpcomglue.$(LIB_SUFFIX)'
Expand Down Expand Up @@ -2013,11 +2013,11 @@ case "$target" in
DLL_PREFIX=
DOXYGEN=:
DSO_LDOPTS=-SUBSYSTEM:WINDOWSCE
DYNAMIC_XPCOM_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xpcom_core.lib'
DYNAMIC_XPCOM_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xpcom_core.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
GARBAGE=
IMPORT_LIB_SUFFIX=lib
LIBS="$LIBS"
LIBXUL_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xul.lib'
LIBXUL_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xul.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
LIB_PREFIX=
LIB_SUFFIX=lib
MKCSHLIB='$(LD) -NOLOGO -DLL -OUT:$@ $(DSO_LDOPTS)'
Expand All @@ -2036,7 +2036,7 @@ case "$target" in
TARGET_NSPR_MDCPUCFG='\"md/_wince.cfg\"'
UNZIP=unzip
XARGS=xargs
XPCOM_FROZEN_LDOPTS='$(LIBXUL_DIST)/lib/xpcom.lib'
XPCOM_FROZEN_LDOPTS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
ZIP=zip
LIBIDL_CFLAGS="-I$MOZ_TOOLS_DIR/include ${GLIB_CFLAGS}"
LIBIDL_LIBS="$MOZ_TOOLS_DIR/lib/libidl-0.6_s.lib $MOZ_TOOLS_DIR/lib/glib-1.2_s.lib"
Expand Down Expand Up @@ -2095,8 +2095,8 @@ case "$target" in
LIBS="$LIBS -lgdi32 -lwinmm -lwsock32"
MOZ_JS_LIBS='-L$(LIBXUL_DIST)/lib -ljs$(MOZ_BITS)$(VERSION_NUMBER)'
MOZ_FIX_LINK_PATHS=
DYNAMIC_XPCOM_LIBS='-L$(LIBXUL_DIST)/lib -lxpcom -lxpcom_core'
XPCOM_FROZEN_LDOPTS='-L$(LIBXUL_DIST)/lib -lxpcom'
DYNAMIC_XPCOM_LIBS='-L$(LIBXUL_DIST)/lib -lxpcom -lxpcom_core -lmozalloc'
XPCOM_FROZEN_LDOPTS='-L$(LIBXUL_DIST)/lib -lxpcom -lmozalloc'
DLL_PREFIX=
IMPORT_LIB_SUFFIX=dll.a
else
Expand Down Expand Up @@ -2135,9 +2135,9 @@ case "$target" in
MOZ_OPTIMIZE_FLAGS='-O1'
MOZ_JS_LIBS='$(LIBXUL_DIST)/lib/js$(MOZ_BITS)$(VERSION_NUMBER).lib'
MOZ_FIX_LINK_PATHS=
DYNAMIC_XPCOM_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xpcom_core.lib'
XPCOM_FROZEN_LDOPTS='$(LIBXUL_DIST)/lib/xpcom.lib'
LIBXUL_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xul.lib'
DYNAMIC_XPCOM_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xpcom_core.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
XPCOM_FROZEN_LDOPTS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
LIBXUL_LIBS='$(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xul.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
MOZ_COMPONENT_NSPR_LIBS='$(NSPR_LIBS)'
if test $_MSC_VER -ge 1400; then
LDFLAGS="$LDFLAGS -NXCOMPAT -SAFESEH"
Expand Down Expand Up @@ -2436,8 +2436,8 @@ case "$target" in
MOZ_DEBUG_FLAGS="-g -fno-inline"
MOZ_OPTIMIZE_FLAGS="-O2"
MOZ_OPTIMIZE_LDFLAGS="-s -Zlinker /EXEPACK:2 -Zlinker /PACKCODE -Zlinker /PACKDATA"
DYNAMIC_XPCOM_LIBS='-L$(LIBXUL_DIST)/lib $(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xpcomcor.lib'
LIBXUL_LIBS='-L$(LIBXUL_DIST)/lib $(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xul.lib'
DYNAMIC_XPCOM_LIBS='-L$(LIBXUL_DIST)/lib $(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xpcomcor.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
LIBXUL_LIBS='-L$(LIBXUL_DIST)/lib $(LIBXUL_DIST)/lib/xpcom.lib $(LIBXUL_DIST)/lib/xul.lib $(LIBXUL_DIST)/lib/mozalloc.lib'
TARGET_MD_ARCH=os2
_PLATFORM_DEFAULT_TOOLKIT="cairo-os2"
MOZ_ENABLE_POSTSCRIPT=
Expand Down Expand Up @@ -4122,7 +4122,52 @@ if test "$ac_cv_trouble_comparing_to_zero" = yes ; then
AC_DEFINE(HAVE_CPP_TROUBLE_COMPARING_TO_ZERO)
fi

dnl Check for the existence of various allocation headers/functions

MALLOC_H=
AC_CHECK_HEADER(malloc.h, [MALLOC_H=malloc.h])
if test "$MALLOC_H" = ""; then
AC_CHECK_HEADER(malloc/malloc.h, [MALLOC_H=malloc/malloc.h])
if test "$MALLOC_H" = ""; then
AC_CHECK_HEADER(sys/malloc.h, [MALLOC_H=sys/malloc.h])
fi
fi
if test "$MALLOC_H" != ""; then
AC_DEFINE_UNQUOTED(MALLOC_H, <$MALLOC_H>)
fi

MOZ_ALLOCATING_FUNCS="strndup posix_memalign memalign valloc"
AC_CHECK_FUNCS(strndup posix_memalign memalign valloc)

dnl See if compiler supports some gcc-style attributes

AC_CACHE_CHECK(for __attribute__((always_inline)),
ac_cv_attribute_always_inline,
[AC_TRY_COMPILE([],
[inline void f(void) __attribute__((always_inline));],
ac_cv_attribute_always_inline=yes,
ac_cv_attribute_always_inline=no)])

AC_CACHE_CHECK(for __attribute__((malloc)),
ac_cv_attribute_malloc,
[AC_TRY_COMPILE([],
[void* f(int) __attribute__((malloc));],
ac_cv_attribute_malloc=yes,
ac_cv_attribute_malloc=no)])

AC_CACHE_CHECK(for __attribute__((warn_unused_result)),
ac_cv_attribute_warn_unused,
[AC_TRY_COMPILE([],
[int f(void) __attribute__((warn_unused_result));],
ac_cv_attribute_warn_unused=yes,
ac_cv_attribute_warn_unused=no)])

AC_CACHE_CHECK(for __attribute__((noreturn)),
ac_cv_attribute_noreturn,
[AC_TRY_COMPILE([],
[void f(void) __attribute__((noreturn));],
ac_cv_attribute_noreturn=yes,
ac_cv_attribute_noreturn=no)])

dnl End of C++ language/feature checks
AC_LANG_C
Expand Down Expand Up @@ -4158,6 +4203,40 @@ dnl ========================================================
dnl The macros used for command line options
dnl are defined in build/autoconf/altoptions.m4.

dnl If the compiler supports these attributes, define them as
dnl convenience macros.
if test "$ac_cv_attribute_always_inline" = yes ; then
AC_DEFINE(NS_ALWAYS_INLINE, [__attribute__((always_inline))])
else
AC_DEFINE(NS_ALWAYS_INLINE,)
fi

if test "$ac_cv_attribute_malloc" = yes ; then
AC_DEFINE(NS_ATTR_MALLOC, [__attribute__((malloc))])
else
AC_DEFINE(NS_ATTR_MALLOC,)
fi

if test "$ac_cv_attribute_warn_unused" = yes ; then
AC_DEFINE(NS_WARN_UNUSED_RESULT, [__attribute__((warn_unused_result))])
else
AC_DEFINE(NS_WARN_UNUSED_RESULT,)
fi

if test "$ac_cv_attribute_noreturn" = yes ; then
AC_DEFINE(NS_NORETURN, [__attribute__((noreturn))])
else
AC_DEFINE(NS_NORETURN,)
fi

dnl We can't run TRY_COMPILE tests on Windows, so hard-code some
dnl features that Windows actually does support.

if test -n "$SKIP_COMPILER_CHECKS"; then
dnl Windows has malloc.h
AC_DEFINE(MALLOC_H, [<malloc.h>])
AC_DEFINE(HAVE_FORCEINLINE)
fi # SKIP_COMPILER_CHECKS

dnl ========================================================
dnl =
Expand Down Expand Up @@ -6480,6 +6559,13 @@ if test "$MOZ_MEMORY"; then
AC_MSG_ERROR([--enable-jemalloc not supported on ${target}])
;;
esac

if test "$OS_ARCH" != "Darwin"; then
dnl NB: this must be kept in sync with jemalloc.h
AC_DEFINE(HAVE_JEMALLOC_VALLOC)
AC_DEFINE(HAVE_JEMALLOC_POSIX_MEMALIGN)
AC_DEFINE(HAVE_JEMALLOC_MEMALIGN)
fi
fi
AC_SUBST(MOZ_MEMORY)
AC_SUBST(MOZ_MEMORY_LDFLAGS)
Expand Down
2 changes: 1 addition & 1 deletion content/base/src/nsScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* A class that handles loading and evaluation of <script> elements.
*/

#include "jscntxt.h"
#include "nsScriptLoader.h"
#include "nsIDOMCharacterData.h"
#include "nsParserUtils.h"
Expand All @@ -60,7 +61,6 @@
#include "nsIScriptElement.h"
#include "nsIDOMHTMLScriptElement.h"
#include "nsIDocShell.h"
#include "jscntxt.h"
#include "nsContentUtils.h"
#include "nsUnicharUtils.h"
#include "nsAutoPtr.h"
Expand Down
2 changes: 1 addition & 1 deletion content/xul/document/src/nsXULContentSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* see http://developer.mozilla.org/en/docs/XUL
*/

#include "jscntxt.h" // for JSVERSION_HAS_XML
#include "nsXULContentSink.h"
#include "nsCOMPtr.h"
#include "nsForwardReference.h"
Expand Down Expand Up @@ -79,7 +80,6 @@
#include "nsXULElement.h"
#include "prlog.h"
#include "prmem.h"
#include "jscntxt.h" // for JSVERSION_HAS_XML
#include "nsCRT.h"

#include "nsXULPrototypeDocument.h" // XXXbe temporary
Expand Down
Loading

0 comments on commit 50b7b03

Please sign in to comment.