ObjFW  Check-in [20c584252c]

Overview
Comment:Add objc_{construct,destruct}Instance().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 20c584252c5c5399b55226970ce8a29291b826332800f581e11bb11679151ddc
User & Date: js on 2012-09-06 19:13:50
Original User & Date: js on 2012-09-06 19:13:51
Other Links: manifest | tags
Context
2012-09-06
19:13
A few small exception fixes. check-in: aecc08c367 user: js tags: trunk
19:13
Add objc_{construct,destruct}Instance(). check-in: 20c584252c user: js tags: trunk
19:13
Improve selector not found message. check-in: 9dfe462848 user: js tags: trunk
Changes

Modified src/OFObject.m from [b19596ca32] to [2f8b3fda71].

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_MEM(mem) ((struct pre_mem*)(void*)((char*)mem - PRE_MEM_ALIGN))

static struct {
	Class isa;
} alloc_failed_exception;

static SEL cxx_construct = NULL;
static SEL cxx_destruct = NULL;

size_t of_pagesize;
size_t of_num_cpus;

#if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__)
static void
uncaught_exception_handler(id exception)
{







<
<
<







83
84
85
86
87
88
89



90
91
92
93
94
95
96
	(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_MEM(mem) ((struct pre_mem*)(void*)((char*)mem - PRE_MEM_ALIGN))

static struct {
	Class isa;
} alloc_failed_exception;




size_t of_pagesize;
size_t of_num_cpus;

#if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__)
static void
uncaught_exception_handler(id exception)
{
202
203
204
205
206
207
208
209





210
211
212
213
214
215
216
		    exceptionWithClass: class];
	}
#endif

	instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN);

	memset(instance, 0, instanceSize);
	object_setClass(instance, class);






	if OF_UNLIKELY (extra != NULL)
		*extra = (char*)instance + instanceSize + extraAlignment;

	return instance;
}








|
>
>
>
>
>







199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
		    exceptionWithClass: class];
	}
#endif

	instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN);

	memset(instance, 0, instanceSize);

	if (!objc_constructInstance(class, instance)) {
		free((char*)instance - PRE_IVAR_ALIGN);
		@throw [OFInitializationFailedException
		    exceptionWithClass: class];
	}

	if OF_UNLIKELY (extra != NULL)
		*extra = (char*)instance + instanceSize + extraAlignment;

	return instance;
}

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
	objc_forward_handler = forward_handler;
#endif

#ifdef HAVE_OBJC_ENUMERATIONMUTATION
	objc_setEnumerationMutationHandler(enumeration_mutation_handler);
#endif

	cxx_construct = sel_registerName(".cxx_construct");
	cxx_destruct = sel_registerName(".cxx_destruct");

	if (cxx_construct == NULL || cxx_destruct == NULL) {
		fputs("Runtime error: Failed to register selector "
		    ".cxx_construct and/or .cxx_destruct!\n", stderr);
		abort();
	}

#if defined(_WIN32)
	SYSTEM_INFO si;
	GetSystemInfo(&si);
	of_pagesize = si.dwPageSize;
	of_num_cpus = si.dwNumberOfProcessors;
#elif defined(__QNX__)
	if ((of_pagesize = sysconf(_SC_PAGESIZE)) < 1)







<
<
<
<
<
<
<
<
<







240
241
242
243
244
245
246









247
248
249
250
251
252
253
	objc_forward_handler = forward_handler;
#endif

#ifdef HAVE_OBJC_ENUMERATIONMUTATION
	objc_setEnumerationMutationHandler(enumeration_mutation_handler);
#endif










#if defined(_WIN32)
	SYSTEM_INFO si;
	GetSystemInfo(&si);
	of_pagesize = si.dwPageSize;
	of_num_cpus = si.dwNumberOfProcessors;
#elif defined(__QNX__)
	if ((of_pagesize = sysconf(_SC_PAGESIZE)) < 1)
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
+ (BOOL)resolveInstanceMethod: (SEL)selector
{
	return NO;
}

- init
{
	Class class;
	void (*last)(id, SEL) = NULL;

	for (class = object_getClass(self); class != Nil;
	    class = class_getSuperclass(class)) {
		void (*construct)(id, SEL);

		if ([class instancesRespondToSelector: cxx_construct]) {
			if ((construct = (void(*)(id, SEL))[class
			    instanceMethodForSelector: cxx_construct]) != last)
				construct(self, cxx_construct);

			last = construct;
		} else
			break;
	}

	return self;
}

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







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







500
501
502
503
504
505
506

















507
508
509
510
511
512
513
+ (BOOL)resolveInstanceMethod: (SEL)selector
{
	return NO;
}

- init
{

















	return self;
}

- (Class)class
{
	return object_getClass(self);
}
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
- (BOOL)isProxy
{
	return NO;
}

