|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | + |
| 29 | +#include "py/objproperty.h" |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "py/runtime0.h" |
| 32 | +#include "shared-bindings/memorymonitor/AllocationAlarm.h" |
| 33 | +#include "shared-bindings/util.h" |
| 34 | +#include "supervisor/shared/translate.h" |
| 35 | + |
| 36 | +//| class AllocationAlarm: |
| 37 | +//| |
| 38 | +//| def __init__(self, *, minimum_block_count=1): |
| 39 | +//| """Throw an exception when an allocation of ``minimum_block_count`` or more blocks |
| 40 | +//| occurs while active. |
| 41 | +//| |
| 42 | +//| Track allocations:: |
| 43 | +//| |
| 44 | +//| import memorymonitor |
| 45 | +//| |
| 46 | +//| aa = memorymonitor.AllocationAlarm(minimum_block_count=2) |
| 47 | +//| x = 2 |
| 48 | +//| # Should not allocate any blocks. |
| 49 | +//| with aa: |
| 50 | +//| x = 5 |
| 51 | +//| |
| 52 | +//| # Should throw an exception when allocating storage for the 20 bytes. |
| 53 | +//| with aa: |
| 54 | +//| x = bytearray(20) |
| 55 | +//| |
| 56 | +//| """ |
| 57 | +//| ... |
| 58 | +//| |
| 59 | +STATIC mp_obj_t memorymonitor_allocationalarm_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 60 | + enum { ARG_minimum_block_count }; |
| 61 | + static const mp_arg_t allowed_args[] = { |
| 62 | + { MP_QSTR_minimum_block_count, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, |
| 63 | + }; |
| 64 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 65 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 66 | + mp_int_t minimum_block_count = args[ARG_minimum_block_count].u_int; |
| 67 | + if (minimum_block_count < 1) { |
| 68 | + mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_minimum_block_count); |
| 69 | + } |
| 70 | + |
| 71 | + memorymonitor_allocationalarm_obj_t *self = m_new_obj(memorymonitor_allocationalarm_obj_t); |
| 72 | + self->base.type = &memorymonitor_allocationalarm_type; |
| 73 | + |
| 74 | + common_hal_memorymonitor_allocationalarm_construct(self, minimum_block_count); |
| 75 | + |
| 76 | + return MP_OBJ_FROM_PTR(self); |
| 77 | +} |
| 78 | + |
| 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); |
| 100 | + |
| 101 | +//| def __enter__(self) -> memorymonitor.AllocationAlarm: |
| 102 | +//| """Enables the alarm.""" |
| 103 | +//| ... |
| 104 | +//| |
| 105 | +STATIC mp_obj_t memorymonitor_allocationalarm_obj___enter__(mp_obj_t self_in) { |
| 106 | + common_hal_memorymonitor_allocationalarm_resume(self_in); |
| 107 | + return self_in; |
| 108 | +} |
| 109 | +MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationalarm___enter___obj, memorymonitor_allocationalarm_obj___enter__); |
| 110 | + |
| 111 | +//| def __exit__(self) -> None: |
| 112 | +//| """Automatically disables the allocation alarm when exiting a context. See |
| 113 | +//| :ref:`lifetime-and-contextmanagers` for more info.""" |
| 114 | +//| ... |
| 115 | +//| |
| 116 | +STATIC mp_obj_t memorymonitor_allocationalarm_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 117 | + (void)n_args; |
| 118 | + common_hal_memorymonitor_allocationalarm_set_ignore(args[0], 0); |
| 119 | + common_hal_memorymonitor_allocationalarm_pause(args[0]); |
| 120 | + return mp_const_none; |
| 121 | +} |
| 122 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(memorymonitor_allocationalarm___exit___obj, 4, 4, memorymonitor_allocationalarm_obj___exit__); |
| 123 | + |
| 124 | +STATIC const mp_rom_map_elem_t memorymonitor_allocationalarm_locals_dict_table[] = { |
| 125 | + // Methods |
| 126 | + { MP_ROM_QSTR(MP_QSTR_ignore), MP_ROM_PTR(&memorymonitor_allocationalarm_ignore_obj) }, |
| 127 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&memorymonitor_allocationalarm___enter___obj) }, |
| 128 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&memorymonitor_allocationalarm___exit___obj) }, |
| 129 | +}; |
| 130 | +STATIC MP_DEFINE_CONST_DICT(memorymonitor_allocationalarm_locals_dict, memorymonitor_allocationalarm_locals_dict_table); |
| 131 | + |
| 132 | +const mp_obj_type_t memorymonitor_allocationalarm_type = { |
| 133 | + { &mp_type_type }, |
| 134 | + .name = MP_QSTR_AllocationAlarm, |
| 135 | + .make_new = memorymonitor_allocationalarm_make_new, |
| 136 | + .locals_dict = (mp_obj_dict_t*)&memorymonitor_allocationalarm_locals_dict, |
| 137 | +}; |
0 commit comments