ObjFW  Diff

Differences From Artifact [a6a4aa7d0f]:

To Artifact [747427397b]:


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

-
+

















-
-
+
+

-
+

-
+




-
+







-
+

-
-
+
+




-
+

-
+















-
+

-
+


-
+


-
+





-
+

-
+









-
+


-
+



-
+

-
+

/*
 * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
 *
 * 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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "ObjFWRT.h"

#import "private.h"

#define TAGGED_POINTER_BITS 4
#define NUM_TAGGED_POINTER_CLASSES (1 << (TAGGED_POINTER_BITS - 1))
#define numTaggedPointerBits 4
#define maxNumTaggedPointerClasses (1 << (numTaggedPointerBits - 1))

Class objc_tagged_pointer_classes[NUM_TAGGED_POINTER_CLASSES];
Class objc_taggedPointerClasses[maxNumTaggedPointerClasses];
static int taggedPointerClassesCount;
uintptr_t objc_tagged_pointer_secret;
uintptr_t objc_taggedPointerSecret;

void
objc_setTaggedPointerSecret(uintptr_t secret)
{
	objc_tagged_pointer_secret = secret & ~(uintptr_t)1;
	objc_taggedPointerSecret = secret & ~(uintptr_t)1;
}

int
objc_registerTaggedPointerClass(Class class)
{
	int i;

	objc_global_mutex_lock();
	objc_globalMutex_lock();

	if (taggedPointerClassesCount == NUM_TAGGED_POINTER_CLASSES) {
		objc_global_mutex_unlock();
	if (taggedPointerClassesCount == maxNumTaggedPointerClasses) {
		objc_globalMutex_unlock();
		return -1;
	}

	i = taggedPointerClassesCount++;
	objc_tagged_pointer_classes[i] = class;
	objc_taggedPointerClasses[i] = class;

	objc_global_mutex_unlock();
	objc_globalMutex_unlock();

	return i;
}

bool
object_isTaggedPointer(id object)
{
	uintptr_t pointer = (uintptr_t)object;

	return pointer & 1;
}

Class
object_getTaggedPointerClass(id object)
{
	uintptr_t pointer = (uintptr_t)object ^ objc_tagged_pointer_secret;
	uintptr_t pointer = (uintptr_t)object ^ objc_taggedPointerSecret;

	pointer &= (1 << TAGGED_POINTER_BITS) - 1;
	pointer &= (1 << numTaggedPointerBits) - 1;
	pointer >>= 1;

	if (pointer >= NUM_TAGGED_POINTER_CLASSES)
	if (pointer >= maxNumTaggedPointerClasses)
		return Nil;

	return objc_tagged_pointer_classes[pointer];
	return objc_taggedPointerClasses[pointer];
}

uintptr_t
object_getTaggedPointerValue(id object)
{
	uintptr_t pointer = (uintptr_t)object ^ objc_tagged_pointer_secret;
	uintptr_t pointer = (uintptr_t)object ^ objc_taggedPointerSecret;

	pointer >>= TAGGED_POINTER_BITS;
	pointer >>= numTaggedPointerBits;

	return pointer;
}

id
objc_createTaggedPointer(int class, uintptr_t value)
{
	uintptr_t pointer;

	if (class < 0 || class >= NUM_TAGGED_POINTER_CLASSES)
	if (class < 0 || class >= maxNumTaggedPointerClasses)
		return nil;

	if (value > (UINTPTR_MAX >> TAGGED_POINTER_BITS))
	if (value > (UINTPTR_MAX >> numTaggedPointerBits))
		return nil;

	pointer = (class << 1) | 1;
	pointer |= (value << TAGGED_POINTER_BITS);
	pointer |= (value << numTaggedPointerBits);

	return (id)(pointer ^ objc_tagged_pointer_secret);
	return (id)(pointer ^ objc_taggedPointerSecret);
}