-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add c.s.j.win32.Psapi.QueryWorkingSetEx, c.s.j.win32.Kernel32.VirtualLock, c.s.j.win32.Kernel32.VirtualUnlock #1459
Add c.s.j.win32.Psapi.QueryWorkingSetEx, c.s.j.win32.Kernel32.VirtualLock, c.s.j.win32.Kernel32.VirtualUnlock #1459
Conversation
…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;
d75a59c
to
45647d5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but given that the mappings don't exactly match the docs I'd prefer if the test could be performed on an array, to confirm the struct size/alignment of the array members.
45647d5
to
1fa2c15
Compare
Valid point. I rewrote the test to use an array as basis: 1fa2c15 Was this what you were thinking about? |
Far more than I was thinking! But looks great. |
6f61dd3
to
d5258ea
Compare
Appveyor and travis are finally happy. @dbwiddis thank you for review/feedback @Crain-32 you can use this immediately with JNA/JNA-platform 5.12.1, by creating a local interface with the definition that derives from public interface PsapiExt extends Psapi {
Psapi INSTANCE = Native.load("psapi", Psapi.class, W32APIOptions.DEFAULT_OPTIONS);
boolean QueryWorkingSetEx(HANDLE hProcess, Pointer pv, int cb);
@FieldOrder({"VirtualAddress", "VirtualAttributes"})
class PSAPI_WORKING_SET_EX_INFORMATION extends Structure {
public Pointer VirtualAddress;
public ULONG_PTR VirtualAttributes;
/**
* If this bit is 1, the subsequent members are valid; otherwise they
* should be ignored.
*/
public boolean isValid() {
return getBitFieldValue(1, 0) == 1;
}
/**
* The number of processes that share this page. The maximum value of
* this member is 7.
*/
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>.
*/
public int getWin32Protection() {
return getBitFieldValue(11, 3 + 1);
}
/**
* If this bit is 1, the page can be shared.
*/
public boolean isShared() {
return getBitFieldValue(1, 11 + 3 + 1) == 1;
}
/**
* The NUMA node. The maximum value of this member is 63.
*/
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 isLocked() {
return getBitFieldValue(1, 6 + 1 + 11 + 3 + 1) == 1;
}
/**
* If this bit is 1, the page is a large page.
*/
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 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/>
* 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.
*/
private int getBitFieldValue(final int maskLength, final int rightShiftAmount) {
long bitMask = 0;
for (int l = 0; l < maskLength; l++) {
bitMask |= 1 << l;
}
return (int) ((VirtualAttributes.longValue() >>> rightShiftAmount) & bitMask);
}
}
} |
This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [net.java.dev.jna:jna](https://github.com/java-native-access/jna) | compile | minor | `5.12.1` -> `5.13.0` | --- ### Release Notes <details> <summary>java-native-access/jna</summary> ### [`v5.13.0`](https://github.com/java-native-access/jna/blob/HEAD/CHANGES.md#Release-5130) [Compare Source](java-native-access/jna@5.12.1...5.13.0) \================ ## Features - [#​1454](java-native-access/jna#1454): Add `c.s.j.p.win32.Psapi.QueryWorkingSetEx` and associated Types - [@​crain-32](https://github.com/Crain-32). - [#​1459](java-native-access/jna#1459): Add `VirtualLock` and `VirtualUnlock` in `c.s.j.p.win32.Kernel32` - [@​matthiasblaesing](https://github.com/matthiasblaesing). - [#​1471](java-native-access/jna#1471): Add `c.s.j.p.win32.Advapi32Util#isCurrentProcessElevated` and associated Types - [@​dbwiddis](https://github.com/dbwiddis). - [#​1474](java-native-access/jna#1474): Add `c.s.j.p.win32.WbemCli#IWbemClassObject.IWbemQualifierSet`, `IWbemServices.GetObject`, `IWbemContext.SetValue` and associated methods - [@​rchateauneu](https://github.com/rchateauneu). - [#​1482](java-native-access/jna#1482): Add multilingual support of `Kernel32Util.formatMessage` - [@​overpathz](https://github.com/overpathz). - [#​1490](java-native-access/jna#1490): Adds support for a custom `SymbolProvider` in `NativeLibrary` & `Library` - [@​soywiz](https://github.com/soywiz). - [#​1491](java-native-access/jna#1491): Update libffi to v3.4.4 - [@​matthiasblaesing](https://github.com/matthiasblaesing). - [#​1487](java-native-access/jna#1487): Add 'uses' information to OSGI metadata in MANIFEST.MF to improve stability of package resolution - [@​sratz](https://github.com/sratz). ## Bug Fixes - [#​1452](java-native-access/jna#1452): Fix memory allocation/handling for error message generation in native library code (`dispatch.c`) - [@​matthiasblaesing](https://github.com/matthiasblaesing). - [#​1460](java-native-access/jna#1460): Fix win32 variant date conversion in DST offest window and with millisecond values - [@​eranl](https://github.com/eranl). - [#​1472](java-native-access/jna#1472): Fix incorrect bitmask in `c.s.j.Pointer#createConstant(int)` - [@​dbwiddis](https://github.com/dbwiddis). - [#​1481](java-native-access/jna#1481): Fix NPE in NativeLibrary when unpacking from classpath is disabled - [@​trespasserw](https://github.com/trespasserw). - [#​1489](java-native-access/jna#1489): Fixes typo in `OpenGL32Util#wglGetProcAddress`, instead of parameter `procName` the hardcoded value `wglEnumGpusNV` was used - [@​soywiz](https://github.com/soywiz). </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yNC4wIiwidXBkYXRlZEluVmVyIjoiMzQuMjQuMCJ9-->
No description provided.