Skip to content

Commit a6a4a15

Browse files
committed
Array of objects in Construct/Destruct/Allocate/Deallocate
1 parent cce6bac commit a6a4a15

File tree

5 files changed

+67
-42
lines changed

5 files changed

+67
-42
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 35 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,36 @@ 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+
/// \param[in] scope Class to construct
812+
/// \param[in] arena If set, this API uses placement new to construct at this
813+
/// address.
814+
/// \param[in] is used to indicate the number of objects to construct.
815+
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
816+
TCppIndex_t count = 1UL);
817+
818+
/// Destroys one or more objects of a class
819+
/// \param[in] This this pointer of the object to destruct. Can also be the
820+
/// starting address of an array of objects
821+
/// \param[in] withFree if true, we call operator delete/free, else just the
822+
/// destructor
823+
/// \param[in] count indicate the number of objects to destruct, if \c This
824+
/// points to an array of objects
806825
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
807-
bool withFree = true);
826+
bool withFree = true, TCppIndex_t count = 0UL);
808827

809828
/// @name Stream Redirection
810829
///

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

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

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

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

@@ -3677,22 +3679,24 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
36773679
return nullptr;
36783680
}
36793681

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

36843687
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3685-
bool withFree) {
3688+
bool withFree, TCppIndex_t nary) {
36863689
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
3687-
(*wrapper)(This, /*nary=*/0, withFree);
3690+
(*wrapper)(This, nary, withFree);
36883691
return;
36893692
}
36903693
// FIXME: Diagnose.
36913694
}
36923695

3693-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3696+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
3697+
TCppIndex_t count /*=0UL*/) {
36943698
auto* Class = static_cast<Decl*>(scope);
3695-
Destruct(getInterp(), This, Class, withFree);
3699+
Destruct(getInterp(), This, Class, withFree, count);
36963700
}
36973701

36983702
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
@@ -2034,7 +2034,7 @@ TEST(FunctionReflectionTest, Construct) {
20342034
testing::internal::CaptureStdout();
20352035
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
20362036
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
2037-
auto object_c = clang_construct(scope_c, nullptr);
2037+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
20382038
EXPECT_TRUE(object_c != nullptr);
20392039
output = testing::internal::GetCapturedStdout();
20402040
EXPECT_EQ(output, "Constructor Executed");
@@ -2157,7 +2157,7 @@ TEST(FunctionReflectionTest, Destruct) {
21572157
testing::internal::CaptureStdout();
21582158
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
21592159
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
2160-
auto object_c = clang_construct(scope_c, nullptr);
2160+
auto object_c = clang_construct(scope_c, nullptr, 0UL);
21612161
clang_destruct(object_c, scope_c, true);
21622162
output = testing::internal::GetCapturedStdout();
21632163
EXPECT_EQ(output, "Destructor Executed");

0 commit comments

Comments
 (0)