Skip to content

Commit f7eae1a

Browse files
committed
Add support for atomvm_minimum_free_size key to system_info/1 on STM32 platform
Adds support for getting the current free heap size on STM32 platform using `erlang:system_info(atomvm_minimum_free_size)`. Signed-off-by: Winford <winford@object.stream>
1 parent 48f274d commit f7eae1a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4949
- Added Function.ex and Protocol.ex improving Elixir 1.18 support
5050
- Added WiFi support for ESP32P4 via esp-wifi-external for build with ESP-IDF v5.4 and later
5151
- Added Process.link/1 and unlink/1 to Elixir Process.ex
52+
- Added `erlang:system_info/1` keys `atomvm_free_heap_size` and `atomvm_minimum_free_size` on STM32 platform
5253

5354
### Changed
5455

src/platforms/stm32/src/lib/sys.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern uint8_t _ebss, _stack;
5151

5252
static uint8_t *_cur_brk = NULL;
5353
static uint8_t *_heap_end = NULL;
54+
static uint8_t *_heap_high_mark = NULL;
5455

5556
/*
5657
* If not overridden, this puts the heap into the left
@@ -63,6 +64,7 @@ static void __local_ram(uint8_t **start, uint8_t **end)
6364
{
6465
*start = &_ebss;
6566
*end = (uint8_t *) (((uintptr_t) &_stack - RESERVE_STACK_SIZE));
67+
_heap_high_mark = 0;
6668
}
6769

6870
void *_sbrk_r(struct _reent *reent, ptrdiff_t diff)
@@ -79,6 +81,10 @@ void *_sbrk_r(struct _reent *reent, ptrdiff_t diff)
7981
return (void *) -1;
8082
}
8183
_cur_brk += diff;
84+
if (_cur_brk > _heap_high_mark) {
85+
_heap_high_mark = _cur_brk;
86+
}
87+
8288
return _old_brk;
8389
}
8490

@@ -87,6 +93,11 @@ static int sys_get_free_heap()
8793
return (int) ((uint8_t *) (((uintptr_t) &_stack - RESERVE_STACK_SIZE))) - (_cur_brk == NULL ? (int) &_ebss : (int) _cur_brk);
8894
}
8995

96+
static int sys_least_free_heap()
97+
{
98+
return (int) ((uint8_t *) (((uintptr_t) &_stack - RESERVE_STACK_SIZE))) - (int) _heap_high_mark;
99+
}
100+
90101
// Monotonically increasing number of milliseconds from reset
91102
static volatile uint64_t system_millis;
92103

@@ -272,10 +283,12 @@ term sys_get_info(Context *ctx, term key)
272283
if (key == globalcontext_existing_term_from_atom_string(glb, ATOM_STR("\x15", "atomvm_free_heap_size"))) {
273284
return term_from_int32(sys_get_free_heap());
274285
}
286+
if (key == globalcontext_existing_term_from_atom_string(glb, ATOM_STR("\x18", "atomvm_minimum_free_size"))) {
287+
return term_from_int32(sys_least_free_heap());
288+
}
275289
return UNDEFINED_ATOM;
276290
}
277291

278-
279292
void sys_enable_flash_cache()
280293
{
281294
flash_unlock_option_bytes();

0 commit comments

Comments
 (0)