blob: dfbea0906e1955f9570ddb2e4de57cefd7ab8d3b [file] [log] [blame]
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +02001#include <stdint.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdarg.h>
5#include <string.h>
6#include <assert.h>
7
8#include "misc.h"
9#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020011#include "obj.h"
Damien George91d457a2014-01-20 10:30:24 +000012#include "runtime.h"
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020013#include "builtin.h"
14
15// Various builtins specific to MicroPython runtime,
16// living in micropython module
17
18#if MICROPY_MEM_STATS
19static mp_obj_t mem_total() {
Damien George91d457a2014-01-20 10:30:24 +000020 return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_total_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020021}
22
23static mp_obj_t mem_current() {
Damien George91d457a2014-01-20 10:30:24 +000024 return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_current_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020025}
26
27static mp_obj_t mem_peak() {
Damien George91d457a2014-01-20 10:30:24 +000028 return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_peak_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020029}
30
31MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_mem_total_obj, mem_total);
32MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_mem_current_obj, mem_current);
33MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_mem_peak_obj, mem_peak);
34#endif
Damien George91d457a2014-01-20 10:30:24 +000035
36void mp_module_micropython_init(void) {
37 mp_obj_t m_mp = mp_obj_new_module(MP_QSTR_micropython);
38 rt_store_name(MP_QSTR_micropython, m_mp);
39
40#if MICROPY_MEM_STATS
Damien George55baff42014-01-21 21:40:13 +000041 rt_store_attr(m_mp, QSTR_FROM_STR_STATIC("mem_total"), (mp_obj_t)&mp_builtin_mem_total_obj);
42 rt_store_attr(m_mp, QSTR_FROM_STR_STATIC("mem_current"), (mp_obj_t)&mp_builtin_mem_current_obj);
43 rt_store_attr(m_mp, QSTR_FROM_STR_STATIC("mem_peak"), (mp_obj_t)&mp_builtin_mem_peak_obj);
Damien George91d457a2014-01-20 10:30:24 +000044#endif
45}