implement newOrNil, changes to stdlib

- newOrNil works like new, but sets the variable to nil
  if the heap allocation failed
- change stdlib to use newOrNil in openfile and openvolumeid
- changes to programs that use openvolumeid
This commit is contained in:
slederer 2025-08-31 23:30:40 +02:00
parent 165517a9c8
commit 14d6de059d
10 changed files with 68 additions and 48 deletions

View file

@ -612,6 +612,9 @@ WAIT1LOOP:
; length must be multiple of wordsize.
; if it is not, the last (partial) word is not cleared.
_CLEARMEM:
OVER ; check for null pointer
CBRANCH.Z CLEARMEM_X
SHR
SHR ; calculate length in words

View file

@ -18,8 +18,9 @@ const IONoError = 0;
IOReadOnly = 8;
IOInvalidOp = 9;
IOInvalidFormat = 10;
IOUserIntr = 11;
IOMaxErr = 11;
IONoMem = 11;
IOUserIntr = 12;
IOMaxErr = 12;
const PArgMax = 7;
@ -206,7 +207,7 @@ procedure readvolumeblks(volumeid:integer; destbuf:^iobuffer; blkno:integer; blk
procedure writevolumeblks(volumeid:integer; srcbuf:^iobuffer; blkno:integer; blkCount: integer; var error:integer);
external;
function findvolume(name:string):integer; external;
procedure openvolumeid(volid:integer); external;
procedure openvolumeid(volid:integer;var error:integer); external;
procedure closevolumeid(volid:integer); external;
function IOResult(var fil:file):integer; external;
function ErrorStr(err:integer):string; external;

View file

@ -26,8 +26,9 @@ const IONoError = 0;
IOReadOnly = 8;
IOInvalidOp = 9;
IOInvalidFormat = 10;
IOUserIntr = 11;
IOMaxErr = 11;
IONoMem = 11;
IOUserIntr = 12;
IOMaxErr = 12;
const PArgMax = 7;
@ -133,7 +134,7 @@ var DefaultVolumeId:integer;
character to the runtime error routine
which takes null-terminated strings.
*)
var ioerrordesc: array [0..11] of string[20] = (
var ioerrordesc: array [0..IOMaxErr] of string[20] = (
'No error',
'File not found',
'Volume not found',
@ -145,6 +146,7 @@ var ioerrordesc: array [0..11] of string[20] = (
'File is readonly',
'Invalid operation',
'Invalid format',
'Not enough memory',
'Interrupted by user'
);
@ -1554,13 +1556,17 @@ begin
end;
end;
procedure openvolumeid(volid:integer);
procedure openvolumeid(volid:integer;var error:integer);
begin
error := 0;
with volumeTable[volid] do
begin
if dirCache = nil then
new(dirCache);
openFilesCount := openFilesCount + 1;
newOrNil(dirCache);
if dirCache <> nil then
openFilesCount := openFilesCount + 1
else
error := IONoMem;
end;
end;
@ -2036,23 +2042,28 @@ begin
aFile.typ := IODiskFile;
aFile.mode := mode;
new(aFile.buffer);
aFile.bufpos := 0;
aFile.bufsize := DefaultBufSize;
aFile.needsflush := false;
aFile.changed := false;
aFile.lastError := 0;
aFile.errorAck := false;
aFile.volumeid := volid;
aFile.fileno := slotno;
aFile.filpos := 0;
aFile.bufStart := 1;
aFile.size := dirslot.sizeBytes;
aFile.sizeExtents := dirslot.sizeBytes div extentSize + 1;
aFile.bufBlocks := DefaultBufBlocks;
aFile.extentBlocks := extentSize div 512;
newOrNil(aFile.buffer);
if aFile.buffer = nil then
fileerror(aFile, IONoMem)
else
begin
aFile.bufpos := 0;
aFile.bufsize := DefaultBufSize;
aFile.needsflush := false;
aFile.changed := false;
aFile.lastError := 0;
aFile.errorAck := false;
aFile.volumeid := volid;
aFile.fileno := slotno;
aFile.filpos := 0;
aFile.bufStart := 1;
aFile.size := dirslot.sizeBytes;
aFile.sizeExtents := dirslot.sizeBytes div extentSize + 1;
aFile.bufBlocks := DefaultBufBlocks;
aFile.extentBlocks := extentSize div 512;
seek(aFile,0);
seek(aFile,0);
end;
end;
procedure updatedirslot(var aFile:file);
@ -2269,8 +2280,9 @@ begin
if volid > 0 then
begin
openvolumeid(volid);
slotno := findfile(volid, fname, dirs, error)
openvolumeid(volid, error);
if error = 0 then
slotno := findfile(volid, fname, dirs, error)
end
else
error := IOVolNotFound;