[qfcc] Merge method lists instead of copying

This is for adding methods to classes and protocols via their interface,
not for adding methods by adding protocols (they still get copied).
Slightly more memory efficient.
This commit is contained in:
Bill Currie 2020-03-03 00:11:54 +09:00
parent 679744fc7d
commit ed04e6fc23
3 changed files with 44 additions and 7 deletions

View file

@ -80,6 +80,8 @@ struct symbol_s *method_symbol (struct class_type_s *class_type,
void method_set_param_names (method_t *dst, method_t *src);
methodlist_t *new_methodlist (void);
//NOTE frees the source list and any methods not copied
void merge_method_lists (methodlist_t *dst, methodlist_t *src);
void copy_methods (methodlist_t *dst, methodlist_t *src);
int method_compare (method_t *m1, method_t *m2);

View file

@ -631,8 +631,7 @@ class_add_methods (class_t *class, methodlist_t *methods)
if (!methods)
return;
copy_methods (class->methods, methods);
free (methods);
merge_method_lists (class->methods, methods);
methods_set_self_type (class, class->methods);
}
@ -1259,8 +1258,7 @@ category_add_methods (category_t *category, methodlist_t *methods)
{
if (!methods)
return;
copy_methods (category->methods, methods);
free (methods);
merge_method_lists (category->methods, methods);
methods_set_self_type (category->class, category->methods);
}
@ -1518,8 +1516,7 @@ protocol_add_methods (protocol_t *protocol, methodlist_t *methods)
{
if (!methods)
return;
copy_methods (protocol->methods, methods);
free (methods);
merge_method_lists (protocol->methods, methods);
}
void

View file

@ -205,6 +205,38 @@ new_methodlist (void)
return l;
}
static int
method_in_list (methodlist_t *method_list, method_t *method)
{
method_t *m;
for (m = method_list->head; m; m = m->next) {
if (method_compare (m, method)) {
return 1;
}
}
return 0;
}
void
merge_method_lists (methodlist_t *dst, methodlist_t *src)
{
while (src->head) {
method_t *s = src->head;
src->head = s->next;
s->next = 0;
if (method_in_list (dst, s)) {
debug (0, "dropping duplicate method: %s", s->name);
free (s);
} else {
// add_method does the duplicate check
*dst->tail = s;
dst->tail = &s->next;
}
}
free (src);
}
void
copy_methods (methodlist_t *dst, methodlist_t *src)
{
@ -212,6 +244,10 @@ copy_methods (methodlist_t *dst, methodlist_t *src)
param_t *self;
for (s = src->head; s; s = s->next) {
if (method_in_list (dst, s)) {
debug (0, "skipping duplicate method: %s", s->name);
continue;
}
d = malloc (sizeof (method_t));
*d = *s;
// The above is only a shallow copy and thus even though the methods
@ -224,7 +260,9 @@ copy_methods (methodlist_t *dst, methodlist_t *src)
*self = *d->params;
d->params = self;
d->next = 0;
add_method (dst, d);
// add_method does the duplicate check
*dst->tail = d;
dst->tail = &d->next;
}
}