00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "comma/runtime/crt_types.h"
00010 #include "comma/runtime/crt_itable.h"
00011
00012 #include <stdarg.h>
00013 #include <stdbool.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016
00017 static bool lookup_instance(domain_info_t info,
00018 domain_view_t *args, domain_instance_t *instance)
00019 {
00020 if (!info->instance_table)
00021 info->instance_table = alloc_itable();
00022
00023 return itable_lookup(info->instance_table, info, args, instance);
00024 }
00025
00026
00027
00028
00029 domain_instance_t _comma_get_domain(domain_info_t info, ...)
00030 {
00031 domain_instance_t instance;
00032 unsigned i;
00033 domain_view_t *args;
00034
00035 if (info->arity) {
00036 args = malloc(sizeof(domain_view_t)*info->arity);
00037 for (i = 0; i < info->arity; ++i) {
00038 va_list ap;
00039 va_start(ap, info);
00040 args[i] = va_arg(ap, domain_view_t);
00041 }
00042 }
00043 else
00044 args = 0;
00045
00046
00047
00048
00049
00050
00051
00052
00053 if (lookup_instance(info, args, &instance))
00054 return instance;
00055
00056 instance->info = info;
00057 instance->params = args;
00058
00059
00060
00061
00062 for (i = 0; i < info->num_signatures; ++i) {
00063 ptrdiff_t offset = info->sig_offsets[i];
00064 instance->views[i].instance = instance;
00065 instance->views[i].index = i;
00066 }
00067
00068
00069
00070
00071 if (info->ctor != 0)
00072 info->ctor(instance);
00073
00074 return instance;
00075 }
00076
00077
00078
00079
00080
00081 domain_instance_t alloc_domain_instance(domain_info_t info)
00082 {
00083 domain_instance_t instance;
00084 instance = malloc(sizeof(struct domain_instance));
00085 instance->views = malloc(sizeof(struct domain_view)
00086 * info->num_signatures);
00087 return instance;
00088 }