stop using reinterpret_cast
parent
6ffdc7fa91
commit
263b12c1c4
|
@ -90,7 +90,8 @@ std::uint32_t *bcode_alloc(internal_state *cs, std::size_t sz) {
|
||||||
auto a = std_allocator<std::uint32_t>{cs};
|
auto a = std_allocator<std::uint32_t>{cs};
|
||||||
std::size_t hdrs = sizeof(bcode_hdr) / sizeof(std::uint32_t);
|
std::size_t hdrs = sizeof(bcode_hdr) / sizeof(std::uint32_t);
|
||||||
auto p = a.allocate(sz + hdrs - 1);
|
auto p = a.allocate(sz + hdrs - 1);
|
||||||
bcode_hdr *hdr = reinterpret_cast<bcode_hdr *>(p);
|
bcode_hdr *hdr;
|
||||||
|
std::memcpy(&hdr, &p, sizeof(hdr));
|
||||||
hdr->cs = cs;
|
hdr->cs = cs;
|
||||||
hdr->asize = sz + hdrs - 1;
|
hdr->asize = sz + hdrs - 1;
|
||||||
return p + hdrs - 1;
|
return p + hdrs - 1;
|
||||||
|
@ -99,7 +100,8 @@ std::uint32_t *bcode_alloc(internal_state *cs, std::size_t sz) {
|
||||||
/* bc's address must be the 'init' member of the header */
|
/* bc's address must be the 'init' member of the header */
|
||||||
static inline void bcode_free(std::uint32_t *bc) {
|
static inline void bcode_free(std::uint32_t *bc) {
|
||||||
auto *rp = bc + 1 - (sizeof(bcode_hdr) / sizeof(std::uint32_t));
|
auto *rp = bc + 1 - (sizeof(bcode_hdr) / sizeof(std::uint32_t));
|
||||||
bcode_hdr *hdr = reinterpret_cast<bcode_hdr *>(rp);
|
bcode_hdr *hdr;
|
||||||
|
std::memcpy(&hdr, &rp, sizeof(hdr));
|
||||||
std_allocator<std::uint32_t>{hdr->cs}.deallocate(rp, hdr->asize);
|
std_allocator<std::uint32_t>{hdr->cs}.deallocate(rp, hdr->asize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,10 @@ std::uint32_t gen_state::peek(std::size_t idx) const {
|
||||||
bcode_ref gen_state::steal_ref() {
|
bcode_ref gen_state::steal_ref() {
|
||||||
auto *cp = bcode_alloc(ts.istate, code.size());
|
auto *cp = bcode_alloc(ts.istate, code.size());
|
||||||
std::memcpy(cp, code.data(), code.size() * sizeof(std::uint32_t));
|
std::memcpy(cp, code.data(), code.size() * sizeof(std::uint32_t));
|
||||||
return bcode_p::make_ref(reinterpret_cast<bcode *>(cp + 1));
|
bcode *b;
|
||||||
|
cp += 1;
|
||||||
|
std::memcpy(&b, &cp, sizeof(b));
|
||||||
|
return bcode_p::make_ref(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
void gen_state::gen_pop() {
|
void gen_state::gen_pop() {
|
||||||
|
@ -179,8 +182,10 @@ void gen_state::gen_val_string(std::string_view v) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
code.push_back(BC_INST_VAL | BC_RET_STRING | std::uint32_t(vsz << 8));
|
code.push_back(BC_INST_VAL | BC_RET_STRING | std::uint32_t(vsz << 8));
|
||||||
auto it = reinterpret_cast<std::uint32_t const *>(v.data());
|
std::uint32_t *wp;
|
||||||
code.append(it, it + (v.size() / sizeof(std::uint32_t)));
|
auto *sp = v.data();
|
||||||
|
std::memcpy(&wp, &sp, sizeof(wp));
|
||||||
|
code.append(wp, wp + (v.size() / sizeof(std::uint32_t)));
|
||||||
std::size_t esz = v.size() % sizeof(std::uint32_t);
|
std::size_t esz = v.size() % sizeof(std::uint32_t);
|
||||||
char c[sizeof(std::uint32_t)] = {0};
|
char c[sizeof(std::uint32_t)] = {0};
|
||||||
std::memcpy(c, v.data() + v.size() - esz, esz);
|
std::memcpy(c, v.data() + v.size() - esz, esz);
|
||||||
|
@ -208,7 +213,8 @@ static void gen_str_filter(
|
||||||
memset(&buf[len], 0, sizeof(std::uint32_t) - len % sizeof(std::uint32_t));
|
memset(&buf[len], 0, sizeof(std::uint32_t) - len % sizeof(std::uint32_t));
|
||||||
/* set the actual length */
|
/* set the actual length */
|
||||||
code.back() |= (len << 8);
|
code.back() |= (len << 8);
|
||||||
auto *ubuf = reinterpret_cast<std::uint32_t *>(buf);
|
std::uint32_t *ubuf;
|
||||||
|
std::memcpy(&ubuf, &buf, sizeof(ubuf));
|
||||||
code.append(ubuf, ubuf + ((len / sizeof(std::uint32_t)) + 1));
|
code.append(ubuf, ubuf + ((len / sizeof(std::uint32_t)) + 1));
|
||||||
al.deallocate(buf, nwords * sizeof(std::uint32_t));
|
al.deallocate(buf, nwords * sizeof(std::uint32_t));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
#include <cubescript/cubescript.hh>
|
#include <cubescript/cubescript.hh>
|
||||||
|
|
||||||
#include "cs_strman.hh"
|
#include "cs_strman.hh"
|
||||||
|
@ -13,9 +14,9 @@ struct string_ref_state {
|
||||||
};
|
};
|
||||||
|
|
||||||
inline string_ref_state *get_ref_state(char const *ptr) {
|
inline string_ref_state *get_ref_state(char const *ptr) {
|
||||||
return const_cast<string_ref_state *>(
|
string_ref_state *r;
|
||||||
reinterpret_cast<string_ref_state const *>(ptr)
|
std::memcpy(&r, &ptr, sizeof(r));
|
||||||
) - 1;
|
return r - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char const *string_pool::add(std::string_view str) {
|
char const *string_pool::add(std::string_view str) {
|
||||||
|
@ -26,7 +27,10 @@ char const *string_pool::add(std::string_view str) {
|
||||||
/* having a null pointer is the same as non-existence */
|
/* having a null pointer is the same as non-existence */
|
||||||
if (st) {
|
if (st) {
|
||||||
++st->refcount;
|
++st->refcount;
|
||||||
return reinterpret_cast<char const *>(st + 1);
|
st += 1;
|
||||||
|
char const *r;
|
||||||
|
std::memcpy(&r, &st, sizeof(r));
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* not present: allocate brand new data */
|
/* not present: allocate brand new data */
|
||||||
|
@ -55,7 +59,10 @@ string_ref string_pool::steal(char *ptr) {
|
||||||
if (st) {
|
if (st) {
|
||||||
/* the buffer is superfluous now */
|
/* the buffer is superfluous now */
|
||||||
cstate->alloc(ss, ss->length + sizeof(string_ref_state) + 1, 0);
|
cstate->alloc(ss, ss->length + sizeof(string_ref_state) + 1, 0);
|
||||||
return string_ref{reinterpret_cast<char const *>(st + 1)};
|
st += 1;
|
||||||
|
char const *rp;
|
||||||
|
std::memcpy(&rp, &st, sizeof(rp));
|
||||||
|
return string_ref{rp};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ss->refcount = 0; /* string_ref will increment it */
|
ss->refcount = 0; /* string_ref will increment it */
|
||||||
|
@ -91,7 +98,10 @@ char const *string_pool::find(std::string_view str) const {
|
||||||
if (it == counts.end()) {
|
if (it == counts.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return reinterpret_cast<char const *>(it->second + 1);
|
auto *sp = it->second + 1;
|
||||||
|
char const *rp;
|
||||||
|
std::memcpy(&rp, &sp, sizeof(rp));
|
||||||
|
return rp;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view string_pool::get(char const *ptr) const {
|
std::string_view string_pool::get(char const *ptr) const {
|
||||||
|
@ -107,7 +117,9 @@ char *string_pool::alloc_buf(std::size_t len) const {
|
||||||
sst->length = len;
|
sst->length = len;
|
||||||
sst->refcount = 1;
|
sst->refcount = 1;
|
||||||
/* pre-terminate */
|
/* pre-terminate */
|
||||||
auto *strp = reinterpret_cast<char *>(sst + 1);
|
char *strp;
|
||||||
|
sst += 1;
|
||||||
|
std::memcpy(&strp, &sst, sizeof(strp));
|
||||||
strp[len] = '\0';
|
strp[len] = '\0';
|
||||||
/* now the user can fill it */
|
/* now the user can fill it */
|
||||||
return strp;
|
return strp;
|
||||||
|
|
34
src/cs_vm.cc
34
src/cs_vm.cc
|
@ -359,24 +359,27 @@ std::uint32_t *vm_exec(
|
||||||
switch (op & BC_INST_RET_MASK) {
|
switch (op & BC_INST_RET_MASK) {
|
||||||
case BC_RET_STRING: {
|
case BC_RET_STRING: {
|
||||||
auto len = op >> 8;
|
auto len = op >> 8;
|
||||||
args.emplace_back().set_string(std::string_view{
|
char const *str;
|
||||||
reinterpret_cast<char const *>(code), len
|
std::memcpy(&str, &code, sizeof(str));
|
||||||
}, cs);
|
std::string_view sv{str, len};
|
||||||
|
args.emplace_back().set_string(sv, cs);
|
||||||
code += len / sizeof(std::uint32_t) + 1;
|
code += len / sizeof(std::uint32_t) + 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case BC_RET_INT:
|
case BC_RET_INT: {
|
||||||
args.emplace_back().set_integer(
|
integer_type i;
|
||||||
*reinterpret_cast<integer_type const *>(code)
|
std::memcpy(&i, code, sizeof(i));
|
||||||
);
|
args.emplace_back().set_integer(i);
|
||||||
code += bc_store_size<integer_type>;
|
code += bc_store_size<integer_type>;
|
||||||
continue;
|
continue;
|
||||||
case BC_RET_FLOAT:
|
}
|
||||||
args.emplace_back().set_float(
|
case BC_RET_FLOAT: {
|
||||||
*reinterpret_cast<float_type const *>(code)
|
float_type f;
|
||||||
);
|
std::memcpy(&f, code, sizeof(f));
|
||||||
|
args.emplace_back().set_float(f);
|
||||||
code += bc_store_size<float_type>;
|
code += bc_store_size<float_type>;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -501,10 +504,11 @@ std::uint32_t *vm_exec(
|
||||||
|
|
||||||
case BC_INST_BLOCK: {
|
case BC_INST_BLOCK: {
|
||||||
std::uint32_t len = op >> 8;
|
std::uint32_t len = op >> 8;
|
||||||
args.emplace_back().set_code(bcode_p::make_ref(
|
bcode *b;
|
||||||
reinterpret_cast<bcode *>(code + 1)
|
code += 1;
|
||||||
));
|
std::memcpy(&b, &code, sizeof(b));
|
||||||
code += len;
|
args.emplace_back().set_code(bcode_p::make_ref(b));
|
||||||
|
code += len - 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue