ObjFW  Check-in [e47ad44290]

Overview
Comment:Reworked OFObject and added append(Wide)CString to OFString.

The reworked OFObject lets one test fail on the GNU runtime.
Unfortunately, I have no idea why...

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e47ad44290654e1387858d9614676b6f37a161bc83dd86784de4216adad73c27
User & Date: js on 2008-10-22 13:32:19
Other Links: manifest | tags
Context
2008-10-25
22:25
newWithFooString -> newAsFooString. check-in: 62abf39400 user: js tags: trunk
2008-10-22
13:32
Reworked OFObject and added append(Wide)CString to OFString. check-in: e47ad44290 user: js tags: trunk
2008-10-11
20:26
Move length from base string class to subclasses & others. check-in: 7b15048e25 user: js tags: trunk
Changes

Modified configure.ac from [01c1f78ef7] to [c6ac65f092].

11
12
13
14
15
16
17
18
19
20
21


22
23
24
25
26
27
28
29
30
31
32
33

case "$CC" in
	gcc | *-gcc | gcc-* | *-gcc-*)
		OBJCFLAGS="$OBJSFLAGS -Wall -Werror -pipe -g"
		;;
esac

OBJCFLAGS="$OBJCFLAGS -fconstant-string-class=OFConstString -fobjc-exceptions"

BUILDSYS_SHARED_LIB



AC_CHECK_LIB(objc, sel_get_name,
	[AC_DEFINE(HAVE_SEL_GET_NAME, 1, [Whether we have sel_get_name])])
AC_CHECK_LIB(objc, sel_getName,
	[AC_DEFINE(HAVE_SEL_GETNAME, 1, [Whether we have sel_getName])])

test x"$have_sel_name" = x"no" -a x"$have_selname" = x"no" && \
	AC_ERROR(You need either sel_get_name or sel_getName in libobjc!)

AC_SUBST(PACKAGE, objfw)
AC_CONFIG_FILES(buildsys.mk)
AC_CONFIG_HEADERS(config.h)
AC_OUTPUT







|



>
>












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

case "$CC" in
	gcc | *-gcc | gcc-* | *-gcc-*)
		OBJCFLAGS="$OBJSFLAGS -Wall -Werror -pipe -g"
		;;
esac

OBJCFLAGS="$OBJCFLAGS -fconstant-string-class=OFConstCString -fobjc-exceptions"

BUILDSYS_SHARED_LIB

AC_CHECK_HEADER(objc/runtime.h,
	[AC_DEFINE(HAVE_OBJC_RUNTIME_H, 1, [Whether we have objc/runtime.h])])
AC_CHECK_LIB(objc, sel_get_name,
	[AC_DEFINE(HAVE_SEL_GET_NAME, 1, [Whether we have sel_get_name])])
AC_CHECK_LIB(objc, sel_getName,
	[AC_DEFINE(HAVE_SEL_GETNAME, 1, [Whether we have sel_getName])])

test x"$have_sel_name" = x"no" -a x"$have_selname" = x"no" && \
	AC_ERROR(You need either sel_get_name or sel_getName in libobjc!)

AC_SUBST(PACKAGE, objfw)
AC_CONFIG_FILES(buildsys.mk)
AC_CONFIG_HEADERS(config.h)
AC_OUTPUT

Modified src/OFCString.h from [2b50f5bd43] to [7ab81c6a80].

22
23
24
25
26
27
28

29

- initWithCString: (char*)str;
- (char*)cString;
- (size_t)length;
- (OFString*)clone;
- (int)compareTo: (OFString*)str;
- (OFString*)append: (OFString*)str;

@end







>

22
23
24
25
26
27
28
29
30

- initWithCString: (char*)str;
- (char*)cString;
- (size_t)length;
- (OFString*)clone;
- (int)compareTo: (OFString*)str;
- (OFString*)append: (OFString*)str;
- (OFString*)appendCString: (const char*)str;
@end

Modified src/OFCString.m from [28eeb45980] to [dc3a9e9583].

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
- (int)compareTo: (OFString*)str
{
	return strcmp(string, [str cString]);
}

- (OFString*)append: (OFString*)str
{





	char   *newstr;
	size_t newlen, strlength;

	if (str == NULL)
		return [self setTo: str];

	strlength = [str length];
	newlen = length + strlength;

	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, [str cString], strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}
@end







>
>
>
>
>



