-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstck.h
131 lines (106 loc) · 2.86 KB
/
stck.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* mµOS - my micro OS
*
* Copyright (C)
* 2019 Christian Thäter <ct@pipapo.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MUOS_STCK_H
#define MUOS_STCK_H
#ifdef MUOS_STCK
#include <muos/muos.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
//FIXME: format docs
//stck_api:
//: void muos_stck_init (void)::
//: initializes the stack checker and puts canarys on the stack.
//: can be used to reset/reinitialize it again
void
muos_stck_init (void);
//TODO: platform specific code -> should go to hw
static inline char*
muos_stck_brk (void)
{
extern char* __brkval;
extern char __heap_start;
return __brkval?__brkval:&__heap_start;
}
//stck_api:
//: bool muos_stck_ok (char* addr)::
//: checks that addr (and preeceding MUOS_STCK_CONS) bytes are unmodified.
static inline bool
muos_stck_ok (char* addr)
{
for (char* mem = addr - MUOS_STCK_CONS; mem < addr; ++mem)
{
if (*mem != MUOS_STCK_CANARY(mem))
return false;
}
return true;
}
//stck_api:
//: fast check that reserved space *was* available at any time on the stack
static inline bool
muos_stck_check (size_t reserved)
{
return muos_stck_ok (muos_stck_brk() + reserved);
}
//stck_api:
//: size_t muos_stck_size (void)
//: returns the stack size (may change with allocations)
static inline size_t
muos_stck_size (void)
{
return RAMEND - (size_t) muos_stck_brk ();
}
//stck_api:
//: size_t muos_stck_avail (void)
//: returns the stackspace currently available
//:
static inline size_t
muos_stck_avail (void)
{
return (char*)SP - muos_stck_brk();
}
//stck_api:
//: size_t muos_stck_used (void)
//: returns the stackspace currently used
//:
static inline size_t
muos_stck_used (void)
{
return RAMEND - SP;
}
//stck_api:
//: size_t muos_stck_minfree (void)
//: returns the minimum space that was available on the stack
//: results may be inaccurate up to MUOS_STCK_CONS bytes.
//:
size_t
muos_stck_minfree (void);
//stck_api:
//: size_t muos_stck_maxused (void)
//: returns the maximum space that was used on the stack
//: results may be inaccurate up to MUOS_STCK_CONS bytes.
//:
static inline size_t
muos_stck_maxused (void)
{
return muos_stck_size () - muos_stck_minfree ();
}
#endif // MUOS_STCK
#endif /* MUOS_STCK_H */