/usr/include/GNUstep/GNUstepBase/GSIArray.h is in libgnustep-base-dev 1.24.0-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | /* A fast (Inline) array implementation without objc method overhead.
* Copyright (C) 1998,1999 Free Software Foundation, Inc.
*
* Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Created: Nov 1998
*
* 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
* Library 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 02111 USA. */
#import <GNUstepBase/GSVersionMacros.h>
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
#if defined(GNUSTEP_BASE_INTERNAL)
#import "Foundation/NSObject.h"
#import "Foundation/NSException.h"
#import "Foundation/NSGarbageCollector.h"
#import "Foundation/NSZone.h"
#else
#import <Foundation/NSObject.h>
#import <Foundation/NSException.h>
#import <Foundation/NSGarbageCollector.h>
#import <Foundation/NSZone.h>
#endif
/* To turn assertions on, define GSI_ARRAY_CHECKS */
#define GSI_ARRAY_CHECKS 1
#if defined(__cplusplus)
extern "C" {
#endif
/* To easily un-inline functions for debugging */
#ifndef INLINE
#define INLINE inline
#endif
#ifdef GSI_ARRAY_CHECKS
#define GSI_ARRAY_CHECK NSCAssert(array->count <= array->cap && array->old <= array->cap, NSInternalInconsistencyException)
#else
#define GSI_ARRAY_CHECK
#endif
/*
* NB. This file is intended for internal use by the GNUstep libraries
* and may change siugnificantly between releases.
* While it is unlikley to be removed from the distributiuon any time
* soon, its use by other software is not officially supported.
*
* This file should be INCLUDED in files wanting to use the GSIArray
* functions - these are all declared inline for maximum performance.
*
* The file including this one may predefine some macros to alter
* the behaviour (default macros assume the items are NSObjects
* that are to be retained in the array) ...
*
* GSI_ARRAY_RETAIN()
* Macro to retain an array item
*
* GSI_ARRAY_RELEASE()
* Macro to release the item.
*
* The next two values can be defined in order to let us optimise
* even further when either retain or release operations are not needed.
*
* GSI_ARRAY_NO_RELEASE
* Defined if no release operation is needed for an item
* GSI_ARRAY_NO_RETAIN
* Defined if no retain operation is needed for a an item
*
* The value GSI_ARRAY_EXTRA may be defined as the type of an extra
* field produced in every array. The name of this field is 'extra'.
*
* The value GSI_ARRAY_TYPE may be defined as an additional type
* which must be valid as an array element. Otherwise the types are
* controlled by the mechanism in GSUnion.h
*/
#ifdef GSI_ARRAY_NO_RETAIN
#ifdef GSI_ARRAY_RETAIN
#undef GSI_ARRAY_RETAIN
#endif
#define GSI_ARRAY_RETAIN(A, X)
#else
#ifndef GSI_ARRAY_RETAIN
#define GSI_ARRAY_RETAIN(A, X) [(X).obj retain]
#endif
#endif
#ifdef GSI_ARRAY_NO_RELEASE
#ifdef GSI_ARRAY_RELEASE
#undef GSI_ARRAY_RELEASE
#endif
#define GSI_ARRAY_RELEASE(A, X)
#else
#ifndef GSI_ARRAY_RELEASE
#define GSI_ARRAY_RELEASE(A, X) [(X).obj release]
#endif
#endif
/*
* If there is no bitmask defined to supply the types that
* may be stored in the array, default to none.
*/
#ifndef GSI_ARRAY_TYPES
#define GSI_ARRAY_TYPES 0
#endif
#ifndef GSIArrayItem
/*
* Set up the name of the union to store array elements.
*/
#ifdef GSUNION
#undef GSUNION
#endif
#define GSUNION GSIArrayItem
/*
* Set up the types that will be storable in the union.
* See 'GSUnion.h' for details.
*/
#ifdef GSUNION_TYPES
#undef GSUNION_TYPES
#endif
#define GSUNION_TYPES GSI_ARRAY_TYPES
#ifdef GSUNION_EXTRA
#undef GSUNION_EXTRA
#endif
/*
* Override extra type used in array value
*/
#ifdef GSI_ARRAY_TYPE
#define GSUNION_EXTRA GSI_ARRAY_TYPE
#endif
/*
* Generate the union typedef
*/
#if defined(GNUSTEP_BASE_INTERNAL)
#include "GNUstepBase/GSUnion.h"
#else
#include <GNUstepBase/GSUnion.h>
#endif
#endif /* #ifndef GSIArrayItem */
struct _GSIArray {
GSIArrayItem *ptr;
unsigned count;
unsigned cap;
unsigned old;
NSZone *zone;
#ifdef GSI_ARRAY_EXTRA
GSI_ARRAY_EXTRA extra;
#endif
};
typedef struct _GSIArray GSIArray_t;
typedef struct _GSIArray *GSIArray;
static INLINE unsigned
GSIArrayCapacity(GSIArray array)
{
return array->cap;
}
static INLINE unsigned
GSIArrayCount(GSIArray array)
{
return array->count;
}
static INLINE void
GSIArrayGrow(GSIArray array)
{
unsigned int next;
unsigned int size;
GSIArrayItem *tmp;
if (array->old == 0)
{
/*
* Statically initialised buffer ... copy into new heap buffer.
*/
array->old = array->cap / 2;
if (array->old < 1)
{
array->old = 1;
array->cap = 1;
}
next = array->cap + array->old;
size = next*sizeof(GSIArrayItem);
#if GS_WITH_GC
tmp = NSAllocateCollectable(size, array->zone ? NSScannedOption : 0);
#else
tmp = NSZoneMalloc(array->zone, size);
#endif
memcpy(tmp, array->ptr, array->count * sizeof(GSIArrayItem));
}
else
{
next = array->cap + array->old;
size = next*sizeof(GSIArrayItem);
#if GS_WITH_GC
tmp = NSReallocateCollectable(array->ptr, size,
array->zone ? NSScannedOption : 0);
#else
tmp = NSZoneRealloc(array->zone, array->ptr, size);
#endif
}
if (tmp == 0)
{
[NSException raise: NSMallocException
format: @"failed to grow GSIArray"];
}
array->ptr = tmp;
array->old = array->cap;
array->cap = next;
}
static INLINE void
GSIArrayGrowTo(GSIArray array, unsigned next)
{
unsigned int size;
GSIArrayItem *tmp;
if (next < array->count)
{
[NSException raise: NSInvalidArgumentException
format: @"attempt to shrink below count"];
}
size = next*sizeof(GSIArrayItem);
if (array->old == 0)
{
/*
* Statically initialised buffer ... copy into new heap buffer.
*/
#if GS_WITH_GC
tmp = NSAllocateCollectable(size, array->zone ? NSScannedOption : 0);
#else
tmp = NSZoneMalloc(array->zone, size);
#endif
memcpy(tmp, array->ptr, array->count * sizeof(GSIArrayItem));
}
else
{
#if GS_WITH_GC
tmp = NSReallocateCollectable(array->ptr, size,
array->zone ? NSScannedOption : 0);
#else
tmp = NSZoneRealloc(array->zone, array->ptr, size);
#endif
}
if (tmp == 0)
{
[NSException raise: NSMallocException
format: @"failed to grow GSIArray"];
}
array->ptr = tmp;
array->old = (array->cap > 0 ? array->cap : 1);
array->cap = next;
}
static INLINE void
GSIArrayInsertItem(GSIArray array, GSIArrayItem item, unsigned index)
{
unsigned int i;
GSI_ARRAY_CHECK;
GSI_ARRAY_RETAIN(array, item);
if (array->count == array->cap)
{
GSIArrayGrow(array);
}
for (i = array->count++; i > index; i--)
{
array->ptr[i] = array->ptr[i-1];
}
array->ptr[i] = item;
GSI_ARRAY_CHECK;
}
static INLINE void
GSIArrayInsertItemNoRetain(GSIArray array, GSIArrayItem item, unsigned index)
{
unsigned int i;
GSI_ARRAY_CHECK;
if (array->count == array->cap)
{
GSIArrayGrow(array);
}
for (i = array->count++; i > index; i--)
{
array->ptr[i] = array->ptr[i-1];
}
array->ptr[i] = item;
GSI_ARRAY_CHECK;
}
static INLINE void
GSIArrayAddItem(GSIArray array, GSIArrayItem item)
{
GSI_ARRAY_CHECK;
GSI_ARRAY_RETAIN(array, item);
if (array->count == array->cap)
{
GSIArrayGrow(array);
}
array->ptr[array->count++] = item;
GSI_ARRAY_CHECK;
}
static INLINE void
GSIArrayAddItemNoRetain(GSIArray array, GSIArrayItem item)
{
GSI_ARRAY_CHECK;
if (array->count == array->cap)
{
GSIArrayGrow(array);
}
array->ptr[array->count++] = item;
GSI_ARRAY_CHECK;
}
/*
* The comparator function takes two items as arguments, the first is the
* item to be added, the second is the item already in the array.
* The function should return NSOrderedAscending if the item to be
* added is 'less than' the item in the array, NSOrderedDescending
* if it is greater, and NSOrderedSame if it is equal.
*/
static INLINE unsigned
GSIArraySearch(GSIArray array, GSIArrayItem item,
NSComparisonResult (*sorter)(GSIArrayItem, GSIArrayItem))
{
unsigned int upper = array->count;
unsigned int lower = 0;
unsigned int index;
/*
* Binary search for an item equal to the one to be inserted.
* Only for sorted array !
*/
for (index = upper/2; upper != lower; index = (upper+lower)/2)
{
NSComparisonResult comparison;
comparison = (*sorter)(item, (array->ptr[index]));
if (comparison == NSOrderedAscending)
{
upper = index;
}
else if (comparison == NSOrderedDescending)
{
lower = index + 1;
}
else
{
break;
}
}
return index;
}
static INLINE unsigned
GSIArrayInsertionPosition(GSIArray array, GSIArrayItem item,
NSComparisonResult (*sorter)(GSIArrayItem, GSIArrayItem))
{
unsigned int index;
index = GSIArraySearch(array,item,sorter);
/*
* Now skip past any equal items so the insertion point is AFTER any
* items that are equal to the new one.
*/
while (index < array->count
&& (*sorter)(item, (array->ptr[index])) != NSOrderedAscending)
{
index++;
}
#ifdef GSI_ARRAY_CHECKS
NSCAssert(index <= array->count, NSInternalInconsistencyException);
#endif
return index;
}
#ifdef GSI_ARRAY_CHECKS
static INLINE void
GSIArrayCheckSort(GSIArray array,
NSComparisonResult (*sorter)(GSIArrayItem, GSIArrayItem))
{
unsigned int i;
for (i = 1; i < array->count; i++)
{
#ifdef GSI_ARRAY_CHECKS
NSCAssert(((*sorter)(array->ptr[i-1], array->ptr[i])
!= NSOrderedDescending), NSInvalidArgumentException);
#endif
}
}
#endif
static INLINE void
GSIArrayInsertSorted(GSIArray array, GSIArrayItem item,
NSComparisonResult (*sorter)(GSIArrayItem, GSIArrayItem))
{
unsigned int index;
#ifdef GSI_ARRAY_CHECKS
GSIArrayCheckSort(array, sorter);
#endif
index = GSIArrayInsertionPosition(array, item, sorter);
GSIArrayInsertItem(array, item, index);
#ifdef GSI_ARRAY_CHECKS
GSIArrayCheckSort(array, sorter);
#endif
}
static INLINE void
GSIArrayInsertSortedNoRetain(GSIArray array, GSIArrayItem item,
NSComparisonResult (*sorter)(GSIArrayItem, GSIArrayItem))
{
unsigned int index;
#ifdef GSI_ARRAY_CHECKS
GSIArrayCheckSort(array, sorter);
#endif
index = GSIArrayInsertionPosition(array, item, sorter);
GSIArrayInsertItemNoRetain(array, item, index);
#ifdef GSI_ARRAY_CHECKS
GSIArrayCheckSort(array, sorter);
#endif
}
static INLINE void
GSIArrayRemoveItemAtIndex(GSIArray array, unsigned index)
{
#if defined(GSI_ARRAY_NO_RELEASE)
# ifdef GSI_ARRAY_CHECKS
NSCAssert(index < array->count, NSInvalidArgumentException);
# endif
while (++index < array->count)
array->ptr[index-1] = array->ptr[index];
array->count--;
# if GS_WITH_GC
array->ptr[array->count] = (GSIArrayItem)(NSUInteger)0;
# endif
#else
GSIArrayItem tmp;
# ifdef GSI_ARRAY_CHECKS
NSCAssert(index < array->count, NSInvalidArgumentException);
# endif
tmp = array->ptr[index];
while (++index < array->count)
array->ptr[index-1] = array->ptr[index];
array->count--;
# if GS_WITH_GC
array->ptr[array->count] = (GSIArrayItem)(NSUInteger)0;
# endif
GSI_ARRAY_RELEASE(array, tmp);
#endif
}
static INLINE void
GSIArrayRemoveLastItem(GSIArray array)
{
#ifdef GSI_ARRAY_CHECKS
NSCAssert(array->count, NSInvalidArgumentException);
#endif
array->count--;
#if !defined(GSI_ARRAY_NO_RELEASE)
GSI_ARRAY_RELEASE(array, array->ptr[array->count]);
#if GS_WITH_GC
array->ptr[array->count] = (GSIArrayItem)(NSUInteger)0;
#endif
#endif
}
static INLINE void
GSIArrayRemoveItemAtIndexNoRelease(GSIArray array, unsigned index)
{
#ifdef GSI_ARRAY_CHECKS
NSCAssert(index < array->count, NSInvalidArgumentException);
#endif
while (++index < array->count)
array->ptr[index-1] = array->ptr[index];
array->count--;
#if GS_WITH_GC && !defined(GSI_ARRAY_NO_RELEASE)
array->ptr[array->count] = (GSIArrayItem)(NSUInteger)0;
#endif
}
static INLINE void
GSIArraySetItemAtIndex(GSIArray array, GSIArrayItem item, unsigned index)
{
#if defined(GSI_ARRAY_NO_RELEASE)
# ifdef GSI_ARRAY_CHECKS
NSCAssert(index < array->count, NSInvalidArgumentException);
# endif
GSI_ARRAY_RETAIN(array, item);
array->ptr[index] = item;
#else
GSIArrayItem tmp;
# ifdef GSI_ARRAY_CHECKS
NSCAssert(index < array->count, NSInvalidArgumentException);
# endif
tmp = array->ptr[index];
GSI_ARRAY_RETAIN(array, item);
array->ptr[index] = item;
GSI_ARRAY_RELEASE(array, tmp);
#endif
}
/*
* For direct access ... unsafe if you change the array in any way while
* examining the contents of this buffer.
*/
static INLINE GSIArrayItem *
GSIArrayItems(GSIArray array)
{
return array->ptr;
}
static INLINE GSIArrayItem
GSIArrayItemAtIndex(GSIArray array, unsigned index)
{
#ifdef GSI_ARRAY_CHECKS
NSCAssert(index < array->count, NSInvalidArgumentException);
#endif
return array->ptr[index];
}
static INLINE GSIArrayItem
GSIArrayLastItem(GSIArray array)
{
#ifdef GSI_ARRAY_CHECKS
NSCAssert(array->count, NSInvalidArgumentException);
#endif
return array->ptr[array->count-1];
}
static INLINE void
GSIArrayClear(GSIArray array)
{
if (array->ptr)
{
#if !GS_WITH_GC
/*
* Only free memory if it was dynamically initialised (old > 0)
*/
if (array->old > 0)
{
NSZoneFree(array->zone, (void*)array->ptr);
}
#endif
array->ptr = 0;
array->cap = 0;
}
}
static INLINE void
GSIArrayRemoveItemsFromIndex(GSIArray array, unsigned index)
{
if (index < array->count)
{
#ifndef GSI_ARRAY_NO_RELEASE
while (array->count-- > index)
{
GSI_ARRAY_RELEASE(array, array->ptr[array->count]);
#if GS_WITH_GC
array->ptr[array->count] = (GSIArrayItem)(NSUInteger)0;
#endif
}
#endif
array->count = index;
}
}
static INLINE void
GSIArrayRemoveAllItems(GSIArray array)
{
#ifndef GSI_ARRAY_NO_RELEASE
while (array->count--)
{
GSI_ARRAY_RELEASE(array, array->ptr[array->count]);
#if GS_WITH_GC
array->ptr[array->count] = (GSIArrayItem)(NSUInteger)0;
#endif
}
#endif
array->count = 0;
}
static INLINE void
GSIArrayEmpty(GSIArray array)
{
GSIArrayRemoveAllItems(array);
GSIArrayClear(array);
}
static INLINE GSIArray
GSIArrayInitWithZoneAndCapacity(GSIArray array, NSZone *zone, size_t capacity)
{
unsigned int size;
array->zone = zone;
array->count = 0;
if (capacity < 2)
capacity = 2;
array->cap = capacity;
array->old = capacity/2;
size = capacity*sizeof(GSIArrayItem);
#if GS_WITH_GC
array->ptr = (GSIArrayItem*)NSAllocateCollectable(size,
array->zone ? NSScannedOption : 0);
#else
array->ptr = (GSIArrayItem*)NSZoneMalloc(zone, size);
#endif
return array;
}
static INLINE GSIArray
GSIArrayInitWithZoneAndStaticCapacity(GSIArray array, NSZone *zone,
size_t capacity, GSIArrayItem *buffer)
{
array->zone = zone;
array->count = 0;
array->cap = capacity;
array->old = 0;
array->ptr = buffer;
return array;
}
static INLINE GSIArray
GSIArrayCopyWithZone(GSIArray array, NSZone *zone)
{
unsigned int i;
GSIArray new;
#if GS_WITH_GC
new = NSAllocateCollectable(sizeof(GSIArray_t), NSScannedOption);
#else
new = NSZoneMalloc(zone, sizeof(GSIArray_t));
#endif
GSIArrayInitWithZoneAndCapacity(new, zone, array->count);
for (i = 0; i < array->count; i++)
{
GSI_ARRAY_RETAIN(array, array->ptr[i]);
new->ptr[new->count++] = array->ptr[i];
}
return new;
}
#if defined(__cplusplus)
}
#endif
#endif /* OS_API_VERSION(GS_API_NONE,GS_API_NONE) */
|