Skip to content

Commit

Permalink
QVM: reuse rounded up data segment space for extra pStack if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ec- committed Jan 25, 2025
1 parent 01eaea8 commit 8aa6fa1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
35 changes: 24 additions & 11 deletions code/qcommon/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,6 @@ static vmHeader_t *VM_LoadQVM( vm_t *vm, qboolean alloc ) {
int length;
unsigned int dataLength;
unsigned int dataAlloc;
int i;
char filename[MAX_QPATH], *errorMsg;
unsigned int crc32sum;
qboolean tryjts;
Expand Down Expand Up @@ -788,17 +787,31 @@ static vmHeader_t *VM_LoadQVM( vm_t *vm, qboolean alloc ) {
}

vm->exactDataLength = header->dataLength + header->litLength + header->bssLength;
dataLength = vm->exactDataLength + PROGRAM_STACK_EXTRA;
if ( dataLength < PROGRAM_STACK_SIZE + PROGRAM_STACK_EXTRA ) {
dataLength = PROGRAM_STACK_SIZE + PROGRAM_STACK_EXTRA;

dataLength = vm->exactDataLength;
if ( dataLength < PROGRAM_STACK_SIZE ) {
dataLength = PROGRAM_STACK_SIZE;
}

vm->programStackExtra = PROGRAM_STACK_EXTRA;

// if rounding difference is larger than extra space we need then reuse it
if ( log2pad( dataLength, 1 ) - dataLength >= PROGRAM_STACK_EXTRA ) {
#ifdef _DEBUG
// keep exact size for debug purposes
#else
// reuse it all for release builds
vm->programStackExtra = log2pad( dataLength, 1 ) - dataLength;
// Com_DPrintf( S_COLOR_CYAN "%s: reuse %i bytes for pStack\n", vm->name, vm->programStackExtra );
#endif
} else {
dataLength += vm->programStackExtra;
}

vm->dataLength = dataLength;

// round up to next power of 2 so all data operations can
// be mask protected
for ( i = 0 ; dataLength > ( 1 << i ) ; i++ )
;
dataLength = 1 << i;
// round up to next power of 2 so all data operations can be mask protected
dataLength = log2pad( dataLength, 1 );

// reserve some space for effective LOCAL+LOAD* checks
dataAlloc = dataLength + VM_DATA_GUARD_SIZE;
Expand All @@ -825,7 +838,7 @@ static vmHeader_t *VM_LoadQVM( vm_t *vm, qboolean alloc ) {
"VM_Restart()\n", filename );
return NULL;
}
Com_Memset( vm->dataBase, 0, vm->dataAlloc );
Com_Memset( vm->dataBase, 0x0, vm->dataAlloc );
}

// copy the intialized data
Expand Down Expand Up @@ -1801,7 +1814,7 @@ vm_t *VM_Create( vmIndex_t index, syscall_t systemCalls, dllSyscall_t dllSyscall

// the stack is implicitly at the end of the image
vm->programStack = vm->dataMask + 1;
vm->stackBottom = vm->programStack - PROGRAM_STACK_SIZE - PROGRAM_STACK_EXTRA;
vm->stackBottom = vm->programStack - PROGRAM_STACK_SIZE - vm->programStackExtra;

vm->compiled = qfalse;

Expand Down
1 change: 1 addition & 0 deletions code/qcommon/vm_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ struct vm_s {
uint32_t dataLength; // data segment length
uint32_t exactDataLength; // from qvm header
uint32_t dataAlloc; // actually allocated, for mmap()/munmap()
uint32_t programStackExtra;

int numSymbols;
vmSymbol_t *symbols;
Expand Down

0 comments on commit 8aa6fa1

Please sign in to comment.