Create C++ static initializer for use in global objects. (#115)
This commit is contained in:
@@ -9,7 +9,8 @@ ArrayHeader* setupArgs(int argc, char** argv) {
|
|||||||
ArrayHeader* args = AllocArrayInstance(theArrayTypeInfo, SCOPE_GLOBAL, argc-1);
|
ArrayHeader* args = AllocArrayInstance(theArrayTypeInfo, SCOPE_GLOBAL, argc-1);
|
||||||
|
|
||||||
for (int i = 0; i < argc-1; i++) {
|
for (int i = 0; i < argc-1; i++) {
|
||||||
Kotlin_Array_set(args, i, AllocStringInstance( argv[i+1], strlen(argv[i+1]) ));
|
Kotlin_Array_set(args, i, AllocStringInstance(
|
||||||
|
SCOPE_GLOBAL, argv[i+1], strlen(argv[i+1]) ));
|
||||||
}
|
}
|
||||||
|
|
||||||
return args;
|
return args;
|
||||||
@@ -27,4 +28,3 @@ int main(int argc, char** argv) {
|
|||||||
// Yes, we have to follow Java convention and return zero.
|
// Yes, we have to follow Java convention and return zero.
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class AutoFree {
|
|||||||
|
|
||||||
// TODO: this method ignores the encoding
|
// TODO: this method ignores the encoding
|
||||||
KString CreateKotlinStringFromCString(const char* str) {
|
KString CreateKotlinStringFromCString(const char* str) {
|
||||||
return AllocStringInstance(str, strlen(str));
|
return AllocStringInstance(SCOPE_GLOBAL, str, strlen(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|||||||
@@ -89,7 +89,8 @@ ArrayHeader* AllocArrayInstance(
|
|||||||
return ArrayContainer(type_info, elements).GetPlace();
|
return ArrayContainer(type_info, elements).GetPlace();
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayHeader* AllocStringInstance(const char* data, uint32_t length) {
|
ArrayHeader* AllocStringInstance(
|
||||||
|
PlacementHint hint, const char* data, uint32_t length) {
|
||||||
ArrayHeader* result = ArrayContainer(theStringTypeInfo, length).GetPlace();
|
ArrayHeader* result = ArrayContainer(theStringTypeInfo, length).GetPlace();
|
||||||
memcpy(
|
memcpy(
|
||||||
ByteArrayAddressOfElementAt(result, 0),
|
ByteArrayAddressOfElementAt(result, 0),
|
||||||
@@ -97,6 +98,35 @@ ArrayHeader* AllocStringInstance(const char* data, uint32_t length) {
|
|||||||
length);
|
length);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ObjHeader* InitInstance(
|
||||||
|
ObjHeader** location, const TypeInfo* type_info, PlacementHint hint,
|
||||||
|
void (*ctor)(ObjHeader*)) {
|
||||||
|
ObjHeader* sentinel = reinterpret_cast<ObjHeader*>(1);
|
||||||
|
ObjHeader* value;
|
||||||
|
// Wait until other initializers.
|
||||||
|
while ((value = __sync_val_compare_and_swap(
|
||||||
|
location, nullptr, sentinel)) == sentinel) {
|
||||||
|
// TODO: consider yielding.
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value != nullptr) {
|
||||||
|
// OK'ish, inited by someone else.
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
ObjHeader* instance = AllocInstance(type_info, hint);
|
||||||
|
try {
|
||||||
|
ctor(instance);
|
||||||
|
__sync_val_compare_and_swap(location, sentinel, instance);
|
||||||
|
return instance;
|
||||||
|
} catch (...) {
|
||||||
|
Release(instance->container());
|
||||||
|
__sync_val_compare_and_swap(location, sentinel, nullptr);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -254,7 +254,11 @@ void InitMemory();
|
|||||||
ObjHeader* AllocInstance(const TypeInfo* type_info, PlacementHint hint);
|
ObjHeader* AllocInstance(const TypeInfo* type_info, PlacementHint hint);
|
||||||
ArrayHeader* AllocArrayInstance(
|
ArrayHeader* AllocArrayInstance(
|
||||||
const TypeInfo* type_info, PlacementHint hint, uint32_t elements);
|
const TypeInfo* type_info, PlacementHint hint, uint32_t elements);
|
||||||
ArrayHeader* AllocStringInstance(const char* data, uint32_t length);
|
ArrayHeader* AllocStringInstance(PlacementHint hint,
|
||||||
|
const char* data, uint32_t length);
|
||||||
|
ObjHeader* InitInstance(
|
||||||
|
ObjHeader** location, const TypeInfo* type_info, PlacementHint hint,
|
||||||
|
void (*ctor)(ObjHeader*));
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user