| 580 | } |
| 581 | |
| 582 | void* BfObjectAllocate(intptr size, bf::System::Type* objType) |
| 583 | { |
| 584 | size_t totalSize = size; |
| 585 | totalSize += 4; // int16 protectBytes, <unused bytes>, int16 sizeOffset |
| 586 | |
| 587 | void* result; |
| 588 | if (ThreadCache::have_tls && |
| 589 | LIKELY(totalSize < ThreadCache::MinSizeForSlowPath())) |
| 590 | { |
| 591 | result = BF_do_malloc_small(ThreadCache::GetCacheWhichMustBePresent(), totalSize); |
| 592 | } |
| 593 | else if (totalSize <= kMaxSize) |
| 594 | { |
| 595 | result = BF_do_malloc_small(ThreadCache::GetCache(), totalSize); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | result = BF_do_malloc_pages(ThreadCache::GetCache(), totalSize); |
| 600 | } |
| 601 | |
| 602 | BF_ASSERT(totalSize - (size + 4) <= kPageSize); |
| 603 | *(uint16*)((uint8*)result + size) = 0xBFBF; |
| 604 | *(uint16*)((uint8*)result + totalSize - 2) = totalSize - size; |
| 605 | |
| 606 | bf::System::Object* obj = (bf::System::Object*)result; |
| 607 | |
| 608 | BFLOG2(GCLog::EVENT_ALLOC, (intptr)obj, (intptr)objType); |
| 609 | |
| 610 | //CheckTcIntegrity(); |
| 611 | |
| 612 | gBFGC.mBytesRequested += size; |
| 613 | // #ifdef BF_GC_PRINTSTATS |
| 614 | // ::InterlockedIncrement((volatile uint32*)&gBFGC.mTotalAllocs); |
| 615 | // #endif |
| 616 | |
| 617 | #ifdef BG_GC_TRACKPTRS |
| 618 | { |
| 619 | Beefy::AutoCrit autoCrit(gBFGC.mCritSect); |
| 620 | BF_LOGASSERT(gTrackPtr.find(obj) == gTrackPtr.end()); |
| 621 | gTrackPtr.insert(obj); |
| 622 | } |
| 623 | #endif |
| 624 | |
| 625 | #ifdef _DEBUG |
| 626 | BF_LOGASSERT((obj->mAllocCheckPtr == 0) || (size > kMaxSize)); |
| 627 | #endif |
| 628 | |
| 629 | //memset((void*)obj, 0, size); |
| 630 | |
| 631 | #ifdef BF_OBJECT_TRACK_ALLOCNUM |
| 632 | obj->mAllocNum = (int)::InterlockedIncrement((volatile uint32*) &BfObject::sCurAllocNum); |
| 633 | #endif |
| 634 | |
| 635 | #ifdef BF_SECTION_NURSERY |
| 636 | BfInternalThread* internalThread = (BfInternalThread*)bf::System::Threading::Thread::CurrentInternalThread_internal(); |
| 637 | if ((internalThread != NULL) && (internalThread->mSectionDepth == 1)) |
| 638 | internalThread->mSectionNursery.PushUnsafe(obj); |
| 639 | #endif |
no test coverage detected