5 // Created by Chaz McGarvey on 12/4/04.
6 // Copyright 2004 Chaz McGarvey. All rights reserved.
9 #import "SearchContext.h"
12 @implementation SearchContext
15 #pragma mark Initialization
18 * There isn't really a designated initializer because the initialization of the types
19 * of searches vary way too much.
22 - (id)initWithPID
:(pid_t
)pid searchOperator
:(TCSearchOperator
)op value
:(Variable
*)val
32 valueSize
= [value valueSize
];
34 _variableType
= [value type
];
35 _integerSign
= [value integerSign
];
37 _searchType
= TCGivenValue
;
38 compareFunc
= [self compareFunction
];
40 // allocate the memory objects which will be used during the search
41 regionCount
= VMCountRegionsWithAttributes( process
, VMREGION_READABLE | VMREGION_WRITABLE
);
42 addresses
= TCMakeArray( TC_BUFFER_SIZE
/ sizeof(TCAddress
), sizeof(TCAddress
) );
43 values
= TCMakeArray( TC_BUFFER_SIZE
/ valueSize
, valueSize
);
44 regions
= TCMakeArray( 0, sizeof(TCAddress
) );
45 perRegion
= TCMakeArray( 0, sizeof(unsigned) );
46 addressPtr
= TCArrayBytes( addresses
);
47 valuePtr
= TCArrayBytes( values
);
49 ChazLog( @
"SearchContext: varType=%i, intSign=%i, op=%i, value=%@", _variableType
, _integerSign
, _operator
, [value stringValue
] );
54 - (id)initWithLastContext
:(SearchContext
*)context searchOperator
:(TCSearchOperator
)op
63 valueSize
= TCArrayElementSize(context
->values
);
64 process
= context
->process
;
65 _variableType
= [context variableType
];
66 _integerSign
= [context integerSign
];
68 _searchType
= TCLastValue
;
69 compareFunc
= [self compareFunction
];
71 regionCount
= TCArrayElementCount( context
->regions
);
72 addresses
= TCMakeArray( TC_BUFFER_SIZE
/ sizeof(TCAddress
), sizeof(TCAddress
) );
73 values
= TCMakeArray( TC_BUFFER_SIZE
/ valueSize
, valueSize
);
74 regions
= TCMakeArray( 0, sizeof(TCAddress
) );
75 perRegion
= TCMakeArray( 0, sizeof(unsigned) );
76 lastAddresses
= context
->addresses
;
77 lastValues
= context
->values
;
78 lastRegions
= context
->regions
;
79 lastPerRegion
= context
->perRegion
;
80 addressPtr
= TCArrayBytes( addresses
);
81 valuePtr
= TCArrayBytes( values
);
82 lastAddressPtr
= TCArrayBytes( lastAddresses
);
83 lastValuePtr
= TCArrayBytes( lastValues
);
84 lastRegionPtr
= TCArrayBytes( lastRegions
);
85 lastPerRegionPtr
= TCArrayBytes( lastPerRegion
);
87 ChazLog( @
"SearchContext: varType=%i, intSign=%i, op=%i", _variableType
, _integerSign
, _operator
);
92 - (id)initWithLastContext
:(SearchContext
*)context searchOperator
:(TCSearchOperator
)op value
:(Variable
*)val
97 if ( !context ||
!val ||
([val type
] != [context variableType
]) // and search values can't be bigger than the last time.
98 ||
(([context variableType
] == TCString
) && ([val valueSize
] > TCArrayElementSize(context
->values
))) ) {
102 value
= [val retain
];
103 valueSize
= [value valueSize
];
104 process
= context
->process
;
105 _variableType
= [context variableType
];
106 _integerSign
= [context integerSign
];
108 _searchType
= TCGivenValue
;
109 compareFunc
= [self compareFunction
];
111 regionCount
= TCArrayElementCount( context
->regions
);
112 addresses
= TCMakeArray( TC_BUFFER_SIZE
/ sizeof(TCAddress
), sizeof(TCAddress
) );
113 values
= TCMakeArray( TC_BUFFER_SIZE
/ valueSize
, valueSize
);
114 regions
= TCMakeArray( 0, sizeof(TCAddress
) );
115 perRegion
= TCMakeArray( 0, sizeof(unsigned) );
116 lastAddresses
= context
->addresses
;
117 lastValues
= context
->values
;
118 lastRegions
= context
->regions
;
119 lastPerRegion
= context
->perRegion
;
120 addressPtr
= TCArrayBytes( addresses
);
121 valuePtr
= TCArrayBytes( values
);
122 lastAddressPtr
= TCArrayBytes( lastAddresses
);
123 lastValuePtr
= TCArrayBytes( lastValues
);
124 lastRegionPtr
= TCArrayBytes( lastRegions
);
125 lastPerRegionPtr
= TCArrayBytes( lastPerRegion
);
127 ChazLog( @
"SearchContext: varType=%i, intSign=%i, op=%i, value=%@", _variableType
, _integerSign
, _operator
, [value stringValue
] );
134 ChazLog( @
"SearchContext %p dealloc", self );
135 TCReleaseArray( addresses
);
136 TCReleaseArray( values
);
137 TCReleaseArray( regions
);
138 TCReleaseArray( perRegion
);
149 #pragma mark Accessors
151 - (TCVariableType
)variableType
153 return _variableType
;
156 - (TCIntegerSign
)integerSign
161 - (TCSearchOperator
)searchOperator
167 - (BOOL (*)(void const *, void const *))compareFunction
169 // here begins a very pretty collection of switch and if statements. enjoy!
170 switch ( _operator
) {
172 switch ( _variableType
) {
173 case TCFloat
: return EqualFloat
;
174 case TCDouble
: return EqualDouble
;
176 if ( _integerSign
== TCSigned
) {
177 switch ( _variableType
) {
178 case TCInt64
: return EqualInt64
;
179 case TCInt32
: return EqualInt32
;
180 case TCInt16
: return EqualInt16
;
181 case TCInt8
: return EqualInt8
;
185 switch ( _variableType
) {
186 case TCInt64
: return EqualUInt64
;
187 case TCInt32
: return EqualUInt32
;
188 case TCInt16
: return EqualUInt16
;
189 case TCInt8
: return EqualUInt8
;
194 switch ( _variableType
) {
195 case TCFloat
: return NotEqualFloat
;
196 case TCDouble
: return NotEqualDouble
;
198 if ( _integerSign
== TCSigned
) {
199 switch ( _variableType
) {
200 case TCInt64
: return NotEqualInt64
;
201 case TCInt32
: return NotEqualInt32
;
202 case TCInt16
: return NotEqualInt16
;
203 case TCInt8
: return NotEqualInt8
;
207 switch ( _variableType
) {
208 case TCInt64
: return NotEqualUInt64
;
209 case TCInt32
: return NotEqualUInt32
;
210 case TCInt16
: return NotEqualUInt16
;
211 case TCInt8
: return NotEqualUInt8
;
216 switch ( _variableType
) {
217 case TCFloat
: return LessThanFloat
;
218 case TCDouble
: return LessThanDouble
;
220 if ( _integerSign
== TCSigned
) {
221 switch ( _variableType
) {
222 case TCInt64
: return LessThanInt64
;
223 case TCInt32
: return LessThanInt32
;
224 case TCInt16
: return LessThanInt16
;
225 case TCInt8
: return LessThanInt8
;
229 switch ( _variableType
) {
230 case TCInt64
: return LessThanUInt64
;
231 case TCInt32
: return LessThanUInt32
;
232 case TCInt16
: return LessThanUInt16
;
233 case TCInt8
: return LessThanUInt8
;
238 switch ( _variableType
) {
239 case TCFloat
: return GreaterThanFloat
;
240 case TCDouble
: return GreaterThanDouble
;
242 if ( _integerSign
== TCSigned
) {
243 switch ( _variableType
) {
244 case TCInt64
: return GreaterThanInt64
;
245 case TCInt32
: return GreaterThanInt32
;
246 case TCInt16
: return GreaterThanInt16
;
247 case TCInt8
: return GreaterThanInt8
;
251 switch ( _variableType
) {
252 case TCInt64
: return GreaterThanUInt64
;
253 case TCInt32
: return GreaterThanUInt32
;
254 case TCInt16
: return GreaterThanUInt16
;
255 case TCInt8
: return GreaterThanUInt8
;
263 - (int (*)(id, unsigned))iterationFunction
265 if ( _searchType
== TCGivenValue
) {
266 if ( !lastAddresses
) {
267 if ( _variableType
== TCString
) {
268 return SearchStringIteration
;
271 return SearchIteration
;
275 if ( _variableType
== TCString
) {
276 return SearchStringIterationAgain
;
279 return SearchIterationAgain
;
283 else if ( _searchType
== TCLastValue
) {
284 if ( _variableType
== TCString
) {
285 return SearchStringIterationLastValue
;
288 return SearchIterationLastValue
;