Skip to content

Commit 7962bb0

Browse files
committed
Array of objects in Construct/Destruct/Allocate/Deallocate
1 parent ca4d153 commit 7962bb0

File tree

5 files changed

+49
-35
lines changed

5 files changed

+49
-35
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ class JitCall {
115115
// FIXME: Figure out how to unify the wrapper signatures.
116116
// FIXME: Hide these implementation details by moving wrapper generation in
117117
// this class.
118-
using GenericCall = void (*)(void*, size_t, void**, void*);
119-
using DestructorCall = void (*)(void*, unsigned long, int);
118+
// (self, nargs, args, result, nary)
119+
using GenericCall = void (*)(void*, size_t, void**, void*, size_t);
120+
// (self, nary, withFree)
121+
using DestructorCall = void (*)(void*, size_t, int);
120122

121123
private:
122124
union {
@@ -162,19 +164,20 @@ class JitCall {
162164
// self can go in the end and be nullptr by default; result can be a nullptr
163165
// by default. These changes should be synchronized with the wrapper if we
164166
// decide to directly.
165-
void Invoke(void* result, ArgList args = {}, void* self = nullptr) const {
167+
void Invoke(void* result, ArgList args = {}, void* self = nullptr,
168+
size_t nary = 0UL) const {
166169
// NOLINTBEGIN(*-type-union-access)
167170
// Forward if we intended to call a dtor with only 1 parameter.
168171
if (m_Kind == kDestructorCall && result && !args.m_Args) {
169-
InvokeDestructor(result, /*nary=*/0UL, /*withFree=*/true);
172+
InvokeDestructor(result, nary, /*withFree=*/true);
170173
return;
171174
}
172175

173176
#ifndef NDEBUG
174177
assert(AreArgumentsValid(result, args, self) && "Invalid args!");
175178
ReportInvokeStart(result, args, self);
176179
#endif // NDEBUG
177-
m_GenericCall(self, args.m_ArgSize, args.m_Args, result);
180+
m_GenericCall(self, args.m_ArgSize, args.m_Args, result, nary);
178181
// NOLINTEND(*-type-union-access)
179182
}
180183
/// Makes a call to a destructor.
@@ -768,19 +771,24 @@ enum : long int {
768771
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);
769772

770773
/// Allocates memory for a given class.
771-
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope);
774+
/// \c count is used to indicate the number of objects to allocate for.
775+
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope,
776+
TCppIndex_t count = 1UL);
772777

773778
/// Deallocates memory for a given class.
774-
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address);
779+
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
780+
TCppIndex_t count = 1UL);
775781

776782
/// Creates an object of class \c scope and calls its default constructor. If
777783
/// \c arena is set it uses placement new.
778-
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr);
784+
/// \c count is used to indicate the number of objects to construct.
785+
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
786+
TCppIndex_t count = 1UL);
779787

780788
/// Calls the destructor of object of type \c type. When withFree is true it
781789
/// calls operator delete/free.
782790
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
783-
bool withFree = true);
791+
bool withFree = true, TCppIndex_t count = 0UL);
784792

785793
/// @name Stream Redirection
786794
///

include/clang-c/CXCppInterOp.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ CINDEX_LINKAGE void clang_deallocate(CXObject address);
332332
* Creates an object of class \c scope and calls its default constructor. If \c
333333
* arena is set it uses placement new.
334334
*/
335-
CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena);
335+
CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena,
336+
size_t count);
336337

337338
/**
338339
* Creates a trampoline function and makes a call to a generic function or
@@ -349,7 +350,7 @@ CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena);
349350
* \param self The 'this pointer' of the object.
350351
*/
351352
CINDEX_LINKAGE void clang_invoke(CXScope func, void* result, void** args,
352-
size_t n, void* self);
353+
size_t n, size_t nary, void* self);
353354

354355
/**
355356
* Calls the destructor of object of type \c type. When withFree is true it
@@ -361,7 +362,8 @@ CINDEX_LINKAGE void clang_invoke(CXScope func, void* result, void** args,
361362
*
362363
* \param withFree Whether to call operator delete/free or not.
363364
*/
364-
CINDEX_LINKAGE void clang_destruct(CXObject This, CXScope S, bool withFree);
365+
CINDEX_LINKAGE void clang_destruct(CXObject This, CXScope S,
366+
bool withFree = true, size_t nary = 0UL);
365367

366368
/**
367369
* @}

lib/CppInterOp/CXCppInterOp.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -599,25 +599,25 @@ void clang_deallocate(CXObject address) { ::operator delete(address); }
599599

600600
namespace Cpp {
601601
void* Construct(compat::Interpreter& interp, TCppScope_t scope,
602-
void* arena /*=nullptr*/);
602+
void* arena /*=nullptr*/, TCppIndex_t count);
603603
} // namespace Cpp
604604

