Przeglądaj źródła

Enhance ems memory allocator (#544)

The total size actually allocated by ealloc_hmu() might be larger than the size required to allocate, we reset the total size after allocating, so that if heap verification feature is enabled, the data to verify can be written to the right place of the suffix verification area. And in gc_realloc_vo_internal(), free the heap lock until the original data is copied to new object to return.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Wenyong Huang 5 lat temu
rodzic
commit
686733170c
1 zmienionych plików z 6 dodań i 3 usunięć
  1. 6 3
      core/shared/mem-alloc/ems/ems_alloc.c

+ 6 - 3
core/shared/mem-alloc/ems/ems_alloc.c

@@ -570,7 +570,6 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
         }
         }
     }
     }
 
 
-
     hmu = alloc_hmu_ex(heap, tot_size);
     hmu = alloc_hmu_ex(heap, tot_size);
     if (!hmu)
     if (!hmu)
         goto finish;
         goto finish;
@@ -578,6 +577,7 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
     bh_assert(hmu_get_size(hmu) >= tot_size);
     bh_assert(hmu_get_size(hmu) >= tot_size);
     /* the total size allocated may be larger than
     /* the total size allocated may be larger than
        the required size, reset it here */
        the required size, reset it here */
+    tot_size = hmu_get_size(hmu);
     g_total_malloc += tot_size;
     g_total_malloc += tot_size;
 
 
     hmu_set_ut(hmu, HMU_VO);
     hmu_set_ut(hmu, HMU_VO);
@@ -590,7 +590,6 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
     ret = hmu_to_obj(hmu);
     ret = hmu_to_obj(hmu);
 
 
 finish:
 finish:
-    os_mutex_unlock(&heap->lock);
 
 
     if (ret) {
     if (ret) {
         obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
         obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
@@ -599,10 +598,14 @@ finish:
             obj_size_old = tot_size_old - HMU_SIZE
             obj_size_old = tot_size_old - HMU_SIZE
                            - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
                            - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
             bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
             bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
-            gc_free_vo(vheap, obj_old);
         }
         }
     }
     }
 
 
+    os_mutex_unlock(&heap->lock);
+
+    if (ret && obj_old)
+        gc_free_vo(vheap, obj_old);
+
     return ret;
     return ret;
 }
 }