ObjFW  Check-in [035fa09270]

Overview
Comment:runtime: Add objc_{get,copy}ClassList().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 035fa09270e1554e404458c1ed38f22aae6cd62bb32bc90bcd47c3aa219789c7
User & Date: js on 2013-12-19 22:21:56
Other Links: manifest | tags
Context
2013-12-20
00:14
runtime: More consistency in the API. check-in: 2dec88a9d5 user: js tags: trunk
2013-12-19
22:21
runtime: Add objc_{get,copy}ClassList(). check-in: 035fa09270 user: js tags: trunk
20:15
runtime: New API for internal unregister methods. check-in: b4231b1df9 user: js tags: trunk
Changes

Modified src/runtime/class.m from [3ec80df1e8] to [0bf2860357].

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

#include "config.h"

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



#import "runtime.h"
#import "runtime-private.h"

struct sparsearray {
	void *next[256];
};

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 unsigned lookups_till_fast_path = 128;
static struct sparsearray *sparsearray = 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);

	if (empty_dtable == NULL)
		empty_dtable = objc_sparsearray_new();

	cls->dtable = empty_dtable;
	cls->metaclass->dtable = empty_dtable;



}

BOOL
class_registerAlias_np(Class cls, const char *name)
{
	if (classes == NULL)
		return NO;







>
>








>



















>
>
>







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

#include "config.h"

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

#include <assert.h>

#import "runtime.h"
#import "runtime-private.h"

struct sparsearray {
	void *next[256];
};

static struct objc_hashtable *classes = NULL;
static unsigned classes_cnt = 0;
static Class *load_queue = NULL;
static size_t load_queue_cnt = 0;
static struct objc_sparsearray *empty_dtable = NULL;
static unsigned lookups_till_fast_path = 128;
static struct sparsearray *sparsearray = 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);

	if (empty_dtable == NULL)
		empty_dtable = objc_sparsearray_new();

	cls->dtable = empty_dtable;
	cls->metaclass->dtable = empty_dtable;

	if (strcmp(cls->name, "Protocol"))
		classes_cnt++;
}

BOOL
class_registerAlias_np(Class cls, const char *name)
{
	if (classes == NULL)
		return NO;
500
501
502
503
504
505
506
































































507
508
509
510
511
512
513
}

Class
objc_get_class(const char *name)
{
	return objc_getRequiredClass(name);
}

































































bool
class_isMetaClass(Class cls)
{
	return (cls->info & OBJC_CLASS_INFO_METACLASS);
}








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







506
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
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
}

Class
objc_get_class(const char *name)
{
	return objc_getRequiredClass(name);
}

unsigned int
objc_getClassList(Class *buf, unsigned int count)
{
	uint32_t i;
	unsigned int j;
	objc_global_mutex_lock();

	if (buf == NULL)
		return classes_cnt;

	if (classes_cnt < count)
		count = classes_cnt;

	for (i = j = 0; i <= classes->last_idx; i++) {
		Class cls;

		if (j >= count) {
			objc_global_mutex_unlock();
			return j;
		}

		if (classes->data[i] == NULL)
			continue;

		if (!strcmp(classes->data[i]->key, "Protocol"))
			continue;

		cls = (Class)classes->data[i]->obj;

		if (cls == Nil || (uintptr_t)cls & 1)
			continue;

		buf[j++] = cls;
	}

	objc_global_mutex_unlock();

	return j;
}

Class*
objc_copyClassList(unsigned int *len)
{
	Class *ret;
	unsigned int count;

	objc_global_mutex_lock();

	if ((ret = malloc((classes_cnt + 1) * sizeof(Class))) == NULL)
		OBJC_ERROR("Failed to allocate memory for class list!");

	count = objc_getClassList(ret, classes_cnt);
	assert(count == classes_cnt);

	ret[count] = Nil;

	if (len != NULL)
		*len = count;

	objc_global_mutex_unlock();

	return ret;
}

