File: Storage.m

package info (click to toggle)
gstep-extensions 0.8.5.990905-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 592 kB
  • ctags: 429
  • sloc: objc: 2,819; sh: 1,836; ansic: 860; makefile: 209; lex: 194
file content (301 lines) | stat: -rw-r--r-- 6,999 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
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
/* Implementation of Objective C NeXT-compatible Storage object
   Copyright (C) 1993,1994, 1996 Free Software Foundation, Inc.

   Written by:  Kresten Krab Thorup <krab@iesd.auc.dk>
   Dept. of Mathematics and Computer Science, Aalborg U., Denmark

   This file is part of the GNUstep Base Library.

   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.

   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.
*/ 

/* #include <config.h> */
#include <objc/Storage.h>
#include <base/preface.h>
/* memcpy() and memcmp() are gcc builtin's */

/* Deal with bzero: */
#if STDC_HEADERS || HAVE_STRING_H
#include <string.h>
/* An ANSI string.h and pre-ANSI memory.h might conflict.  */
#if !STDC_HEADERS && HAVE_MEMORY_H
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#define index strchr
#define rindex strrchr
#define bcopy(s, d, n) memcpy ((d), (s), (n))
#define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
#define bzero(s, n) memset ((s), 0, (n))
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
/* memory.h and strings.h conflict on some systems.  */
#endif /* not STDC_HEADERS and not HAVE_STRING_H */


#define GNU_STORAGE_NTH(x,N)                          \
  ({ GNUStorageId* __s=(GNUStorageId*)(x);            \
     (void*)(((char*)__s->dataPtr)+(__s->elementSize*(N))); })
#define STORAGE_NTH(N) GNU_STORAGE_NTH (self, N)

typedef struct {
    @defs(Storage)
} GNUStorageId;

@implementation Storage

+ initialize
{
  if (self == [Storage class])
    [self setVersion:0];	/* beta release */
  return self;
}

// INITIALIZING, FREEING;

- initCount: (unsigned)numSlots
  elementSize: (unsigned)sizeInBytes
  description: (const char*)elemDesc;
{
  [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;
}

- init
{
  return [self initCount:1 
	       elementSize:sizeof(id) 
	       description:@encode(id)];
}


- free
{
  if (dataPtr)
    free(dataPtr);
  return [super free];
}

- (const char*) description
{
  return description;
}


// COPYING;

- shallowCopy
{
  Storage *c = [super shallowCopy];
  c->dataPtr = (void*) objc_malloc (maxElements * elementSize);
  memcpy(c->dataPtr, dataPtr, numElements * elementSize);
  return c;
}

// COMPARING TWO STORAGES;

- (BOOL)isEqual: anObject
{
  if ([anObject isKindOf: [Storage class]]
      && [anObject count] == [self count]
      && !memcmp(((GNUStorageId*)anObject)->dataPtr,
		 dataPtr, numElements*elementSize))
    return YES;
  else
    return NO;
}
  
// MANAGING THE STORAGE CAPACITY;

static inline void _makeRoomForAnotherIfNecessary(Storage *self)
{
  if (self->numElements == self->maxElements) 
    {
      self->maxElements *= 2;
      self->dataPtr = (void*) 
	objc_realloc (self->dataPtr, self->maxElements*self->elementSize);
    }
}

static inline void _shrinkIfDesired(Storage *self)
{
  if (self->numElements < (self->maxElements / 2)) 
    {
      self->maxElements /= 2;
      self->dataPtr = (void *) 
	objc_realloc (self->dataPtr, self->maxElements*self->elementSize);
    }
}

- setAvailableCapacity:(unsigned)numSlots
{
  if (numSlots > numElements) 
    {
      maxElements = numSlots;
      dataPtr = (void*) objc_realloc (dataPtr, maxElements * elementSize);
    }
  return self;
}

- setNumSlots:(unsigned)numSlots
{
  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;
}

/* Manipulating objects by index */

#define CHECK_INDEX(IND)  if (IND >= numElements) return 0

- (unsigned) count
{
  return numElements;
}

- (void*) elementAt: (unsigned)index
{
  CHECK_INDEX(index);
  return STORAGE_NTH (index);
}

- addElement: (void*)anElement
{
  _makeRoomForAnotherIfNecessary(self);
  memcpy(STORAGE_NTH(numElements), anElement, elementSize);
  numElements++;
  return self;
}

- insertElement: (void*)anElement at: (unsigned)index
{
  int i;

  CHECK_INDEX(index);
  _makeRoomForAnotherIfNecessary(self);
#ifndef STABLE_MEMCPY    
  for (i = numElements; i >= index; i--)
    memcpy (STORAGE_NTH(i+1), STORAGE_NTH(i), elementSize);
#else
  memcpy (STORAGE_NTH (index+1),
	  STORAGE_NTH (index),
	  elementSize*(numElements-index));
#endif    
  memcpy(STORAGE_NTH(i), anElement, elementSize);
  numElements++;
  return self;
}

- removeElementAt: (unsigned)index
{
    int i;

    CHECK_INDEX(index);
    numElements--;
#ifndef STABLE_MEMCPY
    for (i = index; i < numElements; i++)
      memcpy(STORAGE_NTH(i), 
	     STORAGE_NTH(i+1), 
	     elementSize);
#else
    memcpy (STORAGE_NTH (index),
	    STORAGE_NTH (index+1),
	    elementSize*(numElements-index-1));
#endif    
    _shrinkIfDesired(self);
    return self;
}

- removeLastElement
{
  if (numElements) 
    {
      numElements--;
      _shrinkIfDesired(self);
    }
  return self;
}

- replaceElementAt:(unsigned)index with:(void*)newElement
{
    CHECK_INDEX(index);
    memcpy(STORAGE_NTH(index), newElement, elementSize);
    return self;
}

/* Emptying the Storage */

- empty
{
  numElements = 0;
  maxElements = 1;
  dataPtr = (void*) objc_realloc (dataPtr, maxElements * elementSize);
  return self;
}

/* Archiving */

- write: (TypedStream*)aStream
{
  int i;

  [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;
}

- read: (TypedStream*)aStream
{
  int i;

  [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;
}

+ new
{
  return [[self alloc] init];
}

+ newCount:(unsigned)count elementSize:(unsigned)sizeInBytes 
 description:(const char *)descriptor
{
  return [[self alloc] initCount:count elementSize:sizeInBytes
	  description:descriptor];
}

@end