Line data Source code
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/. */
4 :
5 : #include <string.h>
6 : #include "mozmemory_wrap.h"
7 : #include "mozilla/Types.h"
8 :
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"
14 :
15 : #ifdef MOZ_WRAP_NEW_DELETE
16 : /* operator new(unsigned int) */
17 : MOZ_MEMORY_API void *
18 : mozmem_malloc_impl(_Znwj)(unsigned int size)
19 : {
20 : return malloc_impl(size);
21 : }
22 : /* operator new[](unsigned int) */
23 : MOZ_MEMORY_API void *
24 : mozmem_malloc_impl(_Znaj)(unsigned int size)
25 : {
26 : return malloc_impl(size);
27 : }
28 : /* operator delete(void*) */
29 : MOZ_MEMORY_API void
30 : mozmem_malloc_impl(_ZdlPv)(void *ptr)
31 : {
32 : free_impl(ptr);
33 : }
34 : /* operator delete[](void*) */
35 : MOZ_MEMORY_API void
36 : mozmem_malloc_impl(_ZdaPv)(void *ptr)
37 : {
38 : free_impl(ptr);
39 : }
40 : /*operator new(unsigned int, std::nothrow_t const&)*/
41 : MOZ_MEMORY_API void *
42 : mozmem_malloc_impl(_ZnwjRKSt9nothrow_t)(unsigned int size)
43 : {
44 : return malloc_impl(size);
45 : }
46 : /*operator new[](unsigned int, std::nothrow_t const&)*/
47 : MOZ_MEMORY_API void *
48 : mozmem_malloc_impl(_ZnajRKSt9nothrow_t)(unsigned int size)
49 : {
50 : return malloc_impl(size);
51 : }
52 : /* operator delete(void*, std::nothrow_t const&) */
53 : MOZ_MEMORY_API void
54 : mozmem_malloc_impl(_ZdlPvRKSt9nothrow_t)(void *ptr)
55 : {
56 : free_impl(ptr);
57 : }
58 : /* operator delete[](void*, std::nothrow_t const&) */
59 : MOZ_MEMORY_API void
60 : mozmem_malloc_impl(_ZdaPvRKSt9nothrow_t)(void *ptr)
61 : {
62 : free_impl(ptr);
63 : }
64 : #endif
65 :
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
70 :
71 : MOZ_MEMORY_API char *
72 : strndup_impl(const char *src, size_t len)
73 : {
74 9591 : char* dst = (char*) malloc_impl(len + 1);
75 9591 : if (dst) {
76 9591 : strncpy(dst, src, len);
77 9591 : dst[len] = '\0';
78 : }
79 9591 : return dst;
80 : }
81 :
82 : MOZ_MEMORY_API char *
83 : strdup_impl(const char *src)
84 : {
85 9591 : size_t len = strlen(src);
86 9591 : return strndup_impl(src, len);
87 : }
88 :
89 : #ifdef ANDROID
90 : #include <stdarg.h>
91 : #include <stdio.h>
92 :
93 : MOZ_MEMORY_API int
94 : vasprintf_impl(char **str, const char *fmt, va_list ap)
95 : {
96 : char* ptr, *_ptr;
97 : int ret;
98 :
99 : if (str == NULL || fmt == NULL) {
100 : return -1;
101 : }
102 :
103 : ptr = (char*)malloc_impl(128);
104 : if (ptr == NULL) {
105 : *str = NULL;
106 : return -1;
107 : }
108 :
109 : ret = vsnprintf(ptr, 128, fmt, ap);
110 : if (ret < 0) {
111 : free_impl(ptr);
112 : *str = NULL;
113 : return -1;
114 : }
115 :
116 : _ptr = realloc_impl(ptr, ret + 1);
117 : if (_ptr == NULL) {
118 : free_impl(ptr);
119 : *str = NULL;
120 : return -1;
121 : }
122 :
123 : *str = _ptr;
124 :
125 : return ret;
126 : }
127 :
128 : MOZ_MEMORY_API int
129 : asprintf_impl(char **str, const char *fmt, ...)
130 : {
131 : int ret;
132 : va_list ap;
133 : va_start(ap, fmt);
134 :
135 : ret = vasprintf_impl(str, fmt, ap);
136 :
137 : va_end(ap);
138 :
139 : return ret;
140 : }
141 : #endif
142 :
143 : #ifdef XP_WIN
144 : /*
145 : * There's a fun allocator mismatch in (at least) the VS 2010 CRT
146 : * (see the giant comment in $(topsrcdir)/mozglue/build/Makefile.in)
147 : * that gets redirected here to avoid a crash on shutdown.
148 : */
149 : void
150 : dumb_free_thunk(void *ptr)
151 : {
152 : return; /* shutdown leaks that we don't care about */
153 : }
154 :
155 : #include <wchar.h>
156 :
157 : /*
158 : * We also need to provide our own impl of wcsdup so that we don't ask
159 : * the CRT for memory from its heap (which will then be unfreeable).
160 : */
161 : wchar_t *
162 : wcsdup_impl(const wchar_t *src)
163 : {
164 : size_t len = wcslen(src);
165 : wchar_t *dst = (wchar_t*) malloc_impl((len + 1) * sizeof(wchar_t));
166 : if (dst)
167 : wcsncpy(dst, src, len + 1);
168 : return dst;
169 : }
170 :
171 : void *
172 : _aligned_malloc(size_t size, size_t alignment)
173 : {
174 : return memalign_impl(alignment, size);
175 : }
176 : #endif /* XP_WIN */
|