From c0f08f202a1ce241fa4621d09e55b6ae2273f841 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Tue, 26 Jul 2011 19:05:43 +0000 Subject: [PATCH] Use the small object support in libobjc2 trunk for 31 / 61-bit signed NSNumbers on 32 / 64-bit platforms respectively. Still to do: add an NSSmallFloat equivalent for 64-bit platforms, storing a float. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33639 72102866-910b-0410-8b05-ffd578937521 --- Source/NSNumber.m | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Source/NSNumber.m b/Source/NSNumber.m index 7f13287d3..33b1ebc40 100644 --- a/Source/NSNumber.m +++ b/Source/NSNumber.m @@ -47,6 +47,7 @@ #import "Foundation/NSException.h" #import "Foundation/NSValue.h" #import "GNUstepBase/NSObject+GNUstepBase.h" +#include /* * NSNumber implementation. This matches the behaviour of Apple's @@ -345,6 +346,42 @@ return NSOrderedSame; #include "NSNumberMethods.h" @end +#ifdef OBJC_SMALL_OBJECT_SHIFT +static BOOL useSmallInt; + + +@interface NSSmallInt : NSSignedIntegerNumber @end +@implementation NSSmallInt +#undef VALUE +#define VALUE (((intptr_t)self) >> 1) +#define FORMAT @"%d" +#include "NSNumberMethods.h" ++ (void)load +{ + useSmallInt = objc_registerSmallObjectClass_np(self, 1); +} ++ (id)alloc +{ + return (id)1; +} ++ (id)allocWithZone: (NSZone*)aZone +{ + return (id)1; +} +- (id)copy +{ + return self; +} +- (id)copyWithZone: (NSZone*)aZone +{ + return self; +} +- (id)retain { return self; } +- (id)autorelease { return self; } +- (void)release { } +@end +#endif + @implementation NSNumber static Class NSNumberClass; @@ -537,6 +574,14 @@ if (aValue >= -1 && aValue <= 12)\ } CHECK_SINGLETON (aValue); +#ifdef OBJC_SMALL_OBJECT_SHIFT + if (useSmallInt && + (aValue < (INT_MAX>>OBJC_SMALL_OBJECT_SHIFT)) && + (aValue > -(INT_MAX>>OBJC_SMALL_OBJECT_SHIFT))) + { + return (id)((aValue << 1) | 1); + } +#endif n = NSAllocateObject (NSIntNumberClass, 0, 0); n->value = aValue; return AUTORELEASE(n);