/// <summary> /// /// </summary> /// <param name="vmClass"></param> /// <returns>绝对地址</returns> public uint MallocClassStaticArea(VMClass vmClass) { HeapData ret = Malloc(vmClass.StaticFieldSize); // TODO 头部信息暂时不知道填什么 return(MemoryMap.MapToAbsolute(ret.Offset, MemoryTag.STATIC)); }
/// <summary> /// /// </summary> /// <param name="objectClass"></param> /// <returns>绝对地址</returns> public uint New(VMClass objectClass) { HeapData data = Malloc(objectClass.FieldSize); // 类型信息 data.TypeInfo = objectClass.StaticFieldAddress; return(MemoryMap.MapToAbsolute(data.Offset, MemoryTag.HEAP)); }
/// <summary> /// 数组 /// </summary> /// <param name="typeTag"></param> /// <param name="len"></param> /// <returns></returns> public uint MallocArray(VariableTypeTag typeTag, int len) { int size = len * VariableType.GetSize(typeTag) + HeapData.MiscDataSize + HeapData.ArrayLengthSize; HeapData ret = Malloc(size); // 数组长度 BitConverter.TryWriteBytes(new Span <byte>(ret.Data, HeapData.MiscDataSize, HeapData.ArrayLengthSize), len); // GC的数组tag BitConverter.TryWriteBytes(new Span <byte>(ret.Data, sizeof(uint), sizeof(uint)), (uint)GCTag.ArrayMark); // 数组的Type是基本类型 ret.TypeInfo = (uint)typeTag; return(MemoryMap.MapToAbsolute(ret.Offset, MemoryTag.HEAP)); }
/// <summary> /// 注意要和SystemLib中的String的对象格式相同 /// </summary> /// <param name="value"></param> /// <returns>绝对地址</returns> public uint AddConstantString(string value) { if (!StringPool.TryGetValue(value, out HeapData data)) { // 分配byte数组 HeapData stringData = MallocCharArray(Encoding.UTF8.GetByteCount(value)); Encoding.UTF8.GetBytes(value, new Span <byte>(stringData.Data, HeapData.ArrayLengthSize + HeapData.MiscDataSize, stringData.Data.Length - HeapData.ArrayLengthSize - HeapData.MiscDataSize)); // String对象 byte[] vs = new byte[HeapData.MiscDataSize + HeapData.StringLengthSize + HeapData.StringDataSize]; // 头部信息可以不填,因为MethodArea是内存的边界,GC不会继续walk // 长度信息 BitConverter.TryWriteBytes(new Span <byte>(vs, HeapData.MiscDataSize, HeapData.StringLengthSize), value.Length); // Data信息 BitConverter.TryWriteBytes(new Span <byte>(vs, HeapData.MiscDataSize + HeapData.StringLengthSize, HeapData.StringDataSize), MemoryMap.MapToAbsolute(stringData.Offset, MemoryTag.METHOD)); // 字符串 data = Malloc(vs); StringPool.Add(value, data); } return(MemoryMap.MapToAbsolute(data.Offset, MemoryTag.METHOD)); }
public static void CollectGarbage() { GCWatch.Start(); // 从stack出发 foreach (var thread in ThreadManager.Threads) { // 要保证GC执行期间线程全部停止 for (uint i = 0; i < thread.Stack.SP; ++i) { if (thread.Stack.Slots[i].DataTag == SlotDataTag.ADDRESS) { MarkObject(thread.Stack.GetAddress(i)); } } } // 从类的静态区出发 foreach (var staticClassData in StaticArea.Singleton.DataMap.Values) { uint addr = MemoryMap.MapToAbsolute(staticClassData.Offset, MemoryTag.STATIC); ModuleLoader.Classes.TryGetValue(addr, out VMClass vmClass); foreach (var staticField in vmClass.StaticFields) { if (staticField.Type.Tag == VariableTypeTag.ADDRESS) { // 是地址 MarkObject(BitConverter.ToUInt32(staticClassData.Data, staticField.Offset)); } } } // 回收 LinkedListNode <HeapData> cur = Heap.Singleton.Data.First; while (cur != null) { if ((cur.Value.GCInfo & (uint)GCTag.GCMark) == 0) { // 不可达对象,删除 LinkedListNode <HeapData> tmp = cur; cur = cur.Next; Heap.Singleton.DataMap.Remove(tmp.Value.Offset); Heap.Singleton.Data.Remove(tmp); Heap.Singleton.Size -= tmp.Value.Data.Length; FreedSize += tmp.Value.Data.Length / 1024; } else { // 清除GCMark cur.Value.GCInfo = cur.Value.GCInfo & (~(uint)GCTag.GCMark); cur = cur.Next; } } GCWatch.Stop(); GCTotalTime += GCWatch.ElapsedMilliseconds; if (GCWatch.ElapsedMilliseconds > GCMaxTime) { GCMaxTime = GCWatch.ElapsedMilliseconds; } GCCount++; }
public static uint GetAbsoluteAddress(PreservedAddressTag tag) { return(MemoryMap.MapToAbsolute((uint)tag, MemoryTag.PRESERVED)); }