- FIELD_OFFSET returns LONG, not LONG_PTR or size_t.
authorStefan Ginsberg <stefanginsberg@gmail.com>
Sat, 25 Jul 2009 19:38:52 +0000 (19:38 +0000)
committerStefan Ginsberg <stefanginsberg@gmail.com>
Sat, 25 Jul 2009 19:38:52 +0000 (19:38 +0000)
- Define TYPE_ALIGNMENT for msvc.
- Define DbgPrintEx the same way in NDK and DDK
- C_ASSERT is a typedef, so don't put it inside functions as msvc doesn't allow this.
- Kernel now *compiles* with RosBE + msvc _and_ our DDK. Linker errors from the ninth circle of hell remains.

svn path=/trunk/; revision=42216

reactos/include/ddk/ntdef.h
reactos/include/ddk/winddk.h
reactos/include/host/typedefs.h
reactos/include/psdk/winnt.h
reactos/ntoskrnl/ke/wait.c
reactos/ntoskrnl/mm/pe.c

index 747997b..24082bd 100644 (file)
@@ -155,20 +155,20 @@ typedef unsigned long POINTER_64; // FIXME! HACK!!!
 //
 // Returns the byte offset of the specified structure's member
 //
-#ifndef FIELD_OFFSET
 #ifndef __GNUC__
-#define FIELD_OFFSET(Type, Field) \
-  ((LONG)(LONG_PTR) (&(((Type *) 0)->Field)))
+#define FIELD_OFFSET(Type, Field) ((LONG)(LONG_PTR)&(((Type*) 0)->Field))
 #else
 #define FIELD_OFFSET(Type, Field) __builtin_offsetof(Type, Field)
 #endif
-#endif
 
 //
 // Returns the type's alignment
 //
+#if defined(_MSC_VER) && (_MSC_VER >= 1300)
+#define TYPE_ALIGNMENT(t) __alignof(t)
+#else
 #define TYPE_ALIGNMENT(t) FIELD_OFFSET( struct { char x; t test; }, test )
-
+#endif
 
 //
 // Calling Conventions
index 0ecca59..7518597 100644 (file)
@@ -10277,6 +10277,7 @@ DbgPrint(
   IN PCCH  Format,
   IN ...);
 
+NTSYSAPI
 ULONG
 DDKCDECLAPI
 DbgPrintEx(
index 193ec7a..a1feea8 100644 (file)
@@ -219,9 +219,9 @@ typedef const UNICODE_STRING *PCUNICODE_STRING;
 
 #define NT_SUCCESS(x)           ((x)>=0)
 #if !defined(__GNUC__)
-#define FIELD_OFFSET(t,f)       ((LONG_PTR)&(((t*)0)->f))
+#define FIELD_OFFSET(t,f)       ((LONG)(LONG_PTR)&(((t*) 0)->f))
 #else
-#define FIELD_OFFSET(t,f)       __builtin_offsetof(t,f)
+#define FIELD_OFFSET(t,f)       ((LONG)__builtin_offsetof(t,f))
 #endif
 #define RTL_CONSTANT_STRING(s)  { sizeof(s)-sizeof((s)[0]), sizeof(s), s }
 #define CONTAINING_RECORD(address, type, field)  ((type *)(((ULONG_PTR)address) - (ULONG_PTR)(&(((type *)0)->field))))
index ea32ffd..610f6af 100644 (file)
@@ -1311,10 +1311,10 @@ typedef enum {
 #define RTL_CRITSECT_TYPE 0
 #define RTL_RESOURCE_TYPE 1
 /* Also in winddk.h */
-#ifndef __GNUC__
-#define FIELD_OFFSET(t,f) ((LONG_PTR)&(((t*)0)->f))
+#if !defined(__GNUC__)
+#define FIELD_OFFSET(t,f) ((LONG)(LONG_PTR)&(((t*) 0)->f))
 #else
-#define FIELD_OFFSET(t,f) __builtin_offsetof(t,f)
+#define FIELD_OFFSET(t,f) ((LONG)__builtin_offsetof(t,f))
 #endif
 #ifndef CONTAINING_RECORD
 #define CONTAINING_RECORD(address, type, field) \
index 86bdbc1..ac1ae64 100644 (file)
@@ -117,6 +117,9 @@ KiAcquireFastMutex(IN PFAST_MUTEX FastMutex)
                           NULL);
 }
 
+/* KiAcquireGuardedMutex depends on these bits being right */
+C_ASSERT((GM_LOCK_WAITER_WOKEN * 2) == GM_LOCK_WAITER_INC);
+
 VOID
 FASTCALL
 KiAcquireGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex)
@@ -181,9 +184,6 @@ KiAcquireGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex)
         /* Ok, the wait is done, so set the new bits */
         BitsToRemove = GM_LOCK_BIT | GM_LOCK_WAITER_WOKEN;
         BitsToAdd = GM_LOCK_WAITER_WOKEN;
