| 9640 | } |
| 9641 | |
| 9642 | BfIRValue BfModule::AllocBytes(BfAstNode* refNode, const BfAllocTarget& allocTarget, BfType* type, BfIRValue sizeValue, BfIRValue alignValue, BfAllocFlags allocFlags/*, bool zeroMemory, bool defaultToMalloc*/) |
| 9643 | { |
| 9644 | BfIRValue result; |
| 9645 | |
| 9646 | BfType* ptrType; |
| 9647 | if ((type->IsObject()) && ((allocFlags & BfAllocFlags_RawArray) == 0)) |
| 9648 | ptrType = type; |
| 9649 | else |
| 9650 | ptrType = CreatePointerType(type); |
| 9651 | |
| 9652 | if ((allocTarget.mScopedInvocationTarget != NULL) || (allocTarget.mCustomAllocator)) |
| 9653 | { |
| 9654 | auto intType = GetPrimitiveType(BfTypeCode_IntPtr); |
| 9655 | SizedArray<BfExpression*, 2> argExprs; |
| 9656 | |
| 9657 | BfTypedValueExpression typedValueExpr; |
| 9658 | typedValueExpr.Init(BfTypedValue(sizeValue, intType)); |
| 9659 | typedValueExpr.mRefNode = refNode; |
| 9660 | argExprs.push_back(&typedValueExpr); |
| 9661 | |
| 9662 | BfTypedValueExpression appendSizeValueExpr; |
| 9663 | appendSizeValueExpr.Init(BfTypedValue(alignValue, intType)); |
| 9664 | appendSizeValueExpr.mRefNode = refNode; |
| 9665 | argExprs.push_back(&appendSizeValueExpr); |
| 9666 | |
| 9667 | BfExprEvaluator exprEvaluator(this); |
| 9668 | BfTypedValue allocResult; |
| 9669 | |
| 9670 | if (allocTarget.mScopedInvocationTarget != NULL) |
| 9671 | { |
| 9672 | SizedArray<BfTypeReference*, 2> genericArgs; |
| 9673 | exprEvaluator.DoInvocation(allocTarget.mScopedInvocationTarget, NULL, argExprs, BfMethodGenericArguments()); |
| 9674 | allocResult = LoadValue(exprEvaluator.mResult); |
| 9675 | } |
| 9676 | else if (allocTarget.mCustomAllocator) |
| 9677 | { |
| 9678 | auto customTypeInst = allocTarget.mCustomAllocator.mType->ToTypeInstance(); |
| 9679 | if (customTypeInst == NULL) |
| 9680 | { |
| 9681 | if (allocTarget.mCustomAllocator.mType->IsStructPtr()) |
| 9682 | customTypeInst = allocTarget.mCustomAllocator.mType->GetUnderlyingType()->ToTypeInstance(); |
| 9683 | } |
| 9684 | |
| 9685 | if (customTypeInst == NULL) |
| 9686 | { |
| 9687 | Fail(StrFormat("Type '%s' cannot be used as a custom allocator", TypeToString(allocTarget.mCustomAllocator.mType).c_str()), refNode); |
| 9688 | } |
| 9689 | else |
| 9690 | { |
| 9691 | BfTypedValueExpression typeValueExpr; |
| 9692 | String allocMethodName = "Alloc"; |
| 9693 | if (GetRawMethodByName(customTypeInst, "AllocTyped", -1, true, true)) |
| 9694 | { |
| 9695 | allocMethodName = "AllocTyped"; |
| 9696 | auto typeType = ResolveTypeDef(mCompiler->mTypeTypeDef); |
| 9697 | auto typeRefVal = CreateTypeDataRef(type); |
| 9698 | |
| 9699 | typeValueExpr.Init(BfTypedValue(typeRefVal, typeType)); |
no test coverage detected