2010-06-23 23:03:09 -05:00
|
|
|
#ifndef RUST_UTIL_H
|
|
|
|
#define RUST_UTIL_H
|
|
|
|
|
2011-08-10 07:17:30 -05:00
|
|
|
#include <limits.h>
|
2012-04-02 22:18:01 -05:00
|
|
|
#include "rust_task.h"
|
|
|
|
#include "rust_env.h"
|
2011-06-27 21:15:03 -05:00
|
|
|
|
2012-06-18 15:16:38 -05:00
|
|
|
extern struct type_desc str_body_tydesc;
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Inline fn used regularly elsewhere.
|
|
|
|
|
|
|
|
static inline size_t
|
|
|
|
next_power_of_two(size_t s)
|
|
|
|
{
|
|
|
|
size_t tmp = s - 1;
|
|
|
|
tmp |= tmp >> 1;
|
|
|
|
tmp |= tmp >> 2;
|
|
|
|
tmp |= tmp >> 4;
|
|
|
|
tmp |= tmp >> 8;
|
|
|
|
tmp |= tmp >> 16;
|
2011-08-10 12:55:41 -05:00
|
|
|
#ifdef _LP64
|
2010-06-23 23:03:09 -05:00
|
|
|
tmp |= tmp >> 32;
|
|
|
|
#endif
|
|
|
|
return tmp + 1;
|
|
|
|
}
|
|
|
|
|
2011-08-17 20:14:47 -05:00
|
|
|
// Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power
|
|
|
|
// of two.
|
|
|
|
template<typename T>
|
|
|
|
static inline T
|
|
|
|
align_to(T size, size_t alignment) {
|
|
|
|
assert(alignment);
|
|
|
|
T x = (T)(((uintptr_t)size + alignment - 1) & ~(alignment - 1));
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2011-06-13 20:52:04 -05:00
|
|
|
// Interior vectors (rust-user-code level).
|
|
|
|
|
|
|
|
struct
|
2011-08-25 03:18:02 -05:00
|
|
|
rust_vec
|
2011-06-13 20:52:04 -05:00
|
|
|
{
|
|
|
|
size_t fill; // in bytes; if zero, heapified
|
|
|
|
size_t alloc; // in bytes
|
2011-08-25 03:18:02 -05:00
|
|
|
uint8_t data[0];
|
2011-06-13 20:52:04 -05:00
|
|
|
};
|
|
|
|
|
2012-05-21 20:36:52 -05:00
|
|
|
struct
|
|
|
|
rust_vec_box
|
|
|
|
{
|
|
|
|
rust_opaque_box header;
|
|
|
|
rust_vec body;
|
|
|
|
};
|
|
|
|
|
2011-08-25 03:18:02 -05:00
|
|
|
template <typename T>
|
|
|
|
inline size_t vec_size(size_t elems) {
|
2012-05-21 20:36:52 -05:00
|
|
|
return sizeof(rust_vec_box) + sizeof(T) * elems;
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
|
|
|
|
2012-02-28 13:58:50 -06:00
|
|
|
template <typename T>
|
|
|
|
inline T *
|
|
|
|
vec_data(rust_vec *v) {
|
|
|
|
return reinterpret_cast<T*>(v->data);
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:36:52 -05:00
|
|
|
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;
|
2011-08-29 15:35:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:36:52 -05:00
|
|
|
inline void reserve_vec(rust_task* task, rust_vec_box** vpp, size_t size) {
|
2012-02-04 05:16:59 -06:00
|
|
|
reserve_vec_exact(task, vpp, next_power_of_two(size));
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:36:52 -05:00
|
|
|
typedef rust_vec_box rust_str;
|
2011-09-02 19:00:40 -05:00
|
|
|
|
|
|
|
inline rust_str *
|
2012-04-01 01:12:06 -05:00
|
|
|
make_str(rust_kernel* kernel, const char* c, size_t strlen,
|
|
|
|
const char* name) {
|
2011-09-01 15:40:11 -05:00
|
|
|
size_t str_fill = strlen + 1;
|
|
|
|
size_t str_alloc = str_fill;
|
2011-09-02 19:00:40 -05:00
|
|
|
rust_str *str = (rust_str *)
|
2011-09-01 15:40:11 -05:00
|
|
|
kernel->malloc(vec_size<char>(str_fill), name);
|
2012-06-18 15:16:38 -05:00
|
|
|
str->header.td = &str_body_tydesc;
|
2012-05-21 20:36:52 -05:00
|
|
|
str->body.fill = str_fill;
|
|
|
|
str->body.alloc = str_alloc;
|
|
|
|
memcpy(&str->body.data, c, strlen);
|
|
|
|
str->body.data[strlen] = '\0';
|
2011-09-01 15:40:11 -05:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:36:52 -05:00
|
|
|
inline rust_vec_box *
|
2012-02-07 20:55:02 -06:00
|
|
|
make_str_vec(rust_kernel* kernel, size_t nstrs, char **strs) {
|
2012-05-21 20:36:52 -05:00
|
|
|
rust_vec_box *v = (rust_vec_box *)
|
|
|
|
kernel->malloc(vec_size<rust_vec_box*>(nstrs),
|
2012-02-07 20:55:02 -06:00
|
|
|
"str vec interior");
|
2012-06-18 15:16:38 -05:00
|
|
|
// FIXME: should have a real td (Issue #2639)
|
|
|
|
v->header.td = NULL;
|
2012-05-21 20:36:52 -05:00
|
|
|
v->body.fill = v->body.alloc = sizeof(rust_vec_box*) * nstrs;
|
2012-02-07 20:55:02 -06:00
|
|
|
for (size_t i = 0; i < nstrs; ++i) {
|
|
|
|
rust_str *str = make_str(kernel, strs[i],
|
|
|
|
strlen(strs[i]),
|
|
|
|
"str");
|
2012-05-21 20:36:52 -05:00
|
|
|
((rust_str**)&v->body.data)[i] = str;
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-06-13 19:59:21 -05:00
|
|
|
inline size_t get_box_size(size_t body_size, size_t body_align) {
|
|
|
|
size_t header_size = sizeof(rust_opaque_box);
|
|
|
|
// FIXME: This alignment calculation is suspicious. Is it right?
|
|
|
|
size_t total_size = align_to(header_size, body_align) + body_size;
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
// Initialization helpers for ISAAC RNG
|
|
|
|
|
2012-05-21 19:42:32 -05:00
|
|
|
inline void isaac_seed(rust_kernel* kernel, uint8_t* dest)
|
2012-05-20 08:06:54 -05:00
|
|
|
{
|
|
|
|
size_t size = sizeof(ub4) * RANDSIZ;
|
|
|
|
#ifdef __WIN32__
|
|
|
|
HCRYPTPROV hProv;
|
|
|
|
kernel->win32_require
|
|
|
|
(_T("CryptAcquireContext"),
|
|
|
|
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
|
|
|
|
kernel->win32_require
|
|
|
|
(_T("CryptGenRandom"), CryptGenRandom(hProv, size, (BYTE*) dest));
|
|
|
|
kernel->win32_require
|
|
|
|
(_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0));
|
|
|
|
#else
|
|
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
assert(fd > 0);
|
|
|
|
assert(read(fd, dest, size) == (int) size);
|
|
|
|
assert(close(fd) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2012-06-05 01:26:06 -05:00
|
|
|
isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed)
|
2012-05-20 08:06:54 -05:00
|
|
|
{
|
|
|
|
memset(rctx, 0, sizeof(randctx));
|
|
|
|
|
|
|
|
char *env_seed = kernel->env->rust_seed;
|
|
|
|
if (user_seed != NULL) {
|
|
|
|
// ignore bytes after the required length
|
2012-06-05 01:26:06 -05:00
|
|
|
size_t seed_len = user_seed->body.fill < sizeof(rctx->randrsl)
|
|
|
|
? user_seed->body.fill : sizeof(rctx->randrsl);
|
|
|
|
memcpy(&rctx->randrsl, user_seed->body.data, seed_len);
|
2012-05-20 08:06:54 -05:00
|
|
|
} else if (env_seed != NULL) {
|
|
|
|
ub4 seed = (ub4) atoi(env_seed);
|
|
|
|
for (size_t i = 0; i < RANDSIZ; i ++) {
|
|
|
|
memcpy(&rctx->randrsl[i], &seed, sizeof(ub4));
|
|
|
|
seed = (seed + 0x7ed55d16) + (seed << 12);
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-21 19:42:32 -05:00
|
|
|
isaac_seed(kernel, (uint8_t*) &rctx->randrsl);
|
2012-05-20 08:06:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
randinit(rctx, 1);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|
|
|
|
|
|
|
|
#endif
|