Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecm loadhigh #167

Merged
merged 5 commits into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions shell/loadhigh.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
#define mcbNext(mcb) mcbAssign(mcb, nxtMCB(FP_SEG(mcb)))
#define DosAlloc(value) DOSalloc((value), 0xF)

#define BLOCKMAX 256
#define AVAILBLOCKMAX 256
#define REGIONMAX 64

enum error_codes
{
err_help = -1, OK,
Expand Down Expand Up @@ -154,7 +158,7 @@ static int initialise(void)
optS = 0;

/* Allocate dynamic memory for some arrays */
if ((block = malloc(256 * sizeof(*block))) == 0)
if ((block = malloc(BLOCKMAX * sizeof(*block))) == 0)
return err_out_of_memory;

#ifdef FEATURE_XMS_SWAP
Expand All @@ -177,7 +181,7 @@ static int initialise(void)
}
#endif

if ((umbRegion = malloc(64 * sizeof(*umbRegion))) == 0)
if ((umbRegion = malloc(REGIONMAX * sizeof(*umbRegion))) == 0)
return err_out_of_memory;

/* find the UMB regions */
Expand Down Expand Up @@ -339,14 +343,21 @@ static int findUMBRegions(void)
{
sig = mcb->mcb_type;

if (mcb->mcb_ownerPSP == 8 && !_fmemcmp(mcb->mcb_name, "SC", 2))
if (mcb->mcb_ownerPSP == 8
&& (!_fmemcmp(mcb->mcb_name, "SC", 2)
|| !_fmemcmp(mcb->mcb_name, "S\x00\x30", 3))) /* lDOS S MCB type 30h */
{
/* this is a 'hole' in memory */
if (region->start)
{
region->end = FP_SEG(mcb) - 1;
region++;
region->start = 0;
if (! (mcb->mcb_type == 'Z' && 0 == mcb->mcb_size)) {
if ((region - umbRegion) >= REGIONMAX) {
return err_out_of_memory;
}
region++;
region->start = 0;
}
}
}
else
Expand All @@ -367,6 +378,9 @@ static int findUMBRegions(void)
region->end = umb_mcb->mcb_ownerPSP + umb_mcb->mcb_size - 1;
if ((sig = umb_mcb->mcb_type) == 'M')
region->end--;
if ((region - umbRegion) >= REGIONMAX) {
return err_out_of_memory;
}
region++;
region->start = 0;
mcbAssign(mcb, FP_SEG(umb_mcb) + umb_mcb->mcb_size);
Expand All @@ -381,6 +395,9 @@ static int findUMBRegions(void)
if (sig == 'Z')
{
region->end = FP_SEG(mcb) + mcb->mcb_size;
if ((region - umbRegion) >= REGIONMAX) {
return err_out_of_memory;
}
region++;
break;
}
Expand Down Expand Up @@ -424,7 +441,7 @@ static int loadhigh_prepare(void)
dosSetUMBLinkState(1);
dosSetAllocStrategy(0);

if ((availBlock = malloc(256 * sizeof(*availBlock))) == 0)
if ((availBlock = malloc(AVAILBLOCKMAX * sizeof(*availBlock))) == 0)
return err_out_of_memory;

/* Call to force DOS to catenate any successive free memory blocks */
Expand Down Expand Up @@ -454,16 +471,21 @@ static int loadhigh_prepare(void)
int found_one = 0;

for (mcbAssign(mcb, region->start)
; FP_SEG(mcb) < region->end && mcb->mcb_type == 'M' && mcb->mcb_size > 0
; FP_SEG(mcb) < region->end && mcb->mcb_type == 'M'
; mcbNext(mcb))
{
if (!mcb->mcb_ownerPSP)
{
/* Found a free memory block: allocate it */
word bl = DosAlloc(mcb->mcb_size);

if (bl != FP_SEG(mcb) + 1) /* Did we get the block we wanted? */
if (bl != FP_SEG(mcb) + 1) { /* Did we get the block we wanted? */
DOSfree(bl);
for (i = 0; i < availBlocks; i++)
DOSfree(availBlock[i]);
free(availBlock);
return err_mcb_chain;
}

if (region->access) /* /L option allows access to this region */
{
Expand All @@ -478,6 +500,13 @@ static int loadhigh_prepare(void)
mcb->mcb_size >= region->minSize))
{

if (availBlocks >= AVAILBLOCKMAX) {
DOSfree(bl);
for (i = 0; i < availBlocks; i++)
DOSfree(availBlock[i]);
free(availBlock);
return err_out_of_memory;
}
availBlock[availBlocks++] = bl;

if (optS)
Expand All @@ -498,6 +527,13 @@ static int loadhigh_prepare(void)
continue;
}
}
if (allocatedBlocks >= BLOCKMAX) {
DOSfree(bl);
for (i = 0; i < availBlocks; i++)
DOSfree(availBlock[i]);
free(availBlock);
return err_out_of_memory;
}
block[allocatedBlocks++] = bl; /* no access to this block */
}
}
Expand Down Expand Up @@ -570,6 +606,10 @@ static int loadfix_prepare(void)

dprintf(("loadfix: allocated 0x%04x\n",bl));
DOSresize(bl, 0x1000 - bl);
if (allocatedBlocks >= BLOCKMAX) {
DOSfree(bl);
return err_out_of_memory;
}
block[allocatedBlocks++] = bl;
}

Expand Down Expand Up @@ -618,7 +658,7 @@ static int parseArgs(char *cmdline, char **fnam, char **rest)

/* Disable access to all UMB regions not listed here */
for (i = 1; i < umbRegions; i++)
umbRegion[i].access = 0;
umbRegion[i].access = 0;

r = 0;

Expand Down