blob: 87bab0a33e980e809fbed2d23780ab1dc1536cdc [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
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
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020027#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030028#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000029#include "qstr.h"
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020030#include "obj.h"
31#include "builtin.h"
32
33// Various builtins specific to MicroPython runtime,
34// living in micropython module
35
36#if MICROPY_MEM_STATS
Damien George0c36da02014-03-08 15:24:39 +000037STATIC mp_obj_t mp_micropython_mem_total() {
Damien Georgebb4c6f32014-07-31 10:49:14 +010038 return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020039}
40
Damien George0c36da02014-03-08 15:24:39 +000041STATIC mp_obj_t mp_micropython_mem_current() {
Damien Georgebb4c6f32014-07-31 10:49:14 +010042 return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020043}
44
Damien George0c36da02014-03-08 15:24:39 +000045STATIC mp_obj_t mp_micropython_mem_peak() {
Damien Georgebb4c6f32014-07-31 10:49:14 +010046 return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020047}
48
Damien George0c36da02014-03-08 15:24:39 +000049STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
50STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current);
51STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020052#endif
Damien George91d457a2014-01-20 10:30:24 +000053
Dave Hylands5b7fd202014-07-01 23:46:53 -070054#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
55STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
56#endif
57
Damien George0c36da02014-03-08 15:24:39 +000058STATIC const mp_map_elem_t mp_module_micropython_globals_table[] = {
59 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_micropython) },
Damien George91d457a2014-01-20 10:30:24 +000060#if MICROPY_MEM_STATS
Damien George0c36da02014-03-08 15:24:39 +000061 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_total), (mp_obj_t)&mp_micropython_mem_total_obj },
62 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_current), (mp_obj_t)&mp_micropython_mem_current_obj },
63 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_peak), (mp_obj_t)&mp_micropython_mem_peak_obj },
Damien George91d457a2014-01-20 10:30:24 +000064#endif
Dave Hylands5b7fd202014-07-01 23:46:53 -070065#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
66 { MP_OBJ_NEW_QSTR(MP_QSTR_alloc_emergency_exception_buf), (mp_obj_t)&mp_alloc_emergency_exception_buf_obj },
67#endif
Damien George0c36da02014-03-08 15:24:39 +000068};
69
Damien George8b0535e2014-04-05 21:53:54 +010070STATIC const mp_obj_dict_t mp_module_micropython_globals = {
71 .base = {&mp_type_dict},
72 .map = {
73 .all_keys_are_qstrs = 1,
74 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +020075 .used = MP_ARRAY_SIZE(mp_module_micropython_globals_table),
76 .alloc = MP_ARRAY_SIZE(mp_module_micropython_globals_table),
Damien George8b0535e2014-04-05 21:53:54 +010077 .table = (mp_map_elem_t*)mp_module_micropython_globals_table,
78 },
Damien George0c36da02014-03-08 15:24:39 +000079};
80
81const mp_obj_module_t mp_module_micropython = {
82 .base = { &mp_type_module },
83 .name = MP_QSTR_micropython,
Damien George8b0535e2014-04-05 21:53:54 +010084 .globals = (mp_obj_dict_t*)&mp_module_micropython_globals,
Damien George0c36da02014-03-08 15:24:39 +000085};