<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>clib malloc/free calls interrupt safety</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/39613/clib-malloc-free-calls-interrupt-safety</link><description>Hi. I have a question if is it safe to call malloc/free function of clib from the handler of ble events of softdevice. Does anybody know if is it safe? Does Nordic have some memory allocator which is interrupt safe?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 19 Oct 2018 16:21:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/39613/clib-malloc-free-calls-interrupt-safety" /><item><title>RE: clib malloc/free calls interrupt safety</title><link>https://devzone.nordicsemi.com/thread/153690?ContentTypeID=1</link><pubDate>Fri, 19 Oct 2018 16:21:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:81fa539f-bcbf-4399-a947-e7cff1cc8903</guid><dc:creator>kirill.neznamov</dc:creator><description>&lt;p&gt;Thanks for the explanation!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: clib malloc/free calls interrupt safety</title><link>https://devzone.nordicsemi.com/thread/153616?ContentTypeID=1</link><pubDate>Fri, 19 Oct 2018 10:35:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:744b5ed3-589e-4ccc-aec0-f582d6d1aa7c</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;So malloc/free are not interrupt safe, which is why the general rule is never to call them from an interrupt. There exist interrupt safe versions but neither the libc one nor any of the other implementations in the SDK are.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;However. If you are only calling malloc/free from the ble event handler, you&amp;#39;re ok, because that handler is driven from an interrupt, but just one, so you are guaranteed never to end up with nested mallocs. That is only true however if the ble handler is the *only* place you call malloc/free from, no calling it from the main context or any other interrupt(*). If you can guarantee that, you&amp;#39;re fine using the libc malloc/free.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you need more than that, you&amp;#39;ll have to write your own interrupt-safe malloc or use different pools of memory for different interrupts&lt;/p&gt;
&lt;p&gt;(*) technically you could call it from any interrupt with the same priority as the one which drives the ble event handler but it&amp;#39;s better just to say, only call it from the event handler and nowhere else.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>