ObjFW  Check-in [14ba9e517b]

Overview
Comment:Optimize OFObject.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 14ba9e517bb29aeae19a38742bf936ff1fa3dd3013b26a1946c67e2fe22f080a
User & Date: js on 2008-11-08 20:28:20
Other Links: manifest | tags
Context
2008-11-08
21:52
Further optimize OFObject. check-in: c6ee6a2870 user: js tags: trunk
20:28
Optimize OFObject. check-in: 14ba9e517b user: js tags: trunk
17:57
Add missing add: for OFBigArray and fix calculation of nsize. check-in: ae28fbf3ca user: js tags: trunk
Changes

Modified src/OFExceptions.m from [8685793b88] to [aaeb7dcecc].

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Could not allocate %zu bytes for object of"
	    "class %s!\n", req_size, object != nil ? [object name] : "(null)");

	return string;
}

- (size_t)requestedSize
{







|







82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Could not allocate %zu bytes for object of "
	    "class %s!\n", req_size, object != nil ? [object name] : "(null)");

	return string;
}

- (size_t)requestedSize
{

Modified src/OFObject.h from [2a153af38a] to [f0d19652e8].

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
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <objc/Object.h>
#import <stdint.h>

struct __ofobject_allocated_mem {
	void				*ptr;
	struct __ofobject_allocated_mem *prev;
	struct __ofobject_allocated_mem *next;
};

/**
 * The OFObject class is the base class for all other classes inside ObjFW.
 */
@interface OFObject: Object
{
	struct __ofobject_allocated_mem *__mem_pool;

}

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







<
<
<
<
<
<





|
>







8
9
10
11
12
13
14






15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <objc/Object.h>
#import <stdint.h>







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

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

Modified src/OFObject.m from [f326f71c26] to [9ea6f8965e].

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

#import "OFObject.h"
#import "OFExceptions.h"

@implementation OFObject
- init
{
	if ((self = [super init]) != nil)
		__mem_pool = NULL;


	return self;
}

- free
{
	struct __ofobject_allocated_mem *iter, *iter2;

	for (iter = __mem_pool; iter != NULL; iter = iter2) {
		iter2 = iter->prev;
		free(iter->ptr);
		free(iter);
	}



	free(self);

	return nil;
}

- (void*)getMemWithSize: (size_t)size
{
	struct __ofobject_allocated_mem *iter;


	if (size == 0)
		return NULL;


	if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) {


		[[OFNoMemException newWithObject: self
					andSize: sizeof(struct
						     __ofobject_allocated_mem)
		    ] raise];
	}





	if ((iter->ptr = malloc(size)) == NULL) {
		free(iter);
		[[OFNoMemException newWithObject: self
					andSize: size] raise];
	}

	iter->next = NULL;
	iter->prev = __mem_pool;

	if (__mem_pool != NULL)
		__mem_pool->next = iter;

	__mem_pool = iter;

	return iter->ptr;
}

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

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

	memsize = nitems * size;
	return [self getMemWithSize: memsize];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size
{
	struct __ofobject_allocated_mem *iter;

	if (ptr == NULL)
		return [self getMemWithSize: size];

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

	for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
		if (iter->ptr == ptr) {
			if ((ptr = realloc(iter->ptr, size)) == NULL)
				[[OFNoMemException newWithObject: self
							 andSize: size] raise];
			
			iter->ptr = ptr;
			return ptr;
		}
	}

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







|
|
>
>





|

|
<
<
|
|
>
>








|
>




>
|
>
>
|
<
<
<
|
>
>
>
>

|
|

|


<
|
|
<
<
|
<

|




















|









|
|
|



|







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

#import "OFObject.h"
#import "OFExceptions.h"

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

- free
{
	size_t i;

	for (i = 0; i < __memchunks_size; i++)


		free(__memchunks[i]);

	if (__memchunks != NULL)
		free(__memchunks);

	free(self);

	return nil;
}

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

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



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

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


	__memchunks = memchunks;
	__memchunks[__memchunks_size] = ptr;


	__memchunks_size = memchunks_size;


	return ptr;
}

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

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

	memsize = nitems * size;
	return [self getMemWithSize: memsize];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size
{
	size_t i;

	if (ptr == NULL)
		return [self getMemWithSize: size];

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

	for (i = 0; i < __memchunks_size; i++) {
		if (__memchunks[i] == ptr) {
			if ((ptr = realloc(ptr, size)) == NULL)
				[[OFNoMemException newWithObject: self
							 andSize: size] raise];
			
			__memchunks[i] = ptr;
			return ptr;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
	return NULL;	/* never reached, but makes gcc happy */
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
	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}

- freeMem: (void*)ptr;
{
	struct __ofobject_allocated_mem *iter;


	for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
		if (iter->ptr == ptr) {


			if (iter->prev != NULL) 




				iter->prev->next = iter->next;
			if (iter->next != NULL)


				iter->next->prev = iter->prev;
			if (__mem_pool == iter)

				__mem_pool = iter->prev;


			free(iter);







			free(ptr);




			return self;
		}
	}

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







|
>

|
|
>
>
|
>
>
>
>
|
|
>
>
|
|
>
|
>
|
|
>
>
>
>
>
>
>

>
>
>










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
	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}

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

	for (i = 0; i < __memchunks_size; i++) {
		if (__memchunks[i] == ptr) {
			memchunks_size = __memchunks_size - 1;
			last = __memchunks[memchunks_size];

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

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

				__memchunks = NULL;
				__memchunks_size = 0;

				return self;
			}

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

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

			return self;
		}
	}

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