NFSIISE icon indicating copy to clipboard operation
NFSIISE copied to clipboard

Avoid use of 64-bit pointers

Open JayFoxRox opened this issue 5 years ago • 0 comments

Until the entire game code is rewritten, the code will require 32-bit pointers as they might be casted to register values or 32-bit variables.

However, internally, the code will continue to use some native pointers, so the sizeof() structures would change on 64 bit (which is why 64 bit compilation is not supported, even with the Cpp module).

Example (lfbPtr is native pointer):

https://github.com/zaps166/NFSIISE/blob/fd87de061df72c40c76772405c0a45666059eafe/src/Glide2x.h#L294-L300

A possible solution is:

// For Cpp
typedef uint32_t _GuestPointer;
#define GuestPointer(x) _GuestPointer
#define ConstGuestPointer(x) ConstGuestPointer(x)

// For 32-bit / ASM
#define GuestPointer(x) x*
#define ConstGuestPointer(x) const x*

(or a more C++-esque solution)

For allocations, one could use the following (for Linux - Windows code would be similar):

GuestPointer(void) malloc32(size_t size) {
	return mmap(NULL, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_32BIT|MAP_ANONYMOUS|MAP_SHARED, -1, 0);
}

void free32(GuestPointer(void) base) {
	munmap(base, 0); //FIXME: Specify proper size
}

GuestPointer(char) strdup32(ConstGuestPointer(char) s) {
	GuestPointer(char) p = malloc32(strlen(s) + 1);
	strcpy(p, s);
	return p;
}

Alternatively, there could be virtual memory to handle allocations in another address-space.

JayFoxRox avatar Mar 14 '19 19:03 JayFoxRox