Search code examples
memory-managementheap-memorycontiki

Memory management in Contiki-OS


I am trying to create a port for Contiki-os to LPC1347, and i have a question as to how exactly is memory handled in Contiki. Protothreads are stack-less and no "real threads" are used so everything is basically on the same stack, so it is basically static memory allocation. I understand how protothreads work but when a new process is initialized, how is memory allocated for it and also, in case of an event having data, how is memory managed for event data?


Solution

  • All required memory is statically allocated during compilation/linkage. Its done by the PROCESS Macro[1], which allocates a structure containing the necessary information [2]. As for the events, they must allocate their own memory, too[3].

    It is therefore not possible to run the same thread* or schedule the same event twice.

    * Actually it is, but not using the PROCESS macro.

    [1] https://github.com/contiki-os/contiki/blob/5bede26b/core/sys/process.h#L301-311

    [2] https://github.com/contiki-os/contiki/blob/5bede26b/core/sys/process.h#L315-326

    [3] https://github.com/contiki-os/contiki/blob/5bede26b/core/sys/process.c#L62-66