Skip to content

Commit

Permalink
feat: Allocate region file structures dynamically (#348)
Browse files Browse the repository at this point in the history
Previously, there would always be memory reserved for 16 region files.
This is simultaneously a large amount of space to reserve by default, and
also a relatively low limit in case of multiple players and/or a large world.
Now, the memory is allocated as needed, with a capacity of 1024 pointers
to `BASED` items.
  • Loading branch information
meyfa authored Feb 22, 2025
1 parent 36021e3 commit 2f4d02e
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 103 deletions.
31 changes: 7 additions & 24 deletions src/_copybooks/state/DD-REGION-FILES.cpy
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,12 @@
78 REGION-CHUNK-WIDTH VALUE 32.

*> The maximum number of region files that can be open at once.
*> Should always be greater than the number of players or thrashing will occur if players are spread out.
78 MAX-REGION-FILES VALUE 16.
78 MAX-REGION-FILES VALUE 1024.

*> The region file cache.
01 REGION-FILES EXTERNAL.
02 REGION-FILE OCCURS MAX-REGION-FILES TIMES.
*> Whether the region file is currently open.
03 REGION-FILE-OPEN BINARY-CHAR UNSIGNED.
*> When the region file was last accessed.
03 REGION-FILE-ACCESS-TIME BINARY-LONG-LONG.
*> Region coordinates.
03 REGION-FILE-X BINARY-LONG.
03 REGION-FILE-Z BINARY-LONG.
*> The file name.
03 REGION-FILE-NAME PIC X(1024).
*> The open file handle (input/output).
03 REGION-FILE-HANDLE PIC X(4) USAGE COMP-X.
*> Chunk locations within the file, consisting of the offset (3 big-endian bytes) and length (1 byte).
*> Offset and length are measured in 4 kiB sectors.
03 REGION-FILE-OFFSET-BYTES PIC X(REGION-SECTOR-BYTES).
03 REGION-FILE-OFFSETS REDEFINES REGION-FILE-OFFSET-BYTES.
04 REGION-FILE-OFFSET OCCURS REGION-SECTOR-INTS TIMES PIC X(4).
*> TODO: Last-modified timestamps for each chunk
*> Boolean array; one entry per file sector, indicating whether the sector is used.
*> TODO: Make this use less memory (bitset?)
03 REGION-FILE-USED OCCURS REGION-SECTOR-COUNT TIMES BINARY-CHAR UNSIGNED.
01 REGION-FILE-POINTERS EXTERNAL.
*> Number of loaded region files
02 REGION-FILE-COUNT BINARY-LONG UNSIGNED.
*> Pointers to dynamically allocated structures, see DD-REGION-FILE-REF.cpy.
*> The items will be reordered such that all open files are at the beginning of the table.
02 REGION-FILE-POINTER USAGE POINTER OCCURS MAX-REGION-FILES TIMES.
24 changes: 24 additions & 0 deletions src/_copybooks/structs/DD-REGION-FILE-REF.cpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*> --- Copybook: reference to a loaded region file ---
*> This is a BASED item, i.e., a view into a data structure referenced by a pointer. DD-REGION-FILES.cpy contains a
*> table of pointers to these structures.

*> The region file cache.
01 REGION-FILE BASED.
*> When the region file was last accessed.
02 REGION-FILE-ACCESS-TIME BINARY-LONG-LONG.
*> Region coordinates.
02 REGION-FILE-X BINARY-LONG.
02 REGION-FILE-Z BINARY-LONG.
*> The file name.
02 REGION-FILE-NAME PIC X(1024).
*> The open file handle (input/output).
02 REGION-FILE-HANDLE PIC X(4) USAGE COMP-X.
*> Chunk locations within the file, consisting of the offset (3 big-endian bytes) and length (1 byte).
*> Offset and length are measured in 4 kiB sectors.
02 REGION-FILE-OFFSET-BYTES PIC X(REGION-SECTOR-BYTES).
02 REGION-FILE-OFFSETS REDEFINES REGION-FILE-OFFSET-BYTES.
03 REGION-FILE-OFFSET OCCURS REGION-SECTOR-INTS TIMES PIC X(4).
*> TODO: Last-modified timestamps for each chunk
*> Boolean array; one entry per file sector, indicating whether the sector is used.
*> TODO: Make this use less memory (bitset?)
02 REGION-FILE-USED OCCURS REGION-SECTOR-COUNT TIMES BINARY-CHAR UNSIGNED.
Loading

0 comments on commit 2f4d02e

Please sign in to comment.