Skip to content

Commit 0659b80

Browse files
committed
Merge pull request #1586 from esp8266/cxa_guard
Static initialization guards implementation
2 parents 93c446e + 737f6c2 commit 0659b80

File tree

1 file changed

+57
-23
lines changed

1 file changed

+57
-23
lines changed

cores/esp8266/abi.cpp

+57-23
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,91 @@
1919
#include <stdlib.h>
2020
#include <assert.h>
2121
#include <debug.h>
22-
extern "C" {
23-
#include "ets_sys.h"
24-
#include "os_type.h"
25-
#include "osapi.h"
26-
#include "mem.h"
27-
}
22+
#include <Arduino.h>
23+
#include <cxxabi.h>
2824

25+
using __cxxabiv1::__guard;
2926

30-
void *operator new(size_t size) {
31-
size = ((size + 3) & ~((size_t)0x3));
32-
return os_malloc(size);
27+
void *operator new(size_t size)
28+
{
29+
return malloc(size);
3330
}
3431

35-
void *operator new[](size_t size) {
36-
size = ((size + 3) & ~((size_t)0x3));
37-
return os_malloc(size);
32+
void *operator new[](size_t size)
33+
{
34+
return malloc(size);
3835
}
3936

40-
void operator delete(void * ptr) {
41-
os_free(ptr);
37+
void operator delete(void * ptr)
38+
{
39+
free(ptr);
4240
}
4341

44-
void operator delete[](void * ptr) {
45-
os_free(ptr);
42+
void operator delete[](void * ptr)
43+
{
44+
free(ptr);
4645
}
4746

4847
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
4948
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
5049

51-
void __cxa_pure_virtual(void) {
50+
void __cxa_pure_virtual(void)
51+
{
5252
panic();
5353
}
5454

55-
void __cxa_deleted_virtual(void) {
55+
void __cxa_deleted_virtual(void)
56+
{
5657
panic();
5758
}
5859

59-
namespace std {
60-
void __throw_bad_function_call() {
60+
typedef struct {
61+
uint8_t guard;
62+
uint8_t ps;
63+
} guard_t;
64+
65+
extern "C" int __cxa_guard_acquire(__guard* pg)
66+
{
67+
uint8_t ps = xt_rsil(15);
68+
if (reinterpret_cast<guard_t*>(pg)->guard) {
69+
xt_wsr_ps(ps);
70+
return 0;
71+
}
72+
reinterpret_cast<guard_t*>(pg)->ps = ps;
73+
return 1;
74+
}
75+
76+
extern "C" void __cxa_guard_release(__guard* pg)
77+
{
78+
reinterpret_cast<guard_t*>(pg)->guard = 1;
79+
xt_wsr_ps(reinterpret_cast<guard_t*>(pg)->ps);
80+
}
81+
82+
extern "C" void __cxa_guard_abort(__guard* pg)
83+
{
84+
xt_wsr_ps(reinterpret_cast<guard_t*>(pg)->ps);
85+
}
86+
87+
88+
namespace std
89+
{
90+
void __throw_bad_function_call()
91+
{
6192
panic();
6293
}
6394

64-
void __throw_length_error(char const*) {
95+
void __throw_length_error(char const*)
96+
{
6597
panic();
6698
}
6799

68-
void __throw_bad_alloc() {
100+
void __throw_bad_alloc()
101+
{
69102
panic();
70103
}
71104

72-
void __throw_logic_error(const char* str) {
105+
void __throw_logic_error(const char* str)
106+
{
73107
panic();
74108
}
75109
}

0 commit comments

Comments
 (0)