605-
CXObject clang_construct(CXScope scope, void* arena) {
605+
CXObject clang_construct(CXScope scope, void* arena, size_t count) {
606606
return Cpp::Construct(*getInterpreter(scope),
607-
static_cast<void*>(getDecl(scope)), arena);
607+
static_cast<void*>(getDecl(scope)), arena, count);
608608
}
609609

610610
void clang_invoke(CXScope func, void* result, void** args, size_t n,
611-
void* self) {
611+
size_t nary, void* self) {
612612
Cpp::MakeFunctionCallable(getInterpreter(func), getDecl(func))
613-
.Invoke(result, {args, n}, self);
613+
.Invoke(result, {args, n}, self, nary);
614614
}
615615

616616
namespace Cpp {
617617
void Destruct(compat::Interpreter& interp, TCppObject_t This,
618-
clang::Decl* Class, bool withFree);
618+
clang::Decl* Class, bool withFree, size_t nary);
619619
} // namespace Cpp
620620

621-
void clang_destruct(CXObject This, CXScope S, bool withFree) {
622-
Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree);
621+
void clang_destruct(CXObject This, CXScope S, bool withFree, size_t nary) {
622+
Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree, nary);
623623
}

lib/CppInterOp/CppInterOp.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,17 +3527,18 @@ void GetOperator(TCppScope_t scope, Operator op,
35273527
}
35283528
}
35293529

3530-
TCppObject_t Allocate(TCppScope_t scope) {
3531-
return (TCppObject_t)::operator new(Cpp::SizeOf(scope));
3530+
TCppObject_t Allocate(TCppScope_t scope, TCppIndex_t count) {
3531+
return (TCppObject_t)::operator new(Cpp::SizeOf(scope) * count);
35323532
}
35333533

3534-
void Deallocate(TCppScope_t scope, TCppObject_t address) {
3535-
::operator delete(address);
3534+
void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count) {
3535+
size_t bytes = Cpp::SizeOf(scope) * count;
3536+
::operator delete(address, bytes);
35363537
}
35373538

35383539
// FIXME: Add optional arguments to the operator new.
35393540
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
3540-
void* arena /*=nullptr*/) {
3541+
void* arena /*=nullptr*/, TCppIndex_t count /*=1UL*/) {
35413542
auto* Class = (Decl*)scope;
35423543
// FIXME: Diagnose.
35433544
if (!HasDefaultConstructor(Class))
@@ -3546,7 +3547,8 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
35463547
auto* const Ctor = GetDefaultConstructor(interp, Class);
35473548
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
35483549
if (arena) {
3549-
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
3550+
JC.Invoke(&arena, {}, (void*)~0,
3551+
count); // Tell Invoke to use placement new.
35503552
return arena;
35513553
}
35523554

@@ -3557,22 +3559,24 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
35573559
return nullptr;
35583560
}
35593561

3560-
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
3561-
return Construct(getInterp(), scope, arena);
3562+
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
3563+
TCppIndex_t count /*=0UL*/) {
3564+
return Construct(getInterp(), scope, arena, count);
35623565
}
35633566

35643567
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3565-
bool withFree) {
3568+
bool withFree, TCppIndex_t nary) {
35663569
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
3567-
(*wrapper)(This, /*nary=*/0, withFree);
3570+
(*wrapper)(This, nary, withFree);
35683571
return;
35693572
}
35703573
// FIXME: Diagnose.
35713574
}
35723575

3573-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3576+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
3577+
TCppIndex_t count /*=1UL*/) {
35743578
auto* Class = static_cast<Decl*>(scope);
3575-
Destruct(getInterp(), This, Class, withFree);
3579+
Destruct(getInterp(), This, Class, withFree, count);
35763580
}
35773581

35783582
class StreamCaptureInfo {

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
14711471
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
14721472
auto S = clang_getDefaultConstructor(make_scope(Decls[0], I));
14731473
void* object_c = nullptr;
1474-
clang_invoke(S, &object_c, nullptr, 0, nullptr);
1474+
clang_invoke(S, &object_c, nullptr, 0, 0UL, nullptr);
14751475
EXPECT_TRUE(object_c) << "Failed to call the ctor.";
14761476
clang_destruct(object_c, make_scope(Decls[1], I), true);
14771477
// Clean up resources
@@ -1883,7 +1883,7 @@ TEST(FunctionReflectionTest, Construct) {
18831883
testing::internal::CaptureStdout();
18841884
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
18851885
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
1886-
auto object_c = clang_construct(scope_c, nullptr);
1886+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
18871887
EXPECT_TRUE(object_c != nullptr);
18881888
output = testing::internal::GetCapturedStdout();
18891889
EXPECT_EQ(output, "Constructor Executed");
@@ -2006,7 +2006,7 @@ TEST(FunctionReflectionTest, Destruct) {
20062006
testing::internal::CaptureStdout();
20072007
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
20082008
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
2009-
auto object_c = clang_construct(scope_c, nullptr);
2009+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
20102010
clang_destruct(object_c, scope_c, true);
20112011
output = testing::internal::GetCapturedStdout();
20122012
EXPECT_EQ(output, "Destructor Executed");

0 commit comments

Comments
 (0)