mirror of
https://github.com/janet-lang/janet
synced 2024-11-29 03:19:54 +00:00
Be more careful about data alignment
Alingment issues can happen anywhere we do casting on pointer types. Be more careful in the peg module about ensuring that pointers are aligned well.
This commit is contained in:
parent
327d2ed849
commit
9495be328c
@ -987,15 +987,24 @@ static JanetAbstractType peg_type = {
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Used to ensure that if we place several arrays in one memory chunk, each
|
||||||
|
* array will be correctly aligned */
|
||||||
|
static size_t size_padded(size_t offset, size_t size) {
|
||||||
|
size_t x = size + offset - 1;
|
||||||
|
return x - (x % size);
|
||||||
|
}
|
||||||
|
|
||||||
/* Convert Builder to Peg (Janet Abstract Value) */
|
/* Convert Builder to Peg (Janet Abstract Value) */
|
||||||
static Peg *make_peg(Builder *b) {
|
static Peg *make_peg(Builder *b) {
|
||||||
|
size_t bytecode_start = size_padded(sizeof(Peg), sizeof(uint32_t));
|
||||||
size_t bytecode_size = janet_v_count(b->bytecode) * sizeof(uint32_t);
|
size_t bytecode_size = janet_v_count(b->bytecode) * sizeof(uint32_t);
|
||||||
|
size_t constants_start = size_padded(bytecode_start + bytecode_size, sizeof(Janet));
|
||||||
size_t constants_size = janet_v_count(b->constants) * sizeof(Janet);
|
size_t constants_size = janet_v_count(b->constants) * sizeof(Janet);
|
||||||
size_t total_size = bytecode_size + constants_size + sizeof(Peg);
|
size_t total_size = constants_start + constants_size;
|
||||||
char *mem = janet_abstract(&peg_type, total_size);
|
char *mem = janet_abstract(&peg_type, total_size);
|
||||||
Peg *peg = (Peg *)mem;
|
Peg *peg = (Peg *)mem;
|
||||||
peg->bytecode = (uint32_t *)(mem + sizeof(Peg));
|
peg->bytecode = (uint32_t *)(mem + bytecode_start);
|
||||||
peg->constants = (Janet *)(mem + sizeof(Peg) + bytecode_size);
|
peg->constants = (Janet *)(mem + constants_start);
|
||||||
peg->num_constants = janet_v_count(b->constants);
|
peg->num_constants = janet_v_count(b->constants);
|
||||||
memcpy(peg->bytecode, b->bytecode, bytecode_size);
|
memcpy(peg->bytecode, b->bytecode, bytecode_size);
|
||||||
memcpy(peg->constants, b->constants, constants_size);
|
memcpy(peg->constants, b->constants, constants_size);
|
||||||
|
@ -711,7 +711,7 @@ struct JanetAbstractHead {
|
|||||||
JanetGCObject gc;
|
JanetGCObject gc;
|
||||||
const JanetAbstractType *type;
|
const JanetAbstractType *type;
|
||||||
size_t size;
|
size_t size;
|
||||||
char data[];
|
long long data[]; /* Use long long to ensure most general alignment */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Some function definition flags */
|
/* Some function definition flags */
|
||||||
|
Loading…
Reference in New Issue
Block a user