Skip to content

Commit b1aecef

Browse files
committed
Use UNALIGNED_MEMBER_PTR
* internal.h (UNALIGNED_MEMBER_ACCESS, UNALIGNED_MEMBER_PTR): moved from eval_intern.h. * compile.c iseq.c, vm.c: use UNALIGNED_MEMBER_PTR for `entries` in `struct iseq_catch_table`. * vm_eval.c, vm_insnhelper.c: use UNALIGNED_MEMBER_PTR for `body` in `rb_method_definition_t`.
1 parent ea42423 commit b1aecef

File tree

8 files changed

+38
-33
lines changed

8 files changed

+38
-33
lines changed

compile.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,8 @@ update_catch_except_flags(struct rb_iseq_constant_body *body)
12631263
return;
12641264

12651265
for (i = 0; i < ct->size; i++) {
1266-
const struct iseq_catch_table_entry *entry = &ct->entries[i];
1266+
const struct iseq_catch_table_entry *entry =
1267+
UNALIGNED_MEMBER_PTR(ct, entries[i]);
12671268
if (entry->type != CATCH_TYPE_BREAK
12681269
&& entry->type != CATCH_TYPE_NEXT
12691270
&& entry->type != CATCH_TYPE_REDO) {
@@ -2322,7 +2323,7 @@ iseq_set_exception_table(rb_iseq_t *iseq)
23222323

23232324
for (i = 0; i < table->size; i++) {
23242325
ptr = RARRAY_CONST_PTR_TRANSIENT(tptr[i]);
2325-
entry = &table->entries[i];
2326+
entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
23262327
entry->type = (enum catch_type)(ptr[0] & 0xffff);
23272328
entry->start = label_get_position((LABEL *)(ptr[1] & ~1));
23282329
entry->end = label_get_position((LABEL *)(ptr[2] & ~1));

eval_intern.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,6 @@ LONG WINAPI rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *);
157157
# define VAR_NOCLOBBERED(var) var
158158
#endif
159159

160-
#if defined(USE_UNALIGNED_MEMBER_ACCESS) && USE_UNALIGNED_MEMBER_ACCESS && \
161-
(defined(__clang__) || GCC_VERSION_SINCE(9, 0, 0))
162-
# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
163-
COMPILER_WARNING_PUSH; \
164-
COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
165-
typeof(expr) unaligned_member_access_result = (expr); \
166-
COMPILER_WARNING_POP; \
167-
unaligned_member_access_result; \
168-
})
169-
#else
170-
# define UNALIGNED_MEMBER_ACCESS(expr) expr
171-
#endif
172-
#define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
173-
174-
#undef RB_OBJ_WRITE
175-
#define RB_OBJ_WRITE(a, slot, b) UNALIGNED_MEMBER_ACCESS(rb_obj_write((VALUE)(a), (VALUE *)(slot), (VALUE)(b), __FILE__, __LINE__))
176-
177160
/* clear ec->tag->state, and return the value */
178161
static inline int
179162
rb_ec_tag_state(const rb_execution_context_t *ec)

internal.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,23 @@ rb_obj_builtin_type(VALUE obj)
25112511
#define COMPILER_WARNING_PRAGMA(str) COMPILER_WARNING_PRAGMA_(str)
25122512
#define COMPILER_WARNING_PRAGMA_(str) _Pragma(#str)
25132513

