Skip to content

Commit

Permalink
Drop definition of PSAPI_WORKING_SET_EX_BLOCK, move functions to PSAP…
Browse files Browse the repository at this point in the history
…I_WORKING_SET_EX_INFORMATION, adjust function names

The definition of PSAPI_WORKING_SET_EX_BLOCK was not correct. The outer
definition is a UNION with two members:

- ULONG_PTR Flags and
- another anonymous union holding two bitfield definitions

It effectively defines a union with 3 defintions.

The bitfield definition differs between 32bit and 64bit. The 32bit
version defines data elements filling exactly the 32bits a single
ULONG_PTR can hold. The 64bit version adds an additional 32bit reserved
element, which agains fill the full size a single ULONG_PTR can hold on
64bit.

The two bitfield definitions both map to ULONG_PTR, so 
PSAPI_WORKING_SET_EX_BLOCK is basicly just a ULONG_PTR with C syntactic
sugar for decoding.

The PSAPI_WORKING_SET_EX_BLOCK is only used in 
PSAPI_WORKING_SET_EX_INFORMATION and given that the union definition
of PSAPI_WORKING_SET_EX_BLOCK is syntactic sugar, we can drop the
definition and integrate the decoding logic directly into
PSAPI_WORKING_SET_EX_INFORMATION.

-----------------------------------------------------------------------

typedef struct _PSAPI_WORKING_SET_EX_INFORMATION {
    PVOID VirtualAddress;
    PSAPI_WORKING_SET_EX_BLOCK VirtualAttributes;                                                                                                                                                  
} PSAPI_WORKING_SET_EX_INFORMATION, *PPSAPI_WORKING_SET_EX_INFORMATION;

typedef union _PSAPI_WORKING_SET_EX_BLOCK {
    ULONG_PTR Flags;
    union {
        struct {
            ULONG_PTR Valid : 1;
            ULONG_PTR ShareCount : 3;
            ULONG_PTR Win32Protection : 11;
            ULONG_PTR Shared : 1;
            ULONG_PTR Node : 6;
            ULONG_PTR Locked : 1;
            ULONG_PTR LargePage : 1;
            ULONG_PTR Reserved : 7;
            ULONG_PTR Bad : 1;

#if defined(_WIN64)
            ULONG_PTR ReservedUlong : 32;
#endif
        };
        struct {
            ULONG_PTR Valid : 1;            // Valid = 0 in this format.
            ULONG_PTR Reserved0 : 14;
            ULONG_PTR Shared : 1;
            ULONG_PTR Reserved1 : 15;
            ULONG_PTR Bad : 1;

#if defined(_WIN64)
            ULONG_PTR ReservedUlong : 32;
#endif
        } Invalid;
    };
} PSAPI_WORKING_SET_EX_BLOCK, *PPSAPI_WORKING_SET_EX_BLOCK;
  • Loading branch information
matthiasblaesing committed Aug 11, 2022
1 parent d4ccd5c commit dbf8751
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions contrib/platform/src/com/sun/jna/platform/win32/Psapi.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,78 +335,80 @@ class PERFORMANCE_INFORMATION extends Structure {
public DWORD ThreadCount;
}

@FieldOrder({"Flags", "Data"})
class PSAPI_WORKING_SET_EX_BLOCK extends Structure implements ByReference {
public ULONG_PTR Flags;
public ULONG_PTR[] Data = new ULONG_PTR[Native.POINTER_SIZE == 8 ? 1 : 2];
private long innerValue;
@FieldOrder({"VirtualAddress", "VirtualAttributes"})
class PSAPI_WORKING_SET_EX_INFORMATION extends Structure {

@Override
public void read() {
super.read();
innerValue = this.Data[0].longValue();
}
public Pointer VirtualAddress;
public ULONG_PTR VirtualAttributes;

/**
* If this bit is 1, the subsequent members are valid; otherwise they should be ignored.
* If this bit is 1, the subsequent members are valid; otherwise they
* should be ignored.
*/
public boolean Valid() {
public boolean isValid() {
return getBitFieldValue(1, 0) == 1;
}

/**
* The number of processes that share this page. The maximum value of this member is 7.
* The number of processes that share this page. The maximum value of
* this member is 7.
*/
public int ShareCount() {
public int getShareCount() {
return getBitFieldValue(3, 1);
}

/**
* The memory protection attributes of the page. For a list of values see below.
* @see <a href="https://docs.microsoft.com/en-us/windows/desktop/Memory/memory-protection-constants">Memory Protection Constants</a>.
* The memory protection attributes of the page. For a list of values
* see below.
*
* @see
* <a href="https://docs.microsoft.com/en-us/windows/desktop/Memory/memory-protection-constants">Memory
* Protection Constants</a>.
*/
public int Win32Protection() {
public int getWin32Protection() {
return getBitFieldValue(11, 3 + 1);
}

/**
* If this bit is 1, the page can be shared.
*/
public boolean Shared() {
public boolean isShared() {
return getBitFieldValue(1, 11 + 3 + 1) == 1;
}

/**
* The NUMA node. The maximum value of this member is 63.
*/
public int Node() {
public int getNode() {
return getBitFieldValue(6, 1 + 11 + 3 + 1);
}

/**
* If this bit is 1, the virtual page is locked in physical memory.
*/
public boolean Locked() {
public boolean isLocked() {
return getBitFieldValue(1, 6 + 1 + 11 + 3 + 1) == 1;
}

/**
* If this bit is 1, the page is a large page.
*/
public boolean LargePage() {
public boolean isLargePage() {
return getBitFieldValue(1, 1 + 6 + 1 + 11 + 3 + 1) == 1;
}

/**
* If this bit is 1, the page is has been reported as bad.
*/
public boolean Bad() {
return getBitFieldValue(1,1 + 1 + 1 + 6 + 1 + 11 + 3 + 1) == 1;
public boolean isBad() {
return getBitFieldValue(1, 1 + 1 + 1 + 6 + 1 + 11 + 3 + 1) == 1;
}

/**
* Returns innerValue after shifting the value rightShiftAmount, and applying a Bit Mask of size maskLength. Example, <br/>
* Returns innerValue after shifting the value rightShiftAmount, and
* applying a Bit Mask of size maskLength. Example, <br/>
* innerValue = 0011<br/> getBitFieldValue(2, 1) = 0011 >> 1 & 11 = 01
*
* @param maskLength Size of the Bit Mask
* @param rightShiftAmount Amount to Shift innerValue to the right by
* @return innerValue with the mask and shift applied.
Expand All @@ -417,14 +419,7 @@ private int getBitFieldValue(final int maskLength, final int rightShiftAmount) {
for (int l = 0; l < maskLength; l++) {
bitMask |= 1 << l;
}
return (int) ((innerValue >>> rightShiftAmount) & bitMask);
return (int) ((VirtualAttributes.longValue() >>> rightShiftAmount) & bitMask);
}
}


@FieldOrder({"VirtualAddress", "VirtualAttributes"})
class PSAPI_WORKING_SET_EX_INFORMATION extends Structure {
public Pointer VirtualAddress;
public PSAPI_WORKING_SET_EX_BLOCK VirtualAttributes;
}
}

0 comments on commit dbf8751

Please sign in to comment.