[NTUSER] menu.c: Tiny optimizations (#8111)

A bit clearer code. A bit faster execution.
- NtUserGetTitleBarInfo(): Add/Use early returns.
  Addendum to
3b4c9ded42 (r33657).
- NtUserTrackPopupMenuEx():
  Check flags a bit earlier.
  Addendum to
3c35117f97
  (0.4.16-dev-1275).
- NtUserThunkedMenuItemInfo():
  Sort out code and comments
- menu.c: Move UserLeave() a bit earlier.
This commit is contained in:
Serge Gautherie 2025-06-25 14:15:28 +02:00 committed by GitHub
parent aaed9f77d9
commit 4164b8a053
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5577,8 +5577,8 @@ NtUserCalcMenuBar(
if(!(Window = UserGetWindowObject(hwnd))) if(!(Window = UserGetWindowObject(hwnd)))
{ {
EngSetLastError(ERROR_INVALID_WINDOW_HANDLE);
UserLeave(); UserLeave();
EngSetLastError(ERROR_INVALID_WINDOW_HANDLE);
return 0; return 0;
} }
@ -5599,7 +5599,6 @@ NtUserCalcMenuBar(
UserReleaseDC( 0, hdc, FALSE ); UserReleaseDC( 0, hdc, FALSE );
UserLeave(); UserLeave();
return ret; return ret;
} }
@ -5624,8 +5623,8 @@ NtUserCheckMenuItem(
Ret = IntCheckMenuItem(Menu, uIDCheckItem, uCheck); Ret = IntCheckMenuItem(Menu, uIDCheckItem, uCheck);
} }
TRACE("Leave NtUserCheckMenuItem, ret=%lu\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserCheckMenuItem, ret=%lu\n", Ret);
return Ret; return Ret;
} }
@ -5650,8 +5649,8 @@ NtUserDeleteMenu(
Ret = IntRemoveMenuItem(Menu, uPosition, uFlags, TRUE); Ret = IntRemoveMenuItem(Menu, uPosition, uFlags, TRUE);
} }
TRACE("Leave NtUserDeleteMenu, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserDeleteMenu, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -5705,8 +5704,8 @@ NtUserGetSystemMenu(HWND hWnd, BOOL bRevert)
Ret = UserHMGetHandle(Menu); Ret = UserHMGetHandle(Menu);
Exit: Exit:
TRACE("Leave NtUserGetSystemMenu, ret=%p\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserGetSystemMenu, ret=%p\n", Ret);
return Ret; return Ret;
} }
@ -5748,8 +5747,8 @@ NtUserSetSystemMenu(HWND hWnd, HMENU hMenu)
EngSetLastError(ERROR_INVALID_MENU_HANDLE); EngSetLastError(ERROR_INVALID_MENU_HANDLE);
Exit: Exit:
TRACE("Leave NtUserSetSystemMenu, ret=%i\n", Result);
UserLeave(); UserLeave();
TRACE("Leave NtUserSetSystemMenu, ret=%i\n", Result);
return Result; return Result;
} }
@ -5763,56 +5762,55 @@ NtUserGetTitleBarInfo(
{ {
PWND WindowObject; PWND WindowObject;
TITLEBARINFO bartitleinfo; TITLEBARINFO bartitleinfo;
BOOLEAN retValue = TRUE; BOOLEAN retValue = FALSE;
TRACE("Enter NtUserGetTitleBarInfo\n"); TRACE("Enter NtUserGetTitleBarInfo\n");
UserEnterExclusive(); UserEnterExclusive();
/* Vaildate the windows handle */ /* Validate the window handle */
if (!(WindowObject = UserGetWindowObject(hwnd))) if (!(WindowObject = UserGetWindowObject(hwnd)))
{ {
EngSetLastError(ERROR_INVALID_WINDOW_HANDLE); EngSetLastError(ERROR_INVALID_WINDOW_HANDLE);
retValue = FALSE; goto Exit;
} }
/* Copy user mode buffer to local buffer */
_SEH2_TRY _SEH2_TRY
{ {
/* Copy our usermode buffer bti to local buffer bartitleinfo */
ProbeForRead(bti, sizeof(TITLEBARINFO), 1); ProbeForRead(bti, sizeof(TITLEBARINFO), 1);
RtlCopyMemory(&bartitleinfo, bti, sizeof(TITLEBARINFO)); RtlCopyMemory(&bartitleinfo, bti, sizeof(TITLEBARINFO));
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* Fail copy the data */
EngSetLastError(ERROR_INVALID_PARAMETER); EngSetLastError(ERROR_INVALID_PARAMETER);
retValue = FALSE; _SEH2_YIELD(goto Exit);
} }
_SEH2_END _SEH2_END
/* Get the tile bar info */ /* Get the tile bar info */
if (retValue) retValue = intGetTitleBarInfo(WindowObject, &bartitleinfo);
if (!retValue)
{ {
retValue = intGetTitleBarInfo(WindowObject, &bartitleinfo); // intGetTitleBarInfo() set LastError.
if (retValue) goto Exit;
{
_SEH2_TRY
{
/* Copy our buffer to user mode buffer bti */
ProbeForWrite(bti, sizeof(TITLEBARINFO), 1);
RtlCopyMemory(bti, &bartitleinfo, sizeof(TITLEBARINFO));
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
/* Fail copy the data */
EngSetLastError(ERROR_INVALID_PARAMETER);
retValue = FALSE;
}
_SEH2_END
}
} }
TRACE("Leave NtUserGetTitleBarInfo, ret=%u\n", retValue); /* Copy local buffer back to user mode buffer */
_SEH2_TRY
{
ProbeForWrite(bti, sizeof(TITLEBARINFO), 1);
RtlCopyMemory(bti, &bartitleinfo, sizeof(TITLEBARINFO));
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
EngSetLastError(ERROR_INVALID_PARAMETER);
retValue = FALSE;
}
_SEH2_END;
Exit:
UserLeave(); UserLeave();
TRACE("Leave NtUserGetTitleBarInfo, ret=%u\n", retValue);
return retValue; return retValue;
} }
@ -5862,8 +5860,8 @@ NtUserDestroyMenu(
Ret = IntDestroyMenuObject(Menu, TRUE); Ret = IntDestroyMenuObject(Menu, TRUE);
Exit: Exit:
TRACE("Leave NtUserDestroyMenu, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserDestroyMenu, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -5888,8 +5886,8 @@ NtUserEnableMenuItem(
Ret = IntEnableMenuItem(Menu, uIDEnableItem, uEnable); Ret = IntEnableMenuItem(Menu, uIDEnableItem, uEnable);
} }
TRACE("Leave NtUserEnableMenuItem, ret=%u\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserEnableMenuItem, ret=%u\n", Ret);
return Ret; return Ret;
} }
@ -6078,8 +6076,8 @@ NtUserGetMenuBarInfo(
Cleanup: Cleanup:
if (pWnd) UserDerefObjectCo(pWnd); if (pWnd) UserDerefObjectCo(pWnd);
TRACE("Leave NtUserGetMenuBarInfo, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserGetMenuBarInfo, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6114,8 +6112,8 @@ NtUserGetMenuIndex(
} }
Exit: Exit:
TRACE("Leave NtUserGetMenuIndex, ret=%u\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserGetMenuIndex, ret=%u\n", Ret);
return Ret; return Ret;
} }
@ -6200,8 +6198,8 @@ NtUserGetMenuItemRect(
Ret = TRUE; Ret = TRUE;
Exit: Exit:
TRACE("Leave NtUserGetMenuItemRect, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserGetMenuItemRect, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6237,8 +6235,8 @@ NtUserHiliteMenuItem(
Ret = IntHiliteMenuItem(Window, Menu, uItemHilite, uHilite); Ret = IntHiliteMenuItem(Window, Menu, uItemHilite, uHilite);
Exit: Exit:
TRACE("Leave NtUserHiliteMenuItem, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserHiliteMenuItem, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6295,8 +6293,8 @@ NtUserDrawMenuBarTemp(
Ret = IntDrawMenuBarTemp(Window, hDC, &Rect, Menu, hFont); Ret = IntDrawMenuBarTemp(Window, hDC, &Rect, Menu, hFont);
Exit: Exit:
ERR("Leave NtUserDrawMenuBarTemp, ret=%lu\n", Ret);
UserLeave(); UserLeave();
ERR("Leave NtUserDrawMenuBarTemp, ret=%lu\n", Ret);
return Ret; return Ret;
} }
@ -6353,8 +6351,8 @@ NtUserMenuItemFromPoint(
Ret = (mi ? i : NO_SELECTED_ITEM); Ret = (mi ? i : NO_SELECTED_ITEM);
Exit: Exit:
TRACE("Leave NtUserMenuItemFromPoint, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserMenuItemFromPoint, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6377,8 +6375,8 @@ NtUserPaintMenuBar(
if(!(Window = UserGetWindowObject(hWnd))) if(!(Window = UserGetWindowObject(hWnd)))
{ {
EngSetLastError(ERROR_INVALID_WINDOW_HANDLE);
UserLeave(); UserLeave();
EngSetLastError(ERROR_INVALID_WINDOW_HANDLE);
return 0; return 0;
} }
@ -6390,7 +6388,6 @@ NtUserPaintMenuBar(
ret = MENU_DrawMenuBar(hDC, &Rect, Window, FALSE); ret = MENU_DrawMenuBar(hDC, &Rect, Window, FALSE);
UserLeave(); UserLeave();
return ret; return ret;
} }
@ -6415,8 +6412,8 @@ NtUserRemoveMenu(
Ret = IntRemoveMenuItem(Menu, uPosition, uFlags, FALSE); Ret = IntRemoveMenuItem(Menu, uPosition, uFlags, FALSE);
} }
TRACE("Leave NtUserRemoveMenu, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserRemoveMenu, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6458,8 +6455,8 @@ NtUserSetMenu(
Ret = TRUE; Ret = TRUE;
Exit: Exit:
TRACE("Leave NtUserSetMenu, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserSetMenu, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6483,8 +6480,8 @@ NtUserSetMenuContextHelpId(
Ret = IntSetMenuContextHelpId(Menu, dwContextHelpId); Ret = IntSetMenuContextHelpId(Menu, dwContextHelpId);
} }
TRACE("Leave NtUserSetMenuContextHelpId, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserSetMenuContextHelpId, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6509,8 +6506,8 @@ NtUserSetMenuDefaultItem(
Ret = UserSetMenuDefaultItem(Menu, uItem, fByPos); Ret = UserSetMenuDefaultItem(Menu, uItem, fByPos);
} }
TRACE("Leave NtUserSetMenuDefaultItem, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserSetMenuDefaultItem, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6533,8 +6530,8 @@ NtUserSetMenuFlagRtoL(
Ret = IntSetMenuFlagRtoL(Menu); Ret = IntSetMenuFlagRtoL(Menu);
} }
TRACE("Leave NtUserSetMenuFlagRtoL, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserSetMenuFlagRtoL, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6558,8 +6555,8 @@ NtUserThunkedMenuInfo(
Ret = UserMenuInfo(Menu, (PROSMENUINFO)lpcmi, TRUE); Ret = UserMenuInfo(Menu, (PROSMENUINFO)lpcmi, TRUE);
} }
TRACE("Leave NtUserThunkedMenuInfo, ret=%i\n", Ret);
UserLeave(); UserLeave();
TRACE("Leave NtUserThunkedMenuInfo, ret=%i\n", Ret);
return Ret; return Ret;
} }
@ -6577,17 +6574,13 @@ NtUserThunkedMenuItemInfo(
{ {
PMENU Menu; PMENU Menu;
NTSTATUS Status; NTSTATUS Status;
UNICODE_STRING lstrCaption; UNICODE_STRING lstrCaption = { 0 };
BOOL Ret = FALSE; BOOL Ret = FALSE;
TRACE("Enter NtUserThunkedMenuItemInfo\n"); TRACE("Enter NtUserThunkedMenuItemInfo\n");
UserEnterExclusive(); UserEnterExclusive();
/* lpszCaption may be NULL, check for it and call RtlInitUnicodeString()
if bInsert == TRUE call UserInsertMenuItem() else UserSetMenuItemInfo() */
RtlInitEmptyUnicodeString(&lstrCaption, NULL, 0);
if (!(Menu = UserGetMenuObject(hMenu))) if (!(Menu = UserGetMenuObject(hMenu)))
{ {
goto Cleanup; // Return FALSE goto Cleanup; // Return FALSE
@ -6611,19 +6604,21 @@ NtUserThunkedMenuItemInfo(
if (bInsert) if (bInsert)
{ {
Ret = UserInsertMenuItem(Menu, uItem, fByPosition, lpmii, &lstrCaption); Ret = UserInsertMenuItem(Menu, uItem, fByPosition, lpmii, &lstrCaption);
goto Cleanup; }
else
{
Ret = UserMenuItemInfo(Menu, uItem, fByPosition, (PROSMENUITEMINFO)lpmii, TRUE, &lstrCaption);
} }
Ret = UserMenuItemInfo(Menu, uItem, fByPosition, (PROSMENUITEMINFO)lpmii, TRUE, &lstrCaption);
Cleanup: Cleanup:
UserLeave();
if (lstrCaption.Buffer) if (lstrCaption.Buffer)
{ {
ReleaseCapturedUnicodeString(&lstrCaption, UserMode); ReleaseCapturedUnicodeString(&lstrCaption, UserMode);
} }
TRACE("Leave NtUserThunkedMenuItemInfo, ret=%i\n", Ret); TRACE("Leave NtUserThunkedMenuItemInfo, ret=%i\n", Ret);
UserLeave();
return Ret; return Ret;
} }
@ -6651,15 +6646,16 @@ NtUserTrackPopupMenuEx(
USER_REFERENCE_ENTRY WndRef, MenuRef; USER_REFERENCE_ENTRY WndRef, MenuRef;
TRACE("Enter NtUserTrackPopupMenuEx\n"); TRACE("Enter NtUserTrackPopupMenuEx\n");
UserEnterExclusive();
if (fuFlags & ~VALID_TPM_FLAGS) if (fuFlags & ~VALID_TPM_FLAGS)
{ {
ERR("TPME : Invalid flags 0x%X (valid flags are 0x%X)\n", fuFlags, VALID_TPM_FLAGS); ERR("TPME : Invalid flags 0x%X (valid flags are 0x%X)\n", fuFlags, VALID_TPM_FLAGS);
EngSetLastError(ERROR_INVALID_FLAGS); EngSetLastError(ERROR_INVALID_FLAGS);
goto Exit; goto Exit0;
} }
UserEnterExclusive();
/* Parameter check */ /* Parameter check */
if (!(menu = UserGetMenuObject( hMenu ))) if (!(menu = UserGetMenuObject( hMenu )))
{ {
@ -6695,7 +6691,9 @@ NtUserTrackPopupMenuEx(
UserDerefObjectCo(pWnd); UserDerefObjectCo(pWnd);
Exit: Exit:
TRACE("Leave NtUserTrackPopupMenuEx, ret=%i\n",Ret);
UserLeave(); UserLeave();
Exit0:
TRACE("Leave NtUserTrackPopupMenuEx, ret=%i\n", Ret);
return Ret; return Ret;
} }