Skip to content

Commit a1e4814

Browse files
committed
Get AllocationAlarm working
1 parent 518d909 commit a1e4814

File tree

10 files changed

+40
-40
lines changed

10 files changed

+40
-40
lines changed

main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
#include "shared-module/displayio/__init__.h"
6565
#endif
6666

67+
#if CIRCUITPY_MEMORYMONITOR
68+
#include "shared-module/memorymonitor/__init__.h"
69+
#endif
70+
6771
#if CIRCUITPY_NETWORK
6872
#include "shared-module/network/__init__.h"
6973
#endif
@@ -198,6 +202,9 @@ void cleanup_after_vm(supervisor_allocation* heap) {
198202
#if CIRCUITPY_DISPLAYIO
199203
reset_displays();
200204
#endif
205+
#if CIRCUITPY_MEMORYMONITOR
206+
memorymonitor_reset();
207+
#endif
201208
filesystem_flush();
202209
stop_mp();
203210
free_memory(heap);

py/gc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "supervisor/shared/safe_mode.h"
3535

3636
#if CIRCUITPY_MEMORYMONITOR
37-
#include "shared-module/memorymonitor/AllocationSize.h"
37+
#include "shared-module/memorymonitor/__init__.h"
3838
#endif
3939

4040
#if MICROPY_ENABLE_GC
@@ -658,7 +658,7 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser, bool long_lived) {
658658
#endif
659659

660660
#if CIRCUITPY_MEMORYMONITOR
661-
memorymonitor_allocationsizes_track_allocation(end_block - start_block + 1);
661+
memorymonitor_track_allocation(end_block - start_block + 1);
662662
#endif
663663

664664
return ret_ptr;
@@ -915,7 +915,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
915915
#endif
916916

917917
#if CIRCUITPY_MEMORYMONITOR
918-
memorymonitor_allocationsizes_track_allocation(new_blocks);
918+
memorymonitor_track_allocation(new_blocks);
919919
#endif
920920

921921
return ptr_in;
@@ -948,7 +948,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
948948
#endif
949949

950950
#if CIRCUITPY_MEMORYMONITOR
951-
memorymonitor_allocationsizes_track_allocation(new_blocks);
951+
memorymonitor_track_allocation(new_blocks);
952952
#endif
953953

954954
return ptr_in;

shared-bindings/memorymonitor/AllocationAlarm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ STATIC mp_obj_t memorymonitor_allocationalarm_make_new(const mp_obj_type_t *type
7676
return MP_OBJ_FROM_PTR(self);
7777
}
7878

79+
// TODO: Add .countdown(count) to skip allocations and alarm on something after the first.
80+
7981
//| def __enter__(self) -> memorymonitor.AllocationAlarm:
8082
//| """Enables the alarm."""
8183
//| ...

shared-bindings/memorymonitor/AllocationSize.c

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ STATIC mp_obj_t memorymonitor_allocationsize_make_new(const mp_obj_type_t *type,
8282
//| ...
8383
//|
8484
STATIC mp_obj_t memorymonitor_allocationsize_obj___enter__(mp_obj_t self_in) {
85+
common_hal_memorymonitor_allocationsize_clear(self_in);
8586
common_hal_memorymonitor_allocationsize_resume(self_in);
8687
return self_in;
8788
}
@@ -99,48 +100,13 @@ STATIC mp_obj_t memorymonitor_allocationsize_obj___exit__(size_t n_args, const m
99100
}
100101
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(memorymonitor_allocationsize___exit___obj, 4, 4, memorymonitor_allocationsize_obj___exit__);
101102

102-
//| def pause(self) -> None:
103-
//| """Pause allocation tracking"""
104-
//| ...
105-
//|
106-
STATIC mp_obj_t memorymonitor_allocationsize_obj_pause(mp_obj_t self_in) {
107-
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
108-
109-
common_hal_memorymonitor_allocationsize_pause(self);
110-
return mp_const_none;
111-
}
112-
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_pause_obj, memorymonitor_allocationsize_obj_pause);
113-
114-
//| def resume(self) -> None:
115-
//| """Resumes allocation tracking."""
116-
//| ...
117-
//|
118-
STATIC mp_obj_t memorymonitor_allocationsize_obj_resume(mp_obj_t self_in) {
119-
common_hal_memorymonitor_allocationsize_resume(self_in);
120-
return mp_const_none;
121-
}
122-
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_resume_obj, memorymonitor_allocationsize_obj_resume);
123-
124-
//| def clear(self) -> Any:
125-
//| """Clears all captured pulses"""
126-
//| ...
127-
//|
128-
STATIC mp_obj_t memorymonitor_allocationsize_obj_clear(mp_obj_t self_in) {
129-
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
130-
131-
common_hal_memorymonitor_allocationsize_clear(self);
132-
return mp_const_none;
133-
}
134-
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_clear_obj, memorymonitor_allocationsize_obj_clear);
135-
136-
137103
//| bytes_per_block: int = ...
138104
//| """Number of bytes per block"""
139105
//|
140106
STATIC mp_obj_t memorymonitor_allocationsize_obj_get_bytes_per_block(mp_obj_t self_in) {
141107
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
142108

143-
return mp_obj_new_bool(common_hal_memorymonitor_allocationsize_get_bytes_per_block(self));
109+
return MP_OBJ_NEW_SMALL_INT(common_hal_memorymonitor_allocationsize_get_bytes_per_block(self));
144110
}
145111
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_get_bytes_per_block_obj, memorymonitor_allocationsize_obj_get_bytes_per_block);
146112

