Browse Source

stop using reinterpret_cast

master
Daniel Kolesa 3 months ago
parent
commit
263b12c1c4
  1. 6
      src/cs_bcode.cc
  2. 14
      src/cs_gen.cc
  3. 26
      src/cs_strman.cc
  4. 34
      src/cs_vm.cc

6
src/cs_bcode.cc

@ -90,7 +90,8 @@ std::uint32_t *bcode_alloc(internal_state *cs, std::size_t sz) {
auto a = std_allocator<std::uint32_t>{cs};
std::size_t hdrs = sizeof(bcode_hdr) / sizeof(std::uint32_t);
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->asize = sz + 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 */
static inline void bcode_free(std::uint32_t *bc) {
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);
}

14
src/cs_gen.cc

@ -27,7 +27,10 @@ std::uint32_t gen_state::peek(std::size_t idx) const {
bcode_ref gen_state::steal_ref() {
auto *cp = bcode_alloc(ts.istate, code.size());
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() {
@ -179,8 +182,10 @@ void gen_state::gen_val_string(std::string_view v) {
return;
}
code.push_back(BC_INST_VAL | BC_RET_STRING | std::uint32_t(vsz << 8));
auto it = reinterpret_cast<std::uint32_t const *>(v.data());
code.append(it, it + (v.size() / sizeof(std::uint32_t)));
std::uint32_t *wp;
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);
char c[sizeof(std::uint32_t)] = {0};
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));
/* set the actual length */
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));
al.deallocate(buf, nwords * sizeof(std::uint32_t));
}

26
src/cs_strman.cc

@ -1,4 +1,5 @@
#include <cassert>
#include <cstring>
#include <cubescript/cubescript.hh>
#include "cs_strman.hh"
@ -13,9 +14,9 @@ struct string_ref_state {
};
inline string_ref_state *get_ref_state(char const *ptr) {
return const_cast<string_ref_state *>(
reinterpret_cast<string_ref_state const *>(ptr)
) - 1;
string_ref_state *r;
std::memcpy(&r, &ptr, sizeof(r));
return r - 1;
}
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 */
if (st) {
++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 */
@ -55,7 +59,10 @@ string_ref string_pool::steal(char *ptr) {
if (st) {
/* the buffer is superfluous now */
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 */
@ -91,7 +98,10 @@ char const *string_pool::find(std::string_view str) const {
if (it == counts.end()) {
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 {
@ -107,7 +117,9 @@ char *string_pool::alloc_buf(std::size_t len) const {
sst->length = len;
sst->refcount = 1;
/* pre-terminate */
auto *strp = reinterpret_cast<char *>(sst + 1);
char *strp;
sst += 1;
std::memcpy(&strp, &sst, sizeof(strp));
strp[len] = '\0';
/* now the user can fill it */
return strp;

34
src/cs_vm.cc

@ -359,24 +359,27 @@ std::uint32_t *vm_exec(
switch (op & BC_INST_RET_MASK) {
case BC_RET_STRING: {
auto len = op >> 8;
args.emplace_back().set_string(std::string_view{
reinterpret_cast<char const *>(code), len
}, cs);
char const *str;
std::memcpy(&str, &code, sizeof(str));
std::string_view sv{str, len};
args.emplace_back().set_string(sv, cs);
code += len / sizeof(std::uint32_t) + 1;
continue;
}
case BC_RET_INT:
args.emplace_back().set_integer(
*reinterpret_cast<integer_type const *>(code)
);
case BC_RET_INT: {
integer_type i;
std::memcpy(&i, code, sizeof(i));
args.emplace_back().set_integer(i);
code += bc_store_size<integer_type>;
continue;
case BC_RET_FLOAT:
args.emplace_back().set_float(
*reinterpret_cast<float_type const *>(code)
);
}
case BC_RET_FLOAT: {
float_type f;
std::memcpy(&f, code, sizeof(f));
args.emplace_back().set_float(f);
code += bc_store_size<float_type>;
continue;
}
default:
break;
}
@ -501,10 +504,11 @@ std::uint32_t *vm_exec(
case BC_INST_BLOCK: {
std::uint32_t len = op >> 8;
args.emplace_back().set_code(bcode_p::make_ref(
reinterpret_cast<bcode *>(code + 1)
));
code += len;
bcode *b;
code += 1;
std::memcpy(&b, &code, sizeof(b));
args.emplace_back().set_code(bcode_p::make_ref(b));
code += len - 1;
continue;
}

Loading…
Cancel
Save