2017-11-01 10:56:27 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-12-07 12:32:24 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2017-11-01 10:56:27 +03:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-12-07 12:32:24 +04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "mozmemory_wrap.h"
|
|
|
|
#include "mozilla/Types.h"
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Declare malloc implementation functions with the right return and
|
|
|
|
// argument types.
|
2017-11-01 11:20:54 +03:00
|
|
|
#define MALLOC_DECL(name, return_type, ...) \
|
|
|
|
MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
|
2017-09-01 04:52:23 +03:00
|
|
|
#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC
|
2012-12-07 12:32:24 +04:00
|
|
|
#include "malloc_decls.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_WRAP_NEW_DELETE
|
2017-09-15 04:28:33 +03:00
|
|
|
#include <new>
|
|
|
|
|
|
|
|
MFBT_API void*
|
|
|
|
operator new(size_t size)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
return malloc_impl(size);
|
|
|
|
}
|
2017-09-15 04:28:33 +03:00
|
|
|
|
|
|
|
MFBT_API void*
|
|
|
|
operator new[](size_t size)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
return malloc_impl(size);
|
|
|
|
}
|
2017-09-15 04:28:33 +03:00
|
|
|
|
|
|
|
MFBT_API void
|
2018-03-13 19:10:06 +03:00
|
|
|
operator delete(void* ptr) noexcept(true)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
free_impl(ptr);
|
|
|
|
}
|
2017-09-15 04:28:33 +03:00
|
|
|
|
|
|
|
MFBT_API void
|
2018-03-13 19:10:06 +03:00
|
|
|
operator delete[](void* ptr) noexcept(true)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
free_impl(ptr);
|
|
|
|
}
|
2017-09-15 04:28:33 +03:00
|
|
|
|
|
|
|
MFBT_API void*
|
|
|
|
operator new(size_t size, std::nothrow_t const&)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
return malloc_impl(size);
|
|
|
|
}
|
2017-09-15 04:28:33 +03:00
|
|
|
|
|
|
|
MFBT_API void*
|
|
|
|
operator new[](size_t size, std::nothrow_t const&)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
return malloc_impl(size);
|
|
|
|
}
|
2017-09-15 04:28:33 +03:00
|
|
|
|
|
|
|
MFBT_API void
|
2018-03-13 19:10:06 +03:00
|
|
|
operator delete(void* ptr, std::nothrow_t const&) noexcept(true)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
free_impl(ptr);
|
|
|
|
}
|
2017-09-15 04:28:33 +03:00
|
|
|
|
|
|
|
MFBT_API void
|
2018-03-13 19:10:06 +03:00
|
|
|
operator delete[](void* ptr, std::nothrow_t const&) noexcept(true)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
free_impl(ptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// strndup and strdup may be defined as macros in string.h, which would
|
|
|
|
// clash with the definitions below.
|
2012-12-07 12:32:24 +04:00
|
|
|
#undef strndup
|
|
|
|
#undef strdup
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
MOZ_MEMORY_API char*
|
|
|
|
strndup_impl(const char* src, size_t len)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
2017-11-01 11:20:54 +03:00
|
|
|
char* dst = (char*)malloc_impl(len + 1);
|
2013-01-03 19:05:20 +04:00
|
|
|
if (dst) {
|
|
|
|
strncpy(dst, src, len);
|
|
|
|
dst[len] = '\0';
|
|
|
|
}
|
2012-12-07 12:32:24 +04:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
MOZ_MEMORY_API char*
|
|
|
|
strdup_impl(const char* src)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
size_t len = strlen(src);
|
|
|
|
return strndup_impl(src, len);
|
|
|
|
}
|
|
|
|
|
2014-01-17 12:54:06 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <stdarg.h>
|
2014-03-06 14:20:00 +04:00
|
|
|
#include <stdio.h>
|
2014-01-17 12:54:06 +04:00
|
|
|
|
|
|
|
MOZ_MEMORY_API int
|
2017-11-01 11:20:54 +03:00
|
|
|
vasprintf_impl(char** str, const char* fmt, va_list ap)
|
2014-01-17 12:54:06 +04:00
|
|
|
{
|
2017-11-01 11:20:54 +03:00
|
|
|
char *ptr, *_ptr;
|
2014-03-06 14:19:57 +04:00
|
|
|
int ret;
|
|
|
|
|
2014-01-17 12:54:06 +04:00
|
|
|
if (str == NULL || fmt == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-06 14:19:57 +04:00
|
|
|
ptr = (char*)malloc_impl(128);
|
2014-01-17 12:54:06 +04:00
|
|
|
if (ptr == NULL) {
|
|
|
|
*str = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-06 14:19:57 +04:00
|
|
|
ret = vsnprintf(ptr, 128, fmt, ap);
|
2014-01-17 12:54:06 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
free_impl(ptr);
|
|
|
|
*str = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-15 04:19:37 +03:00
|
|
|
_ptr = reinterpret_cast<char*>(realloc_impl(ptr, ret + 1));
|
2014-01-17 12:54:06 +04:00
|
|
|
if (_ptr == NULL) {
|
|
|
|
free_impl(ptr);
|
|
|
|
*str = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*str = _ptr;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_MEMORY_API int
|
2017-11-01 11:20:54 +03:00
|
|
|
asprintf_impl(char** str, const char* fmt, ...)
|
2014-03-06 14:19:57 +04:00
|
|
|
{
|
2017-11-01 11:20:54 +03:00
|
|
|
int ret;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2014-01-17 12:54:06 +04:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
ret = vasprintf_impl(str, fmt, ap);
|
2014-01-17 12:54:06 +04:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
va_end(ap);
|
2014-01-17 12:54:06 +04:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
return ret;
|
2014-01-17 12:54:06 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-07 12:32:24 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <wchar.h>
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// We also need to provide our own impl of wcsdup so that we don't ask
|
|
|
|
// the CRT for memory from its heap (which will then be unfreeable).
|
2017-09-15 04:12:24 +03:00
|
|
|
MOZ_MEMORY_API wchar_t*
|
|
|
|
wcsdup_impl(const wchar_t* src)
|
2012-12-07 12:32:24 +04:00
|
|
|
{
|
|
|
|
size_t len = wcslen(src);
|
2017-11-01 11:20:54 +03:00
|
|
|
wchar_t* dst = (wchar_t*)malloc_impl((len + 1) * sizeof(wchar_t));
|
2012-12-07 12:32:24 +04:00
|
|
|
if (dst)
|
|
|
|
wcsncpy(dst, src, len + 1);
|
|
|
|
return dst;
|
|
|
|
}
|
2015-01-12 06:20:56 +03:00
|
|
|
|
2017-09-15 04:12:24 +03:00
|
|
|
MOZ_MEMORY_API void*
|
2015-01-12 06:20:56 +03:00
|
|
|
_aligned_malloc(size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
return memalign_impl(alignment, size);
|
|
|
|
}
|
2017-11-01 11:15:12 +03:00
|
|
|
#endif // XP_WIN
|