Skip to content

Commit 07f031c

Browse files
committed
Add ignore() and fix docs
1 parent a1e4814 commit 07f031c

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

shared-bindings/memorymonitor/AllocationAlarm.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -76,7 +76,27 @@ 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.
79+
//| def ignore(self, count) -> AllocationAlarm:
80+
//| """Sets the number of applicable allocations to ignore before raising the exception.
81+
//| Automatically set back to zero at context exit.
82+
//|
83+
//| Use it within a ``with`` block::
84+
//|
85+
//| # Will not alarm because the bytearray allocation will be ignored.
86+
//| with aa.ignore(2):
87+
//| x = bytearray(20)
88+
//| """
89+
//| ...
90+
//|
91+
STATIC mp_obj_t memorymonitor_allocationalarm_obj_ignore(mp_obj_t self_in, mp_obj_t count_obj) {
92+
mp_int_t count = mp_obj_get_int(count_obj);
93+
if (count < 0) {
94+
mp_raise_ValueError_varg(translate("%q must be >= 0"), MP_QSTR_count);
95+
}
96+
common_hal_memorymonitor_allocationalarm_set_ignore(self_in, count);
97+
return self_in;
98+
}
99+
MP_DEFINE_CONST_FUN_OBJ_2(memorymonitor_allocationalarm_ignore_obj, memorymonitor_allocationalarm_obj_ignore);
80100

81101
//| def __enter__(self) -> memorymonitor.AllocationAlarm:
82102
//| """Enables the alarm."""
@@ -95,13 +115,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationalarm___enter___obj, memorymon
95115
//|
96116
STATIC mp_obj_t memorymonitor_allocationalarm_obj___exit__(size_t n_args, const mp_obj_t *args) {
97117
(void)n_args;
118+
common_hal_memorymonitor_allocationalarm_set_ignore(args[0], 0);
98119
common_hal_memorymonitor_allocationalarm_pause(args[0]);
99120
return mp_const_none;
100121
}
101122
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(memorymonitor_allocationalarm___exit___obj, 4, 4, memorymonitor_allocationalarm_obj___exit__);
102123

103124
STATIC const mp_rom_map_elem_t memorymonitor_allocationalarm_locals_dict_table[] = {
104125
// Methods
126+
{ MP_ROM_QSTR(MP_QSTR_ignore), MP_ROM_PTR(&memorymonitor_allocationalarm_ignore_obj) },
105127
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&memorymonitor_allocationalarm___enter___obj) },
106128
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&memorymonitor_allocationalarm___exit___obj) },
107129
};

shared-bindings/memorymonitor/AllocationAlarm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ extern const mp_obj_type_t memorymonitor_allocationalarm_type;
3434
void common_hal_memorymonitor_allocationalarm_construct(memorymonitor_allocationalarm_obj_t* self, size_t minimum_block_count);
3535
void common_hal_memorymonitor_allocationalarm_pause(memorymonitor_allocationalarm_obj_t* self);
3636
void common_hal_memorymonitor_allocationalarm_resume(memorymonitor_allocationalarm_obj_t* self);
37+
void common_hal_memorymonitor_allocationalarm_set_ignore(memorymonitor_allocationalarm_obj_t* self, mp_int_t count);
3738

3839
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MEMORYMONITOR_ALLOCATIONALARM_H

shared-bindings/memorymonitor/AllocationSize.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -38,7 +38,7 @@
3838
//| def __init__(self):
3939
//| """Tracks the number of allocations in power of two buckets.
4040
//|
41-
//| It will have 32 16bit buckets to track allocation counts. It is total allocations
41+
//| It will have 16 16bit buckets to track allocation counts. It is total allocations
4242
//| meaning frees are ignored. Reallocated memory is counted twice, at allocation and when
4343
//| reallocated with the larger size.
4444
//|
@@ -47,25 +47,20 @@
4747
//| per block, typically 16. Bucket 2 will be less than or equal to 4 blocks. See
4848
//| `bytes_per_block` to convert blocks to bytes.
4949
//|
50-
//| Multiple AllocationSizes can be used to track different boundaries.
51-
//|
52-
//| Active AllocationSizes will not be freed so make sure and pause before deleting.
50+
//| Multiple AllocationSizes can be used to track different code boundaries.
5351
//|
5452
//| Track allocations::
5553
//|
5654
//| import memorymonitor
5755
//|
58-
//| mm = memorymonitor.AllocationSizes()
59-
//| print("hello world" * 3)
60-
//| mm.pause()
61-
//| for bucket in mm:
62-
//| print("<", 2 ** bucket, mm[bucket])
56+
//| mm = memorymonitor.AllocationSize()
57+
//| with mm:
58+
//| print("hello world" * 3)
6359
//|
64-
//| # Clear the buckets
65-
//| mm.clear()
60+
//| for bucket, count in enumerate(mm):
61+
//| print("<", 2 ** bucket, count)
6662
//|
67-
//| # Resume allocation tracking
68-
//| mm.resume()"""
63+
//| """
6964
//| ...
7065
//|
7166
STATIC mp_obj_t memorymonitor_allocationsize_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@@ -78,7 +73,7 @@ STATIC mp_obj_t memorymonitor_allocationsize_make_new(const mp_obj_type_t *type,
7873
}
7974

