Jamoma API  0.6.0.a19
CATink.h
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #if !defined(__CATink_h__)
42 #define __CATink_h__
43 
44 //=============================================================================
45 // Includes
46 //=============================================================================
47 
48 // System Includes
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50  #include <CoreServices/CoreServices.h>
51 #else
52  #include <CoreServices.h>
53 #endif
54 
55 
56 //=============================================================================
57 // CATink
58 //
59 // A Tink is a small jump island that can make one function appear as if it
60 // has many addresses. Note that Tinks are not supported for 64 bit environments
61 // as the APIs that made tinks useful have been replaced by APIs that do away
62 // with all the reasons for using a tink.
63 //=============================================================================
64 
65 template <class F>
66 class CATink
67 {
68 
69 public:
70  CATink(F proc) { Set(proc); }
71 
72  ~CATink() { Set((F)0xDEADDEAD); } // jump to an obviously bad (odd) address if accessed after destruction
73 
74 #if TARGET_CPU_PPC
75  void Set(F proc)
76  {
77  /*
78  lis r11,0x1234
79  00000000: 3D601234 lis r11,4660
80  ori r11,r11,0x5678
81  00000004: 616B5678 ori r11,r11,$5678
82  mtctr r11
83  00000008: 7D6903A6 mtctr r11
84  bctr
85  0000000C: 4E800420 bctr
86  */
87  UInt32 p = UInt32(proc);
88  mCode[0] = 0x3D600000 | (p >> 16);
89  mCode[1] = 0x616B0000 | (p & 0xFFFF);
90  mCode[2] = 0x7D6903A6;
91  mCode[3] = 0x4E800420;
92  MakeDataExecutable(mCode, sizeof(mCode));
93  }
94 
95  operator F () { return F(mCode); }
96 
97 private:
98  UInt32 mCode[4];
99 #elif TARGET_CPU_X86
100  void Set(F proc)
101  {
102  /*
103  <tink>: push $0x12345678
104  <tink+5>: ret
105  <tink>: 0x34567868
106  <tink+4>: 0x00e8c312
107  */
108  UInt32 p = UInt32(proc);
109  mCode[0] = ((p & 0xFFFFFF) << 8) | 0x00000068;
110  mCode[1] = 0xCCCCC300 | (p >> 24);
111  MakeDataExecutable(mCode, sizeof(mCode));
112  }
113 
114  operator F () { return F(&mCode[0]); }
115 
116 private:
117  UInt32 mCode[2];
118 #else
119  #warning: CPU not directly supported by CATink
120 
121  // For other CPU's, just wrap the function pointer for now.
122  // this bypasses what we're trying to accomplish with the Tink
123  // (multiple unique "instances" of a function), but it will at
124  // least compile and run.
125 
126  void Set(F proc) { mProcPtr = proc; }
127 
128  operator F () { return mProcPtr; }
129 private:
130  F mProcPtr;
131 #endif
132 
133 // Tinks cannot be allocated in STL Containers, so we make a few key methods private
134 private:
135  CATink(){}
136  CATink(const CATink&){}
137  CATink& operator=(const CATink&){ return *this;}
138 };
139 
140 #endif