THRIFT-3788 Compatibility improvements and Win64 support
Client: Delphi
Patch: Jens Geyer

Revised previous patch.
diff --git a/lib/delphi/src/Thrift.Utils.pas b/lib/delphi/src/Thrift.Utils.pas
index 962ef54..a0bf144 100644
--- a/lib/delphi/src/Thrift.Utils.pas
+++ b/lib/delphi/src/Thrift.Utils.pas
@@ -21,16 +21,14 @@
 
 interface
 
-{$IF CompilerVersion >= 23.0}
-  {$LEGACYIFEND ON}
-{$IFEND}
+{$I Thrift.Defines.inc}
 
 uses
-{$IF CompilerVersion < 23.0}
+  {$IFDEF OLD_UNIT_NAMES}
   Classes, Windows, SysUtils, Character, SyncObjs;
-{$ELSE}
+  {$ELSE}
   System.Classes, Winapi.Windows, System.SysUtils, System.Character, System.SyncObjs;
-{$IFEND}
+  {$ENDIF}
 
 type
   IOverlappedHelper = interface
@@ -71,6 +69,9 @@
   end;
 
 
+function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand : Int64) : Int64; stdcall;
+function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; stdcall;
+
 
 implementation
 
@@ -204,32 +205,40 @@
 
 class function CharUtils.IsHighSurrogate( const c : Char) : Boolean;
 begin
-  {$IF RTLVersion  >= 28.0}  // XE7+
-  result := c.IsHighSurrogate();
+  {$IF CompilerVersion < 23.0}
+  result := Character.IsHighSurrogate( c);
   {$ELSE}
-    {$IF CompilerVersion < 23.0}
-      result := Character.IsHighSurrogate( c);
-    {$ELSE}
-      result := c.IsHighSurrogate;
-    {$IFEND}
+  result := c.IsHighSurrogate();
   {$IFEND}
 end;
 
 
 class function CharUtils.IsLowSurrogate( const c : Char) : Boolean;
 begin
-  {$IF RTLVersion  >= 28.0}  // XE7+
-  result := c.IsLowSurrogate();
+  {$IF CompilerVersion < 23.0}
+  result := Character.IsLowSurrogate( c);
   {$ELSE}
-    {$IF CompilerVersion < 23.0}
-      result := Character.IsLowSurrogate( c);
-    {$ELSE}
-      result := c.IsLowSurrogate;
-    {$IFEND}
+  result := c.IsLowSurrogate;
   {$IFEND}
 end;
 
 
+// natively available since stone age
+function InterlockedCompareExchange64;
+external KERNEL32 name 'InterlockedCompareExchange64';
+
+
+// natively available >= Vista
+// implemented this way since there are still some people running Windows XP :-(
+function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; stdcall;
+var old : Int64;
+begin
+  repeat
+    Old := Addend;
+  until (InterlockedCompareExchange64( Addend, Old + Value, Old) = Old);
+  result := Old;
+end;
+
 
 
 end.