cmallocsdccretro-computing

SDCC and malloc() - allocating much less memory than is available


When I run compile this code with SDCC 3.1.0, and run it on an Amstrad CPC 464 (under emulation, with WinCPC 0.9.26 running on Wine):

void _test_malloc()
{
  long idx = 0;
  while (1)
    {
      if (malloc(5))
    {
      printf("%ld\r\n", ++idx);
    }
      else
    {
      printf("done");
      break;
    }
  }
}

... it consistently taps out at 92 malloc()s. I make that 460 bytes, which leads me to a couple of questions:


Solution

  • The answer is that, on Z80 systems, heap size is hard-coded to 1kB. Maarten Brock answered this on the sdcc-user mailing list:

    Hello Duncan,

    You have to create the heap yourself if the standard 1kB is not enough. Copy heap.s into your project dir and modify it to create your preferred size. Then assemble it and link with your project.

    Unlike the mcs51 heap which is defined in _heap.c this is not documented for Z80 in the manual. Feel free to request a documentation update or merge of _heap.c and heap.s in the tracker system.

    Maarten