2514+
#if defined(USE_UNALIGNED_MEMBER_ACCESS) && USE_UNALIGNED_MEMBER_ACCESS && \
2515+
(defined(__clang__) || GCC_VERSION_SINCE(9, 0, 0))
2516+
# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
2517+
COMPILER_WARNING_PUSH; \
2518+
COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
2519+
typeof(expr) unaligned_member_access_result = (expr); \
2520+
COMPILER_WARNING_POP; \
2521+
unaligned_member_access_result; \
2522+
})
2523+
#else
2524+
# define UNALIGNED_MEMBER_ACCESS(expr) expr
2525+
#endif
2526+
#define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
2527+
2528+
#undef RB_OBJ_WRITE
2529+
#define RB_OBJ_WRITE(a, slot, b) UNALIGNED_MEMBER_ACCESS(rb_obj_write((VALUE)(a), (VALUE *)(slot), (VALUE)(b), __FILE__, __LINE__))
2530+
25142531
#if defined(__cplusplus)
25152532
#if 0
25162533
{ /* satisfy cc-mode */

iseq.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ rb_iseq_update_references(rb_iseq_t *iseq)
256256
unsigned int i;
257257
for(i = 0; i < table->size; i++) {
258258
struct iseq_catch_table_entry *entry;
259-
entry = &table->entries[i];
259+
entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
260260
if (entry->iseq) {
261261
entry->iseq = (rb_iseq_t *)rb_gc_location((VALUE)entry->iseq);
262262
}
@@ -315,7 +315,7 @@ rb_iseq_mark(const rb_iseq_t *iseq)
315315
unsigned int i;
316316
for(i = 0; i < table->size; i++) {
317317
const struct iseq_catch_table_entry *entry;
318-
entry = &table->entries[i];
318+
entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
319319
if (entry->iseq) {
320320
rb_gc_mark_no_pin((VALUE)entry->iseq);
321321
}
@@ -2138,7 +2138,8 @@ rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
21382138
rb_str_cat_cstr(indent, "| ");
21392139
indent_str = RSTRING_PTR(indent);
21402140
for (i = 0; i < body->catch_table->size; i++) {
2141-
const struct iseq_catch_table_entry *entry = &body->catch_table->entries[i];
2141+
const struct iseq_catch_table_entry *entry =
2142+
UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
21422143
rb_str_cat(str, indent_str, indent_len);
21432144
rb_str_catf(str,
21442145
"| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
@@ -2272,7 +2273,8 @@ iseq_iterate_children(const rb_iseq_t *iseq, void (*iter_func)(const rb_iseq_t *
22722273

22732274
if (body->catch_table) {
22742275
for (i = 0; i < body->catch_table->size; i++) {
2275-
const struct iseq_catch_table_entry *entry = &body->catch_table->entries[i];
2276+
const struct iseq_catch_table_entry *entry =
2277+
UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
22762278
child = entry->iseq;
22772279
if (child) {
22782280
if (rb_hash_aref(all_children, (VALUE)child) == Qnil) {
@@ -2782,7 +2784,8 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
27822784
/* exception */
27832785
if (iseq_body->catch_table) for (i=0; i<iseq_body->catch_table->size; i++) {
27842786
VALUE ary = rb_ary_new();
2785-
const struct iseq_catch_table_entry *entry = &iseq_body->catch_table->entries[i];
2787+
const struct iseq_catch_table_entry *entry =
2788+
UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
27862789
rb_ary_push(ary, exception_type2symbol(entry->type));
27872790
if (entry->iseq) {
27882791
rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));

thread_sync.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ queue_do_pop(VALUE self, struct rb_queue *q, int should_block)
903903

904904
qw.w.th = GET_THREAD();
905905
qw.as.q = q;
906-
list_add_tail(&qw.as.q->waitq, &qw.w.node);
906+
list_add_tail(queue_waitq(qw.as.q), &qw.w.node);
907907
qw.as.q->num_waiting++;
908908

909909
rb_ensure(queue_sleep, self, queue_sleep_done, (VALUE)&qw);

vm.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
19561956
else {
19571957
ct = cfp->iseq->body->catch_table;
19581958
if (ct) for (i = 0; i < ct->size; i++) {
1959-
entry = &ct->entries[i];
1959+
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
19601960
if (entry->start < epc && entry->end >= epc) {
19611961
if (entry->type == CATCH_TYPE_ENSURE) {
19621962
catch_iseq = entry->iseq;
@@ -1992,7 +1992,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
19921992
if (state == TAG_RAISE) {
19931993
ct = cfp->iseq->body->catch_table;
19941994
if (ct) for (i = 0; i < ct->size; i++) {
1995-
entry = &ct->entries[i];
1995+
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
19961996
if (entry->start < epc && entry->end >= epc) {
19971997

19981998
if (entry->type == CATCH_TYPE_RESCUE ||
@@ -2008,7 +2008,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
20082008
else if (state == TAG_RETRY) {
20092009
ct = cfp->iseq->body->catch_table;
20102010
if (ct) for (i = 0; i < ct->size; i++) {
2011-
entry = &ct->entries[i];
2011+
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
20122012
if (entry->start < epc && entry->end >= epc) {
20132013

20142014
if (entry->type == CATCH_TYPE_ENSURE) {
@@ -2035,7 +2035,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
20352035
search_restart_point:
20362036
ct = cfp->iseq->body->catch_table;
20372037
if (ct) for (i = 0; i < ct->size; i++) {
2038-
entry = &ct->entries[i];
2038+
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
20392039

20402040
if (entry->start < epc && entry->end >= epc) {
20412041
if (entry->type == CATCH_TYPE_ENSURE) {
@@ -2073,7 +2073,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
20732073
else {
20742074
ct = cfp->iseq->body->catch_table;
20752075
if (ct) for (i = 0; i < ct->size; i++) {
2076-
entry = &ct->entries[i];
2076+
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
20772077
if (entry->start < epc && entry->end >= epc) {
20782078

20792079
if (entry->type == CATCH_TYPE_ENSURE) {

vm_eval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ vm_call0_cfunc_with_frame(rb_execution_context_t* ec, struct rb_calling_info *ca
6565
{
6666
VALUE val;
6767
const rb_callable_method_entry_t *me = cc->me;
68-
const rb_method_cfunc_t *cfunc = &me->def->body.cfunc;
68+
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
6969
int len = cfunc->argc;
7070
VALUE recv = calling->recv;
7171
int argc = calling->argc;

vm_insnhelper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,8 @@ vm_throw_start(const rb_execution_context_t *ec, rb_control_frame_t *const reg_c
12011201

12021202
if (!ct) break;
12031203
for (i=0; i < ct->size; i++) {
1204-
const struct iseq_catch_table_entry * const entry = &ct->entries[i];
1204+
const struct iseq_catch_table_entry *const entry =
1205+
UNALIGNED_MEMBER_PTR(ct, entries[i]);
12051206

12061207
if (entry->type == CATCH_TYPE_BREAK &&
12071208
entry->iseq == base_iseq &&
@@ -2183,7 +2184,7 @@ vm_method_cfunc_entry(const rb_callable_method_entry_t *me)
21832184
rb_bug("wrong method type: %d", me->def->type);
21842185
}
21852186
#endif
2186-
return &me->def->body.cfunc;
2187+
return UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
21872188
}
21882189

21892190
static VALUE

0 commit comments

Comments
 (0)