memory/build/mozmemory_wrap.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/build/mozmemory_wrap.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <string.h>
     1.9 +#include "mozmemory_wrap.h"
    1.10 +#include "mozilla/Types.h"
    1.11 +
    1.12 +/* Declare malloc implementation functions with the right return and
    1.13 + * argument types. */
    1.14 +#define MALLOC_DECL(name, return_type, ...) \
    1.15 +  MOZ_MEMORY_API return_type name ## _impl(__VA_ARGS__);
    1.16 +#include "malloc_decls.h"
    1.17 +
    1.18 +#ifdef MOZ_WRAP_NEW_DELETE
    1.19 +/* operator new(unsigned int) */
    1.20 +MOZ_MEMORY_API void *
    1.21 +mozmem_malloc_impl(_Znwj)(unsigned int size)
    1.22 +{
    1.23 +  return malloc_impl(size);
    1.24 +}
    1.25 +/* operator new[](unsigned int) */
    1.26 +MOZ_MEMORY_API void *
    1.27 +mozmem_malloc_impl(_Znaj)(unsigned int size)
    1.28 +{
    1.29 +  return malloc_impl(size);
    1.30 +}
    1.31 +/* operator delete(void*) */
    1.32 +MOZ_MEMORY_API void
    1.33 +mozmem_malloc_impl(_ZdlPv)(void *ptr)
    1.34 +{
    1.35 +  free_impl(ptr);
    1.36 +}
    1.37 +/* operator delete[](void*) */
    1.38 +MOZ_MEMORY_API void
    1.39 +mozmem_malloc_impl(_ZdaPv)(void *ptr)
    1.40 +{
    1.41 +  free_impl(ptr);
    1.42 +}
    1.43 +/*operator new(unsigned int, std::nothrow_t const&)*/
    1.44 +MOZ_MEMORY_API void *
    1.45 +mozmem_malloc_impl(_ZnwjRKSt9nothrow_t)(unsigned int size)
    1.46 +{
    1.47 +  return malloc_impl(size);
    1.48 +}
    1.49 +/*operator new[](unsigned int, std::nothrow_t const&)*/
    1.50 +MOZ_MEMORY_API void *
    1.51 +mozmem_malloc_impl(_ZnajRKSt9nothrow_t)(unsigned int size)
    1.52 +{
    1.53 +  return malloc_impl(size);
    1.54 +}
    1.55 +/* operator delete(void*, std::nothrow_t const&) */
    1.56 +MOZ_MEMORY_API void
    1.57 +mozmem_malloc_impl(_ZdlPvRKSt9nothrow_t)(void *ptr)
    1.58 +{
    1.59 +  free_impl(ptr);
    1.60 +}
    1.61 +/* operator delete[](void*, std::nothrow_t const&) */
    1.62 +MOZ_MEMORY_API void
    1.63 +mozmem_malloc_impl(_ZdaPvRKSt9nothrow_t)(void *ptr)
    1.64 +{
    1.65 +  free_impl(ptr);
    1.66 +}
    1.67 +#endif
    1.68 +
    1.69 +/* strndup and strdup may be defined as macros in string.h, which would
    1.70 + * clash with the definitions below. */
    1.71 +#undef strndup
    1.72 +#undef strdup
    1.73 +
    1.74 +#ifndef XP_DARWIN
    1.75 +MOZ_MEMORY_API char *
    1.76 +strndup_impl(const char *src, size_t len)
    1.77 +{
    1.78 +  char* dst = (char*) malloc_impl(len + 1);
    1.79 +  if (dst) {
    1.80 +    strncpy(dst, src, len);
    1.81 +    dst[len] = '\0';
    1.82 +  }
    1.83 +  return dst;
    1.84 +}
    1.85 +
    1.86 +MOZ_MEMORY_API char *
    1.87 +strdup_impl(const char *src)
    1.88 +{
    1.89 +  size_t len = strlen(src);
    1.90 +  return strndup_impl(src, len);
    1.91 +}
    1.92 +#endif /* XP_DARWIN */
    1.93 +
    1.94 +#ifdef ANDROID
    1.95 +#include <stdarg.h>
    1.96 +#include <stdio.h>
    1.97 +
    1.98 +MOZ_MEMORY_API int
    1.99 +vasprintf_impl(char **str, const char *fmt, va_list ap)
   1.100 +{
   1.101 +  char* ptr, *_ptr;
   1.102 +  int ret;
   1.103 +
   1.104 +  if (str == NULL || fmt == NULL) {
   1.105 +    return -1;
   1.106 +  }
   1.107 +
   1.108 +  ptr = (char*)malloc_impl(128);
   1.109 +  if (ptr == NULL) {
   1.110 +    *str = NULL;
   1.111 +    return -1;
   1.112 +  }
   1.113 +
   1.114 +  ret = vsnprintf(ptr, 128, fmt, ap);
   1.115 +  if (ret < 0) {
   1.116 +    free_impl(ptr);
   1.117 +    *str = NULL;
   1.118 +    return -1;
   1.119 +  }
   1.120 +
   1.121 +  _ptr = realloc_impl(ptr, ret + 1);
   1.122 +  if (_ptr == NULL) {
   1.123 +    free_impl(ptr);
   1.124 +    *str = NULL;
   1.125 +    return -1;
   1.126 +  }
   1.127 +
   1.128 +  *str = _ptr;
   1.129 +
   1.130 +  return ret;
   1.131 +}
   1.132 +
   1.133 +MOZ_MEMORY_API int
   1.134 +asprintf_impl(char **str, const char *fmt, ...)
   1.135 +{
   1.136 +   int ret;
   1.137 +   va_list ap;
   1.138 +   va_start(ap, fmt);
   1.139 +
   1.140 +   ret = vasprintf_impl(str, fmt, ap);
   1.141 +
   1.142 +   va_end(ap);
   1.143 +
   1.144 +   return ret;
   1.145 +}
   1.146 +#endif
   1.147 +
   1.148 +#ifdef XP_WIN
   1.149 +/*
   1.150 + *  There's a fun allocator mismatch in (at least) the VS 2010 CRT
   1.151 + *  (see the giant comment in $(topsrcdir)/mozglue/build/Makefile.in)
   1.152 + *  that gets redirected here to avoid a crash on shutdown.
   1.153 + */
   1.154 +void
   1.155 +dumb_free_thunk(void *ptr)
   1.156 +{
   1.157 +  return; /* shutdown leaks that we don't care about */
   1.158 +}
   1.159 +
   1.160 +#include <wchar.h>
   1.161 +
   1.162 +/*
   1.163 + *  We also need to provide our own impl of wcsdup so that we don't ask
   1.164 + *  the CRT for memory from its heap (which will then be unfreeable).
   1.165 + */
   1.166 +wchar_t *
   1.167 +wcsdup_impl(const wchar_t *src)
   1.168 +{
   1.169 +  size_t len = wcslen(src);
   1.170 +  wchar_t *dst = (wchar_t*) malloc_impl((len + 1) * sizeof(wchar_t));
   1.171 +  if (dst)
   1.172 +    wcsncpy(dst, src, len + 1);
   1.173 +  return dst;
   1.174 +}
   1.175 +#endif /* XP_WIN */

mercurial