1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozmemory_wrap.h"
7 #include "mozilla/Types.h"
9 /* Declare malloc implementation functions with the right return and
11 #define MALLOC_DECL(name, return_type, ...) \
12 MOZ_MEMORY_API return_type name ## _impl(__VA_ARGS__);
13 #include "malloc_decls.h"
15 #ifdef MOZ_WRAP_NEW_DELETE
16 /* operator new(unsigned int) */
18 mozmem_malloc_impl(_Znwj
)(unsigned int size
)
20 return malloc_impl(size
);
22 /* operator new[](unsigned int) */
24 mozmem_malloc_impl(_Znaj
)(unsigned int size
)
26 return malloc_impl(size
);
28 /* operator delete(void*) */
30 mozmem_malloc_impl(_ZdlPv
)(void *ptr
)
34 /* operator delete[](void*) */
36 mozmem_malloc_impl(_ZdaPv
)(void *ptr
)
40 /*operator new(unsigned int, std::nothrow_t const&)*/
42 mozmem_malloc_impl(_ZnwjRKSt9nothrow_t
)(unsigned int size
)
44 return malloc_impl(size
);
46 /*operator new[](unsigned int, std::nothrow_t const&)*/
48 mozmem_malloc_impl(_ZnajRKSt9nothrow_t
)(unsigned int size
)
50 return malloc_impl(size
);
52 /* operator delete(void*, std::nothrow_t const&) */
54 mozmem_malloc_impl(_ZdlPvRKSt9nothrow_t
)(void *ptr
)
58 /* operator delete[](void*, std::nothrow_t const&) */
60 mozmem_malloc_impl(_ZdaPvRKSt9nothrow_t
)(void *ptr
)
66 /* strndup and strdup may be defined as macros in string.h, which would
67 * clash with the definitions below. */
73 strndup_impl(const char *src
, size_t len
)
75 char* dst
= (char*) malloc_impl(len
+ 1);
77 strncpy(dst
, src
, len
);
84 strdup_impl(const char *src
)
86 size_t len
= strlen(src
);
87 return strndup_impl(src
, len
);
89 #endif /* XP_DARWIN */
96 vasprintf_impl(char **str
, const char *fmt
, va_list ap
)
101 if (str
== NULL
|| fmt
== NULL
) {
105 ptr
= (char*)malloc_impl(128);
111 ret
= vsnprintf(ptr
, 128, fmt
, ap
);
118 _ptr
= realloc_impl(ptr
, ret
+ 1);
131 asprintf_impl(char **str
, const char *fmt
, ...)
137 ret
= vasprintf_impl(str
, fmt
, ap
);
147 * There's a fun allocator mismatch in (at least) the VS 2010 CRT
148 * (see the giant comment in $(topsrcdir)/mozglue/build/Makefile.in)
149 * that gets redirected here to avoid a crash on shutdown.
152 dumb_free_thunk(void *ptr
)
154 return; /* shutdown leaks that we don't care about */
160 * We also need to provide our own impl of wcsdup so that we don't ask
161 * the CRT for memory from its heap (which will then be unfreeable).
164 wcsdup_impl(const wchar_t *src
)
166 size_t len
= wcslen(src
);
167 wchar_t *dst
= (wchar_t*) malloc_impl((len
+ 1) * sizeof(wchar_t));
169 wcsncpy(dst
, src
, len
+ 1);