bool
class_isMetaClass(Class cls)
{
	return (cls->info & OBJC_CLASS_INFO_METACLASS);
}

699
700
701
702
703
704
705
706
707
708
709
710
711
712





713
714
715
716
717
718
719
	if (rcls->dtable != NULL && rcls->dtable != empty_dtable)
		objc_sparsearray_free(rcls->dtable);

	rcls->dtable = NULL;

	if (rcls->superclass != Nil)
		cls->superclass = rcls->superclass->name;

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

void
objc_unregister_class(Class cls)
{





	unregister_class(cls);
	unregister_class(cls->isa);
}

static void
free_sparsearray(struct sparsearray *sa, size_t depth)
{







<
<





>
>
>
>
>







769
770
771
772
773
774
775


776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
	if (rcls->dtable != NULL && rcls->dtable != empty_dtable)
		objc_sparsearray_free(rcls->dtable);

	rcls->dtable = NULL;

	if (rcls->superclass != Nil)
		cls->superclass = rcls->superclass->name;


}

void
objc_unregister_class(Class cls)
{
	objc_hashtable_set(classes, cls->name, NULL);

	if (strcmp(cls->name, "Protocol"))
		classes_cnt--;

	unregister_class(cls);
	unregister_class(cls->isa);
}

static void
free_sparsearray(struct sparsearray *sa, size_t depth)
{
742
743
744
745
746
747
748


749
750
751
752
753
754
755
756
757
758
759
760

			if (cls == Nil || (uintptr_t)cls & 1)
				continue;

			objc_unregister_class(cls);
		}
	}



	if (empty_dtable != NULL) {
		objc_sparsearray_free(empty_dtable);
		empty_dtable = NULL;
	}

	free_sparsearray(sparsearray, sizeof(uintptr_t));
	sparsearray = NULL;

	objc_hashtable_free(classes);
	classes = NULL;
}







>
>












815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

			if (cls == Nil || (uintptr_t)cls & 1)
				continue;

			objc_unregister_class(cls);
		}
	}

	assert(classes_cnt == 0);

	if (empty_dtable != NULL) {
		objc_sparsearray_free(empty_dtable);
		empty_dtable = NULL;
	}

	free_sparsearray(sparsearray, sizeof(uintptr_t));
	sparsearray = NULL;

	objc_hashtable_free(classes);
	classes = NULL;
}

Modified src/runtime/runtime.h from [dda778658d] to [eb0edb48c5].

182
183
184
185
186
187
188


189
190
191
192
193
194
195
#endif
extern SEL sel_registerName(const char*);
extern const char* sel_getName(SEL);
extern bool sel_isEqual(SEL, SEL);
extern id objc_lookUpClass(const char*);
extern id objc_getClass(const char*);
extern id objc_getRequiredClass(const char*);


extern bool class_isMetaClass(Class);
extern const char* class_getName(Class);
extern Class class_getSuperclass(Class);
extern bool class_isKindOfClass(Class, Class);
extern unsigned long class_getInstanceSize(Class);
extern bool class_respondsToSelector(Class, SEL);
extern bool class_conformsToProtocol(Class, Protocol*);







>
>







182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#endif
extern SEL sel_registerName(const char*);
extern const char* sel_getName(SEL);
extern bool sel_isEqual(SEL, SEL);
extern id objc_lookUpClass(const char*);
extern id objc_getClass(const char*);
extern id objc_getRequiredClass(const char*);
extern unsigned int objc_getClassList(Class*, unsigned int);
extern Class* objc_copyClassList(unsigned int*);
extern bool class_isMetaClass(Class);
extern const char* class_getName(Class);
extern Class class_getSuperclass(Class);
extern bool class_isKindOfClass(Class, Class);
extern unsigned long class_getInstanceSize(Class);
extern bool class_respondsToSelector(Class, SEL);
extern bool class_conformsToProtocol(Class, Protocol*);