<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>ColdFire/68K Microcontrollers and ProcessorsのトピックRe: malloc() confusion</title>
    <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164141#M5494</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;I think you're being bit by fixed memory pools (hogging all your memory)&amp;nbsp;-- check out this thread:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;A href="http://forums.freescale.com/freescale/board/message?board.id=CWCFCOMM&amp;amp;thread.id=2006" target="_blank"&gt;http://forums.freescale.com/freescale/board/message?board.id=CWCFCOMM&amp;amp;thread.id=2006&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;-- Rich&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 13 Jun 2008 02:55:05 GMT</pubDate>
    <dc:creator>RichTestardi</dc:creator>
    <dc:date>2008-06-13T02:55:05Z</dc:date>
    <item>
      <title>malloc() confusion</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164137#M5490</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;SPAN&gt;Hi all.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope someone can explain to me why this happens. I was having weird behaviour with malloc in a project, so I wrote a simple test program to play with malloc and I found a strange problem. (see code below). In the first loop, nothing is previously allocated, and every attempt at malloc() passes (does not return NULL). The I make an allocation from the heap with pBlock = malloc(8); and re-run the test loop. Now, most of the attempts at malloc fail, with only 8 passes while z is in the range of 5 to 12. Can anyone explain to me why this is happening.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using Codewarrior development studio for Coldfire, special edition V7.0 build 15&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Neil.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;//#include &amp;lt;stdlib.h&amp;gt;/*Including used modules for compiling procedure*/#include "Cpu.h"/*Include shared modules, which are used for whole project*/#include "PE_Types.h"#include "PE_Error.h"#include "PE_Const.h"#include "IO_Map.h"uint8* pTest;uint8* pBlock;uint8 x,y,z,a;uint16 pass,fail;void main(void){&amp;nbsp; /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/&amp;nbsp; PE_low_level_init();&amp;nbsp; /*** End of Processor Expert internal initialization.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ***/x=0;y=0;pass = 0;fail = 0;//// Here, nothing is already allocated//for(z=1; z&amp;lt;250; z++) { pTest = malloc(z); if(pTest == NULL)&amp;nbsp; ++fail; else&amp;nbsp; {&amp;nbsp; ++pass;&amp;nbsp; free(pTest);&amp;nbsp; } } //Here, every attemp at malloc passedpBlock = malloc(8); //Here, we allocate another block of heappass=0;fail=0; for(z=1; z&amp;lt;250; z++) { pTest = malloc(z); if(pTest == NULL)&amp;nbsp; ++fail; else&amp;nbsp; {&amp;nbsp; ++pass;&amp;nbsp; free(pTest);&amp;nbsp; } }&amp;nbsp; for(;;) {}} //Here, we only had 8 attempts at malloc pass, whith z = 5-12&amp;nbsp; //The rest of the attempts failed./* END Project */&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:10:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164137#M5490</guid>
      <dc:creator>Neilp</dc:creator>
      <dc:date>2020-10-29T09:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: malloc() confusion</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164138#M5491</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Oh, by the by, I am running the code on a M5211DEMO board in ram. I have already tried running it from flash too, and the result is the same. Don't know if this is important.&lt;BR /&gt;&lt;BR /&gt;Neil.&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Jun 2008 22:14:34 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164138#M5491</guid>
      <dc:creator>Neilp</dc:creator>
      <dc:date>2008-06-12T22:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: malloc() confusion</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164139#M5492</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Just wondering, what if you put the the&lt;PRE&gt;pBlock = malloc(8); //Here, we allocate another block of heap&lt;/PRE&gt;before the first 'for' loop. I'm thinking some kind of strange fragmention issue.&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:10:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164139#M5492</guid>
      <dc:creator>JimDon</dc:creator>
      <dc:date>2020-10-29T09:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: malloc() confusion</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164140#M5493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi Jim.&lt;BR /&gt;&lt;BR /&gt;No, I get the same result, all attempts at malloc(z) fail except where z = 5-12.&lt;BR /&gt;Some more Info: In the first loop, malloc(z) returns 0x20002DEC from z = 1-67, then returns 0x20002DD4 for the rest of the loop. When pBlock is allocated, it grabs 0x20002DEC. The the second loop returns 0 (NULL), except when z = 5-12, then 0x20002DFC is returned.&lt;BR /&gt;&lt;BR /&gt;Neil.&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Jun 2008 00:51:52 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164140#M5493</guid>
      <dc:creator>Neilp</dc:creator>
      <dc:date>2008-06-13T00:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: malloc() confusion</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164141#M5494</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;I think you're being bit by fixed memory pools (hogging all your memory)&amp;nbsp;-- check out this thread:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;A href="http://forums.freescale.com/freescale/board/message?board.id=CWCFCOMM&amp;amp;thread.id=2006" target="_blank"&gt;http://forums.freescale.com/freescale/board/message?board.id=CWCFCOMM&amp;amp;thread.id=2006&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;-- Rich&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Jun 2008 02:55:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164141#M5494</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2008-06-13T02:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: malloc() confusion</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164142#M5495</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;And I'll just copy the salient information here:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--- snip ---&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;I think the real problem is your use of "fixed" memory pools...&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;You could recompile msl with _MSL_USE_FIX_MALLOC_POOLS set to 0 and that would achieve your goal.&amp;nbsp; I'm sure someone here can tell you how to do that (I don't use msl).&amp;nbsp; Unfortunately,&amp;nbsp;&lt;FONT face="Courier New" size="2"&gt;max_fix_pool_size&lt;/FONT&gt; is a constant, so I think a recompile of msl is required.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I believe the real problem is that each fixed pool uses 4k of memory...&amp;nbsp; And there are *5* fixed pool sizes!&amp;nbsp; You have your heap set to 4k total!&amp;nbsp; So the first pool size you allocate (8 bytes) succeeds, and the second one (16 bytes) fails.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Fixed pools make no sense on a processor with 32k of total memory.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;From the MSL C Reference:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Helvetica" size="1"&gt;&lt;/FONT&gt;&lt;P&gt;&lt;FONT face="Helvetica" size="1"&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;_MSL_USE_FIX_MALLOC_POOLS For tiny allocations, fixed sized pools help&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;significantly speed allocation/deallocation, used&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;only with the modern memory pooling scheme.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;You can reserve a pool for a small range of&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;sizes. The use of fixed sized pools can be&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;disabled by setting&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;_MSL_USE_FIX_MALLOC_POOLS&lt;/SPAN&gt; &lt;SPAN&gt;to 0. The&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;default value is 1. Use of fixed size pools&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;requires further configuration. The current&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;shipping configuration is:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;1. Each pool will handle approximately 4000&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;bytes worth of requests before asking for more&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;memory.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;2. There are 4 pool types. Each type is&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;responsible for a different range of requests:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;a. 0 - 12 bytes&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;b. 13 - 20 bytes&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;c. 21 - 36 bytes&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;d. 37 - 68 bytes&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;Requests for greater than 68 bytes go to the&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;variable size pools. The number of types of pools&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;is configurable below. The range of requests for&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;each type is also configurable.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Jun 2008 02:57:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164142#M5495</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2008-06-13T02:57:09Z</dc:date>
    </item>
    <item>
      <title>Re: malloc() confusion</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164143#M5496</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;(quote)&lt;BR /&gt;You could recompile msl with _MSL_USE_FIX_MALLOC_POOLS set to 0 and that would achieve your goal.&lt;DIV&gt;&lt;/DIV&gt;(quote)&lt;BR /&gt;&lt;BR /&gt;Thanks Rich your a genius. I recompiled alloc.c with _MSL_USE_FIX_MALLOC_POOLS set to 0 and now it works. I'll try it in the real application tomorrow.&lt;BR /&gt;&lt;BR /&gt;Thanks again.&lt;BR /&gt;Neil.&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Jun 2008 03:22:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/malloc-confusion/m-p/164143#M5496</guid>
      <dc:creator>Neilp</dc:creator>
      <dc:date>2008-06-13T03:22:43Z</dc:date>
    </item>
  </channel>
</rss>

