{"id":9104,"date":"2019-10-07T07:01:44","date_gmt":"2019-10-07T06:01:44","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=9104"},"modified":"2026-03-26T16:58:12","modified_gmt":"2026-03-26T20:58:12","slug":"rtos-partition-memory","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2019\/10\/07\/rtos-partition-memory\/","title":{"rendered":"RTOS partition memory"},"content":{"rendered":"<p>For any developer of real-time systems, the word \u201cdynamic\u201d should ring alarm bells, as it is generally a label for something that might compromise determinism. The best and easiest to appreciate example is dynamic memory allocation. Most programmers are familiar with <strong>malloc()<\/strong>\/<strong>free()<\/strong>, but may be unaware of their pitfalls: non-deterministic behavior and ill-defined failure modes \u2026<!--more--><\/p>\n<p>The first problem with <strong>malloc()<\/strong> is that many [perhaps most] implementations are non-deterministic &#8211; it is not possible to specify how long a memory allocation may take. The second one is allocation failure. Obviously, the function can fail because there is insufficient heap space and a <strong>NULL<\/strong> pointer is returned. How many developers routinely check for this? I hope that the answer is \u201cmost\u201d. The more subtle problem is that <strong>malloc()<\/strong> can fail even if there is enough memory, if that memory is not contiguous. The obvious solution of \u201cdefragging\u201d the heap is not possible, as C uses direct pointers and, in any case, it would compromise determinism.<\/p>\n<p>A straightforward solution to this difficulty is provided with most RTOS products: a memory block [also called \u201cpartition\u201d] allocation system. The idea is simple. An application has a number of \u201cmemory pools\u201d, each of which contains a specific number of fixed size blocks. The code uses an API call to request a block and this allocation will be deterministic. If there are no free blocks available, an allocation failure will occur. This either results in an error response or task suspension, pending an available block. Fragmentation cannot occur.<\/p>\n<p>In many RTOSes a pool is created dynamically. With <a href=\"https:\/\/www.mentor.com\/embedded-software\/nucleus\/\" target=\"_blank\" rel=\"noopener noreferrer\">Nucleus RTOS<\/a> the prototype for the API call looks like this:<\/p>\n<pre><strong>STATUS NU_Create_Partition_Pool(NU_PARTITION_POOL *pool\n   CHAR *name, VOID *start_address, UNSIGNED pool_size,\n   UNSIGNED partition_size, OPTION suspend_type)<\/strong><\/pre>\n<p>Here is an example call:<\/p>\n<pre><strong>status = NU_Create_Partition_Pool(&amp;MyPool, \u201cany name\u201d,\n   (VOID *) 0xB000, 2000, 40, NU_FIFO);<\/strong><\/pre>\n<p>This specifies that the descriptor for the object is <strong>MyPool<\/strong>; it has 40-byte partitions\/blocks; a 2000-byte memory area, which is located at <strong>0xB000<\/strong>, and task suspension is in FIFO order.<\/p>\n<p>Another API call is used to request the allocation of a block:<\/p>\n<pre><strong>status = NU_Allocate_Partition(&amp;MyPool, &amp;ptr, NU_SUSPEND);<\/strong><\/pre>\n<p>This requests an allocation of a partition from the pool described by <strong>MyPool<\/strong>. The pointer <strong>ptr<\/strong> will point to the allocated memory and the task should be suspended on allocation failure. Another call may be used to relinquish the block:<\/p>\n<pre><strong>status = NU_Deallocate_Partition(ptr);<\/strong><\/pre>\n<p><a href=\"http:\/\/www.linkedin.com\/in\/colinwalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6579\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/linkedin.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/twitter.com\/colin_walls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6583\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/twitter.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/www.facebook.com\/colinwalls.author\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6591\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/facebook.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"http:\/\/blogs.mentor.com\/colinwalls\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6599\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/wordpress.jpg\" alt=\"\" width=\"44\" height=\"44\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For any developer of real-time systems, the word \u201cdynamic\u201d should ring alarm bells, as it is generally a label for&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[334,300,304,374,378,357,309],"industry":[],"product":[],"coauthors":[],"class_list":["post-9104","post","type-post","status-publish","format-standard","hentry","category-news","tag-dynamic-memory","tag-embedded-software","tag-nucleus","tag-nucleus-os","tag-nucleus-rtos","tag-real-time","tag-rtos"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=9104"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9104\/revisions"}],"predecessor-version":[{"id":10827,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9104\/revisions\/10827"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=9104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=9104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=9104"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=9104"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=9104"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=9104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}