8075
//| def __enter__(self, ) -> Any:
81-
//| """No-op used by Context Managers."""
76+
//| """Clears counts and resumes tracking."""
8277
//| ...
8378
//|
8479
STATIC mp_obj_t memorymonitor_allocationsize_obj___enter__(mp_obj_t self_in) {
@@ -118,12 +113,12 @@ const mp_obj_property_t memorymonitor_allocationsize_bytes_per_block_obj = {
118113
};
119114

120115
//| def __len__(self, ) -> Any:
121-
//| """Returns the current pulse length
116+
//| """Returns the number of allocation buckets.
122117
//|
123118
//| This allows you to::
124119
//|
125-
//| pulses = pulseio.PulseIn(pin)
126-
//| print(len(pulses))"""
120+
//| mm = memorymonitor.AllocationSize()
121+
//| print(len(mm))"""
127122
//| ...
128123
//|
129124
STATIC mp_obj_t memorymonitor_allocationsize_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
@@ -137,12 +132,12 @@ STATIC mp_obj_t memorymonitor_allocationsize_unary_op(mp_unary_op_t op, mp_obj_t
137132
}
138133

139134
//| def __getitem__(self, index: Any) -> Any:
140-
//| """Returns the value at the given index or values in slice.
135+
//| """Returns the allocation count for the given bucket.
141136
//|
142137
//| This allows you to::
143138
//|
144-
//| pulses = pulseio.PulseIn(pin)
145-
//| print(pulses[0])"""
139+
//| mm = memorymonitor.AllocationSize()
140+
//| print(mm[0])"""
146141
//| ...
147142
//|
148143
STATIC mp_obj_t memorymonitor_allocationsize_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) {

shared-module/memorymonitor/AllocationAlarm.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ void common_hal_memorymonitor_allocationalarm_construct(memorymonitor_allocation
3737
self->previous = NULL;
3838
}
3939

40+
void common_hal_memorymonitor_allocationalarm_set_ignore(memorymonitor_allocationalarm_obj_t* self, mp_int_t count) {
41+
self->count = count;
42+
}
43+
4044
void common_hal_memorymonitor_allocationalarm_pause(memorymonitor_allocationalarm_obj_t* self) {
4145
// Check to make sure we aren't already paused. We can be if we're exiting from an exception we
4246
// caused.
@@ -67,12 +71,16 @@ void memorymonitor_allocationalarms_allocation(size_t block_count) {
6771
// Hold onto next in case we remove the alarm from the list.
6872
memorymonitor_allocationalarm_obj_t* next = alarm->next;
6973
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.
74-
common_hal_memorymonitor_allocationalarm_pause(alarm);
75-
alert_count++;
74+
if (alarm->count > 0) {
75+
alarm->count--;
76+
} else {
77+
// Uncomment the breakpoint below if you want to use a C debugger to figure out the C
78+
// call stack for an allocation.
79+
// asm("bkpt");
80+
// Pause now because we may alert when throwing the exception too.
81+
common_hal_memorymonitor_allocationalarm_pause(alarm);
82+
alert_count++;
83+
}
7684
}
7785
alarm = next;
7886
}

shared-module/memorymonitor/AllocationAlarm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct _memorymonitor_allocationalarm_obj_t memorymonitor_allocationalar
3939
typedef struct _memorymonitor_allocationalarm_obj_t {
4040
mp_obj_base_t base;
4141
size_t minimum_block_count;
42+
mp_int_t count;
4243
// Store the location that points to us so we can remove ourselves.
4344
memorymonitor_allocationalarm_obj_t** previous;
4445
memorymonitor_allocationalarm_obj_t* next;

0 commit comments

Comments
 (0)