]> git.lizzy.rs Git - rust.git/blob - src/rt/rust_crate_cache.cpp
47b402102f3b78ea3a2aa77c66eb44987a05d040
[rust.git] / src / rt / rust_crate_cache.cpp
1
2 #include "rust_internal.h"
3 #include <algorithm>
4
5 type_desc *
6 rust_crate_cache::get_type_desc(size_t size,
7                                 size_t align,
8                                 size_t n_descs,
9                                 type_desc const **descs,
10                                 uintptr_t n_obj_params)
11 {
12     I(sched, n_descs > 1);
13     type_desc *td = NULL;
14     size_t keysz = n_descs * sizeof(type_desc*);
15     HASH_FIND(hh, this->type_descs, descs, keysz, td);
16     if (td) {
17         DLOG(sched, cache, "rust_crate_cache::get_type_desc hit");
18
19         // FIXME: This is a gross hack.
20         td->n_obj_params = std::max(td->n_obj_params, n_obj_params);
21
22         return td;
23     }
24     DLOG(sched, cache, "rust_crate_cache::get_type_desc miss");
25     td = (type_desc*) sched->kernel->malloc(sizeof(type_desc) + keysz,
26                                             "crate cache typedesc");
27     if (!td)
28         return NULL;
29     // By convention, desc 0 is the root descriptor.
30     // but we ignore the size and alignment of it and use the
31     // passed-in, computed values.
32     memcpy(td, descs[0], sizeof(type_desc));
33     td->first_param = &td->descs[1];
34     td->size = size;
35     td->align = align;
36     for (size_t i = 0; i < n_descs; ++i) {
37         DLOG(sched, cache,
38                  "rust_crate_cache::descs[%" PRIdPTR "] = 0x%" PRIxPTR,
39                  i, descs[i]);
40         td->descs[i] = descs[i];
41     }
42     td->n_obj_params = n_obj_params;
43     td->n_params = n_descs - 1;
44     HASH_ADD(hh, this->type_descs, descs, keysz, td);
45     return td;
46 }
47
48 rust_crate_cache::rust_crate_cache(rust_scheduler *sched)
49     : type_descs(NULL),
50       sched(sched),
51       idx(0)
52 {
53 }
54
55 void
56 rust_crate_cache::flush() {
57     DLOG(sched, cache, "rust_crate_cache::flush()");
58
59     while (type_descs) {
60         type_desc *d = type_descs;
61         HASH_DEL(type_descs, d);
62         DLOG(sched, mem, "rust_crate_cache::flush() tydesc %" PRIxPTR, d);
63         sched->kernel->free(d);
64     }
65 }
66
67 rust_crate_cache::~rust_crate_cache()
68 {
69     flush();
70 }
71
72 //
73 // Local Variables:
74 // mode: C++
75 // fill-column: 78;
76 // indent-tabs-mode: nil
77 // c-basic-offset: 4
78 // buffer-file-coding-system: utf-8-unix
79 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
80 // End:
81 //