GitHub

Original file line numberDiff line numberDiff line change

@@ -2172,16 +2172,21 @@ heapify(mrb_state *mrb, mrb_value ary, mrb_value *a, mrb_int index, mrb_int size

21722172

static void

21732173

insertion_sort(mrb_state *mrb, mrb_value ary, mrb_value *a, mrb_int size, mrb_value blk)

21742174

{

2175+

int ai = mrb_gc_arena_save(mrb);

21752176

for (mrb_int i = 1; i < size; i++) {

21762177

mrb_value key = a[i];

21772178

mrb_int j = i - 1;

21782179
2180+

/* Protect key from GC - it's temporarily out of the array during sort */

2181+

mrb_gc_protect(mrb, key);

2182+
21792183

/* Move elements that are greater than key to one position ahead */

21802184

while (j >= 0 && sort_cmp(mrb, ary, a[j], key, blk)) {

21812185

a[j + 1] = a[j];

21822186

j--;

21832187

}

21842188

a[j + 1] = key;

2189+

mrb_gc_arena_restore(mrb, ai);

21852190

}

21862191

}

21872192

Read the original on github.com ↗