singrdk/base/Imported/Bartok/runtime/shared/GCs/Allocator.cs

128 lines
4.1 KiB
C#
Raw Normal View History

2008-11-17 18:29:00 -05:00
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-03-05 09:52:00 -05:00
/*******************************************************************/
/* WARNING */
/* This file should be identical in the Bartok and Singularity */
/* depots. Master copy resides in Bartok Depot. Changes should be */
/* made to Bartok Depot and propagated to Singularity Depot. */
/*******************************************************************/
namespace System.GCs
{
using Microsoft.Bartok.Runtime;
using System.Runtime.CompilerServices;
[NoCCtor]
2008-11-17 18:29:00 -05:00
internal abstract class Allocator
2008-03-05 09:52:00 -05:00
{
[Inline]
[NoHeapAllocation]
internal static unsafe void WriteAlignment(UIntPtr addr)
{
*(UIntPtr*)addr = ALIGNMENT_TOKEN;
}
[Inline]
[NoHeapAllocation]
internal static unsafe bool IsAlignmentMarkerAddr(UIntPtr addr)
{
return (*(UIntPtr*)addr == ALIGNMENT_TOKEN);
}
[Inline]
[NoHeapAllocation]
internal static unsafe bool IsAlignment(UIntPtr obj)
{
return *(UIntPtr*)(obj - PreHeader.Size) == ALIGNMENT_TOKEN;
}
[Inline]
[NoHeapAllocation]
internal static UIntPtr SkipAlignment(UIntPtr obj)
{
while (IsAlignment(obj)) {
obj += UIntPtr.Size;
}
return obj;
}
// Pads the allocation to the appropriate alignment.
[Inline]
internal static UIntPtr AlignedAllocationPtr(UIntPtr startAddr,
UIntPtr limitAddr,
uint alignment)
{
if (alignment > UIntPtr.Size) {
uint alignmentMask = alignment - 1;
int offset = PreHeader.Size + PostHeader.Size;
while (((startAddr + offset) & alignmentMask) != 0 &&
startAddr < limitAddr) {
Allocator.WriteAlignment(startAddr);
startAddr += UIntPtr.Size;
}
}
return startAddr;
}
[Inline]
internal static UIntPtr AlignedObjectPtr(UIntPtr startAddr,
uint alignment)
{
if (alignment > UIntPtr.Size) {
uint alignmentMask = alignment - 1;
int offset = PostHeader.Size;
while (((startAddr + offset) & alignmentMask) != 0) {
Allocator.WriteAlignment(startAddr- PreHeader.Size);
startAddr += UIntPtr.Size;
}
}
return startAddr;
}
2008-11-17 18:29:00 -05:00
private static UIntPtr ALIGNMENT_TOKEN
{
[Inline]
[NoHeapAllocation]
get { return ~((UIntPtr)3u); }
}
// REVIEW: Consider moving to BartokObject --Bjarne
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static unsafe
UIntPtr * GetObjectVTableAddress(UIntPtr potentialObject)
{
2008-03-05 09:52:00 -05:00
UIntPtr vtableAddr = potentialObject + Magic.OffsetOfVTable;
return (UIntPtr *) vtableAddr;
}
2008-11-17 18:29:00 -05:00
// REVIEW: Consider moving to BartokObject --Bjarne
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static unsafe UIntPtr GetObjectVTable(UIntPtr potentialObject)
{
2008-03-05 09:52:00 -05:00
return *GetObjectVTableAddress(potentialObject);
}
2008-11-17 18:29:00 -05:00
// REVIEW: Consider moving to BartokObject --Bjarne
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
internal static unsafe void SetObjectVTable(UIntPtr potentialObject,
2008-11-17 18:29:00 -05:00
UIntPtr vtable)
{
2008-03-05 09:52:00 -05:00
*GetObjectVTableAddress(potentialObject) = vtable;
}
2008-11-17 18:29:00 -05:00
// REVIEW: Consider moving to BartokObject --Bjarne
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static bool IsZeroVTable(UIntPtr addr)
{
2008-03-05 09:52:00 -05:00
return GetObjectVTable(addr) == UIntPtr.Zero;
}
}
}