ObjFW  Check-in [3f0940e36d]

Overview
Comment:Use the well hidden __objc_update_dispatch_table_for_class() function.

The old GNU API actually does export a function to update the dtable,
however, it is not defined in the headers anymore starting with gcc 4.6.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3f0940e36d23a847b976ccd4dac2826ff872efed4e17c02c0a3583979dfaf6e9
User & Date: js on 2011-08-03 04:14:18
Other Links: manifest | tags
Context
2011-08-03
16:25
Fix a few OFString tests. check-in: cc93f51348 user: js tags: trunk
04:14
Use the well hidden __objc_update_dispatch_table_for_class() function. check-in: 3f0940e36d user: js tags: trunk
03:53
Always use the old GNU API, even with the new GNU runtime. check-in: 546332b4d7 user: js tags: trunk
Changes

Modified src/OFObject.m from [4d946cf26f] to [b3a44692ae].

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import "OFOutOfRangeException.h"

#import "macros.h"

#if defined(OF_OBJFW_RUNTIME)
# import <objfw-rt.h>
#elif defined(OF_OLD_GNU_RUNTIME)
# import <objc/sarray.h>
# import <objc/Protocol.h>
#endif

#ifdef _WIN32
# include <windows.h>
#endif








<







39
40
41
42
43
44
45

46
47
48
49
50
51
52
#import "OFOutOfRangeException.h"

#import "macros.h"

#if defined(OF_OBJFW_RUNTIME)
# import <objfw-rt.h>
#elif defined(OF_OLD_GNU_RUNTIME)

# import <objc/Protocol.h>
#endif

#ifdef _WIN32
# include <windows.h>
#endif

72
73
74
75
76
77
78




79
80
81
82
83
84
85
#ifndef __BIGGEST_ALIGNMENT__
# define __BIGGEST_ALIGNMENT__ 16
#endif

#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + \
	(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_IVAR ((struct pre_ivar*)(void*)((char*)self - PRE_IVAR_ALIGN))





static struct {
	Class isa;
} alloc_failed_exception;
static Class autoreleasePool = Nil;

static SEL cxx_construct = NULL;







>
>
>
>







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef __BIGGEST_ALIGNMENT__
# define __BIGGEST_ALIGNMENT__ 16
#endif

#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + \
	(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_IVAR ((struct pre_ivar*)(void*)((char*)self - PRE_IVAR_ALIGN))

#ifdef OF_OLD_GNU_RUNTIME
extern void __objc_update_dispatch_table_for_class(Class);
#endif

static struct {
	Class isa;
} alloc_failed_exception;
static Class autoreleasePool = Nil;

static SEL cxx_construct = NULL;
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

/* References for static linking */
void _references_to_categories_of_OFObject(void)
{
	_OFObject_Serialization_reference = 1;
}

#ifdef OF_OLD_GNU_RUNTIME
/*
 * The old GNU runtime is missing functions for changing methods at runtime. It
 * does not even offer a function to update the dtable, so we have to do even
 * that manually. A well designed runtime would not even allow us to touch the
 * dtable, but the old GNU runtime is that crappy that it even forces us to
 * touch it...
 */
static void
update_dtable(Class class)
{
	MethodList_t iter;
	Class subclass;

	for (subclass = class->subclass_list; subclass != Nil;
	    subclass = subclass->sibling_class)
		update_dtable(subclass);

	for (iter = class->methods; iter != NULL; iter = iter->method_next) {
		Method_t methods = iter->method_list;
		int i;

		for (i = 0; i < iter->method_count; i++)
			if (sarray_get_safe(class->dtable,
			    (sidx)methods[i].method_name->sel_id) != NULL)
				sarray_at_put_safe(class->dtable,
				    (sidx)methods[i].method_name->sel_id,
				    methods[i].method_imp);
	}
}
#endif

@implementation OFObject
+ (void)load
{
#ifdef NEED_OBJC_SYNC_INIT
	if (!objc_sync_init()) {
		fputs("Runtime error: objc_sync_init() failed!\n", stderr);
		abort();







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







125
126
127
128
129
130
131
































132
133
134
135
136
137
138

/* References for static linking */
void _references_to_categories_of_OFObject(void)
{
	_OFObject_Serialization_reference = 1;
}

































@implementation OFObject
+ (void)load
{
#ifdef NEED_OBJC_SYNC_INIT
	if (!objc_sync_init()) {
		fputs("Runtime error: objc_sync_init() failed!\n", stderr);
		abort();
389
390
391
392
393
394
395

396
397
398
399
400
401
402
403
			if (sel_eq(iter->method_list[i].method_name,
			    selector)) {
				IMP oldImp;

				oldImp = iter->method_list[i].method_imp;
				iter->method_list[i].method_imp = newImp;


				update_dtable((Class)self->class_pointer);

				return oldImp;
			}
	}

	assert([self addClassMethod: selector
		   withTypeEncoding: method->method_types







>
|







360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
			if (sel_eq(iter->method_list[i].method_name,
			    selector)) {
				IMP oldImp;

				oldImp = iter->method_list[i].method_imp;
				iter->method_list[i].method_imp = newImp;

				__objc_update_dispatch_table_for_class(
				    (Class)self->class_pointer);

				return oldImp;
			}
	}

	assert([self addClassMethod: selector
		   withTypeEncoding: method->method_types
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
			if (sel_eq(iter->method_list[i].method_name,
			    selector)) {
				IMP oldImp;

				oldImp = iter->method_list[i].method_imp;
				iter->method_list[i].method_imp = newImp;

				update_dtable(self);

				return oldImp;
			}
	}

	assert([self addInstanceMethod: selector
		      withTypeEncoding: method->method_types







|







435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
			if (sel_eq(iter->method_list[i].method_name,
			    selector)) {
				IMP oldImp;

				oldImp = iter->method_list[i].method_imp;
				iter->method_list[i].method_imp = newImp;

				__objc_update_dispatch_table_for_class(self);

				return oldImp;
			}
	}

	assert([self addInstanceMethod: selector
		      withTypeEncoding: method->method_types
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

	methodList->method_list[0].method_name = selector;
	methodList->method_list[0].method_types = typeEncoding;
	methodList->method_list[0].method_imp = implementation;

	((Class)self)->methods = methodList;

	update_dtable(self);

	return YES;
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif
}







|







507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

	methodList->method_list[0].method_name = selector;
	methodList->method_list[0].method_types = typeEncoding;
	methodList->method_list[0].method_imp = implementation;

	((Class)self)->methods = methodList;

	__objc_update_dispatch_table_for_class(self);

	return YES;
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif
}
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592

	methodList->method_list[0].method_name = selector;
	methodList->method_list[0].method_types = typeEncoding;
	methodList->method_list[0].method_imp = implementation;

	((Class)self->class_pointer)->methods = methodList;

	update_dtable((Class)self->class_pointer);

	return YES;
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif
}







|







550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

	methodList->method_list[0].method_name = selector;
	methodList->method_list[0].method_types = typeEncoding;
	methodList->method_list[0].method_imp = implementation;

	((Class)self->class_pointer)->methods = methodList;

	__objc_update_dispatch_table_for_class((Class)self->class_pointer);

	return YES;
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif
}