michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "mozmemory_wrap.h" michael@0: #include "mozilla/Types.h" michael@0: michael@0: /* Declare malloc implementation functions with the right return and michael@0: * argument types. */ michael@0: #define MALLOC_DECL(name, return_type, ...) \ michael@0: MOZ_MEMORY_API return_type name ## _impl(__VA_ARGS__); michael@0: #include "malloc_decls.h" michael@0: michael@0: #ifdef MOZ_WRAP_NEW_DELETE michael@0: /* operator new(unsigned int) */ michael@0: MOZ_MEMORY_API void * michael@0: mozmem_malloc_impl(_Znwj)(unsigned int size) michael@0: { michael@0: return malloc_impl(size); michael@0: } michael@0: /* operator new[](unsigned int) */ michael@0: MOZ_MEMORY_API void * michael@0: mozmem_malloc_impl(_Znaj)(unsigned int size) michael@0: { michael@0: return malloc_impl(size); michael@0: } michael@0: /* operator delete(void*) */ michael@0: MOZ_MEMORY_API void michael@0: mozmem_malloc_impl(_ZdlPv)(void *ptr) michael@0: { michael@0: free_impl(ptr); michael@0: } michael@0: /* operator delete[](void*) */ michael@0: MOZ_MEMORY_API void michael@0: mozmem_malloc_impl(_ZdaPv)(void *ptr) michael@0: { michael@0: free_impl(ptr); michael@0: } michael@0: /*operator new(unsigned int, std::nothrow_t const&)*/ michael@0: MOZ_MEMORY_API void * michael@0: mozmem_malloc_impl(_ZnwjRKSt9nothrow_t)(unsigned int size) michael@0: { michael@0: return malloc_impl(size); michael@0: } michael@0: /*operator new[](unsigned int, std::nothrow_t const&)*/ michael@0: MOZ_MEMORY_API void * michael@0: mozmem_malloc_impl(_ZnajRKSt9nothrow_t)(unsigned int size) michael@0: { michael@0: return malloc_impl(size); michael@0: } michael@0: /* operator delete(void*, std::nothrow_t const&) */ michael@0: MOZ_MEMORY_API void michael@0: mozmem_malloc_impl(_ZdlPvRKSt9nothrow_t)(void *ptr) michael@0: { michael@0: free_impl(ptr); michael@0: } michael@0: /* operator delete[](void*, std::nothrow_t const&) */ michael@0: MOZ_MEMORY_API void michael@0: mozmem_malloc_impl(_ZdaPvRKSt9nothrow_t)(void *ptr) michael@0: { michael@0: free_impl(ptr); michael@0: } michael@0: #endif michael@0: michael@0: /* strndup and strdup may be defined as macros in string.h, which would michael@0: * clash with the definitions below. */ michael@0: #undef strndup michael@0: #undef strdup michael@0: michael@0: #ifndef XP_DARWIN michael@0: MOZ_MEMORY_API char * michael@0: strndup_impl(const char *src, size_t len) michael@0: { michael@0: char* dst = (char*) malloc_impl(len + 1); michael@0: if (dst) { michael@0: strncpy(dst, src, len); michael@0: dst[len] = '\0'; michael@0: } michael@0: return dst; michael@0: } michael@0: michael@0: MOZ_MEMORY_API char * michael@0: strdup_impl(const char *src) michael@0: { michael@0: size_t len = strlen(src); michael@0: return strndup_impl(src, len); michael@0: } michael@0: #endif /* XP_DARWIN */ michael@0: michael@0: #ifdef ANDROID michael@0: #include michael@0: #include michael@0: michael@0: MOZ_MEMORY_API int michael@0: vasprintf_impl(char **str, const char *fmt, va_list ap) michael@0: { michael@0: char* ptr, *_ptr; michael@0: int ret; michael@0: michael@0: if (str == NULL || fmt == NULL) { michael@0: return -1; michael@0: } michael@0: michael@0: ptr = (char*)malloc_impl(128); michael@0: if (ptr == NULL) { michael@0: *str = NULL; michael@0: return -1; michael@0: } michael@0: michael@0: ret = vsnprintf(ptr, 128, fmt, ap); michael@0: if (ret < 0) { michael@0: free_impl(ptr); michael@0: *str = NULL; michael@0: return -1; michael@0: } michael@0: michael@0: _ptr = realloc_impl(ptr, ret + 1); michael@0: if (_ptr == NULL) { michael@0: free_impl(ptr); michael@0: *str = NULL; michael@0: return -1; michael@0: } michael@0: michael@0: *str = _ptr; michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: MOZ_MEMORY_API int michael@0: asprintf_impl(char **str, const char *fmt, ...) michael@0: { michael@0: int ret; michael@0: va_list ap; michael@0: va_start(ap, fmt); michael@0: michael@0: ret = vasprintf_impl(str, fmt, ap); michael@0: michael@0: va_end(ap); michael@0: michael@0: return ret; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef XP_WIN michael@0: /* michael@0: * There's a fun allocator mismatch in (at least) the VS 2010 CRT michael@0: * (see the giant comment in $(topsrcdir)/mozglue/build/Makefile.in) michael@0: * that gets redirected here to avoid a crash on shutdown. michael@0: */ michael@0: void michael@0: dumb_free_thunk(void *ptr) michael@0: { michael@0: return; /* shutdown leaks that we don't care about */ michael@0: } michael@0: michael@0: #include michael@0: michael@0: /* michael@0: * We also need to provide our own impl of wcsdup so that we don't ask michael@0: * the CRT for memory from its heap (which will then be unfreeable). michael@0: */ michael@0: wchar_t * michael@0: wcsdup_impl(const wchar_t *src) michael@0: { michael@0: size_t len = wcslen(src); michael@0: wchar_t *dst = (wchar_t*) malloc_impl((len + 1) * sizeof(wchar_t)); michael@0: if (dst) michael@0: wcsncpy(dst, src, len + 1); michael@0: return dst; michael@0: } michael@0: #endif /* XP_WIN */