Skip to content
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

Merged

Conversation

matthiasblaesing
Copy link
Member

No description provided.

Crain-32 and others added 3 commits August 9, 2022 08:40
…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;
Copy link
Contributor

@dbwiddis dbwiddis left a 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.

@matthiasblaesing
Copy link
Member Author

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.

Valid point. I rewrote the test to use an array as basis: 1fa2c15

Was this what you were thinking about?

@dbwiddis
Copy link
Contributor

Was this what you were thinking about?

Far more than I was thinking! But looks great.

@dbwiddis dbwiddis linked an issue Aug 11, 2022 that may be closed by this pull request
@matthiasblaesing
Copy link
Member Author

matthiasblaesing commented Aug 13, 2022

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 Psapi. In your code you use then PsapiExt instead of Psapi:

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);
        }
    }
}

@matthiasblaesing matthiasblaesing merged commit 927e5da into java-native-access:master Aug 13, 2022
@matthiasblaesing matthiasblaesing deleted the github-1454 branch August 13, 2022 14:00
benkard added a commit to benkard/mulkcms2 that referenced this pull request Jan 14, 2023
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

-   [#&#8203;1454](java-native-access/jna#1454): Add `c.s.j.p.win32.Psapi.QueryWorkingSetEx` and associated Types - [@&#8203;crain-32](https://github.com/Crain-32).
-   [#&#8203;1459](java-native-access/jna#1459): Add `VirtualLock` and `VirtualUnlock` in `c.s.j.p.win32.Kernel32` - [@&#8203;matthiasblaesing](https://github.com/matthiasblaesing).
-   [#&#8203;1471](java-native-access/jna#1471): Add `c.s.j.p.win32.Advapi32Util#isCurrentProcessElevated` and associated Types - [@&#8203;dbwiddis](https://github.com/dbwiddis).
-   [#&#8203;1474](java-native-access/jna#1474): Add `c.s.j.p.win32.WbemCli#IWbemClassObject.IWbemQualifierSet`, `IWbemServices.GetObject`, `IWbemContext.SetValue` and associated methods - [@&#8203;rchateauneu](https://github.com/rchateauneu).
-   [#&#8203;1482](java-native-access/jna#1482): Add multilingual support of `Kernel32Util.formatMessage` - [@&#8203;overpathz](https://github.com/overpathz).
-   [#&#8203;1490](java-native-access/jna#1490): Adds support for a custom `SymbolProvider` in `NativeLibrary` & `Library` - [@&#8203;soywiz](https://github.com/soywiz).
-   [#&#8203;1491](java-native-access/jna#1491): Update libffi to v3.4.4  - [@&#8203;matthiasblaesing](https://github.com/matthiasblaesing).
-   [#&#8203;1487](java-native-access/jna#1487): Add 'uses' information to OSGI metadata in MANIFEST.MF to improve stability of package resolution - [@&#8203;sratz](https://github.com/sratz).

## Bug Fixes

-   [#&#8203;1452](java-native-access/jna#1452): Fix memory allocation/handling for error message generation in native library code (`dispatch.c`) - [@&#8203;matthiasblaesing](https://github.com/matthiasblaesing).
-   [#&#8203;1460](java-native-access/jna#1460): Fix win32 variant date conversion in DST offest window and with millisecond values - [@&#8203;eranl](https://github.com/eranl).
-   [#&#8203;1472](java-native-access/jna#1472): Fix incorrect bitmask in `c.s.j.Pointer#createConstant(int)` - [@&#8203;dbwiddis](https://github.com/dbwiddis).
-   [#&#8203;1481](java-native-access/jna#1481): Fix NPE in NativeLibrary when unpacking from classpath is disabled - [@&#8203;trespasserw](https://github.com/trespasserw).
-   [#&#8203;1489](java-native-access/jna#1489): Fixes typo in `OpenGL32Util#wglGetProcAddress`, instead of parameter `procName` the hardcoded value `wglEnumGpusNV` was used - [@&#8203;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-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add in QueryWorkingSetEx from win32's PSAPI
3 participants