Default stack size / memory map for CP/M

Post Reply
alank2
Member
Posts: 44
Joined: Wed Mar 01, 2017 7:24 pm

Default stack size / memory map for CP/M

Post by alank2 »

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?
User avatar
dom
Well known member
Posts: 1195
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

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.
alank2
Member
Posts: 44
Joined: Wed Mar 01, 2017 7:24 pm

Post by alank2 »

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;
}
User avatar
dom
Well known member
Posts: 1195
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

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
alank2
Member
Posts: 44
Joined: Wed Mar 01, 2017 7:24 pm

Post by alank2 »

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