-        
-        /* We depend on these bits being just right */
-        C_ASSERT((GM_LOCK_WAITER_WOKEN * 2) == GM_LOCK_WAITER_INC);
     }
 }
 
index 2b94a32..49b5b19 100644 (file)
@@ -136,6 +136,84 @@ static __inline BOOLEAN AlignUp(OUT PULONG AlignedAddress, IN ULONG Address, IN
   (RTL_FIELD_SIZE(TYPE1_, FIELD_) == RTL_FIELD_SIZE(TYPE2_, FIELD_)) \
  )
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+//
+// FIXME: All this whitespace is "padding" so the C_ASSERTs aren't on the same lines as asserts in other headers.
+// This is necessary because of the way we define C_ASSERT in a gcc compatible way.
+// This can be removed once we upgrade to gcc 4.3.x or later (which implements __COUNTER__).
+//
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+//
+// PeFmtCreateSection depends on the following:
+//
+C_ASSERT(EXEFMT_LOAD_HEADER_SIZE >= sizeof(IMAGE_DOS_HEADER));
+C_ASSERT(sizeof(IMAGE_NT_HEADERS32) <= sizeof(IMAGE_NT_HEADERS64));
+
+C_ASSERT(TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) == TYPE_ALIGNMENT(IMAGE_NT_HEADERS64));
+C_ASSERT(RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader) == RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS64, FileHeader));
+C_ASSERT(FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader) == FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader));
+
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Magic));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SectionAlignment));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, FileAlignment));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Subsystem));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MinorSubsystemVersion));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MajorSubsystemVersion));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, AddressOfEntryPoint));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfCode));
+C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfHeaders));
+
 /*
  References:
   [1] Microsoft Corporation, "Microsoft Portable Executable and Common Object
@@ -178,7 +256,6 @@ NTSTATUS NTAPI PeFmtCreateSection(IN CONST VOID * FileHeader,
 
     ASSERT(Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize));
 
-    C_ASSERT(EXEFMT_LOAD_HEADER_SIZE >= sizeof(IMAGE_DOS_HEADER));
     ASSERT(((UINT_PTR)FileHeader % TYPE_ALIGNMENT(IMAGE_DOS_HEADER)) == 0);
 
 #define DIE(ARGS_) { DPRINT ARGS_; goto l_Return; }
@@ -219,11 +296,6 @@ NTSTATUS NTAPI PeFmtCreateSection(IN CONST VOID * FileHeader,
        pinhNtHeader = (PVOID)((UINT_PTR)FileHeader + pidhDosHeader->e_lfanew);
     }
 
-    C_ASSERT(sizeof(IMAGE_NT_HEADERS32) <= sizeof(IMAGE_NT_HEADERS64));
-    C_ASSERT(TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) == TYPE_ALIGNMENT(IMAGE_NT_HEADERS64));
-    C_ASSERT(RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader) == RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS64, FileHeader));
-    C_ASSERT(FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader) == FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader));
-
     /*
      * the buffer doesn't contain the NT file header, or the alignment is wrong: we
      * need to read the header from the file
@@ -307,8 +379,6 @@ l_ReadHeaderFromFile:
 
     nStatus = STATUS_INVALID_IMAGE_FORMAT;
 
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Magic));
-
     if(!RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Magic))
        DIE(("The optional header doesn't contain the Magic field, SizeOfOptionalHeader is %X\n", cbOptHeaderSize));
 
@@ -324,9 +394,6 @@ l_ReadHeaderFromFile:
            DIE(("Unrecognized optional header, Magic is %X\n", piohOptHeader->Magic));
     }
 
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SectionAlignment));
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, FileAlignment));
-
     if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SectionAlignment) &&
         RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, FileAlignment))
     {
@@ -421,10 +488,6 @@ l_ReadHeaderFromFile:
     if((ULONG_PTR)ImageSectionObject->ImageBase % 0x10000)
        DIE(("ImageBase is not aligned on a 64KB boundary"));
 
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Subsystem));
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MinorSubsystemVersion));
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MajorSubsystemVersion));
-
     if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Subsystem))
     {
        ImageSectionObject->Subsystem = piohOptHeader->Subsystem;
@@ -437,16 +500,12 @@ l_ReadHeaderFromFile:
        }
     }
 
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, AddressOfEntryPoint));
-
     if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
     {
        ImageSectionObject->EntryPoint = piohOptHeader->ImageBase +
                                          piohOptHeader->AddressOfEntryPoint;
     }
 
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfCode));
-
     if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfCode))
        ImageSectionObject->Executable = piohOptHeader->SizeOfCode != 0;
     else
@@ -483,8 +542,6 @@ l_ReadHeaderFromFile:
     if(!Intsafe_AddULong32(&cbSectionHeadersOffsetSize, cbSectionHeadersOffset, cbSectionHeadersSize))
        DIE(("Section headers too large\n"));
 
-    C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfHeaders));
-
     /* size of the executable's headers */
     if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfHeaders))
     {