ObjFW  Diff

Differences From Artifact [6ca92c6b7f]:

To Artifact [8aa04f6052]:


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
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







+













+







}

- (instancetype)init
{
	self = [super init];

	_itemSize = 1;
	_freeWhenDone = true;

	return self;
}

- (instancetype)initWithItemSize: (size_t)itemSize
{
	self = [super init];

	@try {
		if (itemSize == 0)
			@throw [OFInvalidArgumentException exception];

		_itemSize = itemSize;
		_freeWhenDone = true;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
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
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







-
-
-
+


+









-

+


+
-
+
-







-

+



+
-
+
-







{
	self = [super init];

	@try {
		if (itemSize == 0)
			@throw [OFInvalidArgumentException exception];

		_items = [self allocMemoryWithSize: itemSize
					     count: capacity];

		_items = of_malloc(capacity, itemSize);
		_itemSize = itemSize;
		_capacity = capacity;
		_freeWhenDone = true;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithItems: (const void *)items
		     itemSize: (size_t)itemSize
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	self = [super initWithItems: items
			      count: count
			   itemSize: itemSize
			   itemSize: itemSize];
			      count: count];

	_capacity = _count;

	return self;
}

- (instancetype)initWithItemsNoCopy: (void *)items
			   itemSize: (size_t)itemSize
			      count: (size_t)count
			   itemSize: (size_t)itemSize
		       freeWhenDone: (bool)freeWhenDone
{
	self = [self initWithItems: items
			     count: count
			  itemSize: itemSize
			  itemSize: itemSize];
			     count: count];

	if (freeWhenDone)
		free(items);

	return self;
}

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
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







+
-
+
-








-
+
-
-







- (OFData *)subdataWithRange: (of_range_t)range
{
	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _count)
		@throw [OFOutOfRangeException exception];

	return [OFData dataWithItems: _items + (range.location * _itemSize)
			       count: range.length
			    itemSize: _itemSize
			    itemSize: _itemSize];
			       count: range.length];
}

- (void)addItem: (const void *)item
{
	if (SIZE_MAX - _count < 1)
		@throw [OFOutOfRangeException exception];

	if (_count + 1 > _capacity) {
		_items = [self resizeMemory: _items
		_items = of_realloc(_items, _count + 1, _itemSize);
				       size: _itemSize
				      count: _count + 1];
		_capacity = _count + 1;
	}

	memcpy(_items + _count * _itemSize, item, _itemSize);

	_count++;
}
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
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







-
-
-
+















-
-
-
+
















-
-
-
+







- (void)addItems: (const void *)items
	   count: (size_t)count
{
	if (count > SIZE_MAX - _count)
		@throw [OFOutOfRangeException exception];

	if (_count + count > _capacity) {
		_items = [self resizeMemory: _items
				       size: _itemSize
				      count: _count + count];
		_items = of_realloc(_items, _count + count, _itemSize);
		_capacity = _count + count;
	}

	memcpy(_items + _count * _itemSize, items, count * _itemSize);
	_count += count;
}

- (void)insertItems: (const void *)items
	    atIndex: (size_t)idx
	      count: (size_t)count
{
	if (count > SIZE_MAX - _count || idx > _count)
		@throw [OFOutOfRangeException exception];

	if (_count + count > _capacity) {
		_items = [self resizeMemory: _items
				       size: _itemSize
				      count: _count + count];
		_items = of_realloc(_items, _count + count, _itemSize);
		_capacity = _count + count;
	}

	memmove(_items + (idx + count) * _itemSize, _items + idx * _itemSize,
	    (_count - idx) * _itemSize);
	memcpy(_items + idx * _itemSize, items, count * _itemSize);

	_count += count;
}

- (void)increaseCountBy: (size_t)count
{
	if (count > SIZE_MAX - _count)
		@throw [OFOutOfRangeException exception];

	if (_count + count > _capacity) {
		_items = [self resizeMemory: _items
				       size: _itemSize
				      count: _count + count];
		_items = of_realloc(_items, _count + count, _itemSize);
		_capacity = _count + count;
	}

	memset(_items + _count * _itemSize, '\0', count * _itemSize);
	_count += count;
}

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
302
303
304
305
306
307
308
309

310
311
312
313
314
315
316
317

318

319
320
321
322
323









324
325
326
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
302
303
304
305
306

307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323







-
+
-
-













-
+
-
-








-
-
+








+
-
+
-




+
+
+
+
+
+
+
+
+




	memmove(_items + range.location * _itemSize,
	    _items + (range.location + range.length) * _itemSize,
	    (_count - range.location - range.length) * _itemSize);

	_count -= range.length;
	@try {
		_items = [self resizeMemory: _items
		_items = of_realloc(_items, _count, _itemSize);
				       size: _itemSize
				      count: _count];
		_capacity = _count;
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
	}
}

- (void)removeLastItem
{
	if (_count == 0)
		return;

	_count--;
	@try {
		_items = [self resizeMemory: _items
		_items = of_realloc(_items, _count, _itemSize);
				       size: _itemSize
				      count: _count];
		_capacity = _count;
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only made it smaller */
	}
}

- (void)removeAllItems
{
	[self freeMemory: _items];

	free(_items);
	_items = NULL;
	_count = 0;
	_capacity = 0;
}

- (id)copy
{
	return [[OFData alloc] initWithItems: _items
				       count: _count
				    itemSize: _itemSize
				    itemSize: _itemSize];
				       count: _count];
}

- (void)makeImmutable
{
	if (_capacity != _count) {
		@try {
			_items = of_realloc(_items, _count, _itemSize);
			_capacity = _count;
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */
		}
	}

	object_setClass(self, [OFData class]);
}
@end