Skip to content

Commit dbe7e84

Browse files
committed
Array of objects in Construct/Destruct/Allocate/Deallocate
1 parent f23844b commit dbe7e84

File tree

5 files changed

+64
-42
lines changed

5 files changed

+64
-42
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 32 additions & 16 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.
@@ -791,20 +794,33 @@ enum : long int {
791794
/// Gets the size/dimensions of a multi-dimension array.
792795
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);
793796

794-
/// Allocates memory for a given class.
795-
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope);
797+
/// Allocates memory required by an object of a given class
798+
/// \c scope Given class for which to allocate memory for
799+
/// \c count is used to indicate the number of objects to allocate for.
800+
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope,
801+
TCppIndex_t count = 1UL);
796802

797803
/// Deallocates memory for a given class.
798-
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address);
799-
800-
/// Creates an object of class \c scope and calls its default constructor. If
801-
/// \c arena is set it uses placement new.
802-
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr);
803-
804-
/// Calls the destructor of object of type \c type. When withFree is true it
805-
/// calls operator delete/free.
804+
/// \c scope Class to indicate size of memory to deallocate
805+
/// \c count is used to indicate the number of objects to dallocate for
806+
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
807+
TCppIndex_t count = 1UL);
808+
809+
/// Creates one or more objects of class \c scope by calling its default
810+
/// constructor.
811+
/// \c arena If set, this API uses placement new to construct at this address.
812+
/// \c count is used to indicate the number of objects to construct.
813+
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
814+
TCppIndex_t count = 1UL);
815+
816+
/// Destroys one or more objects of a class
817+
/// \c This this pointer of the object to destruct. Can also be the starting
818+
/// address of an array of objects
819+
/// \c withFree if true, we call operator delete/free, else just the destructor
820+
/// \c count indicate the number of objects to destruct, if \c This points to
821+
/// an array of objects
806822
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
807-
bool withFree = true);
823+
bool withFree = true, TCppIndex_t count = 0UL);
808824

809825
/// @name Stream Redirection
810826
///

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
@@ -597,25 +597,25 @@ void clang_deallocate(CXObject address) { ::operator delete(address); }
597597

598598
namespace Cpp {
599599
void* Construct(compat::Interpreter& interp, TCppScope_t scope,
600-
void* arena /*=nullptr*/);
600+
void* arena /*=nullptr*/, TCppIndex_t count);
601601
} // namespace Cpp
602602

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

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

614614
namespace Cpp {
615615
void Destruct(compat::Interpreter& interp, TCppObject_t This,
616-
clang::Decl* Class, bool withFree);
616+
clang::Decl* Class, bool withFree, size_t nary);
617617
} // namespace Cpp
618618

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

lib/CppInterOp/CppInterOp.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,17 +3638,18 @@ void GetOperator(TCppScope_t scope, Operator op,
36383638
}
36393639
}
36403640

3641-
TCppObject_t Allocate(TCppScope_t scope) {
3642-
return (TCppObject_t)::operator new(Cpp::SizeOf(scope));
3641+
TCppObject_t Allocate(TCppScope_t scope, TCppIndex_t count) {
3642+
return (TCppObject_t)::operator new(Cpp::SizeOf(scope) * count);
36433643
}
36443644

3645-
void Deallocate(TCppScope_t scope, TCppObject_t address) {
3646-
::operator delete(address);
3645+
void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count) {
3646+
size_t bytes = Cpp::SizeOf(scope) * count;
3647+
::operator delete(address, bytes);
36473648
}
36483649

36493650
// FIXME: Add optional arguments to the operator new.
36503651
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
3651-
void* arena /*=nullptr*/) {
3652+
void* arena /*=nullptr*/, TCppIndex_t count /*=1UL*/) {
36523653
auto* Class = (Decl*)scope;
36533654
// FIXME: Diagnose.
36543655
if (!HasDefaultConstructor(Class))
@@ -3657,7 +3658,8 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
36573658
auto* const Ctor = GetDefaultConstructor(interp, Class);
36583659
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
36593660
if (arena) {
3660-
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
3661+
JC.Invoke(&arena, {}, (void*)~0,
3662+
count); // Tell Invoke to use placement new.
36613663
return arena;
36623664
}
36633665

@@ -3668,22 +3670,24 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
36683670
return nullptr;
36693671
}
36703672

3671-
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
3672-
return Construct(getInterp(), scope, arena);
3673+
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
3674+
TCppIndex_t count /*=1UL*/) {
3675+
return Construct(getInterp(), scope, arena, count);
36733676
}
36743677

36753678
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3676-
bool withFree) {
3679+
bool withFree, TCppIndex_t nary) {
36773680
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
3678-
(*wrapper)(This, /*nary=*/0, withFree);
3681+
(*wrapper)(This, nary, withFree);
36793682
return;
36803683
}
36813684
// FIXME: Diagnose.
36823685
}
36833686

3684-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3687+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
3688+
TCppIndex_t count /*=0UL*/) {
36853689
auto* Class = static_cast<Decl*>(scope);
3686-
Destruct(getInterp(), This, Class, withFree);
3690+
Destruct(getInterp(), This, Class, withFree, count);
36873691
}
36883692

36893693
class StreamCaptureInfo {

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
14811481
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
14821482
auto S = clang_getDefaultConstructor(make_scope(Decls[0], I));
14831483
void* object_c = nullptr;
1484-
clang_invoke(S, &object_c, nullptr, 0, nullptr);
1484+
clang_invoke(S, &object_c, nullptr, 0, 0UL, nullptr);
14851485
EXPECT_TRUE(object_c) << "Failed to call the ctor.";
14861486
clang_destruct(object_c, make_scope(Decls[1], I), true);
14871487
// Clean up resources
@@ -1974,7 +1974,7 @@ TEST(FunctionReflectionTest, Construct) {
19741974
testing::internal::CaptureStdout();
19751975
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
19761976
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
1977-
auto object_c = clang_construct(scope_c, nullptr);
1977+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
19781978
EXPECT_TRUE(object_c != nullptr);
19791979
output = testing::internal::GetCapturedStdout();
19801980
EXPECT_EQ(output, "Constructor Executed");
@@ -2097,7 +2097,7 @@ TEST(FunctionReflectionTest, Destruct) {
20972097
testing::internal::CaptureStdout();
20982098
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
20992099
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
2100-
auto object_c = clang_construct(scope_c, nullptr);
2100+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
21012101
clang_destruct(object_c, scope_c, true);
21022102
output = testing::internal::GetCapturedStdout();
21032103
EXPECT_EQ(output, "Destructor Executed");

0 commit comments

Comments
 (0)