]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/mdns_objects/mdns_object.c
mDNSResponder-1310.40.42.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / mdns_objects / mdns_object.c
1 /*
2 * Copyright (c) 2019 Apple Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "mdns_internal.h"
18 #include "mdns_object.h"
19
20 #include "mdns_objects.h"
21
22 #include <CoreUtils/CoreUtils.h>
23
24 //======================================================================================================================
25 // MARK: - Object Kind Definition
26
27 static char *
28 _mdns_object_copy_description(mdns_object_t object, bool debug, bool privacy);
29
30 const struct mdns_kind_s _mdns_object_kind = {
31 NULL, // No superkind.
32 "object", // Name.
33 _mdns_object_copy_description,
34 NULL, // No equal method.
35 NULL // No finalize method.
36 };
37
38 static const void *
39 _mdns_cf_collection_callback_retain(CFAllocatorRef allocator, const void *object);
40
41 static void
42 _mdns_cf_collection_callback_release(CFAllocatorRef allocator, const void *object);
43
44 static CFStringRef
45 _mdns_cf_collection_callback_copy_description(const void *object);
46
47 static Boolean
48 _mdns_cf_collection_callback_equal(const void *object1, const void *object2);
49
50 const CFArrayCallBacks mdns_cfarray_callbacks = {
51 0, // version
52 _mdns_cf_collection_callback_retain, // retain
53 _mdns_cf_collection_callback_release, // release
54 _mdns_cf_collection_callback_copy_description, // copy description
55 _mdns_cf_collection_callback_equal // equal
56 };
57
58 //======================================================================================================================
59 // MARK: - Object Public Methods
60
61 mdns_object_t
62 mdns_retain(mdns_object_t me)
63 {
64 return os_retain(me);
65 }
66
67 //======================================================================================================================
68
69 void
70 mdns_release(mdns_object_t me)
71 {
72 os_release(me);
73 }
74
75 //======================================================================================================================
76
77 char *
78 mdns_copy_description(mdns_object_t me)
79 {
80 return mdns_object_copy_description(me, false, false);
81 }
82
83 //======================================================================================================================
84
85 bool
86 mdns_equal(mdns_object_t me, mdns_object_t other)
87 {
88 if (me == other) {
89 return true;
90 }
91 if (me->kind != other->kind) {
92 return false;
93 }
94 if (me->kind->equal) {
95 return me->kind->equal(me, other);
96 }
97 return false;
98 }
99
100 //======================================================================================================================
101 // MARK: - Object Private Methods
102
103 static char *
104 _mdns_object_copy_description(mdns_object_t me, __unused bool debug, __unused bool privacy)
105 {
106 char *description = NULL;
107 asprintf(&description, "<%s: %p>", me->kind->name, (void *)me);
108 return description;
109 }
110
111 //======================================================================================================================
112
113 char *
114 mdns_object_copy_description(mdns_object_t me, bool debug, bool privacy)
115 {
116 for (mdns_kind_t kind = me->kind; kind; kind = kind->superkind) {
117 if (kind->copy_description) {
118 return kind->copy_description(me, debug, privacy);
119 }
120 }
121 return NULL;
122 }
123
124 //======================================================================================================================
125
126 CFStringRef
127 mdns_object_copy_description_as_cfstring_ex(mdns_object_t me, bool debug, bool privacy)
128 {
129 CFStringRef description = NULL;
130 char *cstring = mdns_object_copy_description(me, debug, privacy);
131 require_quiet(cstring, exit);
132
133 description = CFStringCreateWithCStringNoCopy(NULL, cstring, kCFStringEncodingUTF8, kCFAllocatorMalloc);
134 require_quiet(description, exit);
135 cstring = NULL;
136
137 exit:
138 FreeNullSafe(cstring);
139 return description;
140 }
141
142 //======================================================================================================================
143
144 void
145 mdns_object_finalize(mdns_object_t me)
146 {
147 for (mdns_kind_t kind = me->kind; kind; kind = kind->superkind) {
148 if (kind->finalize) {
149 kind->finalize(me);
150 }
151 }
152 }
153
154 //======================================================================================================================
155
156 static const void *
157 _mdns_cf_collection_callback_retain(__unused CFAllocatorRef allocator, const void *obj)
158 {
159 mdns_retain((mdns_object_t)obj);
160 return obj;
161 }
162
163 //======================================================================================================================
164
165 static void
166 _mdns_cf_collection_callback_release(__unused CFAllocatorRef allocator, const void *obj)
167 {
168 mdns_release((mdns_object_t)obj);
169 }
170
171 //======================================================================================================================
172
173 static CFStringRef
174 _mdns_cf_collection_callback_copy_description(const void *obj)
175 {
176 return mdns_object_copy_description_as_cfstring((mdns_object_t)obj);
177 }
178
179 //======================================================================================================================
180
181 static Boolean
182 _mdns_cf_collection_callback_equal(const void *object1, const void *object2)
183 {
184 return mdns_equal((mdns_object_t)object1, (mdns_object_t)object2);
185 }