1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/**Implementation for NSConcretePointerFunctions for GNUStep
Copyright (C) 2009 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: 2009
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "common.h"
#import "NSConcretePointerFunctions.h"
static void*
acquireMallocMemory(const void *item,
NSUInteger (*size)(const void *item), BOOL shouldCopy)
{
if (shouldCopy == YES)
{
NSUInteger len = (*size)(item);
void *newItem = malloc(len);
memcpy(newItem, item, len);
item = newItem;
}
return (void*)item;
}
static void*
acquireRetainedObject(const void *item,
NSUInteger (*size)(const void *item), BOOL shouldCopy)
{
if (shouldCopy == YES)
{
return [(NSObject*)item copy];
}
return [(NSObject*)item retain];
}
static void*
acquireExistingMemory(const void *item,
NSUInteger (*size)(const void *item), BOOL shouldCopy)
{
return (void*)item;
}
static NSString*
describeString(const void *item)
{
return [NSString stringWithFormat: @"%s", (char*)item];
}
static NSString*
describeInteger(const void *item)
{
return [NSString stringWithFormat: @"%"PRIdPTR, (intptr_t)item];
}
static NSString*
describeObject(const void *item)
{
return [(NSObject*)item description];
}
static NSString*
describePointer(const void *item)
{
return [NSString stringWithFormat: @"%p", item];
}
static BOOL
equalDirect(const void *item1, const void *item2,
NSUInteger (*size)(const void *item))
{
return (item1 == item2) ? YES : NO;
}
static BOOL
equalObject(const void *item1, const void *item2,
NSUInteger (*size)(const void *item))
{
return [(NSObject*)item1 isEqual: (NSObject*)item2];
}
static BOOL
equalMemory(const void *item1, const void *item2,
NSUInteger (*size)(const void *item))
{
NSUInteger s1 = (*size)(item1);
NSUInteger s2 = (*size)(item2);
return (s1 == s2 && memcmp(item1, item2, s1) == 0) ? YES : NO;
}
static BOOL
equalString(const void *item1, const void *item2,
NSUInteger (*size)(const void *item))
{
return (strcmp((const char*)item1, (const char*)item2) == 0) ? YES : NO;
}
static NSUInteger
hashDirect(const void *item, NSUInteger (*size)(const void *item))
{
return (NSUInteger)(uintptr_t)item;
}
static NSUInteger
hashObject(const void *item, NSUInteger (*size)(const void *item))
{
return [(NSObject*)item hash];
}
static NSUInteger
hashMemory(const void *item, NSUInteger (*size)(const void *item))
{
unsigned len = (*size)(item);
NSUInteger hash = 0;
while (len-- > 0)
{
hash = (hash << 5) + hash + *(const uint8_t*)item++;
}
return hash;
}
static NSUInteger
hashShifted(const void *item, NSUInteger (*size)(const void *item))
{
return ((NSUInteger)(uintptr_t)item) >> 2;
}
static NSUInteger
hashString(const void *item, NSUInteger (*size)(const void *item))
{
NSUInteger hash = 0;
while (*(const uint8_t*)item != 0)
{
hash = (hash << 5) + hash + *(const uint8_t*)item++;
}
return hash;
}
static void
relinquishMallocMemory(const void *item,
NSUInteger (*size)(const void *item))
{
free((void*)item);
}
static void
relinquishRetainedMemory(const void *item,
NSUInteger (*size)(const void *item))
{
[(NSObject*)item release];
}
@implementation NSConcretePointerFunctions
+ (id) allocWithZone: (NSZone*)zone
{
return (id) NSAllocateObject(self, 0, zone);
}
- (id) copyWithZone: (NSZone*)zone
{
return NSCopyObject(self, 0, zone);
}
- (id) initWithOptions: (NSPointerFunctionsOptions)options
{
_x.options = options;
/* First we look at the memory management options to see which function
* should be used to relinquish contents of a container with these
* options.
*/
if (options & NSPointerFunctionsZeroingWeakMemory)
{
_x.relinquishFunction = 0;
}
else if (options & NSPointerFunctionsOpaqueMemory)
{
_x.relinquishFunction = 0;
}
else if (options & NSPointerFunctionsMallocMemory)
{
_x.relinquishFunction = relinquishMallocMemory;
}
else if (options & NSPointerFunctionsMachVirtualMemory)
{
_x.relinquishFunction = relinquishMallocMemory;
}
else
{
_x.relinquishFunction = relinquishRetainedMemory;
}
/* Now we look at the personality options to determine other functions.
*/
if (options & NSPointerFunctionsOpaquePersonality)
{
_x.acquireFunction = acquireExistingMemory;
_x.descriptionFunction = describePointer;
_x.hashFunction = hashShifted;
_x.isEqualFunction = equalDirect;
}
else if (options & NSPointerFunctionsObjectPointerPersonality)
{
if (options & NSPointerFunctionsZeroingWeakMemory)
{
_x.acquireFunction = acquireExistingMemory;
}
else
{
_x.acquireFunction = acquireRetainedObject;
}
_x.descriptionFunction = describeObject;
_x.hashFunction = hashShifted;
_x.isEqualFunction = equalDirect;
}
else if (options & NSPointerFunctionsCStringPersonality)
{
_x.acquireFunction = acquireMallocMemory;
_x.descriptionFunction = describeString;
_x.hashFunction = hashString;
_x.isEqualFunction = equalString;
}
else if (options & NSPointerFunctionsStructPersonality)
{
_x.acquireFunction = acquireMallocMemory;
_x.descriptionFunction = describePointer;
_x.hashFunction = hashMemory;
_x.isEqualFunction = equalMemory;
}
else if (options & NSPointerFunctionsIntegerPersonality)
{
_x.acquireFunction = acquireExistingMemory;
_x.descriptionFunction = describeInteger;
_x.hashFunction = hashDirect;
_x.isEqualFunction = equalDirect;
}
else /* objects */
{
if (options & NSPointerFunctionsZeroingWeakMemory)
{
_x.acquireFunction = acquireExistingMemory;
}
else
{
_x.acquireFunction = acquireRetainedObject;
}
_x.descriptionFunction = describeObject;
_x.hashFunction = hashObject;
_x.isEqualFunction = equalObject;
}
return self;
}
- (void* (*)(const void *item,
NSUInteger (*size)(const void *item), BOOL shouldCopy)) acquireFunction
{
return _x.acquireFunction;
}
- (NSString *(*)(const void *item)) descriptionFunction
{
return _x.descriptionFunction;
}
- (NSUInteger (*)(const void *item,
NSUInteger (*size)(const void *item))) hashFunction
{
return _x.hashFunction;
}
- (BOOL (*)(const void *item1, const void *item2,
NSUInteger (*size)(const void *item))) isEqualFunction
{
return _x.isEqualFunction;
}
- (void (*)(const void *item,
NSUInteger (*size)(const void *item))) relinquishFunction
{
return _x.relinquishFunction;
}
- (void) setAcquireFunction: (void* (*)(const void *item,
NSUInteger (*size)(const void *item), BOOL shouldCopy))func
{
_x.acquireFunction = func;
}
- (void) setDescriptionFunction: (NSString *(*)(const void *item))func
{
_x.descriptionFunction = func;
}
- (void) setHashFunction: (NSUInteger (*)(const void *item,
NSUInteger (*size)(const void *item)))func
{
_x.hashFunction = func;
}
- (void) setIsEqualFunction: (BOOL (*)(const void *item1, const void *item2,
NSUInteger (*size)(const void *item)))func
{
_x.isEqualFunction = func;
}
- (void) setRelinquishFunction: (void (*)(const void *item,
NSUInteger (*size)(const void *item))) func
{
_x.relinquishFunction = func;
}
- (void) setSizeFunction: (NSUInteger (*)(const void *item))func
{
_x.sizeFunction = func;
}
- (void) setUsesStrongWriteBarrier: (BOOL)flag
{
_x.options &=
~(NSPointerFunctionsZeroingWeakMemory
|NSPointerFunctionsOpaqueMemory
|NSPointerFunctionsMallocMemory
|NSPointerFunctionsMachVirtualMemory);
}
- (void) setUsesWeakReadAndWriteBarriers: (BOOL)flag
{
_x.options |= NSPointerFunctionsZeroingWeakMemory;
_x.options &=
~(NSPointerFunctionsOpaqueMemory
|NSPointerFunctionsMallocMemory
|NSPointerFunctionsMachVirtualMemory);
}
- (NSUInteger (*)(const void *item)) sizeFunction
{
return _x.sizeFunction;
}
- (BOOL) usesStrongWriteBarrier
{
if ((_x.options &
(NSPointerFunctionsZeroingWeakMemory
|NSPointerFunctionsOpaqueMemory
|NSPointerFunctionsMallocMemory
|NSPointerFunctionsMachVirtualMemory)) == NSPointerFunctionsStrongMemory)
return YES;
return NO;
}
- (BOOL) usesWeakReadAndWriteBarriers
{
if (_x.options & NSPointerFunctionsZeroingWeakMemory)
return YES;
return NO;
}
@end
|