Skip to content

Commit

Permalink
Defined and added saving critical region for uninitialized objref ins…
Browse files Browse the repository at this point in the history
…tances.
  • Loading branch information
kekyo committed Apr 6, 2019
1 parent 3655cbf commit 96f648d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 77 deletions.
4 changes: 2 additions & 2 deletions IL2C.Runtime/include/System/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ typedef const struct
// IL2C_REF_HEADER
void* pNext; // Const string will not collect by GC, so this link is always NULL.
IL2C_RUNTIME_TYPE type; // Const string always fixed runtime type pointer from "System_String_RUNTIME_TYPE__."
interlock_t characteristic; // Const string always marked (IL2C_CHARACTERISTIC_CONST)
interlock_t characteristic; // Const string always marked (IL2C_CHARACTERISTIC_CONST | IL2C_CHARACTERISTIC_INITIALIZED)

// Instance's vptr
System_String_VTABLE_DECL__* vptr0__; // Const string always fixed VTable pointer from "System_String_VTABLE__."
Expand All @@ -105,7 +105,7 @@ typedef const struct

#define IL2C_CONST_STRING(name, string_body) \
static IL2C_CONST_STRING_DECL name##_CONST_STRING__ = { \
NULL, il2c_typeof(System_String), /* IL2C_CHARACTERISTIC_CONST */ (interlock_t)0x80000000UL, &System_String_VTABLE__, string_body }; \
NULL, il2c_typeof(System_String), /* IL2C_CHARACTERISTIC_CONST | IL2C_CHARACTERISTIC_INITIALIZED */ (interlock_t)0xc0000000UL, &System_String_VTABLE__, string_body }; \
System_String* const name = ((System_String*)&(name##_CONST_STRING__.vptr0__))

#ifdef __cplusplus
Expand Down
15 changes: 10 additions & 5 deletions IL2C.Runtime/src/System/Array.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,30 @@ System_Array* il2c_new_array__(
il2c_assert(0);
}

uintptr_t elementSize = il2c_sizeof__(elementType);
const uintptr_t elementSize = il2c_sizeof__(elementType);
il2c_assert(elementSize >= 1);

// -1 is "uint8_t Item[1]"
uintptr_t size = (uintptr_t)sizeof(System_Array) + ((uintptr_t)length) * elementSize;
const uintptr_t size = (uintptr_t)sizeof(System_Array) + ((uintptr_t)length) * elementSize;

#if defined(IL2C_USE_LINE_INFORMATION)
System_Array* arr = il2c_get_uninitialized_object_internal__(
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(
il2c_typeof(System_Array), size, pFile, line);
#else
System_Array* arr = il2c_get_uninitialized_object_internal__(
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(
il2c_typeof(System_Array), size);
#endif

arr->vptr0__ = &System_Array_VTABLE__;
System_Array* arr = (System_Array*)(pHeader + 1);

il2c_assert(arr->vptr0__ == &System_Array_VTABLE__);

arr->elementType__ = elementType;
arr->Length = length;

// Marked instance is initialized. (and will handle by GC)
il2c_ior(&pHeader->characteristic, IL2C_CHARACTERISTIC_INITIALIZED);

return arr;
}

Expand Down
40 changes: 28 additions & 12 deletions IL2C.Runtime/src/System/Delegate.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,29 @@ System_Delegate* System_Delegate_Combine(System_Delegate* a, System_Delegate* b)
il2c_assert(0);
}

uintptr_t count = a->count__ + b->count__;
uintptr_t size = sizeof(System_Delegate) +
const uintptr_t count = a->count__ + b->count__;
const uintptr_t size = sizeof(System_Delegate) +
(uintptr_t)(count - 1 /* included System_Delegate */) * sizeof(IL2C_METHOD_TABLE);

#if defined(IL2C_USE_LINE_INFORMATION)
System_Delegate* dlg = il2c_get_uninitialized_object_internal__(pHeaderA->type, size, __FILE__, __LINE__);
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(pHeaderA->type, size, __FILE__, __LINE__);
#else
System_Delegate* dlg = il2c_get_uninitialized_object_internal__(pHeaderA->type, size);
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(pHeaderA->type, size);
#endif

dlg->vptr0__ = &System_Delegate_VTABLE__;
System_Delegate* dlg = (System_Delegate*)(pHeader + 1);

il2c_assert(dlg->vptr0__ == &System_Delegate_VTABLE__);

dlg->count__ = count;

struct IL2C_METHOD_TABLE_DECL* pMethodtbl = (struct IL2C_METHOD_TABLE_DECL*)&dlg->methodtbl__[0];
memcpy(&pMethodtbl[0], &a->methodtbl__[0], a->count__ * sizeof(IL2C_METHOD_TABLE));
memcpy(&pMethodtbl[a->count__], &b->methodtbl__[0], b->count__ * sizeof(IL2C_METHOD_TABLE));

// Marked instance is initialized. (and will handle by GC)
il2c_ior(&pHeader->characteristic, IL2C_CHARACTERISTIC_INITIALIZED);

return dlg;
}

Expand Down Expand Up @@ -159,19 +165,24 @@ System_Delegate* System_Delegate_Remove(System_Delegate* source, System_Delegate
(uintptr_t)(count - 1 /* included System_Delegate */) * sizeof(IL2C_METHOD_TABLE);

#if defined(IL2C_USE_LINE_INFORMATION)
System_Delegate* dlg = il2c_get_uninitialized_object_internal__(pHeaderSource->type, size, __FILE__, __LINE__);
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(pHeaderSource->type, size, __FILE__, __LINE__);
#else
System_Delegate* dlg = il2c_get_uninitialized_object_internal__(pHeaderSource->type, size);
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(pHeaderSource->type, size);
#endif

dlg->vptr0__ = &System_Delegate_VTABLE__;
System_Delegate* dlg = (System_Delegate*)(pHeader + 1);

il2c_assert(dlg->vptr0__ == &System_Delegate_VTABLE__);

dlg->count__ = count;

struct IL2C_METHOD_TABLE_DECL* pMethodtbl = (struct IL2C_METHOD_TABLE_DECL*)&dlg->methodtbl__[0];
memcpy(&pMethodtbl[0], &source->methodtbl__[0], ((size_t)index) * sizeof(IL2C_METHOD_TABLE));
memcpy(&pMethodtbl[index], &source->methodtbl__[((uintptr_t)index) + value->count__], ((size_t)count - (size_t)index) * sizeof(IL2C_METHOD_TABLE));

// Marked instance is initialized. (and will handle by GC)
il2c_ior(&pHeader->characteristic, IL2C_CHARACTERISTIC_INITIALIZED);

return dlg;
}
}
Expand All @@ -195,19 +206,24 @@ System_Delegate* il2c_new_delegate__(
il2c_assert(method != 0);

#if defined(IL2C_USE_LINE_INFORMATION)
System_Delegate* dlg = il2c_get_uninitialized_object_internal__(delegateType, sizeof(System_Delegate), pFile, line);
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(delegateType, sizeof(System_Delegate), pFile, line);
#else
System_Delegate* dlg = il2c_get_uninitialized_object_internal__(delegateType, sizeof(System_Delegate));
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(delegateType, sizeof(System_Delegate));
#endif

dlg->vptr0__ = &System_Delegate_VTABLE__;

System_Delegate* dlg = (System_Delegate*)(pHeader + 1);

il2c_assert(dlg->vptr0__ == &System_Delegate_VTABLE__);

dlg->count__ = 1;

struct IL2C_METHOD_TABLE_DECL* pMethodTbl = (struct IL2C_METHOD_TABLE_DECL*)&dlg->methodtbl__[0];
pMethodTbl->target = object;
pMethodTbl->methodPtr = method;

// Marked instance is initialized. (and will handle by GC)
il2c_ior(&pHeader->characteristic, IL2C_CHARACTERISTIC_INITIALIZED);

return dlg;
}

