C module globals table: mp_map_elem_t vs mp_rom_map_elem_t [EDIT]

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
zinahe
Posts: 3
Joined: Sun Jul 08, 2018 12:10 am

C module globals table: mp_map_elem_t vs mp_rom_map_elem_t [EDIT]

Post by zinahe » Sun Nov 11, 2018 2:03 am

Greetings all,

A complete noob here. So be gentle please.

I have seen two variations of the global table in c modules implemented as:

Code: Select all

STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_mymodule) },
};

or

Code: Select all

STATIC const mp_map_elem_t global_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_mymodule) },
};

I have tried to search in code-base; and both seem to be equally used. I am guessing one would be used to freeze the table in flash and the other in RAM; but I haven't seen anything to confirm that assumption. What are the circumstances where I choose one over the other ? Is this documented somewhere ?
Last edited by zinahe on Mon Nov 12, 2018 5:04 am, edited 1 time in total.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: C module globals table: mp_map_elem_t vs mp_rom_map_elem_t

Post by dhylands » Sun Nov 11, 2018 4:30 pm

Originally, there was only mp_map_elem_t. The mp_rom_xxx types were introduced to indicate that the particular struct was stored in flash and thus can't be modified.

Many, but not all, of the places have been updated (where appropriate). Personally, I would use mp_rom_map_elem_t for anything new, since the intent is that the local dict tables will be stored in flash.

zinahe
Posts: 3
Joined: Sun Jul 08, 2018 12:10 am

Re: C module globals table: mp_map_elem_t vs mp_rom_map_elem_t

Post by zinahe » Sun Nov 11, 2018 4:50 pm

@dhylands: Thanks a lot.

When one is familiar with the historical context of things, it is pretty straight forward. But you can imagine how confusing it could quickly get for someone who's just getting started with upy.

Cheers,

Post Reply