ObjFW  Check-in [cbdd534337]

Overview
Comment:Get rid of the dependency on Object.

This allows libobjfw to run on ObjC2-only runtimes like the one on the
iPhone. However, it's still relying on objc_msgSendv for plugins, which
is unavailable in ObjC2-only runtimes, thus OFPlugins are unavailable
on the iPhone until I write a replacement for objc_msgSendv.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: cbdd5343377803ab915547d262c0048d7da7b339a5f29358c10a0131ec86d0b1
User & Date: js on 2009-04-12 14:51:00
Other Links: manifest | tags
Context
2009-04-13
16:29
OFPlugin: Properly close handle on error. check-in: 297dbbb2b1 user: js tags: trunk
2009-04-12
14:51
Get rid of the dependency on Object. check-in: cbdd534337 user: js tags: trunk
14:05
Add OFNotImplementedException. check-in: ca4a07a578 user: js tags: trunk
Changes

Modified configure.ac from [7cf98941d7] to [e131a02e5c].

13
14
15
16
17
18
19



20
21
22
23
24
25
26
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29







+
+
+







OBJCFLAGS="$OBJCFLAGS -Wall -fobjc-exceptions"
OBJCFLAGS="$OBJCFLAGS -fconstant-string-class=OFConstString"
LIBS="$LIBS -lobjc"

AX_CHECK_COMPILER_FLAGS(-pipe, [
	CFLAGS="$CFLAGS -pipe"
	OBJCFLAGS="$OBJCFLAGS -pipe"])
AX_CHECK_COMPILER_FLAGS(-fno-common, [
	CFLAGS="$CFLAGS -fno-common"
	OBJCFLAGS="$OBJCFLAGS -fno-common"])
AX_CHECK_COMPILER_FLAGS(-fno-constant-cfstrings,
	[OBJCFLAGS="$OBJCFLAGS -fno-constant-cfstrings"])

AC_DEFINE(OF_CONFIG_H, 1, [Define so that we know we got our config.h])

BUILDSYS_LIB
AC_DEFINE_UNQUOTED(PLUGIN_SUFFIX, "$PLUGIN_SUFFIX", [Suffix for plugins])

Modified src/OFConstString.h from [5ebc134608] to [d9f2f102aa].

8
9
10
11
12
13
14


15
16
17
18
19
20
21

22
23
24
25
26
27
28
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

23
24
25
26
27
28
29
30







+
+






-
+







 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"

#ifndef __objc_INCLUDE_GNU
#import <objc/runtime.h>

extern void *_OFConstStringClassReference;
#endif

/**
 * A class for storing static strings using the @"" literal.
 */
@interface OFConstString: Object <OFHashable, OFRetainRelease>
@interface OFConstString: OFObject
{
	char   *string;
	size_t length;
}

/**
 * \return The OFString as a C string

Modified src/OFDictionary.h from [6fa1c50c8b] to [0e40b77f27].

53
54
55
56
57
58
59
60
61


62
63
64
65
66
67

68
69
70
71
72
73
74

75
53
54
55
56
57
58
59


60
61
62
63
64
65
66

67
68
69
70
71
72
73

74
75







-
-
+
+





-
+






-
+


/*
 * Sets a key to an object. A key can be any object.
 *
 * \param key The key to set
 * \param obj The object to set the key to
 */
- set: (id <OFHashable, OFRetainRelease>)key
   to: (id <OFRetainRelease>)obj;
- set: (OFObject*)key
   to: (OFObject*)obj;

/*
 * \param key The key whose object should be returned
 * \return The object for the given key
 */
- get: (id <OFHashable>)key;
- get: (OFObject*)key;

/*
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed
 */
- remove: (id <OFHashable, OFRetainRelease>)key;
- remove: (OFObject*)key;
@end

Modified src/OFDictionary.m from [8d722a0684] to [065f154d58].

78
79
80
81
82
83
84
85
86


87
88
89
90
91
92
93
78
79
80
81
82
83
84


