2010-09-11 08:35:25 +00:00
|
|
|
/* Implementation of Objective C NeXT-compatible Storage object
|
2010-09-11 10:03:41 +00:00
|
|
|
Copyright (C) 1993,1994, 1996 Free Software Foundation, Inc.
|
2010-09-11 08:35:25 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
Written by: Kresten Krab Thorup <krab@iesd.auc.dk>
|
|
|
|
Dept. of Mathematics and Computer Science, Aalborg U., Denmark
|
2010-09-11 08:35:25 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
2010-09-11 08:35:25 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library 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.
|
2010-09-11 08:35:25 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
2010-09-11 08:35:25 +00:00
|
|
|
|
|
|
|
/* #include <config.h> */
|
|
|
|
#include "Storage.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define GNU_STORAGE_NTH(x,N) \
|
2010-09-11 10:03:41 +00:00
|
|
|
({ GNUStorageId* __s=(GNUStorageId*)(x); \
|
|
|
|
(void*)(((char*)__s->dataPtr)+(__s->elementSize*(N))); })
|
2010-09-11 08:35:25 +00:00
|
|
|
#define STORAGE_NTH(N) GNU_STORAGE_NTH (self, N)
|
|
|
|
|
|
|
|
typedef struct {
|
2010-09-11 10:03:41 +00:00
|
|
|
@defs (Storage)
|
2010-09-11 08:35:25 +00:00
|
|
|
} GNUStorageId;
|
|
|
|
|
|
|
|
@implementation Storage
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
+initialize
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if (self ==[Storage class])
|
|
|
|
[self setVersion:0]; /* beta release */
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// INITIALIZING, FREEING;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-initCount: (NSUInteger) numSlots elementSize: (NSUInteger) sizeInBytes description:(const char *)
|
|
|
|
elemDesc;
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
[super init];
|
|
|
|
numElements = numSlots;
|
|
|
|
maxElements = (numSlots > 0) ? numSlots : 1;
|
|
|
|
elementSize = sizeInBytes;
|
|
|
|
description = elemDesc;
|
|
|
|
dataPtr = (void *) objc_malloc (maxElements * elementSize);
|
|
|
|
bzero (dataPtr, numElements * elementSize);
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-init
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
return[self initCount: 1 elementSize:sizeof (id)
|
|
|
|
description:@encode (id)];
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-(void) dealloc
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if (dataPtr)
|
|
|
|
free (dataPtr);
|
|
|
|
[super dealloc];
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-(const char *) description
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
return description;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// COPYING;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-copy
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
Storage *c =[super copy];
|
|
|
|
|
|
|
|
c->dataPtr = (void *) objc_malloc (maxElements * elementSize);
|
|
|
|
memcpy (c->dataPtr, dataPtr, numElements * elementSize);
|
|
|
|
return c;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// COMPARING TWO STORAGES;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-(BOOL) isEqual:anObject
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if ([anObject isKindOfClass:[Storage class]]
|
|
|
|
&&[anObject count] ==[self count]
|
|
|
|
&& !memcmp (((GNUStorageId *) anObject)->dataPtr,
|
|
|
|
dataPtr, numElements * elementSize))
|
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-09-11 08:35:25 +00:00
|
|
|
// MANAGING THE STORAGE CAPACITY;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
static inline void
|
|
|
|
_makeRoomForAnotherIfNecessary (Storage * self)
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if (self->numElements == self->maxElements) {
|
|
|
|
self->maxElements *= 2;
|
|
|
|
self->dataPtr = (void *)
|
|
|
|
objc_realloc (self->dataPtr, self->maxElements * self->elementSize);
|
|
|
|
}
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
static inline void
|
|
|
|
_shrinkIfDesired (Storage * self)
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if (self->numElements < (self->maxElements / 2)) {
|
|
|
|
self->maxElements /= 2;
|
|
|
|
self->dataPtr = (void *)
|
|
|
|
objc_realloc (self->dataPtr, self->maxElements * self->elementSize);
|
|
|
|
}
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-setAvailableCapacity:(NSUInteger) numSlots
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if (numSlots > numElements) {
|
|
|
|
maxElements = numSlots;
|
|
|
|
dataPtr = (void *) objc_realloc (dataPtr, maxElements * elementSize);
|
|
|
|
}
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-setNumSlots:(NSUInteger) numSlots
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if (numSlots > numElements) {
|
|
|
|
maxElements = numSlots;
|
|
|
|
dataPtr = (void *) objc_realloc (dataPtr, maxElements * elementSize);
|
|
|
|
bzero (STORAGE_NTH (numElements),
|
|
|
|
(maxElements - numElements) * elementSize);
|
|
|
|
} else if (numSlots < numElements) {
|
|
|
|
numElements = numSlots;
|
|
|
|
_shrinkIfDesired (self);
|
|
|
|
}
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Manipulating objects by index */
|
|
|
|
|
|
|
|
#define CHECK_INDEX(IND) if (IND >= numElements) return 0
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-(NSUInteger) count
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
return numElements;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-(void *) elementAt:(NSUInteger) index
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
CHECK_INDEX (index);
|
|
|
|
return STORAGE_NTH (index);
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-addElement:(void *) anElement
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
_makeRoomForAnotherIfNecessary (self);
|
|
|
|
memcpy (STORAGE_NTH (numElements), anElement, elementSize);
|
|
|
|
numElements++;
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-insertElement:(void *)
|
|
|
|
anElement at:(NSUInteger) index
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-27 18:01:07 +00:00
|
|
|
NSUInteger i;
|
2010-09-11 08:35:25 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
CHECK_INDEX (index);
|
|
|
|
_makeRoomForAnotherIfNecessary (self);
|
|
|
|
#ifndef STABLE_MEMCPY
|
|
|
|
for (i = numElements; i >= index; i--)
|
|
|
|
memcpy (STORAGE_NTH (i + 1), STORAGE_NTH (i), elementSize);
|
2010-09-11 08:35:25 +00:00
|
|
|
#else
|
2010-09-11 10:03:41 +00:00
|
|
|
memcpy (STORAGE_NTH (index + 1),
|
|
|
|
STORAGE_NTH (index), elementSize * (numElements - index));
|
|
|
|
#endif
|
|
|
|
memcpy (STORAGE_NTH (i), anElement, elementSize);
|
|
|
|
numElements++;
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-removeElementAt:(NSUInteger) index
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-27 18:01:07 +00:00
|
|
|
NSUInteger i;
|
2010-09-11 08:35:25 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
CHECK_INDEX (index);
|
|
|
|
numElements--;
|
2010-09-11 08:35:25 +00:00
|
|
|
#ifndef STABLE_MEMCPY
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = index; i < numElements; i++)
|
|
|
|
memcpy (STORAGE_NTH (i), STORAGE_NTH (i + 1), elementSize);
|
2010-09-11 08:35:25 +00:00
|
|
|
#else
|
2010-09-11 10:03:41 +00:00
|
|
|
memcpy (STORAGE_NTH (index),
|
|
|
|
STORAGE_NTH (index + 1), elementSize * (numElements - index - 1));
|
|
|
|
#endif
|
|
|
|
_shrinkIfDesired (self);
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-removeLastElement
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
if (numElements) {
|
|
|
|
numElements--;
|
|
|
|
_shrinkIfDesired (self);
|
|
|
|
}
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-replaceElementAt:(NSUInteger)
|
|
|
|
index with:(void *) newElement
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
CHECK_INDEX (index);
|
|
|
|
memcpy (STORAGE_NTH (index), newElement, elementSize);
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Emptying the Storage */
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-empty
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
numElements = 0;
|
|
|
|
maxElements = 1;
|
|
|
|
dataPtr = (void *) objc_realloc (dataPtr, maxElements * elementSize);
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Archiving */
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-write:(TypedStream *) aStream
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-27 18:01:07 +00:00
|
|
|
NSUInteger i;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
[super write:aStream];
|
|
|
|
objc_write_types (aStream, "III*",
|
|
|
|
&numElements, &maxElements, &elementSize, &description);
|
|
|
|
for (i = 0; i < numElements; i++)
|
|
|
|
objc_write_type (aStream, description, STORAGE_NTH (i));
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
-read:(TypedStream *) aStream
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-27 18:01:07 +00:00
|
|
|
NSUInteger i;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
[super read:aStream];
|
|
|
|
objc_read_types (aStream, "III*",
|
|
|
|
&numElements, &maxElements, &elementSize, &description);
|
|
|
|
dataPtr = (void *) objc_malloc (maxElements * elementSize);
|
|
|
|
for (i = 0; i < numElements; i++)
|
|
|
|
objc_read_type (aStream, description, STORAGE_NTH (i));
|
|
|
|
return self;
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
+new
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
return[[self alloc] init];
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
+newCount:(NSUInteger)
|
|
|
|
count elementSize:(NSUInteger) sizeInBytes
|
|
|
|
description:(const char *) descriptor
|
2010-09-11 08:35:25 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
return[[self alloc] initCount: count elementSize: sizeInBytes description:descriptor];
|
2010-09-11 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|