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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
/**Implementation for NSPointerArray 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., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#import "common.h"
#import "Foundation/NSPointerArray.h"
#import "GNUstepBase/GSObjCRuntime.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSException.h"
#import "Foundation/NSValue.h"
#import "Foundation/NSKeyedArchiver.h"
#import "GSPrivate.h"
#import "NSConcretePointerFunctions.h"
static Class abstractClass = Nil;
static Class concreteClass = Nil;
@interface NSConcretePointerArray : NSPointerArray
{
PFInfo _pf;
NSUInteger _count;
void **_contents;
unsigned _capacity;
unsigned _grow_factor;
unsigned long _version;
}
@end
@implementation NSPointerArray
+ (id) allocWithZone: (NSZone*)z
{
if (abstractClass == self)
{
return NSAllocateObject(concreteClass, 0, z);
}
return [super allocWithZone: z];
}
+ (void) initialize
{
if (abstractClass == Nil)
{
abstractClass = [NSPointerArray class];
concreteClass = [NSConcretePointerArray class];
}
}
+ (id) pointerArrayWithOptions: (NSPointerFunctionsOptions)options
{
return AUTORELEASE([[self alloc] initWithOptions: options]);
}
+ (id) pointerArrayWithPointerFunctions: (NSPointerFunctions *)functions
{
return AUTORELEASE([[self alloc] initWithPointerFunctions: functions]);
}
+ (id) strongObjectsPointerArray
{
return [self pointerArrayWithOptions: NSPointerFunctionsObjectPersonality |
NSPointerFunctionsStrongMemory];
}
+ (id) weakObjectsPointerArray
{
return [self pointerArrayWithOptions: NSPointerFunctionsObjectPersonality |
NSPointerFunctionsWeakMemory];
}
- (void) compact
{
[self subclassResponsibility: _cmd];
}
- (id) copyWithZone: (NSZone*)zone
{
[self subclassResponsibility: _cmd];
return nil;
}
- (NSUInteger) count
{
[self subclassResponsibility: _cmd];
return 0;
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[self subclassResponsibility: _cmd];
}
- (id) init
{
return [self initWithOptions: 0];
}
- (id) initWithCoder: (NSCoder*)aCoder
{
[self subclassResponsibility: _cmd];
return nil;
}
- (id) initWithOptions: (NSPointerFunctionsOptions)options
{
NSPointerFunctions *functions;
functions = [NSPointerFunctions pointerFunctionsWithOptions: options];
return [self initWithPointerFunctions: functions];
}
- (id) initWithPointerFunctions: (NSPointerFunctions*)functions
{
[self subclassResponsibility: _cmd];
return nil;
}
- (BOOL) isEqual: (id)other
{
NSUInteger count;
if (other == self)
{
return YES;
}
if ([other isKindOfClass: abstractClass] == NO)
{
return NO;
}
if ([other hash] != [self hash])
{
return NO;
}
count = [self count];
while (count-- > 0)
{
// FIXME
}
return YES;
}
- (void) addPointer: (void*)pointer
{
[self insertPointer: pointer atIndex: [self count]];
}
- (void) insertPointer: (void*)pointer atIndex: (NSUInteger)index
{
[self subclassResponsibility: _cmd];
}
- (void*) pointerAtIndex: (NSUInteger)index
{
[self subclassResponsibility: _cmd];
return 0;
}
- (NSPointerFunctions*) pointerFunctions
{
[self subclassResponsibility: _cmd];
return nil;
}
- (void) removePointerAtIndex: (NSUInteger)index
{
[self subclassResponsibility: _cmd];
}
- (void) replacePointerAtIndex: (NSUInteger)index withPointer: (void*)item
{
[self subclassResponsibility: _cmd];
}
- (void) setCount: (NSUInteger)count
{
[self subclassResponsibility: _cmd];
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (__unsafe_unretained id[])stackbuf
count: (NSUInteger)len
{
NSInteger count;
state->mutationsPtr = state->mutationsPtr;
count = MIN(len, [self count] - state->state);
if (count > 0)
{
IMP imp = [self methodForSelector: @selector(pointerAtIndex:)];
int p = state->state;
int i;
for (i = 0; i < count; i++, p++)
{
stackbuf[i] = (*imp)(self, @selector(pointerAtIndex:), p);
}
state->state += count;
}
else
{
count = 0;
}
state->itemsPtr = stackbuf;
return count;
}
@end
@implementation NSPointerArray (NSArrayConveniences)
+ (id) pointerArrayWithStrongObjects
{
GSOnceMLog(@"Garbage Collection no longer supported."
@" Using +strongObjectsPointerArray");
return [self strongObjectsPointerArray];
}
+ (id) pointerArrayWithWeakObjects
{
GSOnceMLog(@"Garbage Collection no longer supported."
@" Using +weakObjectsPointerArray");
return [self weakObjectsPointerArray];
}
- (NSArray*) allObjects
{
[self subclassResponsibility: _cmd];
return nil;
}
@end
@implementation NSConcretePointerArray
- (void) _raiseRangeExceptionWithIndex: (NSUInteger)index from: (SEL)sel
{
NSDictionary *info;
NSException *exception;
NSString *reason;
info = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInteger: index], @"Index",
[NSNumber numberWithUnsignedInteger: _count], @"Count",
self, @"Array", nil, nil];
reason = [NSString stringWithFormat:
@"Index %"PRIuPTR" is out of range %"PRIuPTR" (in '%@')",
index, _count, NSStringFromSelector(sel)];
exception = [NSException exceptionWithName: NSRangeException
reason: reason
userInfo: info];
[exception raise];
}
- (NSArray*) allObjects
{
NSUInteger i;
NSUInteger c = 0;
for (i = 0; i < _count; i++)
{
if (pointerFunctionsRead(&_pf, &_contents[i]) != 0)
{
c++;
}
}
if (0 == c)
{
return [NSArray array];
}
else
{
GSMutableArray *a = [GSMutableArray arrayWithCapacity: c];
for (i = 0; i < _count; i++)
{
id obj = pointerFunctionsRead(&_pf, &_contents[i]);
if (obj != 0)
{
[a addObject: obj];
}
}
return GS_IMMUTABLE(a);
}
}
- (void) compact
{
NSUInteger insert = 0;
NSUInteger i;
_version++;
/* We can't use memmove here for __weak pointers, because that would omit the
* required read barriers. We could use objc_memmoveCollectable() for strong
* pointers, but we may as well use the same code path for everything
*/
for (i = 0 ; i < _count; i++)
{
id obj = pointerFunctionsRead(&_pf, &_contents[i]);
/* If this object is not nil, but at least one before it has been, then
* move it back to the correct location.
*/
if (nil != obj && i != insert)
{
pointerFunctionsAssign(&_pf, &_contents[insert++], obj);
}
}
_count = insert;
_version++;
}
- (id) copyWithZone: (NSZone*)zone
{
NSConcretePointerArray *c;
unsigned i;
c = (NSConcretePointerArray*)NSCopyObject(self, 0, NSDefaultMallocZone());
c->_capacity = c->_count;
c->_grow_factor = c->_capacity/2;
c->_contents = NSZoneCalloc([self zone], _count, sizeof(id));
for (i = 0; i < _count; i++)
{
NSLog(@"Copying %d, %p", i, _contents[i]);
pointerFunctionsAssign(&_pf, &c->_contents[i],
pointerFunctionsAcquire(&_pf,
pointerFunctionsRead(&_pf, &_contents[i])));
}
return c;
}
- (NSUInteger) count
{
return _count;
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (__unsafe_unretained id[])stackbuf
count: (NSUInteger)len
{
state->mutationsPtr = &_version;
return [super countByEnumeratingWithState: state
objects: stackbuf
count: len];
}
- (void) dealloc
{
int i;
[self finalize];
/* For weak memory, we must zero all of the elements, or the runtime will
* keep pointers to them lying around. For strong memory, we must release
* things or they will leak.
*/
for (i = 0; i < _count; i++)
{
pointerFunctionsRelinquish(&_pf, &_contents[i]);
}
if (_contents != 0)
{
NSZoneFree([self zone], _contents);
}
[super dealloc];
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
/* FIXME ... how can we meaningfully encode the pointer functions???
*/
[self notImplemented: _cmd];
if ([aCoder allowsKeyedCoding])
{
[super encodeWithCoder: aCoder];
}
else
{
/* For performace we encode directly ... must exactly match the
* superclass implemenation. */
[aCoder encodeValueOfObjCType: @encode(NSUInteger)
at: &_count];
if (_count > 0)
{
[aCoder encodeArrayOfObjCType: @encode(id)
count: _count
at: _contents];
}
}
}
- (NSUInteger) hash
{
return _count;
}
- (id) initWithCoder: (NSCoder*)aCoder
{
/* FIXME ... how can we meaningfully encode the pointer functions???
*/
[self notImplemented: _cmd];
if ([aCoder allowsKeyedCoding])
{
self = [super initWithCoder: aCoder];
}
else
{
/* for performance, we decode directly into memory rather than
* using the superclass method. Must exactly match superclass. */
[aCoder decodeValueOfObjCType: @encode(NSUInteger)
at: &_count];
if (_count > 0)
{
_contents = NSZoneCalloc([self zone], _count, sizeof(id));
if (_contents == 0)
{
[NSException raise: NSMallocException
format: @"Unable to make array"];
}
[aCoder decodeArrayOfObjCType: @encode(id)
count: _count
at: _contents];
}
}
return self;
}
- (id) initWithOptions: (NSPointerFunctionsOptions)options
{
NSConcretePointerFunctions *f;
f = [[NSConcretePointerFunctions alloc] initWithOptions: options];
self = [self initWithPointerFunctions: f];
[f release];
return self;
}
- (id) initWithPointerFunctions: (NSPointerFunctions*)functions
{
if (![functions isKindOfClass: [NSConcretePointerFunctions class]])
{
static NSConcretePointerFunctions *defaultFunctions = nil;
if (defaultFunctions == nil)
{
defaultFunctions
= [[NSConcretePointerFunctions alloc] initWithOptions: 0];
}
functions = defaultFunctions;
}
memcpy(&_pf, &((NSConcretePointerFunctions*)functions)->_x, sizeof(_pf));
return self;
}
- (void) insertPointer: (void*)pointer atIndex: (NSUInteger)index
{
NSUInteger i;
if (index > _count)
{
[self _raiseRangeExceptionWithIndex: index from: _cmd];
}
i = _count;
[self setCount: _count + 1];
while (i > index)
{
pointerFunctionsMove(&_pf, _contents+i, _contents + i-1);
i--;
}
pointerFunctionsAssign(&_pf, &_contents[index],
pointerFunctionsAcquire(&_pf, pointer));
}
- (BOOL) isEqual: (id)other
{
NSUInteger count;
if (other == self)
{
return YES;
}
if ([other isKindOfClass: abstractClass] == NO)
{
return NO;
}
if ([other hash] != [self hash])
{
return NO;
}
count = [self count];
while (count-- > 0)
{
if (pointerFunctionsEqual(&_pf,
pointerFunctionsRead(&_pf, &_contents[count]),
[other pointerAtIndex: count]) == NO)
{
return NO;
}
}
return YES;
}
- (void*) pointerAtIndex: (NSUInteger)index
{
if (index >= _count)
{
[self _raiseRangeExceptionWithIndex: index from: _cmd];
}
return pointerFunctionsRead(&_pf, &_contents[index]);
}
- (NSPointerFunctions*) pointerFunctions
{
NSConcretePointerFunctions *pf = [NSConcretePointerFunctions new];
pf->_x = _pf;
return [pf autorelease];
}
- (void) removePointerAtIndex: (NSUInteger)index
{
_version++;
if (index >= _count)
{
[self _raiseRangeExceptionWithIndex: index from: _cmd];
}
pointerFunctionsRelinquish(&_pf, &_contents[index]);
while (++index < _count)
{
pointerFunctionsMove(&_pf, &_contents[index-1], &_contents[index]);
}
_contents[--_count] = NULL;
_version++;
}
- (void) replacePointerAtIndex: (NSUInteger)index withPointer: (void*)item
{
_version++;
if (index >= _count)
{
[self _raiseRangeExceptionWithIndex: index from: _cmd];
}
pointerFunctionsReplace(&_pf, &_contents[index], item);
_version++;
}
#define ZEROING 0
- (void) setCount: (NSUInteger)count
{
_version++;
if (count > _count)
{
#if ZEROING
NSUInteger index = _count;
#endif
_count = count;
if (_count >= _capacity)
{
void **ptr;
size_t size;
size_t new_cap = _capacity;
size_t new_gf = _grow_factor ? _grow_factor : 2;
while (new_cap + new_gf < _count)
{
new_cap += new_gf;
new_gf = new_cap/2;
}
size = (new_cap + new_gf)*sizeof(void*);
new_cap += new_gf;
new_gf = new_cap / 2;
#if ZEROING
/* The objc2 API for zeroing weak references passes the addresses of
* pointers, so an implementation could zero the reference when the
* associated object is deallocated. This can potentially cause an
* issue if a chunk of memory cntaining a weak reference is returned
* to the heap and the runtime zeros part of it after it has been
* re-used. To be safe in that case we must move the weak references
* explicitly before returning memry to the heap.
*/
ptr = NSZoneMalloc([self zone], size);
if (0 == ptr)
{
[NSException raise: NSMallocException
format: @"Unable to grow array"];
}
memset(ptr, '\0', size);
if (_contents)
{
while (index-- > 0)
{
pointerFunctionsMove(&_pf, ptr + index, _contents + index);
}
NSZoneFree([self zone], _contents);
}
#else
/* The gnustep libobjc2 implementation of the weak reference methods
* does not zero the memory location until/unless something tries to
* load a weakly referenced object from it, so it is safe to simply
* copy the array.
*/
ptr = NSZoneRealloc([self zone], _contents, size);
if (0 == ptr)
{
[NSException raise: NSMallocException
format: @"Unable to grow array"];
}
memset(ptr + _capacity, '\0',
(new_cap - _capacity) * sizeof(void*));
#endif
_contents = ptr;
_capacity = new_cap;
_grow_factor = new_gf;
}
}
else
{
while (count < _count)
{
_count--;
pointerFunctionsRelinquish(&_pf, &_contents[_count]);
}
}
_version++;
}
@end
|