Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/TsudaKageyu/minhook.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTsuda Kageyu <tsuda.kageyu@gmail.com>2015-05-27 21:50:04 +0300
committerTsuda Kageyu <tsuda.kageyu@gmail.com>2015-05-27 21:50:04 +0300
commit42cbaea87730f4e250a9271196b28b29a6bb878f (patch)
treede276b8f6336a037563235d90d6d8309b1b4fd04
parent68e11d9e925d37ef8638c69af90e498a5224abaf (diff)
Reduce #ifdefs for MinGW.
-rw-r--r--src/buffer.c15
-rw-r--r--src/hook.c53
2 files changed, 29 insertions, 39 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 68d2b30..774e473 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -26,14 +26,12 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#define STRICT
+#define NOMINMAX
+#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include "buffer.h"
-#ifndef max
-#define max(a,b) (((a) > (b)) ? (a) : (b))
-#define min(a,b) (((a) < (b)) ? (a) : (b))
-#endif
-
// Size of each memory block. (= page size of VirtualAlloc)
#define MEMORY_BLOCK_SIZE 0x1000
@@ -166,10 +164,11 @@ static PMEMORY_BLOCK GetMemoryBlock(LPVOID pOrigin)
maxAddr = (ULONG_PTR)si.lpMaximumApplicationAddress;
// pOrigin ± 512MB
- if ((ULONG_PTR)pOrigin > MAX_MEMORY_RANGE)
- minAddr = max(minAddr, (ULONG_PTR)pOrigin - MAX_MEMORY_RANGE);
+ if ((ULONG_PTR)pOrigin > MAX_MEMORY_RANGE && minAddr < (ULONG_PTR)pOrigin - MAX_MEMORY_RANGE)
+ minAddr = (ULONG_PTR)pOrigin - MAX_MEMORY_RANGE;
- maxAddr = min(maxAddr, (ULONG_PTR)pOrigin + MAX_MEMORY_RANGE);
+ if (maxAddr > (ULONG_PTR)pOrigin + MAX_MEMORY_RANGE)
+ maxAddr = (ULONG_PTR)pOrigin + MAX_MEMORY_RANGE;
// Make room for MEMORY_BLOCK_SIZE bytes.
maxAddr -= MEMORY_BLOCK_SIZE - 1;
diff --git a/src/hook.c b/src/hook.c
index a7fd374..074bba4 100644
--- a/src/hook.c
+++ b/src/hook.c
@@ -26,27 +26,20 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _MSC_VER
-#define _WIN32_WINNT 0x0501
-#define WIN32_LEAN_AND_MEAN
-#endif
+#define STRICT
+#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <TlHelp32.h>
-#ifndef _MSC_VER
-#include <x86intrin.h>
#include <limits.h>
-#ifndef ARRAYSIZE
- #define ARRAYSIZE(x) sizeof(x)/sizeof(*x)
-#endif
-#else
-#include <intrin.h>
-#include <xmmintrin.h>
-#endif
#include "../include/MinHook.h"
#include "buffer.h"
#include "trampoline.h"
+#ifndef ARRAYSIZE
+ #define ARRAYSIZE(x) (sizeof(x)/sizeof(*x))
+#endif
+
// Initial capacity of the HOOK_ENTRY buffer.
#define INITIAL_HOOK_CAPACITY 32
@@ -442,27 +435,29 @@ static MH_STATUS EnableAllHooksLL(BOOL enable)
}
//-------------------------------------------------------------------------
+static FORCEINLINE VOID MemoryBarrierEx()
+{
+#if _MSC_VER
+ MemoryBarrier();
+#elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
+ __sync_synchronize();
+#else
+ __asm__ __volatile__("" : : : "memory");
+#endif
+}
+
+//-------------------------------------------------------------------------
static VOID EnterSpinLock(VOID)
{
SIZE_T spinCount = 0;
// Wait until the flag is FALSE.
-#ifndef _MSC_VER
while (InterlockedCompareExchange(&g_isLocked, TRUE, FALSE) != FALSE)
{
-#else
- while (_InterlockedCompareExchange(&g_isLocked, TRUE, FALSE) != FALSE)
- {
- _ReadWriteBarrier();
-#endif
+ MemoryBarrierEx();
+
// Prevent the loop from being too busy.
- if (spinCount < 16)
-#ifndef _MSC_VER
- Sleep(0);
-#else
- _mm_pause();
-#endif
- else if (spinCount < 32)
+ if (spinCount < 32)
Sleep(0);
else
Sleep(1);
@@ -474,12 +469,8 @@ static VOID EnterSpinLock(VOID)
//-------------------------------------------------------------------------
static VOID LeaveSpinLock(VOID)
{
-#ifndef _MSC_VER
+ MemoryBarrierEx();
InterlockedExchange(&g_isLocked, FALSE);
-#else
- _ReadWriteBarrier();
- _InterlockedExchange(&g_isLocked, FALSE);
-#endif
}
//-------------------------------------------------------------------------