2020-07-15 20:58:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-07-17 20:02:13 -04:00
|
|
|
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
|
2020-07-15 20:58:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/runtime0.h"
|
|
|
|
#include "shared-bindings/memorymonitor/AllocationAlarm.h"
|
|
|
|
#include "shared-bindings/util.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2020-07-15 20:58:38 -04:00
|
|
|
|
|
|
|
//| class AllocationAlarm:
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def __init__(self, *, minimum_block_count: int = 1) -> None:
|
2020-07-15 20:58:38 -04:00
|
|
|
//| """Throw an exception when an allocation of ``minimum_block_count`` or more blocks
|
|
|
|
//| occurs while active.
|
|
|
|
//|
|
|
|
|
//| Track allocations::
|
|
|
|
//|
|
|
|
|
//| import memorymonitor
|
|
|
|
//|
|
|
|
|
//| aa = memorymonitor.AllocationAlarm(minimum_block_count=2)
|
|
|
|
//| x = 2
|
|
|
|
//| # Should not allocate any blocks.
|
|
|
|
//| with aa:
|
|
|
|
//| x = 5
|
|
|
|
//|
|
|
|
|
//| # Should throw an exception when allocating storage for the 20 bytes.
|
|
|
|
//| with aa:
|
|
|
|
//| x = bytearray(20)
|
|
|
|
//|
|
|
|
|
//| """
|
|
|
|
//| ...
|
2021-10-15 14:43:12 -04:00
|
|
|
STATIC mp_obj_t memorymonitor_allocationalarm_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *all_args, mp_map_t *kw_args) {
|
2020-07-15 20:58:38 -04:00
|
|
|
enum { ARG_minimum_block_count };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_minimum_block_count, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2022-05-13 15:33:43 -04:00
|
|
|
|
|
|
|
mp_int_t minimum_block_count =
|
|
|
|
mp_arg_validate_int_min(args[ARG_minimum_block_count].u_int, 1, MP_QSTR_minimum_block_count);
|
|
|
|
|
2020-07-15 20:58:38 -04:00
|
|
|
|
|
|
|
memorymonitor_allocationalarm_obj_t *self = m_new_obj(memorymonitor_allocationalarm_obj_t);
|
|
|
|
self->base.type = &memorymonitor_allocationalarm_type;
|
|
|
|
|
|
|
|
common_hal_memorymonitor_allocationalarm_construct(self, minimum_block_count);
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def ignore(self, count: int) -> AllocationAlarm:
|
2020-07-17 20:02:13 -04:00
|
|
|
//| """Sets the number of applicable allocations to ignore before raising the exception.
|
2022-09-27 16:21:42 -04:00
|
|
|
//| Automatically set back to zero at context exit.
|
2020-07-17 20:02:13 -04:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| Use it within a ``with`` block::
|
2020-07-17 20:02:13 -04:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| # Will not alarm because the bytearray allocation will be ignored.
|
|
|
|
//| with aa.ignore(2):
|
|
|
|
//| x = bytearray(20)
|
|
|
|
//| """
|
2020-07-17 20:02:13 -04:00
|
|
|
//| ...
|
|
|
|
STATIC mp_obj_t memorymonitor_allocationalarm_obj_ignore(mp_obj_t self_in, mp_obj_t count_obj) {
|
|
|
|
mp_int_t count = mp_obj_get_int(count_obj);
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_arg_validate_int_min(count, 0, MP_QSTR_count);
|
|
|
|
|
2020-07-17 20:02:13 -04:00
|
|
|
common_hal_memorymonitor_allocationalarm_set_ignore(self_in, count);
|
|
|
|
return self_in;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(memorymonitor_allocationalarm_ignore_obj, memorymonitor_allocationalarm_obj_ignore);
|
2020-07-16 22:01:43 -04:00
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def __enter__(self) -> AllocationAlarm:
|
2020-07-15 20:58:38 -04:00
|
|
|
//| """Enables the alarm."""
|
|
|
|
//| ...
|
|
|
|
STATIC mp_obj_t memorymonitor_allocationalarm_obj___enter__(mp_obj_t self_in) {
|
|
|
|
common_hal_memorymonitor_allocationalarm_resume(self_in);
|
|
|
|
return self_in;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationalarm___enter___obj, memorymonitor_allocationalarm_obj___enter__);
|
|
|
|
|
|
|
|
//| def __exit__(self) -> None:
|
|
|
|
//| """Automatically disables the allocation alarm when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
|
|
|
STATIC mp_obj_t memorymonitor_allocationalarm_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args;
|
2020-07-17 20:02:13 -04:00
|
|
|
common_hal_memorymonitor_allocationalarm_set_ignore(args[0], 0);
|
2020-07-15 20:58:38 -04:00
|
|
|
common_hal_memorymonitor_allocationalarm_pause(args[0]);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(memorymonitor_allocationalarm___exit___obj, 4, 4, memorymonitor_allocationalarm_obj___exit__);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t memorymonitor_allocationalarm_locals_dict_table[] = {
|
|
|
|
// Methods
|
2020-07-17 20:02:13 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ignore), MP_ROM_PTR(&memorymonitor_allocationalarm_ignore_obj) },
|
2020-07-15 20:58:38 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&memorymonitor_allocationalarm___enter___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&memorymonitor_allocationalarm___exit___obj) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(memorymonitor_allocationalarm_locals_dict, memorymonitor_allocationalarm_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t memorymonitor_allocationalarm_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_AllocationAlarm,
|
|
|
|
.make_new = memorymonitor_allocationalarm_make_new,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&memorymonitor_allocationalarm_locals_dict,
|
2020-07-15 20:58:38 -04:00
|
|
|
};
|