ObjFW  Check-in [490cbb8e60]

Overview
Comment:Add a blocks runtime.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 490cbb8e604daaa222a5b9d409fc6d924bf4168bb36a117686eabaf973181222
User & Date: js on 2010-09-06 14:52:07
Other Links: manifest | tags
Context
2010-09-06
15:39
Add flags for blocks to objfw-config. check-in: cc3239f5f7 user: js tags: trunk
14:52
Add a blocks runtime. check-in: 490cbb8e60 user: js tags: trunk
13:47
Make OFList conform to OFCollection and OFFastEnumeration protocol. check-in: 2cb79baae4 user: js tags: trunk
Changes

Modified configure.ac from [4a2a23a23f] to [a3c471e387].

70
71
72
73
74
75
76

77
78
79
80
81
82
83
	], [
	AC_DEFINE(OF_HAVE_BLOCKS, 1, [Compiler support for blocks])
	AC_MSG_RESULT(yes)
	], [
	AC_MSG_RESULT(no)
	OBJCFLAGS="$old_OBJCFLAGS"
	])


AC_CHECK_HEADERS([objfw-rt.h objc/objc.h])

test x"$ac_cv_header_objfw_rt_h" = x"yes" && objc_runtime="ObjFW-RT"

if test x"$ac_cv_header_objc_objc_h" = x"yes"; then
	dnl TODO: This is ugly. Let's think of a better check.







>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	], [
	AC_DEFINE(OF_HAVE_BLOCKS, 1, [Compiler support for blocks])
	AC_MSG_RESULT(yes)
	], [
	AC_MSG_RESULT(no)
	OBJCFLAGS="$old_OBJCFLAGS"
	])
AC_CHECK_FUNC(_Block_copy,, AC_SUBST(BLOCK_M, "block.m"))

AC_CHECK_HEADERS([objfw-rt.h objc/objc.h])

test x"$ac_cv_header_objfw_rt_h" = x"yes" && objc_runtime="ObjFW-RT"

if test x"$ac_cv_header_objc_objc_h" = x"yes"; then
	dnl TODO: This is ugly. Let's think of a better check.

Modified extra.mk.in from [2a6468d265] to [278e8d8f53].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20
OBJFW_SHARED_LIB = @OBJFW_SHARED_LIB@
OBJFW_STATIC_LIB = @OBJFW_STATIC_LIB@
OBJFW_LIB_MAJOR = 2
OBJFW_LIB_MINOR = 0
OBJFW_LIB_MAJOR_MINOR = ${OBJFW_LIB_MAJOR}.${OBJFW_LIB_MINOR}

ASPRINTF_M = @ASPRINTF_M@
ATOMIC_H = @ATOMIC_H@

OBJC_PROPERTIES_M = @OBJC_PROPERTIES_M@
OBJC_SYNC_M = @OBJC_SYNC_M@
OFPLUGIN_M = @OFPLUGIN_M@
OFPLUGINTESTS_M = @OFPLUGINTESTS_M@
OFTHREAD_M = @OFTHREAD_M@
OFTHREADTESTS_M = @OFTHREADTESTS_M@
PROPERTIESTESTS_M = @PROPERTIESTESTS_M@
REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@
TESTPLUGIN = @TESTPLUGIN@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@
THREADING_H = @THREADING_H@








>












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
OBJFW_SHARED_LIB = @OBJFW_SHARED_LIB@
OBJFW_STATIC_LIB = @OBJFW_STATIC_LIB@
OBJFW_LIB_MAJOR = 2
OBJFW_LIB_MINOR = 0
OBJFW_LIB_MAJOR_MINOR = ${OBJFW_LIB_MAJOR}.${OBJFW_LIB_MINOR}

ASPRINTF_M = @ASPRINTF_M@
ATOMIC_H = @ATOMIC_H@
BLOCK_M = @BLOCK_M@
OBJC_PROPERTIES_M = @OBJC_PROPERTIES_M@
OBJC_SYNC_M = @OBJC_SYNC_M@
OFPLUGIN_M = @OFPLUGIN_M@
OFPLUGINTESTS_M = @OFPLUGINTESTS_M@
OFTHREAD_M = @OFTHREAD_M@
OFTHREADTESTS_M = @OFTHREADTESTS_M@
PROPERTIESTESTS_M = @PROPERTIESTESTS_M@
REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@
TESTPLUGIN = @TESTPLUGIN@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@
THREADING_H = @THREADING_H@

Modified src/Makefile from [75150cb36f] to [2ddb212bf2].

50
51
52
53
54
55
56
57

58
59
60
61
62
63
	    objfw-defs.h	\
	    ${THREADING_H}

SRCS += ${ASPRINTF_M} 		\
	iso_8859_15.m		\
	windows_1252.m		\
	${OBJC_PROPERTIES_M}	\
	${OBJC_SYNC_M}


include ../buildsys.mk

CPPFLAGS += -I..
LD = ${OBJC}
LDFLAGS += ${REEXPORT_LIBOBJC}







|
>






50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	    objfw-defs.h	\
	    ${THREADING_H}

SRCS += ${ASPRINTF_M} 		\
	iso_8859_15.m		\
	windows_1252.m		\
	${OBJC_PROPERTIES_M}	\
	${OBJC_SYNC_M}		\
	${BLOCK_M}

include ../buildsys.mk

CPPFLAGS += -I..
LD = ${OBJC}
LDFLAGS += ${REEXPORT_LIBOBJC}

Modified src/OFConstString.m from [948949780e] to [6da12923e2].

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include "config.h"

#import "OFConstString.h"
#import "OFExceptions.h"