Expand Down
13 changes: 10 additions & 3 deletions IL2C.Runtime/src/System/String.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ static System_String* new_string_internal__(uintptr_t byteSize, const char* pFil
static System_String* new_string_internal__(uintptr_t byteSize)
#endif
{
uintptr_t bodySize = sizeof(System_String) + byteSize;
const uintptr_t bodySize = sizeof(System_String) + byteSize;

#if defined(IL2C_USE_LINE_INFORMATION)
System_String* pString = il2c_get_uninitialized_object_internal__(
IL2C_REF_HEADER* pHeader = il2c_get_uninitialized_object_internal__(
il2c_typeof(System_String),
bodySize, pFile, line);
#else
Expand All @@ -50,9 +50,16 @@ static System_String* new_string_internal__(uintptr_t byteSize)
bodySize);
#endif

pString->vptr0__ = &System_String_VTABLE__;
System_String* pString = (System_String*)(pHeader + 1);

il2c_assert(pString->vptr0__ == &System_String_VTABLE__);

wchar_t* string_body = (wchar_t*)(((uint8_t*)pString) + sizeof(System_String));
pString->string_body__ = string_body;

// Marked instance is initialized. (and will handle by GC)
il2c_ior(&pHeader->characteristic, IL2C_CHARACTERISTIC_INITIALIZED);

return pString;
}

Expand Down
Loading

0 comments on commit 96f648d

Please sign in to comment.