Various changes for self-describing vecs

This commit is contained in:
Brian Anderson
2012-05-21 18:36:52 -07:00
parent 846dfbc922
commit 09a1b94907
9 changed files with 204 additions and 110 deletions

View File

@@ -42,9 +42,16 @@ rust_vec
uint8_t data[0];
};
struct
rust_vec_box
{
rust_opaque_box header;
rust_vec body;
};
template <typename T>
inline size_t vec_size(size_t elems) {
return sizeof(rust_vec) + sizeof(T) * elems;
return sizeof(rust_vec_box) + sizeof(T) * elems;
}
template <typename T>
@@ -53,19 +60,20 @@ vec_data(rust_vec *v) {
return reinterpret_cast<T*>(v->data);
}
inline void reserve_vec_exact(rust_task* task, rust_vec** vpp, size_t size) {
if (size > (*vpp)->alloc) {
*vpp = (rust_vec*)task->kernel
->realloc(*vpp, size + sizeof(rust_vec));
(*vpp)->alloc = size;
inline void reserve_vec_exact(rust_task* task, rust_vec_box** vpp,
size_t size) {
if (size > (*vpp)->body.alloc) {
*vpp = (rust_vec_box*)task->kernel
->realloc(*vpp, size + sizeof(rust_vec_box));
(*vpp)->body.alloc = size;
}
}
inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) {
inline void reserve_vec(rust_task* task, rust_vec_box** vpp, size_t size) {
reserve_vec_exact(task, vpp, next_power_of_two(size));
}
typedef rust_vec rust_str;
typedef rust_vec_box rust_str;
inline rust_str *
make_str(rust_kernel* kernel, const char* c, size_t strlen,
@@ -74,24 +82,24 @@ make_str(rust_kernel* kernel, const char* c, size_t strlen,
size_t str_alloc = str_fill;
rust_str *str = (rust_str *)
kernel->malloc(vec_size<char>(str_fill), name);
str->fill = str_fill;
str->alloc = str_alloc;
memcpy(&str->data, c, strlen);
str->data[strlen] = '\0';
str->body.fill = str_fill;
str->body.alloc = str_alloc;
memcpy(&str->body.data, c, strlen);
str->body.data[strlen] = '\0';
return str;
}
inline rust_vec *
inline rust_vec_box *
make_str_vec(rust_kernel* kernel, size_t nstrs, char **strs) {
rust_vec *v = (rust_vec *)
kernel->malloc(vec_size<rust_vec*>(nstrs),
rust_vec_box *v = (rust_vec_box *)
kernel->malloc(vec_size<rust_vec_box*>(nstrs),
"str vec interior");
v->fill = v->alloc = sizeof(rust_vec*) * nstrs;
v->body.fill = v->body.alloc = sizeof(rust_vec_box*) * nstrs;
for (size_t i = 0; i < nstrs; ++i) {
rust_str *str = make_str(kernel, strs[i],
strlen(strs[i]),
"str");
((rust_str**)&v->data)[i] = str;
((rust_str**)&v->body.data)[i] = str;
}
return v;
}