ObjFW  Check-in [ae9f0dc0e0]

Overview
Comment:Add OF_ROUND_UP_POW2 macro
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ae9f0dc0e09f8c1c0889cd8b1c812ebe8b232d29b5bc801bb385d64cea9a8217
User & Date: js on 2014-03-05 01:36:04
Other Links: manifest | tags
Context
2014-03-16
22:32
OFSystemInfo: Add +[user{Config,Data}Path] check-in: e3a2dc6180 user: js tags: trunk
2014-03-05
01:36
Add OF_ROUND_UP_POW2 macro check-in: ae9f0dc0e0 user: js tags: trunk
00:34
OFKernelEventObserver: Only use 1 internal socket check-in: a2f2a3289d user: js tags: trunk
Changes

Modified src/OFBigDataArray.m from [b4d602f2c6] to [294c220cb4].

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







+
+




















-
+







-
-
+
+


-
+




















-
+

-
-
+
+
-




















-
+

-
-
+
+
-








#import "OFBigDataArray.h"
#import "OFSystemInfo.h"

#import "OFInvalidArgumentException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

@implementation OFBigDataArray
- init
{
	return [self initWithItemSize: 1
			     capacity: 0];
}

- initWithItemSize: (size_t)itemSize
{
	return [self initWithItemSize: itemSize
			     capacity: 0];
}

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

	@try {
		size_t size, lastPageByte;
		size_t size, pageSize;

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

		if (capacity > SIZE_MAX / itemSize)
			@throw [OFOutOfRangeException exception];

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = (capacity * itemSize + lastPageByte) & ~lastPageByte;
		pageSize = [OFSystemInfo pageSize];
		size = OF_ROUND_UP_POW2(pageSize, capacity * itemSize);

		if (size == 0)
			size = lastPageByte + 1;
			size = pageSize;

		_items = [self allocMemoryWithSize: size];

		_itemSize = itemSize;
		_capacity = size / itemSize;
		_size = size;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	if (_count + 1 > _capacity) {
		size_t size, lastPageByte;
		size_t size, pageSize;

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = ((_count + 1) * _itemSize + lastPageByte) &
		pageSize = [OFSystemInfo pageSize];
		size = OF_ROUND_UP_POW2(pageSize, (_count + 1) * _itemSize);
		    ~lastPageByte;

		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size / _itemSize;
		_size = size;
	}

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

	_count++;
}

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

	if (_count + count > _capacity) {
		size_t size, lastPageByte;
		size_t size, pageSize;

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = ((_count + count) * _itemSize + lastPageByte) &
		pageSize = [OFSystemInfo pageSize];
		size = OF_ROUND_UP_POW2(pageSize, (_count + count) * _itemSize);
		    ~lastPageByte;

		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size / _itemSize;
		_size = size;
	}
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
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







-
+

-
-
+
+
-

















-
+










-
-
+
+

-
+













-
+





-
-
+
+

-
+







	      count: (size_t)count
{
	if (count > SIZE_MAX - _count || index > _count ||
	    _count + count > SIZE_MAX / _itemSize)
		@throw [OFOutOfRangeException exception];

	if (_count + count > _capacity) {
		size_t size, lastPageByte;
		size_t size, pageSize;

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = ((_count + count) * _itemSize + lastPageByte) &
		pageSize = [OFSystemInfo pageSize];
		size = OF_ROUND_UP_POW2(pageSize, (_count + count) * _itemSize);
		    ~lastPageByte;

		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size / _itemSize;
		_size = size;
	}

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

	_count += count;
}

- (void)removeItemsInRange: (of_range_t)range
{
	size_t lastPageByte, size;
	size_t pageSize, size;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _count)
		@throw [OFOutOfRangeException exception];

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

	_count -= range.length;
	lastPageByte = [OFSystemInfo pageSize] - 1;
	size = (_count * _itemSize + lastPageByte) & ~lastPageByte;
	pageSize = [OFSystemInfo pageSize];
	size = OF_ROUND_UP_POW2(pageSize, _count * _itemSize);

	if (_size != size && size > lastPageByte) {
	if (_size != size && size >= pageSize) {
		@try {
			_items = [self resizeMemory: _items
					       size: size];
			_capacity = size / _itemSize;
			_size = size;
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */
		}
	}
}

- (void)removeLastItem
{
	size_t lastPageByte, size;
	size_t pageSize, size;

	if (_count == 0)
		return;

	_count--;
	lastPageByte = [OFSystemInfo pageSize] - 1;
	size = (_count * _itemSize + lastPageByte) & ~lastPageByte;
	pageSize = [OFSystemInfo pageSize];
	size = OF_ROUND_UP_POW2(pageSize, _count * _itemSize);

	if (_size != size && size > lastPageByte) {
	if (_size != size && size >= pageSize) {
		@try {
			_items = [self resizeMemory: _items
					       size: size];
			_capacity = size / _itemSize;
			_size = size;
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */

Modified src/macros.h from [e55751e201] to [76ff6117a8].

353
354
355
356
357
358
359


360
361
362
363
364
365
366
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368







+
+








#define OF_ROL(value, bits)						   \
	(((value) << ((bits) % (sizeof(value) * 8))) |			   \
	((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))))
#define OF_ROR(value, bits)						   \
	(((value) >> ((bits) % (sizeof(value) * 8))) |			   \
	((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))))

#define OF_ROUND_UP_POW2(pow2, value) (((value) + (pow2) - 1) & ~((pow2) - 1))

#define OF_HASH_INIT(hash) hash = of_hash_seed;
#define OF_HASH_ADD(hash, byte)			\
	{					\
		hash += (uint8_t)(byte);	\
		hash += (hash << 10);		\
		hash ^= (hash >> 6);		\