@@ -2172,16 +2172,21 @@ heapify(mrb_state *mrb, mrb_value ary, mrb_value *a, mrb_int index, mrb_int size
|
2172 | 2172 | static void |
2173 | 2173 | insertion_sort(mrb_state *mrb, mrb_value ary, mrb_value *a, mrb_int size, mrb_value blk) |
2174 | 2174 | { |
| 2175 | +int ai = mrb_gc_arena_save(mrb); |
2175 | 2176 | for (mrb_int i = 1; i < size; i++) { |
2176 | 2177 | mrb_value key = a[i]; |
2177 | 2178 | mrb_int j = i - 1; |
2178 | 2179 | |
| 2180 | +/* Protect key from GC - it's temporarily out of the array during sort */ |
| 2181 | +mrb_gc_protect(mrb, key); |
| 2182 | + |
2179 | 2183 | /* Move elements that are greater than key to one position ahead */ |
2180 | 2184 | while (j >= 0 && sort_cmp(mrb, ary, a[j], key, blk)) { |
2181 | 2185 | a[j + 1] = a[j]; |
2182 | 2186 | j--; |
2183 | 2187 | } |
2184 | 2188 | a[j + 1] = key; |
| 2189 | +mrb_gc_arena_restore(mrb, ai); |
2185 | 2190 | } |
2186 | 2191 | } |
2187 | 2192 | |
|