AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

Index

Loading...
Searching...
No Matches
ram-usage.h
Go to the documentation of this file.
1/* Copyright (c) 2025 Georg-Johann Lay
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in
12 the documentation and/or other materials provided with the
13 distribution.
14
15 * Neither the name of the copyright holders nor the names of
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE. */
30
31#ifndef _UTIL_RAM_USAGE_H_
32#define _UTIL_RAM_USAGE_H_
33
34#include <stdint.h>
35#include <bits/attribs.h>
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/** \file */
42/** \defgroup util_ram_usage <util/ram-usage.h>: Determine dynamic RAM usage
43 \code
44 #include <util/ram-usage.h>
45 \endcode
46
47 This header provides a single function, _get_ram_unused(), that can
48 be used during development to get a rough estimate of how much RAM might
49 be used by an application. This works as follows:
50
51 -# The startup code paints the RAM location with a specific value. This
52 happens in \ref sec_dot_init ".init3", and only when _get_ram_unused()
53 is actually used. The coloring extends from #__heap_start (which is
54 the RAM location right after static storage) up to and
55 including \c RAMEND as defined in <avr/io.h>.
56
57 -# The application calls _get_ram_unused() and determines how much
58 of the colored bytes are still intact. This can be used to calculate
59 a <b>lower bound</b> of how much RAM is used by the application.
60
61 \since AVR-LibC v2.3
62*/
63
64/** \ingroup util_ram_usage
65
66 Determines how much of the initial RAM coloring is still intact.
67 It can be used to get a <b>lower bound</b> of how much RAM might be
68 used by an application.
69
70 The function is written in such a way that it has a small register foot
71 print, so that it is not a big issue to call it in an #ISR.
72 Though that's not required for the intended purpose:
73 _get_ram_unused() can simply be called after some time has elapsed
74 and enough ISRs and other functions have been invoked.
75
76 \return The value returned by _get_ram_unused() is an <em>upper bound</em>
77 for how much bytes of RAM are still
78 <em>unused at the time of invocation</em>.<br>
79 The return value will never increase with time (except for very rare
80 occasions where #__ram_color_value is written to the top of the stack).
81
82 \par Limitations
83 - The start of the coloring is hard coded as #__heap_start, which
84 is the beginning of the RAM area after static storage.
85 - The algorithm assumes that the stack is located <em>after
86 static storage</em> and grows downwards towards #__heap_start.
87 - The current implementation is <b>not compatible with malloc</b> et al.
88 (alloca() is no problem though, since it allocates on the stack.)
89*/
90#ifdef __DOXYGEN__
91extern inline uint16_t _get_ram_unused (void);
92#else
93extern __ATTR_ALWAYS_INLINE__ __ATTR_GNU_INLINE__
95{
96 register uint16_t __r24 __asm("r24");
97 __asm ("%~call _get_ram_unused"
98 : "=r" (__r24) :: "r30", "r31");
99 return __r24;
100}
101#endif /* !DOXYGEN */
102
103#ifdef __DOXYGEN__
104
105/** \name Symbols */
106
107/** \ingroup util_ram_usage
108 A symbol defined in the default linker script.
109 It points one byte past static storage (\ref sec_dot_data ".data",
110 \ref sec_dot_bss ".bss", \ref sec_dot_noinit ".noinit"). */
111extern __heap_start;
112
113/** \ingroup util_ram_usage
114 A weak symbol that defaults to <tt>RAMEND + 1</tt>.
115 It points one byte past the last location that is colored by
116 the startup code.
117 It can be adjusted by say<br>
118\code
119 avr-gcc ... -Wl,-defsym,__ram_color_end=<value>
120\endcode
121 in the link command, or by means of a global inline assembly statement
122 like:
123\code
124 __asm (".global __ram_color_end\n"
125 "__ram_color_end = <value>");
126\endcode */
128
129/** \ingroup util_ram_usage
130 A weak symbol that defaults to 0xaa.
131 It represents the "color" that's being used by the startup code to
132 paint the RAM, and the value that _get_ram_unused() will check against.
133 It can be adjusted by say
134\code
135 avr-gcc ... -Wl,-defsym,__ram_color_value=<value>
136\endcode
137 in the link command. */
139
140#endif /* DOXYGEN */
141
142#ifdef __cplusplus
143} // extern "C"
144#endif
145
146#endif /* _UTIL_RAM_USAGE_H_ */
unsigned int uint16_t
Definition: stdint.h:91
__heap_start
__ram_color_value
Definition: ram-usage.h:138
__ram_color_end
Definition: ram-usage.h:127
uint16_t _get_ram_unused(void)