85
86
87
88
89
90
91
92
93







-
-
+
+







	for (i = 0; i < size; i++)
		if (data[i] != nil)
			[data[i] release];

	return [super free];
}

- set: (id <OFHashable, OFRetainRelease>)key
   to: (id <OFRetainRelease>)obj
- set: (OFObject*)key
   to: (OFObject*)obj
{
	uint32_t hash = [key hash] & (size - 1);
	of_list_object_t *iter;

	if (data[hash] == nil)
		data[hash] = [OFList new];

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







-
+














-
+








	[data[hash] append: key];
	[data[hash] append: obj];

	return self;
}

- get: (id <OFHashable>)key
- get: (OFObject*)key
{
	uint32_t hash = [key hash] & (size - 1);
	of_list_object_t *iter;

	if (data[hash] == nil)
		return nil;

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next)
		if ([iter->object isEqual: key])
			return iter->next->object;

	return nil;
}

- remove: (id <OFHashable, OFRetainRelease>)key
- remove: (OFObject*)key
{
	uint32_t hash = [key hash] & (size - 1);
	of_list_object_t *iter;

	if (data[hash] == nil)
		return self; // FIXME: Throw exception?

Modified src/OFObject.h from [6f74f6252f] to [e051be24f6].

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











-
-
+


+
+
+
+
-
+

+
+
+
+
+
+
-
+
+
+
+
+
+

+
-
+
+
+
+
+


+
+
+
-
+
+
+

+
-
+
+
+
+
+


+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+

+
-
+
+
+
+
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+

+
+
-
-
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+


















-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <objc/Object.h>

#include <stddef.h>
#include <stdint.h>

#import <objc/objc.h>
#ifndef __objc_INCLUDE_GNU
#import <objc/message.h>
#endif
@protocol OFRetainRelease

/**
 * The OFObject class is the base class for all other classes inside ObjFW.
 */
@interface OFObject
{
	Class  isa;
}
 * Increases the retain count.

/**
 * This code is executed once when a method of the class is called for the first
 * time.
 * Derived classes can override this to execute their own code on
 * initialization.
 */
+ initialize;
- retain;

/**
 * Allocates memory for an instance of the class.
 */
+ alloc;

/**
 * Allocated memory for an instance of the class and initializes the instance.
 */
+ new;
 * Decreases the retain cound and frees the object if it reaches 0.

/**
 * \return The class pointer
 */
+ (Class)class;
- (void)release;

/**
 * \return The name of the class as a C string
 */
+ (const char*)name;

/**
 * Replace a method with a method from another class.
 *
 * \param selector The selector of the method to replace
 * \param class The class from which the new method should be taken
 * \return The old implementation
 */
+ (IMP)replaceMethod: (SEL)selector
 withMethodFromClass: (Class)class;
 * Adds the object to the autorelease pool that is on top of the thread's stack.

/**
 * Initialize the already allocated object.
 * Also sets up the memory pool for the object.
 *
 * \return An initialized object
 */
- init;

/**
 * \return A pointer to the class of the instance
 */
- (Class)class;
- autorelease;

/**
 * \return The name of the instance's class as a C string
 */
- (const char*)name;

/**
 * \param class The class whose kind is checked
 *
 * \return A boolean whether the object is of the specified kind
 */
- (BOOL)isKindOf: (Class)class;

/**
 * \param selector The selector which should be checked
 *
 * \return A boolean whether the objects responds to the specified selector
 */
- (BOOL)respondsTo: (SEL)selector;

/**
 * \param selector The selector for which the method should be returned
 *
 * \return The implementation for the specified selector
 */
- (IMP)methodFor: (SEL)selector;

/**
 * This method is called when a method was called which isn't implemented.
 * It's possible to override it so the method call cann be forwarded to another
 * object.
 *
 * \param selector The selector which was called
 * \param args The arguments with which the selector was called
 * \return The retain count
 * \return The return value of the call
 */
#ifdef __objc_INCLUDE_GNU
- (retval_t)forward: (SEL)selector
- (size_t)retainCount;
@end
		   : (arglist_t)args;
#else
- (id)forward: (SEL)selector
	     : (marg_list)args;
#endif

/**
 * Perform the given selector with the given arguments.
 *
 * \param selector The selector to perform
 * \param args The arguments with which the selector is performed
 * \return The return value of the performed selector
 */
#ifdef __objc_INCLUDE_GNU
- (retval_t)performv: (SEL)selector
		    : (arglist_t)args;
#else
- performv: (SEL)selector
	  : (marg_list)args;
#endif
@protocol OFHashable

/**
 * Compare two objects.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \param obj The object which is tested for equality
 * \return A boolean whether the object is equal to the other object
 */
- (BOOL)isEqual: (id)obj;

/**
 * Calculate a hash for the object.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \return A 32 bit hash for the object
 */
- (uint32_t)hash;
@end

/**
 * The OFObject class is the base class for all other classes inside ObjFW.
 */
@interface OFObject: Object <OFRetainRelease, OFHashable>
{
	void   **__memchunks;
	size_t __memchunks_size;
	size_t __retain_count;
}

/**
 * Initialize the already allocated object.
 * Also sets up the memory pool for the object.
 *
 * \return An initialized object
 */
- init;

/**
 * Frees the object and also frees all memory allocated via its memory pool.
 */
- free;

/**
 * Adds a pointer to the memory pool.
 * This is useful to add memory allocated by functions such as asprintf to the
 * pool so it gets freed automatically when the object is freed.
 *
 * \param ptr A pointer to add to the memory pool
133
134
135
136
137
138
139

























140
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


/**
 * Frees allocated memory and removes it from the memory pool.
 *
 * \param ptr A pointer to the allocated memory
 */
- freeMem: (void*)ptr;

/**
 * Increases the retain count.
 */
- retain;

/**
 * Adds the object to the autorelease pool that is on top of the thread's stack.
 */
- autorelease;

/**
 * \return The retain count
 */
- (size_t)retainCount;

/**
 * Decreases the retain cound and frees the object if it reaches 0.
 */
- (void)release;

/**
 * Frees the object and also frees all memory allocated via its memory pool.
 */
- free;
@end

Modified src/OFObject.m from [1ab2516eb4] to [fd94d50205].

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











+
+
+
+
+
+











+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
-
-
-
+
+

+
+
-
+


-
+

+
-
-
-
-
+
+
+
+
+
+
+
+
+

+
+
+
+
+
-
-
+
+

-
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+



+
-
+
+

-
-
+

-
-
+
+
+

-
-
-
-
+
+
+
+
+
+
+
+

-
-
-
+
+
+
+
+
+
+
+
+
+

+


















-
+

-
+



-
+




-
-
-
+
+
+












-
+

-
+







-
+






-
-
-
+
+
+







/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

/*
 * Get rid of the stupid warnings until we have our own implementation for
 * objc_msgSendv.
 */
#define OBJC2_UNAVAILABLE

#import "config.h"

#include <stdlib.h>
#include <string.h>
#include <limits.h>

#import "OFObject.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"

#import <objc/objc-api.h>
#ifndef __objc_INCLUDE_GNU
#import <objc/runtime.h>
#endif

struct pre_ivar {
	void   **memchunks;
	size_t memchunks_size;
	size_t retain_count;
};

/* Hopefully no arch needs more than 16 bytes padding */
#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + 15) & ~15)
#define PRE_IVAR ((struct pre_ivar*)((char*)self - PRE_IVAR_ALIGN))

@implementation OFObject
#ifndef __objc_INCLUDE_GNU
+ load
{
	return self;
}
#endif

+ initialize
{
	return self;
}

+ alloc
{
	OFObject *instance;
#ifdef __objc_INCLUDE_GNU
	size_t isize = class_get_instance_size(self);
#else
	size_t isize = class_getInstanceSize(self);
#endif

	if ((instance = malloc(isize + PRE_IVAR_ALIGN)) == NULL)
		return nil;

	((struct pre_ivar*)instance)->memchunks = NULL;
	((struct pre_ivar*)instance)->memchunks_size = 0;
	((struct pre_ivar*)instance)->retain_count = 1;

	instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN);
	memset(instance, 0, isize);
	instance->isa = self;

	return instance;
}

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

