Maschell / dynamic_libs

21 stars 5 forks source link

Size of BOOL? #18

Closed neuschaefer closed 6 years ago

neuschaefer commented 6 years ago

Commit bd0862328bf4f6adf introducted the use of BOOL:

    BOOL headphone;              /* Set to TRUE if headphones are plugged in, FALSE otherwise */

Can I safely typedef BOOL as u8?

CreeperMario commented 6 years ago

A boolean is supposed to only have two values: false (0) and true (1). With most compilers, a boolean is equivalent to an 8-bit number (u8), but I'm not sure about how PowerPC GCC defines it.

OSFatal(sizeof(BOOL));

This small piece of code will make the console freeze and display a number. This number is how many bytes of RAM your BOOL will consume. If this number is 1, that represents one byte, which is equivalent to 8 bits. So if you see 1, then you can cast your BOOL as a u8 without changing the memory layout. If you see 2, you should cast it as a u16 and if you see 4, you should cast it as a u32.

Hope that helps!

neuschaefer commented 6 years ago

The thing is: The person who contributed that piece of code (@Crayon2000) must have intended one particular size. Otherwise the offsets in VPADData are off¹. I use dynamic_libs in a home-grown environment that didn't define BOOL (or bool) until now, so I can't just ask the compiler how big it is².

Maybe it's better to avoid such environment-sensitive types in reverse-engineered structs.

¹) I could check if the struct size matches the struct size before that commit if I insert u8, but that's more guesswork than I'd like to do for now. ²) I could temporarily switch to DevkitPPC, but I'm lazy.

Maschell commented 6 years ago

I see the problem. If I remember correctly, the BOOL is an int (also see https://decaf-emu.github.io/wut/wut__types_8h_source.html) The best solution would be a conversion to s32? Edit: I will double check the size of the BOOL when I'm home

Crayon2000 commented 6 years ago

@Maschell, I think you are right, s32 is the size of BOOL for this code.

Do you want to add something like this:

#ifndef BOOL
typedef int                 BOOL;
#endif  // BOOL

Or it's better to replace the BOOL value with s32?

If you want, I can validate this and do a PR?

neuschaefer commented 6 years ago

Hi, @Crayon2000, I'd prefer s32, because there's less that can go wrong with it.

#ifdef/typdef/#endif won't work, as far as I can see, because typedef doesn't create a preprocessor-level definition. I'd have to be something like this instead (which I also didn't test):

#ifndef BOOL
typdef int BOOL;
#define BOOL BOOL   /* This won't create a macro loop, but it will make sure that #ifdef BOOL works */
#endif // BOOL

That raises the question whether other libraries that define BOOL #define BOOL, or #define something else to indicate the presence of BOOL, etc.

Thanks

neuschaefer commented 6 years ago

Thanks!