- (void)dealloc
{
	Class class;
	void (*last)(id, SEL) = NULL;
	struct pre_mem *iter;

	for (class = object_getClass(self); class != Nil;
	    class = class_getSuperclass(class)) {
		void (*destruct)(id, SEL);

		if ([class instancesRespondToSelector: cxx_destruct]) {
			if ((destruct = (void(*)(id, SEL))[class
			    instanceMethodForSelector: cxx_destruct]) != last)
				destruct(self, cxx_destruct);

			last = destruct;
		} else
			break;
	}

	iter = PRE_IVAR->firstMem;
	while (iter != NULL) {
		struct pre_mem *next = iter->next;

		/*
		 * We can use owner as a sentinel to prevent exploitation in







<
<


<
<
<
<
<
<
<
<
<
|
<
<
<







799
800
801
802
803
804
805


806
807









808



809
810
811
812
813
814
815
- (BOOL)isProxy
{
	return NO;
}

- (void)dealloc
{


	struct pre_mem *iter;










	objc_destructInstance(self);




	iter = PRE_IVAR->firstMem;
	while (iter != NULL) {
		struct pre_mem *next = iter->next;

		/*
		 * We can use owner as a sentinel to prevent exploitation in

Modified src/runtime/class.m from [cde4d41e21] to [d471bf4a2f].

26
27
28
29
30
31
32



33
34
35
36
37
38
39
#import "runtime-private.h"

static struct objc_hashtable *classes = NULL;
static Class *load_queue = NULL;
static size_t load_queue_cnt = 0;
static struct objc_sparsearray *empty_dtable = NULL;




static void
register_class(struct objc_abi_class *cls)
{
	if (classes == NULL)
		classes = objc_hashtable_new(2);

	objc_hashtable_set(classes, cls->name, cls);







>
>
>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "runtime-private.h"

static struct objc_hashtable *classes = NULL;
static Class *load_queue = NULL;
static size_t load_queue_cnt = 0;
static struct objc_sparsearray *empty_dtable = NULL;

static SEL cxx_construct = NULL;
static SEL cxx_destruct = NULL;

static void
register_class(struct objc_abi_class *cls)
{
	if (classes == NULL)
		classes = objc_hashtable_new(2);

	objc_hashtable_set(classes, cls->name, cls);
289
290
291
292
293
294
295





296
297
298
299
300
301
302
}

void
objc_register_all_classes(struct objc_abi_symtab *symtab)
{
	uint_fast32_t i;






	for (i = 0; i < symtab->cls_def_cnt; i++) {
		struct objc_abi_class *cls =
		    (struct objc_abi_class*)symtab->defs[i];

		register_class(cls);
		register_selectors(cls);
		register_selectors(cls->metaclass);







>
>
>
>
>







292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
}

void
objc_register_all_classes(struct objc_abi_symtab *symtab)
{
	uint_fast32_t i;

	if (cxx_construct == NULL)
		cxx_construct = sel_registerName(".cxx_construct");
	if (cxx_destruct == NULL)
		cxx_destruct = sel_registerName(".cxx_destruct");

	for (i = 0; i < symtab->cls_def_cnt; i++) {
		struct objc_abi_class *cls =
		    (struct objc_abi_class*)symtab->defs[i];

		register_class(cls);
		register_selectors(cls);
		register_selectors(cls->metaclass);
529
530
531
532
533
534
535






















































536
537
538
539
540
541
542

	objc_update_dtable(cls);

	objc_global_mutex_unlock();

	return (IMP)nil;
}























































static void
free_class(Class rcls)
{
	struct objc_abi_class *cls = (struct objc_abi_class*)rcls;

	if (rcls->subclass_list != NULL) {







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







537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604

	objc_update_dtable(cls);

	objc_global_mutex_unlock();

	return (IMP)nil;
}

id
objc_constructInstance(Class cls, void *bytes)
{
	id obj = (id)bytes;
	BOOL (*last)(id, SEL) = NULL;

	if (cls == Nil || bytes == NULL)
		return nil;

	object_setClass(obj, cls);

	/* FIXME: Constructors of superclasses should be called first */
	for (; cls != Nil; cls = class_getSuperclass(cls)) {
		BOOL (*ctor)(id, SEL);

		if (class_respondsToSelector(cls, cxx_construct)) {
			if ((ctor = (BOOL(*)(id, SEL))
			    class_getMethodImplementation(cls,
			    cxx_construct)) != last)
				if (!ctor(obj, cxx_construct))
					return nil;

			last = ctor;
		} else
			break;
	}

	return obj;
}

void*
objc_destructInstance(id obj)
{
	Class cls;
	void (*last)(id, SEL) = NULL;

	for (cls = object_getClass(obj); cls != Nil;
	    cls = class_getSuperclass(cls)) {
		void (*dtor)(id, SEL);

		if (class_respondsToSelector(cls, cxx_destruct)) {
			if ((dtor = (void(*)(id, SEL))
			    class_getMethodImplementation(cls,
			    cxx_destruct)) != last)
				dtor(obj, cxx_destruct);

			last = dtor;
		} else
			break;
	}

	return obj;
}

static void
free_class(Class rcls)
{
	struct objc_abi_class *cls = (struct objc_abi_class*)rcls;

	if (rcls->subclass_list != NULL) {

Modified src/runtime/runtime.h from [b190fb62d1] to [995079990b].

187
188
189
190
191
192
193


194
195
196
197
198
199
200
extern BOOL protocol_conformsToProtocol(Protocol*, Protocol*);
extern void objc_thread_add(void);
extern void objc_thread_remove(void);
extern void objc_exit(void);
extern objc_uncaught_exception_handler objc_setUncaughtExceptionHandler(
    objc_uncaught_exception_handler);
extern IMP (*objc_forward_handler)(id, SEL);


extern id objc_autorelease(id);
extern void* objc_autoreleasePoolPush(void);
extern void objc_autoreleasePoolPop(void*);
extern id _objc_rootAutorelease(id);

static inline Class
object_getClass(id obj_)







>
>







187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
extern BOOL protocol_conformsToProtocol(Protocol*, Protocol*);
extern void objc_thread_add(void);
extern void objc_thread_remove(void);
extern void objc_exit(void);
extern objc_uncaught_exception_handler objc_setUncaughtExceptionHandler(
    objc_uncaught_exception_handler);
extern IMP (*objc_forward_handler)(id, SEL);
extern id objc_constructInstance(Class, void*);
extern void* objc_destructInstance(id);
extern id objc_autorelease(id);
extern void* objc_autoreleasePoolPush(void);
extern void objc_autoreleasePoolPop(void*);
extern id _objc_rootAutorelease(id);

static inline Class
object_getClass(id obj_)