#ifdef OF_APPLE_RUNTIME
#import <objc/runtime.h>

void *_OFConstStringClassReference;
#endif

@implementation OFConstString
#ifdef OF_APPLE_RUNTIME
+ (void)load







|







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include "config.h"

#import "OFConstString.h"
#import "OFExceptions.h"

#ifdef OF_APPLE_RUNTIME
# import <objc/runtime.h>

void *_OFConstStringClassReference;
#endif

@implementation OFConstString
#ifdef OF_APPLE_RUNTIME
+ (void)load

Modified src/OFSeekableStream.h from [9408d385d6] to [19f283eaf8].

1
2
3
4
5
6
7
8
9
10



11
12
13
14
15
16
17
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. 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.
 */




#include <unistd.h>

#import "OFStream.h"

/**
 * \brief A stream that supports seeking.










>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. 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.
 */

/* Required to prevent a name conflict in glibc when using -fblocks */
#undef __USE_XOPEN

#include <unistd.h>

#import "OFStream.h"

/**
 * \brief A stream that supports seeking.

Added src/block.m version [576dbf7c91].











































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. 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.
 */

#include "config.h"

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

#ifdef OF_OBJFW_RUNTIME
# import <objfw-rt.h>
#else
# import <objc/objc.h>
# ifdef OF_APPLE_RUNTIME
#  import <objc/runtime.h>
# endif
#endif

@interface RetainRelease
- retain;
- (void)release;
@end

struct block_literal {
	void *isa;
	int flags;
	int reserved;
	void (*invoke)(void *, ...);
	struct block_descriptor {
		unsigned long reserved;
		unsigned long size;
		void (*copy_helper)(void *dest, void *src);
		void (*dispose_helper)(void *src);
		const char *signature;
	} *descriptor;
};

struct block_byref {
	void *isa;
	struct block_byref *forwarding;
	int flags;
	int size;
	void (*byref_keep)(void *dest, void *src);
	void (*byref_dispose)(void*);
};

enum {
	BLOCK_HAS_COPY_DISPOSE = (1 << 25),
	BLOCK_HAS_CTOR	       = (1 << 26),
	BLOCK_IS_GLOBAL	       = (1 << 28),
	BLOCK_HAS_STRET	       = (1 << 29),
	BLOCK_HAS_SIGNATURE    = (1 << 30),
};

enum {
	BLOCK_FIELD_IS_OBJECT =   3,
	BLOCK_FIELD_IS_BLOCK  =   7,
	BLOCK_FIELD_IS_BYREF  =   8,
	BLOCK_FIELD_IS_WEAK   =  16,
	BLOCK_BYREF_CALLER    = 128,
};

#ifndef __OBJC2__
struct objc_class _NSConcreteStackBlock;
struct objc_class _NSConcreteGlobalBlock;
struct objc_class _NSConcreteMallocBlock;
#endif

struct block_literal*
Block_copy(struct block_literal *block)
{
	if (block->isa == &_NSConcreteStackBlock) {
		struct block_literal *copy;

		if ((copy = malloc(block->descriptor->size)) == NULL) {
			fputs("Not enough memory to copy block!\n", stderr);
			exit(1);
		}
		memcpy(copy, block, block->descriptor->size);

		copy->isa = &_NSConcreteMallocBlock;
		copy->reserved++;

		if (block->flags & BLOCK_HAS_COPY_DISPOSE)
			block->descriptor->copy_helper(copy, block);

		return copy;
	}

	if (block->isa == &_NSConcreteMallocBlock)
		block->reserved++;

	return block;
}

void
Block_release(struct block_literal *block)
{
	if (block->isa != &_NSConcreteMallocBlock)
		return;

	if (--block->reserved == 0) {
		if (block->flags & BLOCK_HAS_COPY_DISPOSE)
			block->descriptor->dispose_helper(block);

		free(block);
	}
}

void
_Block_object_assign(void *dst, void *src, int flags)
{
	flags &= BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_OBJECT |
	    BLOCK_FIELD_IS_BYREF;

	switch (flags) {
	case BLOCK_FIELD_IS_BLOCK:
		*(struct block_literal**)dst = Block_copy(src);
		break;
	case BLOCK_FIELD_IS_OBJECT:
		*(id*)dst = [(id)src retain];
		break;
	case BLOCK_FIELD_IS_BYREF:;
		struct block_byref *src_ = src;
		struct block_byref **dst_ = dst;

		if ((src_->flags & ~BLOCK_HAS_COPY_DISPOSE) == 0) {
			if ((*dst_ = malloc(src_->size)) == NULL) {
				fputs("Not enough memory for block "
				    "variables!\n", stderr);
				exit(1);
			}

			if (src_->forwarding == src)
				src_->forwarding = *dst_;

			memcpy(*dst_, src_, src_->size);

			if (src_->size >= sizeof(struct block_byref))
				src_->byref_keep(*dst_, src_);
		} else
			*dst_ = src_;

		(*dst_)->flags++;
		break;
	}
}

void
_Block_object_dispose(void *obj, int flags)
{
	flags &= BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_OBJECT |
	    BLOCK_FIELD_IS_BYREF;

	switch (flags) {
	case BLOCK_FIELD_IS_BLOCK:
		Block_release(obj);
		break;
	case BLOCK_FIELD_IS_OBJECT:
		[(id)obj release];
		break;
	case BLOCK_FIELD_IS_BYREF:;
		struct block_byref *obj_ = obj;

		if ((--obj_->flags & ~BLOCK_HAS_COPY_DISPOSE) == 0) {
			if (obj_->size >= sizeof(struct block_byref))
				obj_->byref_dispose(obj_);

			free(obj_);
		}
		break;
	}
}