|
|

|





|







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
- (int)compareTo: (OFString*)str
{
	return strcmp(string, [str cString]);
}

- (OFString*)append: (OFString*)str
{
	return [self appendCString: [str cString]];
}

- (OFString*)appendCString: (const char*)str
{
	char   *newstr;
	size_t newlen, strlength;

	if (string == NULL) 
		return [self setTo: [OFString newWithCString: (char*)str]];

	strlength = strlen(str);
	newlen = length + strlength;

	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}
@end

Modified src/OFExceptions.m from [36864f42d3] to [aa5a117197].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


18
19
20
21
22
23
24
/*
 * Copyright (c) 2008
 *   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 "config.h"

#import <objc/objc-api.h>

#import <stdio.h>
#import <stdlib.h>



#import "OFExceptions.h"

#if defined HAVE_SEL_GET_NAME
#define SEL_NAME(x) sel_get_name(x)
#elif defined HAVE_SEL_GETNAME
#define SEL_NAME(x) sel_getName(x)













<
|


>
>







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
/*
 * Copyright (c) 2008
 *   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 "config.h"


#define _GNU_SOURCE
#import <stdio.h>
#import <stdlib.h>

#import <objc/objc-api.h>

#import "OFExceptions.h"

#if defined HAVE_SEL_GET_NAME
#define SEL_NAME(x) sel_get_name(x)
#elif defined HAVE_SEL_GETNAME
#define SEL_NAME(x) sel_getName(x)

Modified src/OFObject.h from [20796294ad] to [1d4691aa4c].

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct __ofobject_allocated_mem {
	void				*ptr;
	struct __ofobject_allocated_mem *prev;
	struct __ofobject_allocated_mem *next;
};

@interface OFObject: Object
{
	struct __ofobject_allocated_mem *__mem_pool;
}

- init;
- (void*)getMemWithSize: (size_t)size;
- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size;
- (void)freeMem: (void*)ptr;
- free;
@end







<
|
<








14
15
16
17
18
19
20

21

22
23
24
25
26
27
28
29
struct __ofobject_allocated_mem {
	void				*ptr;
	struct __ofobject_allocated_mem *prev;
	struct __ofobject_allocated_mem *next;
};

@interface OFObject: Object

+ alloc;


- init;
- (void*)getMemWithSize: (size_t)size;
- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size;
- (void)freeMem: (void*)ptr;
- free;
@end

Modified src/OFObject.m from [85971766c6] to [5509180fd4].

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

#import "config.h"

#import <stdlib.h>







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









@implementation OFObject
























- init
{
	if ((self = [super init]) != nil)
		__mem_pool  = NULL;
	return self;
}
















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

	if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) {
		[[OFNoMemException newWithObject: self







>
>
>
>
>
>




>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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

#import "config.h"

#import <stdlib.h>
#import <string.h>

#import <objc/objc-api.h>
#ifdef HAVE_OBJC_RUNTIME_H
#import <objc/runtime.h>
#endif

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

#ifdef HAVE_OBJC_RUNTIME_H
#define MEM_POOL (*(struct __ofobject_allocated_mem**)((char*)self + \
	class_getInstanceSize([self class])))
#else
#define MEM_POOL (*(struct __ofobject_allocated_mem**)((char*)self + \
	([self class])->instance_size))
#endif

@implementation OFObject
+ alloc
{
	Class class = [self class];
	id inst = nil;

#ifdef HAVE_OBJC_RUNTIME_H
	if ((inst = (id)malloc(class_getInstanceSize(class) +
	    sizeof(struct __ofobject_allocated_mem*))) != nil) {
		memset(inst, 0, class_getInstanceSize(class) +
		    sizeof(struct __ofobject_allocated_mem*));
		inst->isa = class;
	}
#else
	if ((inst = (id)malloc(class->instance_size) +
	    sizeof(struct __ofobject_allocated_mem*)) != nil) {
		memset(inst, 0, class->instance_size +
		    sizeof(struct __ofobject_allocated_mem*));
		inst->class_pointer = class;
	}
#endif

	return inst;
}

- 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 ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) {
		[[OFNoMemException newWithObject: self
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
		free(iter);
		[[OFNoMemException newWithObject: self
					andSize: size] raise];
		return NULL;
	}

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

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

	__mem_pool = iter;

	return iter->ptr;
}

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

	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];
				return NULL;
			}
			







|

|
|

|









|







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
		free(iter);
		[[OFNoMemException newWithObject: self
					andSize: size] raise];
		return NULL;
	}

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

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

	MEM_POOL = iter;

	return iter->ptr;
}

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

	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];
				return NULL;
			}
			
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
	return NULL;
}

- (void)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 = NULL;

			free(iter);
			free(ptr);

			return;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
}

- free
{
	struct __ofobject_allocated_mem *iter, *iter2;

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

	return [super free];
}
@end







|





|
|











<
<
<
<
<
<
<
<
<
<
<
<
<

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

- (void)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 = NULL;

			free(iter);
			free(ptr);

			return;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
}













@end

Modified src/OFString.h from [f6f06366e5] to [19fb63cd94].

22
23
24
25
26
27
28


29
- (char*)cString;
- (wchar_t*)wcString;
- (size_t)length;
- (OFString*)setTo: (OFString*)str;
- (OFString*)clone;
- (int)compareTo: (OFString*)str;
- (OFString*)append: (OFString*)str;


@end







>
>

22
23
24
25
26
27
28
29
30
31
- (char*)cString;
- (wchar_t*)wcString;
- (size_t)length;
- (OFString*)setTo: (OFString*)str;
- (OFString*)clone;
- (int)compareTo: (OFString*)str;
- (OFString*)append: (OFString*)str;
- (OFString*)appendCString: (const char*)str;
- (OFString*)appendWideCString: (const char*)str;
@end

Modified src/OFString.m from [bf98b3db86] to [34596a2a5f].

87
88
89
90
91
92
93

















94

- (OFString*)append: (OFString*)str
{
	[[OFNotImplementedException newWithObject: self
				      andSelector: @selector(append:)] raise];
	return nil;
}

















@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

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

- (OFString*)append: (OFString*)str
{
	[[OFNotImplementedException newWithObject: self
				      andSelector: @selector(append:)] raise];
	return nil;
}

- (OFString*)appendCString: (const char*)str
{
	[[OFNotImplementedException newWithObject: self
				      andSelector: @selector(appendCString:)]
	    raise];
	return nil;
}

- (OFString*)appendWideCString: (const char*)str
{
	[[OFNotImplementedException newWithObject: self
				      andSelector: @selector(
						       appendWideCString:)]
	    raise];
	return nil;
}
@end

Modified src/OFWideCString.h from [c68b96f11d] to [a0db324d5d].

22
23
24
25
26
27
28

29

- initWithWideCString: (wchar_t*)str;
- (wchar_t*)wcString;
- (size_t)length;
- (OFString*)clone;
- (int)compareTo: (OFString*)str;
- (OFString*)append: (OFString*)str;

@end







>

22
23
24
25
26
27
28
29
30

- initWithWideCString: (wchar_t*)str;
- (wchar_t*)wcString;
- (size_t)length;
- (OFString*)clone;
- (int)compareTo: (OFString*)str;
- (OFString*)append: (OFString*)str;
- (OFString*)appendWideCString: (const wchar_t*)str;
@end

Modified src/OFWideCString.m from [9dbc23240b] to [39f7445e2b].

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
- (int)compareTo: (OFString*)str
{
	return wcscmp(string, [str wcString]);
}

- (OFString*)append: (OFString*)str
{





	wchar_t	*newstr;
	size_t	newlen, strlength;

	if ([str wcString] == NULL)
		return [self setTo: str];


	strlength = [str length];
	newlen = length + strlength;

	newstr = [self resizeMem: string
			  toSize: (newlen + 1) * sizeof(wchar_t)];

	wmemcpy(newstr + length, [str wcString], strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}
@end







>
>
>
>
>



|
|
>

|





|







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
- (int)compareTo: (OFString*)str
{
	return wcscmp(string, [str wcString]);
}

- (OFString*)append: (OFString*)str
{
	return [self appendWideCString: [str wcString]];
}

- (OFString*)appendWideCString: (const wchar_t*)str
{
	wchar_t	*newstr;
	size_t	newlen, strlength;

	if (string == NULL) 
		return [self setTo: [OFString
					newWithWideCString: (wchar_t*)str]];

	strlength = wcslen(str);
	newlen = length + strlength;

	newstr = [self resizeMem: string
			  toSize: (newlen + 1) * sizeof(wchar_t)];

	wmemcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}
@end