This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Allocating memory from heap from within a ISR on Cortex

Is there is recommandations, hints on do-do-not-do when ISR requires to allocate dynamic memory?

Parents
  • Can you provide a little more information for me for what the buffer will be used for / what kind of interrupt we're speaking of, so I can provide a better answer ?

    (It might also be helpful to know how much RAM you have available).

    I usually recommend not to call any routines from within an interrupt.

    In my code, allocating memory from interrupts is strictly forbidden.

    An interrupt should be serviced quickly, which means it should take only a few nanoseconds to execute, so that the user will never feel the interrupt.

    In case it takes a long time, you might get in trouble, because the next interrupt of the same kind may become pending, thus as soon as this interrupt finishes, it will be invoked again.

    I understand that you probably don't intend to allocate memory from an interrupt without having to, so my guess is that you probably need the memory for data received via for instance ethernet.

    The best way to do this, might be to write your own memory allocator (I did this once for a Web-browser, and it turned out to be blazing fast).

    Whether or not it's possible for you to call malloc from within the interrupt, depends on if your malloc is reentrant. If it isn't, then you'll need to roll your own allocator or pre-allocate memory (outside the interrupt) for the data you receive in the interrupt.

    If you need only a single buffer of a fixed size, then I highly recommend that you pre-allocate memory when your firmware starts up.

    This could be done using a static array for instance.

    Depending on your needs, you could also pre-allocate - say - 4 buffers of a fixed size, then the interrupt should not need to do any allocation itself, except from setting a bit, when a buffer is used.

    If we're speaking about a UART/USART type of interrupt, then it might be a much better idea to use a FIFO of a fixed size (say 256 bytes), because normally U(S)ARTs are slow and does not transfer a lot of data.

    Receiving data on slow U(S)ARTs would usually never cause overflows.

Reply
  • Can you provide a little more information for me for what the buffer will be used for / what kind of interrupt we're speaking of, so I can provide a better answer ?

    (It might also be helpful to know how much RAM you have available).

    I usually recommend not to call any routines from within an interrupt.

    In my code, allocating memory from interrupts is strictly forbidden.

    An interrupt should be serviced quickly, which means it should take only a few nanoseconds to execute, so that the user will never feel the interrupt.

    In case it takes a long time, you might get in trouble, because the next interrupt of the same kind may become pending, thus as soon as this interrupt finishes, it will be invoked again.

    I understand that you probably don't intend to allocate memory from an interrupt without having to, so my guess is that you probably need the memory for data received via for instance ethernet.

    The best way to do this, might be to write your own memory allocator (I did this once for a Web-browser, and it turned out to be blazing fast).

    Whether or not it's possible for you to call malloc from within the interrupt, depends on if your malloc is reentrant. If it isn't, then you'll need to roll your own allocator or pre-allocate memory (outside the interrupt) for the data you receive in the interrupt.

    If you need only a single buffer of a fixed size, then I highly recommend that you pre-allocate memory when your firmware starts up.

    This could be done using a static array for instance.

    Depending on your needs, you could also pre-allocate - say - 4 buffers of a fixed size, then the interrupt should not need to do any allocation itself, except from setting a bit, when a buffer is used.

    If we're speaking about a UART/USART type of interrupt, then it might be a much better idea to use a FIFO of a fixed size (say 256 bytes), because normally U(S)ARTs are slow and does not transfer a lot of data.

    Receiving data on slow U(S)ARTs would usually never cause overflows.

Children