z88dk forums

z80 Development Kit

You are not logged in.

#1 2019-09-22 23:47:36

alank2
Member
Registered: 2017-03-01
Posts: 44

Default stack size / memory map for CP/M

Does the stack start at one side and the malloc from the other and they grow towards each other?  Or is there is a fixed stack size?  Is there stack overflow checking?

Offline

 

#2 2019-09-23 10:18:19

dom
Administrator
Registered: 2007-07-15
Posts: 1453
Website

Re: Default stack size / memory map for CP/M

If you're using newlib, then Alvin explained heap sizing in this post: https://www.z88dk.org/forum/viewtopic.p … 713#p14713

For classic, AMALLOC is the easiest way to do things, it's detailed in this thread: https://www.z88dk.org/forum/viewtopic.php?id=5859

For CP/M, any heap will start from the end of BSS memory and extend up to the values configured above. If it's dynamically sized using the above mechanisms, the actual size will change depending on the TPA of the machine that it's running on.

There's no bounds checking, it would be possible to add on entry to a function, but even with a fudge factor we might still overlap depending on the complexity of the function.

Offline

 

#3 2019-09-23 13:05:34

alank2
Member
Registered: 2017-03-01
Posts: 44

Re: Default stack size / memory map for CP/M

I am using newlib (zcc +cpm -clib=sdcc_iy -create-app %1.c -o%1.com).

What I want to do is keep the dynamic area from running into a specific stack size.  I'd rather it return NULL on malloc when it is out of memory.

I tried this as a test to see how large a block I can malloc, but changing the stack size does not diminish the block size.

#include <stdio.h>
#include <alloc.h>

#define CRT_STACK_SIZE 16384

int main()
  {
    unsigned int i1;
    char *p;

    for (i1=65280;i1!=0;i1-=256)
      {
        printf("%u\n",i1);

        p=malloc(i1);
        if (p!=NULL)
          {
            printf ("%04x",p);
            break;
          }
      }

    return 0;
  }

Offline

 

#4 2019-09-23 15:11:07

dom
Administrator
Registered: 2007-07-15
Posts: 1453
Website

Re: Default stack size / memory map for CP/M

Try replacing the #define with following:

#pragma define CRT_STACK_SIZE = 16384

With that option I get a value of 44800 using zxcc, without it it's 60672

Offline

 

#5 2019-09-23 15:13:48

alank2
Member
Registered: 2017-03-01
Posts: 44

Re: Default stack size / memory map for CP/M

Excellent - yes - that does it!  Thank you dom!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson