ObjFW  Diff

Differences From Artifact [16b59ee246]:

To Artifact [19577f9cbf]:


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







-
+



-
-
+
+



-
-
+
+
















-
+







	if (size == 0)
		return NULL;

	memchunks_size = __memchunks_size + 1;

	if (SIZE_MAX - __memchunks_size == 0 ||
	    memchunks_size > SIZE_MAX / sizeof(void*))
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];
	
	if ((memchunks = realloc(__memchunks,
	    memchunks_size * sizeof(void*))) == NULL)
		[[OFNoMemException newWithObject: self
					 andSize: memchunks_size] raise];
		@throw [OFNoMemException newWithObject: self
					       andSize: memchunks_size];

	if ((ptr = malloc(size)) == NULL) {
		free(memchunks);
		[[OFNoMemException newWithObject: self
					 andSize: size] raise];
		@throw [OFNoMemException newWithObject: self
					       andSize: size];
	}

	__memchunks = memchunks;
	__memchunks[__memchunks_size] = ptr;
	__memchunks_size = memchunks_size;

	return ptr;
}

- (void*)getMemForNItems: (size_t)nitems
		  ofSize: (size_t)size
{
	if (nitems == 0 || size == 0)
		return NULL;

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	return [self getMemWithSize: nitems * size];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size
{
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
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







-
-
+
+






-
-
+
+



















-
+







	}

	iter = __memchunks + __memchunks_size;

	while (iter-- > __memchunks) {
		if (OF_UNLIKELY(*iter == ptr)) {
			if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL))
				[[OFNoMemException newWithObject: self
							 andSize: size] raise];
				@throw [OFNoMemException newWithObject: self
							       andSize: size];
			
			*iter = ptr;
			return ptr;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
	@throw [OFMemNotPartOfObjException newWithObject: self
					      andPointer: ptr];
	return NULL;	/* never reached, but makes gcc happy */
}

- (void*)resizeMem: (void*)ptr
	  toNItems: (size_t)nitems
	    ofSize: (size_t)size
{
	size_t memsize;

	if (ptr == NULL)
		return [self getMemForNItems: nitems
				      ofSize: size];
	
	if (nitems == 0 || size == 0) {
		[self freeMem: ptr];
		return NULL;
	}

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];
		@throw [OFOutOfRangeException newWithObject: self];

	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}

- freeMem: (void*)ptr;
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
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







-
-
+
+













+
-
-
+
+
-
-










-
-
+
+




		if (OF_UNLIKELY(*iter == ptr)) {
			memchunks_size = __memchunks_size - 1;
			last = __memchunks[memchunks_size];

			if (OF_UNLIKELY(__memchunks_size == 0 ||
			    memchunks_size > SIZE_MAX / sizeof(void*)))
				[[OFOutOfRangeException newWithObject: self]
				    raise];
				@throw [OFOutOfRangeException
				    newWithObject: self];

			if (OF_UNLIKELY(memchunks_size == 0)) {
				free(ptr);
				free(__memchunks);

				__memchunks = NULL;
				__memchunks_size = 0;

				return self;
			}

			if (OF_UNLIKELY((memchunks = realloc(__memchunks,
			    memchunks_size * sizeof(void*))) == NULL))
				@throw [OFNoMemException
				[[OFNoMemException newWithObject: self
							 andSize:
				    newWithObject: self
					  andSize: memchunks_size];
							     memchunks_size]
				    raise];

			free(ptr);
			__memchunks = memchunks;
			__memchunks[i] = last;
			__memchunks_size = memchunks_size;

			return self;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
	@throw [OFMemNotPartOfObjException newWithObject: self
					      andPointer: ptr];
	return self;	/* never reached, but makes gcc happy */
}
@end