Differences From Artifact [b25cee6a5c]:
- File
src/OFNumber.m
— part of check-in
[bc9998913a]
at
2020-08-30 16:22:17
on branch tagged-pointers
— OFNumber: Never access ivars after init
This is a requirement for tagged pointer numbers, which cannot have
ivars. (user: js, size: 22577) [annotate] [blame] [check-ins using]
To Artifact [ee11ae97a1]:
- File src/OFNumber.m — part of check-in [8c32791836] at 2020-09-04 16:39:06 on branch tagged-pointers — OFNumber: Add support for tagged pointers (user: js, size: 27313) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
38 39 40 41 42 43 44 45 46 47 48 49 50 51 | @end @interface OFNumberPlaceholder: OFNumber @end @interface OFNumberSingleton: OFNumber @end static struct { Class isa; } placeholder; #define SINGLETON(var, sel, val) \ static OFNumberSingleton *var; \ | > > > > > > > > > > > > > > > > > > > > | 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 | @end @interface OFNumberPlaceholder: OFNumber @end @interface OFNumberSingleton: OFNumber @end #ifdef OF_OBJFW_RUNTIME enum { TAG_CHAR, TAG_SHORT, TAG_INT, TAG_LONG, TAG_LONG_LONG, TAG_UNSIGNED_CHAR, TAG_UNSIGNED_SHORT, TAG_UNSIGNED_INT, TAG_UNSIGNED_LONG, TAG_UNSIGNED_LONG_LONG, }; # define TAG_BITS 4 # define TAG_MASK 0xF @interface OFTaggedPointerNumber: OFNumberSingleton @end #endif static struct { Class isa; } placeholder; #define SINGLETON(var, sel, val) \ static OFNumberSingleton *var; \ |
︙ | ︙ | |||
66 67 68 69 70 71 72 73 74 75 76 77 78 79 | SINGLETON(unsignedShortZeroNumber, initWithUnsignedShort:, 0) SINGLETON(unsignedIntZeroNumber, initWithUnsignedInt:, 0) SINGLETON(unsignedLongZeroNumber, initWithUnsignedLong:, 0) SINGLETON(unsignedLongLongZeroNumber, initWithUnsignedLongLong:, 0) SINGLETON(floatZeroNumber, initWithFloat:, 0) SINGLETON(doubleZeroNumber, initWithDouble:, 0) #undef SINGLETON static bool isUnsigned(OFNumber *number) { switch (*number.objCType) { case 'B': return true; | > > > > | 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | SINGLETON(unsignedShortZeroNumber, initWithUnsignedShort:, 0) SINGLETON(unsignedIntZeroNumber, initWithUnsignedInt:, 0) SINGLETON(unsignedLongZeroNumber, initWithUnsignedLong:, 0) SINGLETON(unsignedLongLongZeroNumber, initWithUnsignedLongLong:, 0) SINGLETON(floatZeroNumber, initWithFloat:, 0) SINGLETON(doubleZeroNumber, initWithDouble:, 0) #undef SINGLETON #ifdef OF_OBJFW_RUNTIME static int numberTag; #endif static bool isUnsigned(OFNumber *number) { switch (*number.objCType) { case 'B': return true; |
︙ | ︙ | |||
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 | } else { static of_once_t once = OF_ONCE_INIT; of_once(&once, falseNumberInit); return (id)falseNumber; } } - (instancetype)initWithChar: (signed char)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, charZeroNumberInit); return (id)charZeroNumber; } return (id)[[OFNumber of_alloc] initWithChar: value]; } - (instancetype)initWithShort: (short)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, shortZeroNumberInit); return (id)shortZeroNumber; } return (id)[[OFNumber of_alloc] initWithShort: value]; } - (instancetype)initWithInt: (int)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, intZeroNumberInit); return (id)intZeroNumber; } return (id)[[OFNumber of_alloc] initWithInt: value]; } - (instancetype)initWithLong: (long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, longZeroNumberInit); return (id)longZeroNumber; } return (id)[[OFNumber of_alloc] initWithLong: value]; } - (instancetype)initWithLongLong: (long long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, longLongZeroNumberInit); return (id)longLongZeroNumber; } return (id)[[OFNumber of_alloc] initWithLongLong: value]; } - (instancetype)initWithUnsignedChar: (unsigned char)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedCharZeroNumberInit); return (id)unsignedCharZeroNumber; } return (id)[[OFNumber of_alloc] initWithUnsignedChar: value]; } - (instancetype)initWithUnsignedShort: (unsigned short)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedShortZeroNumberInit); return (id)unsignedShortZeroNumber; } return (id)[[OFNumber of_alloc] initWithUnsignedShort: value]; } - (instancetype)initWithUnsignedInt: (unsigned int)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedIntZeroNumberInit); return (id)unsignedIntZeroNumber; } return (id)[[OFNumber of_alloc] initWithUnsignedInt: value]; } - (instancetype)initWithUnsignedLong: (unsigned long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedLongZeroNumberInit); return (id)unsignedLongZeroNumber; } return (id)[[OFNumber of_alloc] initWithUnsignedLong: value]; } - (instancetype)initWithUnsignedLongLong: (unsigned long long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedLongLongZeroNumberInit); return (id)unsignedLongLongZeroNumber; } return (id)[[OFNumber of_alloc] initWithUnsignedLongLong: value]; } - (instancetype)initWithFloat: (float)value { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | } else { static of_once_t once = OF_ONCE_INIT; of_once(&once, falseNumberInit); return (id)falseNumber; } } #ifdef __clang__ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" #endif - (instancetype)initWithChar: (signed char)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, charZeroNumberInit); return (id)charZeroNumber; } else if ((unsigned char)value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)(unsigned char)value << TAG_BITS) | TAG_CHAR); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithChar: value]; } - (instancetype)initWithShort: (short)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, shortZeroNumberInit); return (id)shortZeroNumber; } else if ((unsigned short)value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)(unsigned short)value << TAG_BITS) | TAG_SHORT); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithShort: value]; } - (instancetype)initWithInt: (int)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, intZeroNumberInit); return (id)intZeroNumber; } else if ((unsigned int)value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)(unsigned int)value << TAG_BITS) | TAG_INT); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithInt: value]; } - (instancetype)initWithLong: (long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, longZeroNumberInit); return (id)longZeroNumber; } else if ((unsigned long)value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)(unsigned long)value << TAG_BITS) | TAG_LONG); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithLong: value]; } - (instancetype)initWithLongLong: (long long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, longLongZeroNumberInit); return (id)longLongZeroNumber; } else if ((unsigned long long)value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)(unsigned long long)value << TAG_BITS) | TAG_LONG_LONG); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithLongLong: value]; } - (instancetype)initWithUnsignedChar: (unsigned char)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedCharZeroNumberInit); return (id)unsignedCharZeroNumber; } else if (value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_CHAR); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithUnsignedChar: value]; } - (instancetype)initWithUnsignedShort: (unsigned short)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedShortZeroNumberInit); return (id)unsignedShortZeroNumber; } else if (value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_SHORT); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithUnsignedShort: value]; } - (instancetype)initWithUnsignedInt: (unsigned int)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedIntZeroNumberInit); return (id)unsignedIntZeroNumber; } else if (value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_INT); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithUnsignedInt: value]; } - (instancetype)initWithUnsignedLong: (unsigned long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedLongZeroNumberInit); return (id)unsignedLongZeroNumber; } else if (value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_LONG); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithUnsignedLong: value]; } - (instancetype)initWithUnsignedLongLong: (unsigned long long)value { if (value == 0) { static of_once_t once = OF_ONCE_INIT; of_once(&once, unsignedLongLongZeroNumberInit); return (id)unsignedLongLongZeroNumber; } else if (value <= (UINTPTR_MAX >> TAG_BITS)) { id ret = objc_createTaggedPointer(numberTag, ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_LONG_LONG); if (ret != nil) return ret; } return (id)[[OFNumber of_alloc] initWithUnsignedLongLong: value]; } - (instancetype)initWithFloat: (float)value { |
︙ | ︙ | |||
270 271 272 273 274 275 276 277 278 279 280 281 282 283 | return (id)[[OFNumber of_alloc] initWithDouble: value]; } - (instancetype)initWithSerialization: (OFXMLElement *)element { return (id)[[OFNumber of_alloc] initWithSerialization: element]; } @end @implementation OFNumberSingleton - (instancetype)autorelease { return self; } | > > > | 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | return (id)[[OFNumber of_alloc] initWithDouble: value]; } - (instancetype)initWithSerialization: (OFXMLElement *)element { return (id)[[OFNumber of_alloc] initWithSerialization: element]; } #ifdef __clang__ # pragma clang diagnostic pop #endif @end @implementation OFNumberSingleton - (instancetype)autorelease { return self; } |
︙ | ︙ | |||
292 293 294 295 296 297 298 299 300 301 302 | } - (unsigned int)retainCount { return OF_RETAIN_COUNT_MAX; } @end @implementation OFNumber + (void)initialize { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > | > > > > > | 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 | } - (unsigned int)retainCount { return OF_RETAIN_COUNT_MAX; } @end #ifdef OF_OBJFW_RUNTIME @implementation OFTaggedPointerNumber - (const char *)objCType { uintptr_t value = object_getTaggedPointerValue(self); switch (value & TAG_MASK) { case TAG_CHAR: return @encode(signed char); case TAG_SHORT: return @encode(short); case TAG_INT: return @encode(int); case TAG_LONG: return @encode(long); case TAG_LONG_LONG: return @encode(long long); case TAG_UNSIGNED_CHAR: return @encode(unsigned char); case TAG_UNSIGNED_SHORT: return @encode(unsigned short); case TAG_UNSIGNED_INT: return @encode(unsigned int); case TAG_UNSIGNED_LONG: return @encode(unsigned long); case TAG_UNSIGNED_LONG_LONG: return @encode(unsigned long long); default: @throw [OFInvalidArgumentException exception]; } } # define RETURN_VALUE \ uintptr_t value = object_getTaggedPointerValue(self); \ \ switch (value & TAG_MASK) { \ case TAG_CHAR: \ return (signed char)(unsigned char)(value >> TAG_BITS); \ case TAG_SHORT: \ return (short)(unsigned short)(value >> TAG_BITS); \ case TAG_INT: \ return (int)(unsigned int)(value >> TAG_BITS); \ case TAG_LONG: \ return (long)(unsigned long)(value >> TAG_BITS); \ case TAG_LONG_LONG: \ return (long long)(unsigned long long)(value >> TAG_BITS); \ case TAG_UNSIGNED_CHAR: \ return (unsigned char)(value >> TAG_BITS); \ case TAG_UNSIGNED_SHORT: \ return (unsigned short)(value >> TAG_BITS); \ case TAG_UNSIGNED_INT: \ return (unsigned int)(value >> TAG_BITS); \ case TAG_UNSIGNED_LONG: \ return (unsigned long)(value >> TAG_BITS); \ case TAG_UNSIGNED_LONG_LONG: \ return (unsigned long long)(value >> TAG_BITS); \ default: \ @throw [OFInvalidArgumentException exception]; \ } - (long long)longLongValue { RETURN_VALUE } - (unsigned long long)unsignedLongLongValue { RETURN_VALUE } - (double)doubleValue { RETURN_VALUE } @end # undef RETURN_VALUE #endif @implementation OFNumber + (void)initialize { if (self != [OFNumber class]) return; placeholder.isa = [OFNumberPlaceholder class]; #ifdef OF_OBJFW_RUNTIME numberTag = objc_registerTaggedPointerClass([OFTaggedPointerNumber class]); #endif } + (instancetype)of_alloc { return [super alloc]; } |
︙ | ︙ | |||
551 552 553 554 555 556 557 | { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); OFString *typeString; | | | 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); OFString *typeString; if (![element.name isEqual: @"OFNumber"] || ![element.namespace isEqual: OF_SERIALIZATION_NS]) @throw [OFInvalidArgumentException exception]; typeString = [element attributeForName: @"type"].stringValue; if ([typeString isEqual: @"bool"]) { OFString *stringValue = element.stringValue; |
︙ | ︙ | |||
848 849 850 851 852 853 854 | } - (OFXMLElement *)XMLElementBySerializing { void *pool = objc_autoreleasePoolPush(); OFXMLElement *element; | | | 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | } - (OFXMLElement *)XMLElementBySerializing { void *pool = objc_autoreleasePoolPush(); OFXMLElement *element; element = [OFXMLElement elementWithName: @"OFNumber" namespace: OF_SERIALIZATION_NS stringValue: self.description]; if (*self.objCType == 'B') [element addAttributeWithName: @"type" stringValue: @"bool"]; else if (isFloat(self)) { |
︙ | ︙ |