Skip to content

Commit b1bac95

Browse files
committed
[examples] Add common files for examples
1 parent e186e4a commit b1bac95

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

examples/common/cxxabi.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2009-2011, Fabian Greif
3+
* Copyright (c) 2010, Martin Rosekeit
4+
* Copyright (c) 2012, Sascha Schade
5+
* Copyright (c) 2012-2014, Niklas Hauser
6+
* Copyright (c) 2018, Christopher Durand
7+
*
8+
* This file is part of the modm project.
9+
*
10+
* This Source Code Form is subject to the terms of the Mozilla Public
11+
* License, v. 2.0. If a copy of the MPL was not distributed with this
12+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
13+
*/
14+
// ----------------------------------------------------------------------------
15+
16+
extern "C"
17+
{
18+
/**
19+
* \brief Pure-virtual workaround.
20+
*
21+
* The avr-libc does not support a default implementation for handling
22+
* possible pure-virtual calls. This is a short and empty workaround for this.
23+
*/
24+
void
25+
__cxa_pure_virtual()
26+
{
27+
__builtin_abort();
28+
}
29+
30+
__extension__ typedef int __guard __attribute__((mode (__DI__)));
31+
32+
int
33+
__cxa_guard_acquire(__guard *g)
34+
{
35+
return !*(char *)(g);
36+
}
37+
38+
void
39+
__cxa_guard_release (__guard *g)
40+
{
41+
*(char *)g = 1;
42+
}
43+
44+
void
45+
__cxa_guard_abort (__guard *)
46+
{
47+
}
48+
49+
int
50+
__cxa_atexit (void (* /*destructor*/)(void*), void* /*object*/, void* /*dso_handle*/)
51+
{
52+
return 0;
53+
}
54+
55+
void* __dso_handle = (void*) &__dso_handle;
56+
}

examples/common/new.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2018, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <new>
13+
#include <bits/functexcept.h>
14+
#include <cstdlib>
15+
#include <cstddef>
16+
17+
template<bool abortOnFail>
18+
static inline void*
19+
allocate(size_t size) _GLIBCXX_USE_NOEXCEPT
20+
{
21+
void* ptr = malloc(size);
22+
23+
if constexpr(abortOnFail) {
24+
if(!ptr) {
25+
std::__throw_bad_alloc();
26+
}
27+
}
28+
29+
return ptr;
30+
}
31+
32+
void *
33+
operator new(size_t size) _GLIBCXX_USE_NOEXCEPT
34+
{
35+
return allocate<true>(size);
36+
}
37+
38+
void *
39+
operator new[](size_t size) _GLIBCXX_USE_NOEXCEPT
40+
{
41+
return allocate<true>(size);
42+
}
43+
44+
void*
45+
operator new(std::size_t size, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
46+
{
47+
return allocate<false>(size);
48+
}
49+
50+
void*
51+
operator new[](std::size_t size, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
52+
{
53+
return allocate<false>(size);
54+
}
55+
56+
void
57+
operator delete(void* ptr) _GLIBCXX_USE_NOEXCEPT
58+
{
59+
free(ptr);
60+
}
61+
62+
void
63+
operator delete(void* ptr, size_t) _GLIBCXX_USE_NOEXCEPT
64+
{
65+
free(ptr);
66+
}
67+
68+
void
69+
operator delete[](void* ptr) _GLIBCXX_USE_NOEXCEPT
70+
{
71+
free(ptr);
72+
}
73+
74+
void
75+
operator delete[](void* ptr, size_t) _GLIBCXX_USE_NOEXCEPT
76+
{
77+
free(ptr);
78+
}
79+
80+
void
81+
operator delete(void* ptr, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
82+
{
83+
free(ptr);
84+
}
85+
86+
void
87+
operator delete[](void* ptr, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
88+
{
89+
free(ptr);
90+
}

0 commit comments

Comments
 (0)