Skip to content

Commit ee6e034

Browse files
committed
Array of objects in Construct/Destruct/Allocate/Deallocate
1 parent 7bb23b9 commit ee6e034

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
@@ -3646,17 +3646,18 @@ void GetOperator(TCppScope_t scope, Operator op,
36463646
}
36473647
}
36483648

3649-
TCppObject_t Allocate(TCppScope_t scope) {
3650-
return (TCppObject_t)::operator new(Cpp::SizeOf(scope));
3649+
TCppObject_t Allocate(TCppScope_t scope, TCppIndex_t count) {
3650+
return (TCppObject_t)::operator new(Cpp::SizeOf(scope) * count);
36513651
}
36523652

3653-
void Deallocate(TCppScope_t scope, TCppObject_t address) {
3654-
::operator delete(address);
3653+
void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count) {
3654+
size_t bytes = Cpp::SizeOf(scope) * count;
3655+
::operator delete(address, bytes);
36553656
}
36563657

36573658
// FIXME: Add optional arguments to the operator new.
36583659
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
3659-
void* arena /*=nullptr*/) {
3660+
void* arena /*=nullptr*/, TCppIndex_t count /*=1UL*/) {
36603661
auto* Class = (Decl*)scope;
36613662
// FIXME: Diagnose.
36623663
if (!HasDefaultConstructor(Class))
@@ -3665,7 +3666,8 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
36653666
auto* const Ctor = GetDefaultConstructor(interp, Class);
36663667
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
36673668
if (arena) {
3668-
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
3669+
JC.Invoke(&arena, {}, (void*)~0,
3670+
count); // Tell Invoke to use placement new.
36693671
return arena;
36703672
}
36713673

@@ -3676,22 +3678,24 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
36763678
return nullptr;
36773679
}
36783680

3679-
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
3680-
return Construct(getInterp(), scope, arena);
3681+
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
3682+
TCppIndex_t count /*=1UL*/) {
3683+
return Construct(getInterp(), scope, arena, count);
36813684
}
36823685

36833686
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3684-
bool withFree) {
3687+
bool withFree, TCppIndex_t nary) {
36853688
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
3686-
(*wrapper)(This, /*nary=*/0, withFree);
3689+
(*wrapper)(This, nary, withFree);
36873690
return;
36883691
}
36893692
// FIXME: Diagnose.
36903693
}
36913694

3692-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3695+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
3696+
TCppIndex_t count /*=0UL*/) {
36933697
auto* Class = static_cast<Decl*>(scope);
3694-
Destruct(getInterp(), This, Class, withFree);
3698+
Destruct(getInterp(), This, Class, withFree, count);
36953699
}
36963700

36973701
class StreamCaptureInfo {

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
14831483
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
14841484
auto S = clang_getDefaultConstructor(make_scope(Decls[0], I));
14851485
void* object_c = nullptr;
1486-
clang_invoke(S, &object_c, nullptr, 0, nullptr);
1486+
clang_invoke(S, &object_c, nullptr, 0, 0UL, nullptr);
14871487
EXPECT_TRUE(object_c) << "Failed to call the ctor.";
14881488
clang_destruct(object_c, make_scope(Decls[1], I), true);
14891489
// Clean up resources
@@ -2014,7 +2014,7 @@ TEST(FunctionReflectionTest, Construct) {
20142014
testing::internal::CaptureStdout();
20152015
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
20162016
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
2017-
auto object_c = clang_construct(scope_c, nullptr);
2017+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
20182018
EXPECT_TRUE(object_c != nullptr);
20192019
output = testing::internal::GetCapturedStdout();
20202020
EXPECT_EQ(output, "Constructor Executed");
@@ -2137,7 +2137,7 @@ TEST(FunctionReflectionTest, Destruct) {
21372137
testing::internal::CaptureStdout();
21382138
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
21392139
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
2140-
auto object_c = clang_construct(scope_c, nullptr);
2140+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
21412141
clang_destruct(object_c, scope_c, true);
21422142
output = testing::internal::GetCapturedStdout();
21432143
EXPECT_EQ(output, "Destructor Executed");

0 commit comments

Comments
 (0)