+ (Class)class
{
	return self;
}

+ (const char*)name
{
#ifdef __objc_INCLUDE_GNU
	return class_get_class_name(self);
#else
	return class_getName(self);
#endif
}

+ (IMP)replaceMethod: (SEL)selector
 withMethodFromClass: (Class)class;
{
#ifdef __objc_INCLUDE_GNU
	Method_t method = class_get_instance_method(self, selector);
	IMP oldimp, newimp;

	if (method == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	oldimp = method_get_imp(method);
	newimp = method_get_imp(class_get_instance_method(class, selector));

	if (oldimp == (IMP)0 || newimp == (IMP)0)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	method->method_imp = newimp;
	return oldimp;
#else
	IMP imp = class_getMethodImplementation(class, selector);
	Method method = class_getInstanceMethod(self, selector);

	if (imp == NULL || method == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	return method_setImplementation(method, imp);
#endif
}

- init
{
	if ((self = [super init]) != nil) {
		__memchunks = NULL;
		__memchunks_size = 0;
		__retain_count = 1;
	}
	return self;
}

- (Class)class
{
	return self;
	return isa;
}

- free
- (const char*)name
{
#ifdef __objc_INCLUDE_GNU
	void **iter = __memchunks + __memchunks_size;

	while (iter-- > __memchunks)
		free(*iter);
	return object_get_class_name(self);
#else
	return class_getName(isa);
#endif
}

- (BOOL)isKindOf: (Class)class
{
	Class iter;

#ifdef __objc_INCLUDE_GNU
	for (iter = isa; iter != Nil; iter = class_get_super_class(iter))
#else
	for (iter = isa; iter != Nil; iter = class_getSuperclass(iter))
#endif
	if (__memchunks != NULL)
		free(__memchunks);
		if (iter == class)
			return YES;

	return [super free];
	return NO;
}

- (BOOL)respondsTo: (SEL)selector
{
#ifdef __objc_INCLUDE_GNU
	if (object_is_instance(self))
		return class_get_instance_method(isa, selector) != METHOD_NULL;
	else
		return class_get_class_method(isa, selector) != METHOD_NULL;
#else
	return class_respondsToSelector(isa, selector);
#endif
}

- (IMP)methodFor: (SEL)selector
{
#ifdef __objc_INCLUDE_GNU
	if (object_is_instance(self))
		return method_get_imp(class_get_instance_method(isa, selector));
	else
		return method_get_imp(class_get_class_method(isa, selector));
#else
	return class_getMethodImplementation(isa, selector);
#endif

}
- retain
{
	__retain_count++;


#ifdef __objc_INCLUDE_GNU
- (retval_t)forward: (SEL)selector
		   : (arglist_t)args
#else
- (id)forward: (SEL)selector
	     : (marg_list)args
#endif
{
	@throw [OFNotImplementedException newWithClass: [self class]
					   andSelector: _cmd];
	return self;
}

#ifdef __objc_INCLUDE_GNU
- (void)release
- (retval_t)performv: (SEL)selector
		    : (arglist_t)args
{
	if (!--__retain_count)
		[self free];
	return objc_msg_sendv(self, selector, args);
}

- autorelease
#else
- performv: (SEL)selector
	  : (marg_list)args
{
	[OFAutoreleasePool addToPool: self];

	return self;
}
#if !__OBJC2__
	Method method;
	unsigned size;

	if ((method = class_getInstanceMethod(isa, selector)) != NULL)
		size = method_getSizeOfArguments(method);
	else
		size = 0;

- (size_t)retainCount
{
	return __retain_count;
	if (!size)
		return [self forward: selector
				    : args];

	return objc_msgSendv(self, selector, size, args);
#else
#warning ObjC2 removed objc_msgSendv and there is
#warning no own implementation for it yet!
	return nil;
#endif
}
#endif

- (BOOL)isEqual: (id)obj
{
	/* Classes containing data should reimplement this! */
	return (self == obj ? YES : NO);
}

- (uint32_t)hash
{
	/* Classes containing data should reimplement this! */
	return (uint32_t)self;
}

- addToMemoryPool: (void*)ptr
{
	void **memchunks;
	size_t memchunks_size;

	memchunks_size = __memchunks_size + 1;
	memchunks_size = PRE_IVAR->memchunks_size + 1;

	if (SIZE_MAX - __memchunks_size < 1 ||
	if (SIZE_MAX - PRE_IVAR->memchunks_size < 1 ||
	    memchunks_size > SIZE_MAX / sizeof(void*))
		@throw [OFOutOfRangeException newWithClass: [self class]];

	if ((memchunks = realloc(__memchunks,
	if ((memchunks = realloc(PRE_IVAR->memchunks,
	    memchunks_size * sizeof(void*))) == NULL)
		@throw [OFNoMemException newWithClass: [self class]
					      andSize: memchunks_size];

	__memchunks = memchunks;
	__memchunks[__memchunks_size] = ptr;
	__memchunks_size = memchunks_size;
	PRE_IVAR->memchunks = memchunks;
	PRE_IVAR->memchunks[PRE_IVAR->memchunks_size] = ptr;
	PRE_IVAR->memchunks_size = memchunks_size;

	return self;
}

- (void*)getMemWithSize: (size_t)size
{
	void *ptr, **memchunks;
	size_t memchunks_size;

	if (size == 0)
		return NULL;

	memchunks_size = __memchunks_size + 1;
	memchunks_size = PRE_IVAR->memchunks_size + 1;

	if (SIZE_MAX - __memchunks_size == 0 ||
	if (SIZE_MAX - PRE_IVAR->memchunks_size == 0 ||
	    memchunks_size > SIZE_MAX / sizeof(void*))
		@throw [OFOutOfRangeException newWithClass: [self class]];

	if ((ptr = malloc(size)) == NULL)
		@throw [OFNoMemException newWithClass: [self class]
					      andSize: size];

	if ((memchunks = realloc(__memchunks,
	if ((memchunks = realloc(PRE_IVAR->memchunks,
	    memchunks_size * sizeof(void*))) == NULL) {
		free(ptr);
		@throw [OFNoMemException newWithClass: [self class]
					      andSize: memchunks_size];
	}

	__memchunks = memchunks;
	__memchunks[__memchunks_size] = ptr;
	__memchunks_size = memchunks_size;
	PRE_IVAR->memchunks = memchunks;
	PRE_IVAR->memchunks[PRE_IVAR->memchunks_size] = ptr;
	PRE_IVAR->memchunks_size = memchunks_size;

	return ptr;
}

- (void*)getMemForNItems: (size_t)nitems
		  ofSize: (size_t)size
{
158
159
160
161
162
163
164
165

166
167

168
169
170
171
172
173
174
319
320
321
322
323
324
325

326
327

328
329
330
331
332
333
334
335







-
+

-
+







		return [self getMemWithSize: size];

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

	iter = __memchunks + __memchunks_size;
	iter = PRE_IVAR->memchunks + PRE_IVAR->memchunks_size;

	while (iter-- > __memchunks) {
	while (iter-- > PRE_IVAR->memchunks) {
		if (OF_UNLIKELY(*iter == ptr)) {
			if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL))
				@throw [OFNoMemException
				    newWithClass: [self class]
					 andSize: size];

			*iter = ptr;
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
366
367
368
369
370
371
372


373
374
375

376
377
378
379


380
381
382

383
384
385
386
387
388
389

390
391


392
393
394
395
396
397


398
399
400
401
402
403
404
405



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457







-
-
+
+

-
+



-
-
+
+

-
+






-
+

-
-
+
+




-
-
+
+
+





-
-
-
+
+
+









+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

}

- freeMem: (void*)ptr;
{
	void **iter, *last, **memchunks;
	size_t i, memchunks_size;

	iter = __memchunks + __memchunks_size;
	i = __memchunks_size;
	iter = PRE_IVAR->memchunks + PRE_IVAR->memchunks_size;
	i = PRE_IVAR->memchunks_size;

	while (iter-- > __memchunks) {
	while (iter-- > PRE_IVAR->memchunks) {
		i--;

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

			if (OF_UNLIKELY(__memchunks_size == 0 ||
			if (OF_UNLIKELY(PRE_IVAR->memchunks_size == 0 ||
			    memchunks_size > SIZE_MAX / sizeof(void*)))
				@throw [OFOutOfRangeException
				    newWithClass: [self class]];

			if (OF_UNLIKELY(memchunks_size == 0)) {
				free(ptr);
				free(__memchunks);
				free(PRE_IVAR->memchunks);

				__memchunks = NULL;
				__memchunks_size = 0;
				PRE_IVAR->memchunks = NULL;
				PRE_IVAR->memchunks_size = 0;

				return self;
			}

			if (OF_UNLIKELY((memchunks = realloc(__memchunks,
			    memchunks_size * sizeof(void*))) == NULL))
			if (OF_UNLIKELY((memchunks = realloc(
			    PRE_IVAR->memchunks, memchunks_size *
			    sizeof(void*))) == NULL))
				@throw [OFNoMemException
				    newWithClass: [self class]
					 andSize: memchunks_size];

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

			return self;
		}
	}

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

- retain
{
	PRE_IVAR->retain_count++;

	return self;
}

- autorelease
{
	[OFAutoreleasePool addToPool: self];

	return self;
}

- (size_t)retainCount
{
	return PRE_IVAR->retain_count;
}

- (void)release
{
	if (!--PRE_IVAR->retain_count)
		[self free];
}

- free
{
	void **iter = PRE_IVAR->memchunks + PRE_IVAR->memchunks_size;

	while (iter-- > PRE_IVAR->memchunks)
		free(*iter);

	if (PRE_IVAR->memchunks != NULL)
		free(PRE_IVAR->memchunks);

	free((char*)self - PRE_IVAR_ALIGN);
	return nil;
}
@end

Modified tests/OFAutoreleasePool/OFAutoreleasePool.m from [42a1c16a8e] to [5d50ad0603].

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







+
+
+
+











-
+










-
+









-
+






+
-
+
+
+
+
+








#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

IMP init;
IMP retain;
IMP release;

@interface TestObject: OFObject
- init;
- retain;
- (void)release;
@end

@implementation TestObject
- init
{
	id ret;
       
	ret = [super init];
	ret = init(self, @selector(init));
	printf("New %s with retain cnt " ZD "\n", [self name],
	    [ret retainCount]);

	return ret;
}

- retain
{
	id ret;

	ret = [super retain];
	ret = retain(self, @selector(retain));
	printf("Retaining %s to " ZD "\n", [self name], [ret retainCount]);

	return ret;
}

- (void)release
{
	printf("Releasing %s to " ZD "\n", [self name], [self retainCount] - 1);

	return [super release];
	release(self, @selector(release));
}
@end

int
main()
{
	init    = [OFObject replaceMethod: @selector(init)
	[TestObject poseAs: [OFObject class]];
		      withMethodFromClass: [TestObject class]];
	retain  = [OFObject replaceMethod: @selector(retain)
		      withMethodFromClass: [TestObject class]];
	release = [OFObject replaceMethod: @selector(release)
		      withMethodFromClass: [TestObject class]];

	OFObject *o1, *o2, *o3;
	OFAutoreleasePool *pool1, *pool2;

	o1 = [[OFObject new] autorelease];

	pool1 = [OFAutoreleasePool new];