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"
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Reference counted objects
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
rc_base<T>::rc_base() :
|
2010-07-19 16:05:18 -05:00
|
|
|
ref_count(1)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
rc_base<T>::~rc_base()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility type: pointer-vector.
|
|
|
|
|
|
|
|
template <typename T>
|
2011-06-27 21:15:03 -05:00
|
|
|
ptr_vec<T>::ptr_vec(rust_task *task) :
|
|
|
|
task(task),
|
2010-06-23 23:03:09 -05:00
|
|
|
alloc(INIT_SIZE),
|
|
|
|
fill(0),
|
2011-07-18 14:02:26 -05:00
|
|
|
data(new (task, "ptr_vec<T>") T*[alloc])
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data);
|
|
|
|
DLOG(task->sched, mem, "new ptr_vec(data=0x%" PRIxPTR ") -> 0x%" PRIxPTR,
|
2011-04-19 05:21:57 -05:00
|
|
|
(uintptr_t)data, (uintptr_t)this);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
ptr_vec<T>::~ptr_vec()
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data);
|
|
|
|
DLOG(task->sched, mem, "~ptr_vec 0x%" PRIxPTR ", data=0x%" PRIxPTR,
|
2011-04-19 05:21:57 -05:00
|
|
|
(uintptr_t)this, (uintptr_t)data);
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, fill == 0);
|
2011-06-27 21:15:03 -05:00
|
|
|
task->free(data);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> T *&
|
|
|
|
ptr_vec<T>::operator[](size_t offset) {
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data[offset]->idx == offset);
|
2010-06-23 23:03:09 -05:00
|
|
|
return data[offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
ptr_vec<T>::push(T *p)
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data);
|
|
|
|
I(task->sched, fill <= alloc);
|
2010-06-23 23:03:09 -05:00
|
|
|
if (fill == alloc) {
|
|
|
|
alloc *= 2;
|
2011-06-27 21:15:03 -05:00
|
|
|
data = (T **)task->realloc(data, alloc * sizeof(T*));
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, fill < alloc);
|
2010-06-23 23:03:09 -05:00
|
|
|
p->idx = fill;
|
|
|
|
data[fill++] = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T *
|
|
|
|
ptr_vec<T>::pop()
|
|
|
|
{
|
|
|
|
return data[--fill];
|
|
|
|
}
|
|
|
|
|
2010-08-09 10:01:40 -05:00
|
|
|
template <typename T>
|
|
|
|
T *
|
|
|
|
ptr_vec<T>::peek()
|
|
|
|
{
|
|
|
|
return data[fill - 1];
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
ptr_vec<T>::trim(size_t sz)
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data);
|
2010-06-23 23:03:09 -05:00
|
|
|
if (sz <= (alloc / 4) &&
|
|
|
|
(alloc / 2) >= INIT_SIZE) {
|
|
|
|
alloc /= 2;
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, alloc >= fill);
|
2011-06-27 21:15:03 -05:00
|
|
|
data = (T **)task->realloc(data, alloc * sizeof(T*));
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void
|
2010-07-19 16:05:18 -05:00
|
|
|
ptr_vec<T>::swap_delete(T *item)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
|
|
|
/* Swap the endpoint into i and decr fill. */
|
2011-06-28 14:15:41 -05:00
|
|
|
I(task->sched, data);
|
|
|
|
I(task->sched, fill > 0);
|
|
|
|
I(task->sched, item->idx < fill);
|
2010-06-23 23:03:09 -05:00
|
|
|
fill--;
|
|
|
|
if (fill > 0) {
|
|
|
|
T *subst = data[fill];
|
|
|
|
size_t idx = item->idx;
|
|
|
|
data[idx] = subst;
|
|
|
|
subst->idx = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
#if SIZE_MAX == UINT64_MAX
|
|
|
|
tmp |= tmp >> 32;
|
|
|
|
#endif
|
|
|
|
return tmp + 1;
|
|
|
|
}
|
|
|
|
|
2010-07-25 23:45:09 -05:00
|
|
|
// Initialization helper for ISAAC RNG
|
|
|
|
|
|
|
|
static inline void
|
2011-06-28 14:15:41 -05:00
|
|
|
isaac_init(rust_scheduler *sched, randctx *rctx)
|
2010-07-25 23:45:09 -05:00
|
|
|
{
|
|
|
|
memset(rctx, 0, sizeof(randctx));
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
|
|
|
{
|
|
|
|
HCRYPTPROV hProv;
|
2011-06-28 14:15:41 -05:00
|
|
|
sched->kernel->win32_require
|
2010-07-25 23:45:09 -05:00
|
|
|
(_T("CryptAcquireContext"),
|
|
|
|
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
|
2011-06-28 14:15:41 -05:00
|
|
|
sched->kernel->win32_require
|
2010-07-25 23:45:09 -05:00
|
|
|
(_T("CryptGenRandom"),
|
|
|
|
CryptGenRandom(hProv, sizeof(rctx->randrsl),
|
|
|
|
(BYTE*)(&rctx->randrsl)));
|
2011-06-28 14:15:41 -05:00
|
|
|
sched->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
|
|
|
char *rust_seed = getenv("RUST_SEED");
|
|
|
|
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 {
|
|
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
2011-06-28 14:15:41 -05:00
|
|
|
I(sched, fd > 0);
|
2011-07-13 17:44:09 -05:00
|
|
|
I(sched,
|
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));
|
2011-06-28 14:15:41 -05:00
|
|
|
I(sched, close(fd) == 0);
|
2010-08-09 09:30:48 -05:00
|
|
|
}
|
2010-07-25 23:45:09 -05:00
|
|
|
#endif
|
|
|
|
randinit(rctx, 1);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Vectors (rust-user-code level).
|
|
|
|
|
|
|
|
struct
|
|
|
|
rust_vec : public rc_base<rust_vec>
|
|
|
|
{
|
|
|
|
size_t alloc;
|
|
|
|
size_t fill;
|
2011-04-02 21:03:43 -05:00
|
|
|
size_t pad; // Pad to align data[0] to 16 bytes.
|
2010-06-23 23:03:09 -05:00
|
|
|
uint8_t data[];
|
2011-07-18 14:02:26 -05:00
|
|
|
rust_vec(size_t alloc, size_t fill,
|
2011-07-13 17:44:09 -05:00
|
|
|
uint8_t const *d)
|
2011-06-28 14:15:41 -05:00
|
|
|
: alloc(alloc),
|
|
|
|
fill(fill)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-03-10 08:56:51 -06:00
|
|
|
if (d)
|
2010-06-23 23:03:09 -05:00
|
|
|
memcpy(&data[0], d, fill);
|
|
|
|
}
|
|
|
|
~rust_vec() {}
|
2011-07-07 15:06:38 -05:00
|
|
|
|
|
|
|
inline void *operator new(size_t size, void *mem) {
|
|
|
|
return mem;
|
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Rust types vec and str look identical from our perspective.
|
|
|
|
typedef rust_vec rust_str;
|
|
|
|
|
2011-06-13 20:52:04 -05:00
|
|
|
// Interior vectors (rust-user-code level).
|
|
|
|
|
|
|
|
struct
|
|
|
|
rust_ivec_heap
|
|
|
|
{
|
|
|
|
size_t fill;
|
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
|
2011-07-19 18:55:09 -05:00
|
|
|
// Note that the payload is actually size 4*sizeof(elem), even when heapified
|
2011-06-13 20:52:04 -05:00
|
|
|
union
|
|
|
|
rust_ivec_payload
|
|
|
|
{
|
2011-06-14 20:22:36 -05:00
|
|
|
rust_ivec_heap *ptr; // if on heap
|
|
|
|
uint8_t data[]; // if on stack
|
2011-06-13 20:52:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct
|
|
|
|
rust_ivec
|
|
|
|
{
|
|
|
|
size_t fill; // in bytes; if zero, heapified
|
|
|
|
size_t alloc; // in bytes
|
|
|
|
rust_ivec_payload payload;
|
|
|
|
};
|
|
|
|
|
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
|
2011-07-13 15:51:20 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-06-23 23:03:09 -05:00
|
|
|
// End:
|
|
|
|
//
|
|
|
|
|
|
|
|
#endif
|