2010-06-23 23:03:09 -05:00
|
|
|
#ifndef RUST_UTIL_H
|
|
|
|
#define RUST_UTIL_H
|
|
|
|
|
2011-06-27 21:15:03 -05:00
|
|
|
#include "rust_task.h"
|
2011-08-10 07:17:30 -05:00
|
|
|
#include <limits.h>
|
2011-06-27 21:15:03 -05:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-25 23:45:09 -05:00
|
|
|
// Initialization helper for ISAAC RNG
|
|
|
|
|
2012-02-03 20:42:12 -06:00
|
|
|
inline void
|
|
|
|
isaac_init(rust_kernel *kernel, randctx *rctx)
|
2010-07-25 23:45:09 -05:00
|
|
|
{
|
|
|
|
memset(rctx, 0, sizeof(randctx));
|
|
|
|
|
2012-02-03 20:42:12 -06:00
|
|
|
char *rust_seed = kernel->env->rust_seed;
|
2011-08-18 18:24:36 -05:00
|
|
|
if (rust_seed != NULL) {
|
|
|
|
ub4 seed = (ub4) atoi(rust_seed);
|
|
|
|
for (size_t i = 0; i < RANDSIZ; i ++) {
|
|
|
|
memcpy(&rctx->randrsl[i], &seed, sizeof(ub4));
|
|
|
|
seed = (seed + 0x7ed55d16) + (seed << 12);
|
|
|
|
}
|
|
|
|
} else {
|
2010-07-25 23:45:09 -05:00
|
|
|
#ifdef __WIN32__
|
|
|
|
HCRYPTPROV hProv;
|
2012-02-03 20:42:12 -06:00
|
|
|
kernel->win32_require
|
2010-07-25 23:45:09 -05:00
|
|
|
(_T("CryptAcquireContext"),
|
|
|
|
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
|
2012-02-03 20:42:12 -06:00
|
|
|
kernel->win32_require
|
2010-07-25 23:45:09 -05:00
|
|
|
(_T("CryptGenRandom"),
|
|
|
|
CryptGenRandom(hProv, sizeof(rctx->randrsl),
|
|
|
|
(BYTE*)(&rctx->randrsl)));
|
2012-02-03 20:42:12 -06:00
|
|
|
kernel->win32_require
|
2010-07-25 23:45:09 -05:00
|
|
|
(_T("CryptReleaseContext"),
|
|
|
|
CryptReleaseContext(hProv, 0));
|
|
|
|
#else
|
2010-08-09 09:30:48 -05:00
|
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
2012-02-03 20:42:12 -06:00
|
|
|
I(kernel, fd > 0);
|
|
|
|
I(kernel,
|
2011-06-27 21:15:03 -05:00
|
|
|
read(fd, (void*) &rctx->randrsl, sizeof(rctx->randrsl))
|
2010-08-09 09:30:48 -05:00
|
|
|
== sizeof(rctx->randrsl));
|
2012-02-03 20:42:12 -06:00
|
|
|
I(kernel, close(fd) == 0);
|
2010-07-25 23:45:09 -05:00
|
|
|
#endif
|
2011-08-18 18:24:36 -05:00
|
|
|
}
|
|
|
|
|
2010-07-25 23:45:09 -05:00
|
|
|
randinit(rctx, 1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2011-08-25 03:18:02 -05:00
|
|
|
template <typename T>
|
|
|
|
inline size_t vec_size(size_t elems) {
|
|
|
|
return sizeof(rust_vec) + sizeof(T) * elems;
|
|
|
|
}
|
|
|
|
|
2012-02-04 05:16:59 -06:00
|
|
|
inline void reserve_vec_exact(rust_task* task, rust_vec** vpp, size_t size) {
|
2011-08-29 15:35:29 -05:00
|
|
|
if (size > (*vpp)->alloc) {
|
2012-02-04 05:16:59 -06:00
|
|
|
*vpp = (rust_vec*)task->kernel->realloc(*vpp, size + sizeof(rust_vec));
|
|
|
|
(*vpp)->alloc = size;
|
2011-08-29 15:35:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-04 05:16:59 -06:00
|
|
|
inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) {
|
|
|
|
reserve_vec_exact(task, vpp, next_power_of_two(size));
|
|
|
|
}
|
|
|
|
|
2011-09-02 19:00:40 -05:00
|
|
|
typedef rust_vec rust_str;
|
|
|
|
|
|
|
|
inline rust_str *
|
2012-01-11 11:09:46 -06: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);
|
|
|
|
str->fill = str_fill;
|
|
|
|
str->alloc = str_alloc;
|
|
|
|
memcpy(&str->data, c, strlen);
|
|
|
|
str->data[strlen] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
inline rust_vec *
|
|
|
|
make_str_vec(rust_kernel* kernel, size_t nstrs, char **strs) {
|
|
|
|
rust_vec *v = (rust_vec *)
|
|
|
|
kernel->malloc(vec_size<rust_vec*>(nstrs),
|
|
|
|
"str vec interior");
|
|
|
|
v->fill = v->alloc = sizeof(rust_vec*) * 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;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
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
|