Skip to content

Commit 81a81ba

Browse files
committed
Array of objects in Construct/Destruct/Allocate/Deallocate
1 parent 360a117 commit 81a81ba

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.
@@ -778,20 +781,33 @@ enum : long int {
778781
/// Gets the size/dimensions of a multi-dimension array.
779782
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);
780783

781-
/// Allocates memory for a given class.
782-
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope);
784+
/// Allocates memory required by an object of a given class
785+
/// \c scope Given class for which to allocate memory for
786+
/// \c count is used to indicate the number of objects to allocate for.
787+
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope,
788+
TCppIndex_t count = 1UL);
783789

784790
/// Deallocates memory for a given class.
785-
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address);
786-
787-
/// Creates an object of class \c scope and calls its default constructor. If
788-
/// \c arena is set it uses placement new.
789-
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr);
790-
791-
/// Calls the destructor of object of type \c type. When withFree is true it
792-
/// calls operator delete/free.
791+
/// \c scope Class to indicate size of memory to deallocate
792+
/// \c count is used to indicate the number of objects to dallocate for
793+
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
794+
TCppIndex_t count = 1UL);
795+
796+
/// Creates one or more objects of class \c scope by calling its default
797+
/// constructor.
798+
/// \c arena If set, this API uses placement new to construct at this address.
799+
/// \c count is used to indicate the number of objects to construct.
800+
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
801+
TCppIndex_t count = 1UL);
802+
803+
/// Destroys one or more objects of a class
804+
/// \c This this pointer of the object to destruct. Can also be the starting
805+
/// address of an array of objects
806+
/// \c withFree if true, we call operator delete/free, else just the destructor
807+
/// \c count indicate the number of objects to destruct, if \c This points to
808+
/// an array of objects
793809
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
794-
bool withFree = true);
810+
bool withFree = true, TCppIndex_t count = 0UL);
795811

796812
/// @name Stream Redirection
797813
///

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
@@ -3612,17 +3612,18 @@ void GetOperator(TCppScope_t scope, Operator op,
36123612
}
36133613
}
36143614

3615-
TCppObject_t Allocate(TCppScope_t scope) {
3616-
return (TCppObject_t)::operator new(Cpp::SizeOf(scope));
3615+
TCppObject_t Allocate(TCppScope_t scope, TCppIndex_t count) {
3616+
return (TCppObject_t)::operator new(Cpp::SizeOf(scope) * count);
36173617
}
36183618

3619-
void Deallocate(TCppScope_t scope, TCppObject_t address) {
3620-
::operator delete(address);
3619+
void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count) {
3620+
size_t bytes = Cpp::SizeOf(scope) * count;
3621+
::operator delete(address, bytes);
36213622
}
36223623

36233624
// FIXME: Add optional arguments to the operator new.
36243625
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
3625-
void* arena /*=nullptr*/) {
3626+
void* arena /*=nullptr*/, TCppIndex_t count /*=1UL*/) {
36263627
auto* Class = (Decl*)scope;
36273628
// FIXME: Diagnose.
36283629
if (!HasDefaultConstructor(Class))
@@ -3631,7 +3632,8 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
36313632
auto* const Ctor = GetDefaultConstructor(interp, Class);
36323633
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
36333634
if (arena) {
3634-
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
3635+
JC.Invoke(&arena, {}, (void*)~0,
3636+
count); // Tell Invoke to use placement new.
36353637
return arena;
36363638
}
36373639

@@ -3642,22 +3644,24 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
36423644
return nullptr;
36433645
}
36443646

3645-
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
3646-
return Construct(getInterp(), scope, arena);
3647+
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
3648+
TCppIndex_t count /*=1UL*/) {
3649+
return Construct(getInterp(), scope, arena, count);
36473650
}
36483651

36493652
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3650-
bool withFree) {
3653+
bool withFree, TCppIndex_t nary) {
36513654
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
3652-
(*wrapper)(This, /*nary=*/0, withFree);
3655+
(*wrapper)(This, nary, withFree);
36533656
return;
36543657
}
36553658
// FIXME: Diagnose.
36563659
}
36573660

3658-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3661+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
3662+
TCppIndex_t count /*=0UL*/) {
36593663
auto* Class = static_cast<Decl*>(scope);
3660-
Destruct(getInterp(), This, Class, withFree);
3664+
Destruct(getInterp(), This, Class, withFree, count);
36613665
}
36623666

36633667
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)