shared-module/memorymonitor/AllocationAlarm.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ void common_hal_memorymonitor_allocationalarm_construct(memorymonitor_allocation
3838
}
3939

4040
void common_hal_memorymonitor_allocationalarm_pause(memorymonitor_allocationalarm_obj_t* self) {
41+
// Check to make sure we aren't already paused. We can be if we're exiting from an exception we
42+
// caused.
43+
if (self->previous == NULL) {
44+
return;
45+
}
4146
*self->previous = self->next;
4247
self->next = NULL;
4348
self->previous = NULL;
@@ -62,6 +67,10 @@ void memorymonitor_allocationalarms_allocation(size_t block_count) {
6267
// Hold onto next in case we remove the alarm from the list.
6368
memorymonitor_allocationalarm_obj_t* next = alarm->next;
6469
if (block_count >= alarm->minimum_block_count) {
70+
// Uncomment the breakpoint below if you want to use a C debugger to figure out the C
71+
// call stack for an allocation.
72+
// asm("bkpt");
73+
// Pause now because we may alert when throwing the exception too.
6574
common_hal_memorymonitor_allocationalarm_pause(alarm);
6675
alert_count++;
6776
}
@@ -71,3 +80,7 @@ void memorymonitor_allocationalarms_allocation(size_t block_count) {
7180
mp_raise_memorymonitor_AllocationError(translate("Attempt to allocate %d blocks"), block_count);
7281
}
7382
}
83+
84+
void memorymonitor_allocationalarms_reset(void) {
85+
MP_STATE_VM(active_allocationalarms) = NULL;
86+
}

shared-module/memorymonitor/AllocationAlarm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ typedef struct _memorymonitor_allocationalarm_obj_t {
4545
} memorymonitor_allocationalarm_obj_t;
4646

4747
void memorymonitor_allocationalarms_allocation(size_t block_count);
48+
void memorymonitor_allocationalarms_reset(void);
4849

4950
#endif // MICROPY_INCLUDED_SHARED_MODULE_MEMORYMONITOR_ALLOCATIONALARM_H

shared-module/memorymonitor/AllocationSize.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ void memorymonitor_allocationsizes_track_allocation(size_t block_count) {
8585
as = as->next;
8686
}
8787
}
88+
89+
void memorymonitor_allocationsizes_reset(void) {
90+
MP_STATE_VM(active_allocationsizes) = NULL;
91+
}

shared-module/memorymonitor/AllocationSize.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ typedef struct _memorymonitor_allocationsize_obj_t {
4646
} memorymonitor_allocationsize_obj_t;
4747

4848
void memorymonitor_allocationsizes_track_allocation(size_t block_count);
49+
void memorymonitor_allocationsizes_reset(void);
4950

5051
#endif // MICROPY_INCLUDED_SHARED_MODULE_MEMORYMONITOR_ALLOCATIONSIZE_H

shared-module/memorymonitor/__init__.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ void memorymonitor_track_allocation(size_t block_count) {
3232
memorymonitor_allocationalarms_allocation(block_count);
3333
memorymonitor_allocationsizes_track_allocation(block_count);
3434
}
35+
36+
void memorymonitor_reset(void) {
37+
memorymonitor_allocationalarms_reset();
38+
memorymonitor_allocationsizes_reset();
39+
}

shared-module/memorymonitor/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
#include <stddef.h>
3131

3232
void memorymonitor_track_allocation(size_t block_count);
33+
void memorymonitor_reset(void);
3334

3435
#endif // MICROPY_INCLUDED_MEMORYMONITOR___INIT___H

0 commit comments

Comments
 (0)