forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unew.cc
48 lines (38 loc) · 1.32 KB
/
unew.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#include "unew.h"
void* tmalloc (size_t n)
{
void* p = malloc (n);
if (!p)
throw ustl::bad_alloc (n);
return p;
}
void nfree (void* p) noexcept
{
if (p)
free (p);
}
#if WITHOUT_LIBSTDCPP
#if __APPLE__ // MacOS lives in the stone age and does not support aliases
void* operator new (size_t n) { return tmalloc(n); }
void* operator new[] (size_t n) { return tmalloc(n); }
void operator delete (void* p) noexcept { nfree(p); }
void operator delete[] (void* p) noexcept { nfree(p); }
#if HAVE_CPP14
void operator delete (void* p, size_t) noexcept { nfree(p); }
void operator delete[] (void* p, size_t) noexcept { nfree(p); }
#endif // HAVE_CPP14
#else // __APPLE__
void* operator new (size_t n) WEAKALIAS("tmalloc");
void* operator new[] (size_t n) WEAKALIAS("tmalloc");
void operator delete (void* p) noexcept WEAKALIAS("nfree");
void operator delete[] (void* p) noexcept WEAKALIAS("nfree");
#if HAVE_CPP14
void operator delete (void* p, size_t n) noexcept WEAKALIAS("nfree");
void operator delete[] (void* p, size_t n) noexcept WEAKALIAS("nfree");
#endif // HAVE_CPP14
#endif // __APPLE__
#endif // WITHOUT_LIBSTDCPP