Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | // -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*- |
2 | |||
3 | // |
||
4 | // C++ runtime support not provided by Arduino |
||
5 | // |
||
6 | // Note: use new/delete with caution. The heap is small and |
||
7 | // easily fragmented. |
||
8 | // |
||
9 | |||
10 | #include <stdlib.h> |
||
11 | #include "c++.h" |
||
12 | #if defined(ARDUINO) && ARDUINO >= 100 |
||
13 | #include "Arduino.h" |
||
14 | #else |
||
15 | #include "WProgram.h" |
||
16 | #endif |
||
17 | |||
18 | void * operator new(size_t size) |
||
19 | { |
||
20 | #ifdef AP_DISPLAYMEM |
||
21 | displayMemory(); |
||
22 | #endif |
||
23 | return(calloc(size, 1)); |
||
24 | } |
||
25 | |||
26 | void operator delete(void *p) |
||
27 | { |
||
28 | if (p) free(p); |
||
29 | } |
||
30 | |||
31 | extern "C" void __cxa_pure_virtual() |
||
32 | { |
||
33 | while (1) |
||
34 | { |
||
35 | Serial.println("Error: pure virtual call"); |
||
36 | delay(1000); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | void * operator new[](size_t size) |
||
41 | { |
||
42 | #ifdef AP_DISPLAYMEM |
||
43 | displayMemory(); |
||
44 | #endif |
||
45 | return(calloc(size, 1)); |
||
46 | } |
||
47 | |||
48 | void operator delete[](void * ptr) |
||
49 | { |
||
50 | if (ptr) free(ptr); |
||
51 | } |
||
52 | |||
53 | __extension__ typedef int __guard __attribute__((mode (__DI__))); |
||
54 | |||
55 | int __cxa_guard_acquire(__guard *g) |
||
56 | { |
||
57 | return !*(char *)(g); |
||
58 | }; |
||
59 | |||
60 | void __cxa_guard_release (__guard *g) |
||
61 | { |
||
62 | *(char *)g = 1; |
||
63 | }; |
||
64 | |||
65 | void __cxa_guard_abort (__guard *) {}; |
||
66 | |||
67 | // free memory |
||
68 | extern unsigned int __bss_end; |
||
69 | extern void *__brkval; |
||
70 | |||
71 | void displayMemory() |
||
72 | { |
||
73 | static int minMemFree=0; |
||
74 | if (minMemFree<=0 || freeMemory()<minMemFree) { |
||
75 | minMemFree = freeMemory(); |
||
76 | Serial.print("bytes free: "); |
||
77 | Serial.println(minMemFree); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | int freeMemory() |
||
82 | { |
||
83 | int free_memory; |
||
84 | |||
85 | if ((intptr_t)__brkval == 0) |
||
86 | free_memory = ((intptr_t)&free_memory) - ((intptr_t)&__bss_end); |
||
87 | else |
||
88 | free_memory = ((intptr_t)&free_memory) - ((intptr_t)__brkval); |
||
89 | |||
90 | return free_memory; |
||
91 | } |