memory/build/mozmemory_wrap.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     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)
    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
    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)
    74 {
    75   char* dst = (char*) malloc_impl(len + 1);
    76   if (dst) {
    77     strncpy(dst, src, len);
    78     dst[len] = '\0';
    79   }
    80   return dst;
    81 }
    83 MOZ_MEMORY_API char *
    84 strdup_impl(const char *src)
    85 {
    86   size_t len = strlen(src);
    87   return strndup_impl(src, len);
    88 }
    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)
    97 {
    98   char* ptr, *_ptr;
    99   int ret;
   101   if (str == NULL || fmt == NULL) {
   102     return -1;
   103   }
   105   ptr = (char*)malloc_impl(128);
   106   if (ptr == NULL) {
   107     *str = NULL;
   108     return -1;
   109   }
   111   ret = vsnprintf(ptr, 128, fmt, ap);
   112   if (ret < 0) {
   113     free_impl(ptr);
   114     *str = NULL;
   115     return -1;
   116   }
   118   _ptr = realloc_impl(ptr, ret + 1);
   119   if (_ptr == NULL) {
   120     free_impl(ptr);
   121     *str = NULL;
   122     return -1;
   123   }
   125   *str = _ptr;
   127   return ret;
   128 }
   130 MOZ_MEMORY_API int
   131 asprintf_impl(char **str, const char *fmt, ...)
   132 {
   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;
   142 }
   143 #endif
   145 #ifdef XP_WIN
   146 /*
   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.
   150  */
   151 void
   152 dumb_free_thunk(void *ptr)
   153 {
   154   return; /* shutdown leaks that we don't care about */
   155 }
   157 #include <wchar.h>
   159 /*
   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).
   162  */
   163 wchar_t *
   164 wcsdup_impl(const wchar_t *src)
   165 {
   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;
   171 }
   172 #endif /* XP_WIN */

mercurial