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
This commit is contained in:
David Chisnall 2011-07-26 19:05:43 +00:00
parent 9895dac736
commit c0f08f202a

View file

@ -47,6 +47,7 @@
#import "Foundation/NSException.h"
#import "Foundation/NSValue.h"
#import "GNUstepBase/NSObject+GNUstepBase.h"
#include <objc/runtime.h>
/*
* 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);