Bumping manifests a=b2g-bump
[gecko.git] / memory / build / mozmemory_wrap.c
blobad7a1d0091fa05ad5cdd53f2bd60aefb6bc675f4
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/. */
5 #include <string.h>
6 #include "mozmemory_wrap.h"
7 #include "mozilla/Types.h"
9 /* Declare malloc implementation functions with the right return and
10 * argument types. */
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) */
17 MOZ_MEMORY_API void *
18 mozmem_malloc_impl(_Znwj)(unsigned int size)
20 return malloc_impl(size);
22 /* operator new[](unsigned int) */
23 MOZ_MEMORY_API void *
24 mozmem_malloc_impl(_Znaj)(unsigned int size)
26 return malloc_impl(size);
28 /* operator delete(void*) */
29 MOZ_MEMORY_API void
30 mozmem_malloc_impl(_ZdlPv)(void *ptr)
32 free_impl(ptr);
34 /* operator delete[](void*) */
35 MOZ_MEMORY_API void
36 mozmem_malloc_impl(_ZdaPv)(void *ptr)
38 free_impl(ptr);
40 /*operator new(unsigned int, std::nothrow_t const&)*/
41 MOZ_MEMORY_API void *
42 mozmem_malloc_impl(_ZnwjRKSt9nothrow_t)(unsigned int size)
44 return malloc_impl(size);
46 /*operator new[](unsigned int, std::nothrow_t const&)*/
47 MOZ_MEMORY_API void *
48 mozmem_malloc_impl(_ZnajRKSt9nothrow_t)(unsigned int size)
50 return malloc_impl(size);
52 /* operator delete(void*, std::nothrow_t const&) */
53 MOZ_MEMORY_API void
54 mozmem_malloc_impl(_ZdlPvRKSt9nothrow_t)(void *ptr)
56 free_impl(ptr);
58 /* operator delete[](void*, std::nothrow_t const&) */
59 MOZ_MEMORY_API void
60 mozmem_malloc_impl(_ZdaPvRKSt9nothrow_t)(void *ptr)
62 free_impl(ptr);
64 #endif
66 /* strndup and strdup may be defined as macros in string.h, which would
67 * clash with the definitions below. */
68 #undef strndup
69 #undef strdup
71 #ifndef XP_DARWIN
72 MOZ_MEMORY_API char *
73 strndup_impl(const char *src, size_t len)
75 char* dst = (char*) malloc_impl(len + 1);
76 if (dst) {
77 strncpy(dst, src, len);
78 dst[len] = '\0';
80 return dst;
83 MOZ_MEMORY_API char *
84 strdup_impl(const char *src)
86 size_t len = strlen(src);
87 return strndup_impl(src, len);
89 #endif /* XP_DARWIN */
91 #ifdef ANDROID
92 #include <stdarg.h>
93 #include <stdio.h>
95 MOZ_MEMORY_API int
96 vasprintf_impl(char **str, const char *fmt, va_list ap)
98 char* ptr, *_ptr;
99 int ret;
101 if (str == NULL || fmt == NULL) {
102 return -1;
105 ptr = (char*)malloc_impl(128);
106 if (ptr == NULL) {
107 *str = NULL;
108 return -1;
111 ret = vsnprintf(ptr, 128, fmt, ap);
112 if (ret < 0) {
113 free_impl(ptr);
114 *str = NULL;
115 return -1;
118 _ptr = realloc_impl(ptr, ret + 1);
119 if (_ptr == NULL) {
120 free_impl(ptr);
121 *str = NULL;
122 return -1;
125 *str = _ptr;
127 return ret;
130 MOZ_MEMORY_API int
131 asprintf_impl(char **str, const char *fmt, ...)
133 int ret;
134 va_list ap;
135 va_start(ap, fmt);
137 ret = vasprintf_impl(str, fmt, ap);
139 va_end(ap);
141 return ret;
143 #endif
145 #ifdef XP_WIN
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.
151 void
152 dumb_free_thunk(void *ptr)
154 return; /* shutdown leaks that we don't care about */
157 #include <wchar.h>
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).
163 wchar_t *
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));
168 if (dst)
169 wcsncpy(dst, src, len + 1);
170 return dst;
172 #endif /* XP_WIN */