Implemented copy methods and fixed bug in insertion that was introduced

when I switched to using NSZoneRealloc()


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3114 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-10-26 08:15:20 +00:00
parent 95a8d9f0c2
commit 7bb89f66e9

View file

@ -38,8 +38,24 @@
#include <gnustep/base/Unicode.h>
#include <gnustep/base/fast.x>
static SEL csInitSel = @selector(initWithCStringNoCopy:length:fromZone:);
static SEL msInitSel = @selector(initWithCapacity:);
static IMP csInitImp; /* designated initialiser for cString */
static IMP msInitImp; /* designated initialiser for mutable */
@implementation NSGCString
+ (void) initialize
{
static int done = 0;
if (!done)
{
done = 1;
csInitImp = [NSGCString instanceMethodForSelector: csInitSel];
msInitImp = [NSGMutableCString instanceMethodForSelector: msInitSel];
}
}
+ allocWithZone: (NSZone*)z
{
return NSAllocateObject (self, 0, z);
@ -52,7 +68,8 @@
- (void)dealloc
{
if (_zone) {
if (_zone)
{
NSZoneFree(_zone, (void*)_contents_chars);
_zone = 0;
}
@ -62,21 +79,25 @@
- (unsigned) hash
{
if (_hash == 0)
{
_hash = _fastImp._NSString_hash(self, @selector(hash));
}
return _hash;
}
/* This is the GNUstep designated initializer for this class. */
/*
* This is the GNUstep designated initializer for this class.
* NB. this does NOT change the '_hash' instance variable, so the copy
* methods can safely allocate a new object, copy the _hash into place,
* and then invoke this method to complete the copy operation.
*/
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
{
self = [super init];
if (self) {
_count = length;
_contents_chars = byteString;
_zone = byteString ? zone : 0;
}
return self;
}
@ -85,18 +106,17 @@
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
self = [super init];
if (self) {
_count = length;
_contents_chars = byteString;
if (flag) {
_zone = NSZoneFromPointer(byteString);
NSZone *z;
if (flag)
{
z = NSZoneFromPointer(byteString);
}
else {
_zone = 0;
else
{
z = 0;
}
}
return self;
return (*csInitImp)(self, csInitSel, byteString, length, z);
}
- (id) initWithCharactersNoCopy: (unichar*)chars
@ -135,7 +155,8 @@
- (void) _collectionDealloc
{
if (_zone) {
if (_zone)
{
NSZoneFree(_zone, (void*)_contents_chars);
_zone = 0;
}
@ -173,7 +194,8 @@
{
[aCoder decodeValueOfObjCType:@encode(unsigned)
at:&_count];
if (_count > 0) {
if (_count > 0)
{
_zone = fastZone(self);
_contents_chars = NSZoneMalloc(_zone, _count);
[aCoder decodeArrayOfObjCType:@encode(unsigned char)
@ -183,11 +205,99 @@
return self;
}
- copy
{
NSZone *z = NSDefaultMallocZone();
if (NSShouldRetainWithZone(self, z) == NO)
{
NSGCString *obj;
char *tmp;
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
obj->_hash = _hash;
}
return obj;
}
else
{
return [self retain];
}
}
- copyWithZone: (NSZone*)z
{
if (NSShouldRetainWithZone(self, z) == NO)
{
NSGCString *obj;
char *tmp;
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
obj->_hash = _hash;
}
return obj;
}
else
{
return [self retain];
}
}
- mutableCopy
{
NSGMutableCString *obj;
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString,
0, NSDefaultMallocZone());
if (obj)
{
obj = (*msInitImp)(obj, msInitSel, _count);
if (obj)
{
NSGCString *tmp = (NSGCString*)obj; // Same ivar layout!
memcpy(tmp->_contents_chars, _contents_chars, _count);
tmp->_count = _count;
tmp->_hash = _hash;
}
}
return obj;
}
- mutableCopyWithZone: (NSZone*)z
{
NSGMutableCString *obj;
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString, 0, z);
if (obj)
{
obj = (*msInitImp)(obj, msInitSel, _count);
if (obj)
{
NSGCString *tmp = (NSGCString*)obj; // Same ivar layout!
memcpy(tmp->_contents_chars, _contents_chars, _count);
tmp->_count = _count;
tmp->_hash = _hash;
}
}
return obj;
}
- (const char *) cString
{
char *r;
char *r = NSZoneMalloc(NSDefaultMallocZone(), _count+1);
OBJC_MALLOC(r, char, _count+1);
memcpy(r, _contents_chars, _count);
r[_count] = '\0';
[NSData dataWithBytesNoCopy:r length: _count+1];
@ -554,11 +664,28 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
/* This is the designated initializer for this class */
- initWithCapacity: (unsigned)capacity
{
[super init];
_count = 0;
_capacity = MAX(capacity, 3);
_capacity = capacity;
if (capacity)
{
_zone = fastZone(self);
_contents_chars = NSZoneMalloc(_zone, _capacity);
}
return self;
}
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
{
self = (*msInitImp)(self, msInitSel, 0);
if (self)
{
_count = length;
_capacity = length;
_contents_chars = byteString;
_zone = byteString ? zone : 0;
}
return self;
}
@ -586,6 +713,79 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
return a;
}
- copy
{
char *tmp;
NSGCString *obj;
NSZone *z = NSDefaultMallocZone();
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
NSGMutableCString *tmp = (NSGMutableCString*)obj; // Same ivar layout
tmp->_hash = _hash;
}
return obj;
}
- copyWithZone: (NSZone*)z
{
char *tmp;
NSGCString *obj;
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
NSGMutableCString *tmp = (NSGMutableCString*)obj; // Same ivar layout
tmp->_hash = _hash;
}
return obj;
}
- mutableCopy
{
NSGMutableCString *obj;
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString,
0, NSDefaultMallocZone());
if (obj)
{
obj = (*msInitImp)(obj, msInitSel, _count);
if (obj)
{
memcpy(obj->_contents_chars, _contents_chars, _count);
obj->_count = _count;
obj->_hash = _hash;
}
}
return obj;
}
- mutableCopyWithZone: (NSZone*)z
{
NSGMutableCString *obj;
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString, 0, z);
if (obj)
{
obj = (*msInitImp)(obj, msInitSel, _count);
if (obj)
{
memcpy(obj->_contents_chars, _contents_chars, _count);
obj->_count = _count;
obj->_hash = _hash;
}
}
}
- (void) deleteCharactersInRange: (NSRange)range
{
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
@ -607,7 +807,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
if (_count + c >= _capacity)
{
_capacity = MAX(_capacity*2, _count+c+1);
NSZoneRealloc(_zone, _contents_chars, _capacity);
_contents_chars = NSZoneRealloc(_zone, _contents_chars, _capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
save = _contents_chars[index+c]; // getCString will put a nul here.
@ -668,7 +868,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (id) init
{
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
};
}
/* For IndexedCollecting Protocol and other GNU libobjects conformity. */
@ -711,6 +911,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
if (_count >= _capacity)
{
_capacity *= 2;
_contents_chars =
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, 1);