polar-objc 5.44
An Objective-C runtime library
Loading...
Searching...
No Matches
objc-method-description.c
1/* Internal API: protocol method description utility routines for polar-objc.
2 Copyright (C) 2022-2025 Michael Malicoat <[email protected]>
3
4 This file is part of polar-objc.
5
6 polar-objc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
8
9 polar-objc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 details.
12
13 You should have received a copy of the GNU General Public License along with this program; see the file LICENSE. If
14 not, see <http://www.gnu.org/licenses/>.
15*/
16
17// Public ==============================================================================================================
18
19POLAR_FUNCTION_INTERNAL struct objc_list_method_descriptions *
20objc_list_method_descriptions_new( int count_methods )
21{
22 struct objc_list_method_descriptions *result;
23 size_t size_result;
24
25 result = NULL;
26
27 if( count_methods > 0 )
28 {
29 size_result = sizeof(*result);
30
31 if( count_methods > 1 )
32 size_result += ( (count_methods - 1) * sizeof(struct objc_method_description) );
33
34 result = (struct objc_list_method_descriptions *)polar_memory_allocate(size_result);
35 result->count_methods = count_methods;
36 }
37
38 return result;
39}
40
41POLAR_FUNCTION_INTERNAL void
42objc_list_method_descriptions_add_method( struct objc_list_method_descriptions **p_list_target,
43 const char *name_method, const char *type_encoding_method )
44{
45 struct objc_list_method_descriptions *list_old, *list_new;
46 int count_descriptions;
47 size_t size_list_old, size_list_new;
48
49 assert( p_list_target != NULL );
50 assert( name_method != NULL );
51 assert( *name_method != 0 );
52 assert( type_encoding_method != NULL );
53 assert( *type_encoding_method != 0 );
54
55 list_old = *p_list_target;
56 if( list_old != NULL )
57 {
58 count_descriptions = list_old->count_methods;
59
60 size_list_old = objc_list_method_descriptions_get_size(list_old);
61 size_list_new = ( size_list_old + sizeof(struct objc_method_description) );
62
63 list_new = (struct objc_list_method_descriptions *)
64 polar_memory_reallocate(list_old, size_list_new, size_list_old);
65
66 list_new->count_methods++;
67 }
68
69 else
70 {
71 list_new = objc_list_method_descriptions_new(1);
72 count_descriptions = 0;
73 }
74
75 list_new->array_methods[count_descriptions].name_method = name_method;
76 list_new->array_methods[count_descriptions].type_encoding_method = type_encoding_method;
77
78 if( list_new != list_old )
79 *p_list_target = list_new;
80}
81
82POLAR_FUNCTION_INTERNAL int
83objc_list_method_descriptions_add_methods( struct objc_list_method_descriptions **p_list_target,
84 int count_methods, struct objc_method_description *array_descriptions )
85{
86 int result;
87 struct objc_list_method_descriptions *list_old, *list_new;
88 int count_descriptions;
89 size_t size_list_old, size_list_new;
90 struct objc_method_description *description_dest, *description_source;
91
92 assert( p_list_target != NULL );
93
94 if( (count_methods > 0) && (array_descriptions != NULL) )
95 ;
96
97 else
98 return 0;
99
100 list_old = *p_list_target;
101 if( list_old != NULL )
102 {
103 count_descriptions = list_old->count_methods;
104
105 size_list_old = objc_list_method_descriptions_get_size(list_old);
106 size_list_new = ( size_list_old + (sizeof(struct objc_method_description) * count_methods) );
107
108 list_new = (struct objc_list_method_descriptions *)
109 polar_memory_reallocate(list_old, size_list_new, size_list_old);
110
111 list_new->count_methods++;
112 }
113
114 else
115 {
116 list_new = objc_list_method_descriptions_new(count_methods);
117 count_descriptions = 0;
118 }
119
120 result = 0;
121 description_dest = &list_new->array_methods[count_descriptions];
122 description_source = array_descriptions;
123
124 for( ; count_descriptions < list_new->count_methods; count_descriptions++ )
125 {
126 if( (description_source->name_method != NULL) && (*(description_source->name_method) != 0) )
127 ;
128
129 else
130 break;
131
132 if( (description_source->type_encoding_method != NULL) && (*(description_source->type_encoding_method) != 0) )
133 ;
134
135 else
136 break;
137
138 description_dest->name_method = description_source->name_method;
139 description_dest->type_encoding_method = description_source->type_encoding_method;
140
141 description_dest++;
142 description_source++;
143 result++;
144 }
145
146 if( list_new != list_old )
147 *p_list_target = list_new;
148
149 return result;
150}
151
152POLAR_FUNCTION_INTERNAL OBJC_FUNCTION_PURE size_t
153objc_list_method_descriptions_get_size( struct objc_list_method_descriptions *list_target )
154{
155 size_t result;
156
157 if( list_target != NULL )
158 ;
159
160 else
161 return 0;
162
163 result = sizeof(*list_target);
164
165 if( list_target->count_methods > 1 )
166 result += ( (list_target->count_methods - 1) * sizeof(list_target->array_methods[0]) );
167
168 return result;
169}