Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <string.h> |
michael@0 | 6 | #include "mozmemory_wrap.h" |
michael@0 | 7 | #include "mozilla/Types.h" |
michael@0 | 8 | |
michael@0 | 9 | /* Declare malloc implementation functions with the right return and |
michael@0 | 10 | * argument types. */ |
michael@0 | 11 | #define MALLOC_DECL(name, return_type, ...) \ |
michael@0 | 12 | MOZ_MEMORY_API return_type name ## _impl(__VA_ARGS__); |
michael@0 | 13 | #include "malloc_decls.h" |
michael@0 | 14 | |
michael@0 | 15 | #ifdef MOZ_WRAP_NEW_DELETE |
michael@0 | 16 | /* operator new(unsigned int) */ |
michael@0 | 17 | MOZ_MEMORY_API void * |
michael@0 | 18 | mozmem_malloc_impl(_Znwj)(unsigned int size) |
michael@0 | 19 | { |
michael@0 | 20 | return malloc_impl(size); |
michael@0 | 21 | } |
michael@0 | 22 | /* operator new[](unsigned int) */ |
michael@0 | 23 | MOZ_MEMORY_API void * |
michael@0 | 24 | mozmem_malloc_impl(_Znaj)(unsigned int size) |
michael@0 | 25 | { |
michael@0 | 26 | return malloc_impl(size); |
michael@0 | 27 | } |
michael@0 | 28 | /* operator delete(void*) */ |
michael@0 | 29 | MOZ_MEMORY_API void |
michael@0 | 30 | mozmem_malloc_impl(_ZdlPv)(void *ptr) |
michael@0 | 31 | { |
michael@0 | 32 | free_impl(ptr); |
michael@0 | 33 | } |
michael@0 | 34 | /* operator delete[](void*) */ |
michael@0 | 35 | MOZ_MEMORY_API void |
michael@0 | 36 | mozmem_malloc_impl(_ZdaPv)(void *ptr) |
michael@0 | 37 | { |
michael@0 | 38 | free_impl(ptr); |
michael@0 | 39 | } |
michael@0 | 40 | /*operator new(unsigned int, std::nothrow_t const&)*/ |
michael@0 | 41 | MOZ_MEMORY_API void * |
michael@0 | 42 | mozmem_malloc_impl(_ZnwjRKSt9nothrow_t)(unsigned int size) |
michael@0 | 43 | { |
michael@0 | 44 | return malloc_impl(size); |
michael@0 | 45 | } |
michael@0 | 46 | /*operator new[](unsigned int, std::nothrow_t const&)*/ |
michael@0 | 47 | MOZ_MEMORY_API void * |
michael@0 | 48 | mozmem_malloc_impl(_ZnajRKSt9nothrow_t)(unsigned int size) |
michael@0 | 49 | { |
michael@0 | 50 | return malloc_impl(size); |
michael@0 | 51 | } |
michael@0 | 52 | /* operator delete(void*, std::nothrow_t const&) */ |
michael@0 | 53 | MOZ_MEMORY_API void |
michael@0 | 54 | mozmem_malloc_impl(_ZdlPvRKSt9nothrow_t)(void *ptr) |
michael@0 | 55 | { |
michael@0 | 56 | free_impl(ptr); |
michael@0 | 57 | } |
michael@0 | 58 | /* operator delete[](void*, std::nothrow_t const&) */ |
michael@0 | 59 | MOZ_MEMORY_API void |
michael@0 | 60 | mozmem_malloc_impl(_ZdaPvRKSt9nothrow_t)(void *ptr) |
michael@0 | 61 | { |
michael@0 | 62 | free_impl(ptr); |
michael@0 | 63 | } |
michael@0 | 64 | #endif |
michael@0 | 65 | |
michael@0 | 66 | /* strndup and strdup may be defined as macros in string.h, which would |
michael@0 | 67 | * clash with the definitions below. */ |
michael@0 | 68 | #undef strndup |
michael@0 | 69 | #undef strdup |
michael@0 | 70 | |
michael@0 | 71 | #ifndef XP_DARWIN |
michael@0 | 72 | MOZ_MEMORY_API char * |
michael@0 | 73 | strndup_impl(const char *src, size_t len) |
michael@0 | 74 | { |
michael@0 | 75 | char* dst = (char*) malloc_impl(len + 1); |
michael@0 | 76 | if (dst) { |
michael@0 | 77 | strncpy(dst, src, len); |
michael@0 | 78 | dst[len] = '\0'; |
michael@0 | 79 | } |
michael@0 | 80 | return dst; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | MOZ_MEMORY_API char * |
michael@0 | 84 | strdup_impl(const char *src) |
michael@0 | 85 | { |
michael@0 | 86 | size_t len = strlen(src); |
michael@0 | 87 | return strndup_impl(src, len); |
michael@0 | 88 | } |
michael@0 | 89 | #endif /* XP_DARWIN */ |
michael@0 | 90 | |
michael@0 | 91 | #ifdef ANDROID |
michael@0 | 92 | #include <stdarg.h> |
michael@0 | 93 | #include <stdio.h> |
michael@0 | 94 | |
michael@0 | 95 | MOZ_MEMORY_API int |
michael@0 | 96 | vasprintf_impl(char **str, const char *fmt, va_list ap) |
michael@0 | 97 | { |
michael@0 | 98 | char* ptr, *_ptr; |
michael@0 | 99 | int ret; |
michael@0 | 100 | |
michael@0 | 101 | if (str == NULL || fmt == NULL) { |
michael@0 | 102 | return -1; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | ptr = (char*)malloc_impl(128); |
michael@0 | 106 | if (ptr == NULL) { |
michael@0 | 107 | *str = NULL; |
michael@0 | 108 | return -1; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | ret = vsnprintf(ptr, 128, fmt, ap); |
michael@0 | 112 | if (ret < 0) { |
michael@0 | 113 | free_impl(ptr); |
michael@0 | 114 | *str = NULL; |
michael@0 | 115 | return -1; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | _ptr = realloc_impl(ptr, ret + 1); |
michael@0 | 119 | if (_ptr == NULL) { |
michael@0 | 120 | free_impl(ptr); |
michael@0 | 121 | *str = NULL; |
michael@0 | 122 | return -1; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | *str = _ptr; |
michael@0 | 126 | |
michael@0 | 127 | return ret; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | MOZ_MEMORY_API int |
michael@0 | 131 | asprintf_impl(char **str, const char *fmt, ...) |
michael@0 | 132 | { |
michael@0 | 133 | int ret; |
michael@0 | 134 | va_list ap; |
michael@0 | 135 | va_start(ap, fmt); |
michael@0 | 136 | |
michael@0 | 137 | ret = vasprintf_impl(str, fmt, ap); |
michael@0 | 138 | |
michael@0 | 139 | va_end(ap); |
michael@0 | 140 | |
michael@0 | 141 | return ret; |
michael@0 | 142 | } |
michael@0 | 143 | #endif |
michael@0 | 144 | |
michael@0 | 145 | #ifdef XP_WIN |
michael@0 | 146 | /* |
michael@0 | 147 | * There's a fun allocator mismatch in (at least) the VS 2010 CRT |
michael@0 | 148 | * (see the giant comment in $(topsrcdir)/mozglue/build/Makefile.in) |
michael@0 | 149 | * that gets redirected here to avoid a crash on shutdown. |
michael@0 | 150 | */ |
michael@0 | 151 | void |
michael@0 | 152 | dumb_free_thunk(void *ptr) |
michael@0 | 153 | { |
michael@0 | 154 | return; /* shutdown leaks that we don't care about */ |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | #include <wchar.h> |
michael@0 | 158 | |
michael@0 | 159 | /* |
michael@0 | 160 | * We also need to provide our own impl of wcsdup so that we don't ask |
michael@0 | 161 | * the CRT for memory from its heap (which will then be unfreeable). |
michael@0 | 162 | */ |
michael@0 | 163 | wchar_t * |
michael@0 | 164 | wcsdup_impl(const wchar_t *src) |
michael@0 | 165 | { |
michael@0 | 166 | size_t len = wcslen(src); |
michael@0 | 167 | wchar_t *dst = (wchar_t*) malloc_impl((len + 1) * sizeof(wchar_t)); |
michael@0 | 168 | if (dst) |
michael@0 | 169 | wcsncpy(dst, src, len + 1); |
michael@0 | 170 | return dst; |
michael@0 | 171 | } |
michael@0 | 172 | #endif /* XP_WIN */ |