public void AllocReturnsAddressTest() { var bh = new Heap(); // Returns the beginning of the newly allocated memory. Assert.Equal(0, bh.Alloc(10)); Assert.Equal(10, bh.Alloc(10)); // Returns the address of the last byte in the allocated space. Assert.Equal(14, bh.Alloc(-5)); // Returns the address of the last byte in the allocated space. Assert.Equal(14, bh.Alloc(0)); }
public void SystemOnFrame_1_type_3_component() { var positionTypeId = ECS_COMPONENT <Position>(world); var velTypeId = ECS_COMPONENT <Velocity>(world); var massTypeId = ECS_COMPONENT <Mass>(world); var systemEntityId = ECS_SYSTEM(world, Iter, SystemKind.OnUpdate, "Position, Velocity, Mass"); var ctx = Heap.Alloc <SysTestData>(); ecs.set_context(world, (IntPtr)ctx); var e_1 = ECS_ENTITY(world, "e_1", "Position, Velocity, Mass"); var e_2 = ECS_ENTITY(world, "e_2", "Position, Velocity, Mass"); var e_3 = ECS_ENTITY(world, "e_3", "Position, Velocity, Mass"); ecs.progress(world, 1); Assert.IsTrue(ctx->count == 3); Assert.IsTrue(ctx->invoked == 1); Assert.IsTrue(ctx->system.Value == systemEntityId.Value); Assert.IsTrue(ctx->column_count == 3); Assert.IsTrue(ctx->e[0] == e_1.Value); Assert.IsTrue(ctx->e[1] == e_2.Value); Assert.IsTrue(ctx->e[2] == e_3.Value); Assert.IsTrue(ctx->GetC(0, 0) == ecs.type_to_entity(world, positionTypeId).Value); Assert.IsTrue(ctx->GetS(0, 0) == 0); Assert.IsTrue(ctx->GetC(0, 1) == ecs.type_to_entity(world, velTypeId).Value); Assert.IsTrue(ctx->GetS(0, 1) == 0); Assert.IsTrue(ctx->GetC(0, 2) == ecs.type_to_entity(world, massTypeId).Value); Assert.IsTrue(ctx->GetS(0, 2) == 0); var p = (Position *)ecs.get_ptr(world, e_1, positionTypeId); Assert.IsTrue(p->x == 10); Assert.IsTrue(p->y == 20); p = (Position *)ecs.get_ptr(world, e_2, positionTypeId); Assert.IsTrue(p->x == 10); Assert.IsTrue(p->y == 20); p = (Position *)ecs.get_ptr(world, e_3, positionTypeId); Assert.IsTrue(p->x == 10); Assert.IsTrue(p->y == 20); var v = (Velocity *)ecs.get_ptr(world, e_1, velTypeId); Assert.IsTrue(v->x == 30); Assert.IsTrue(v->y == 40); v = (Velocity *)ecs.get_ptr(world, e_2, velTypeId); Assert.IsTrue(v->x == 30); Assert.IsTrue(v->y == 40); v = (Velocity *)ecs.get_ptr(world, e_3, velTypeId); Assert.IsTrue(v->x == 30); Assert.IsTrue(v->y == 40); }
public void Singleton_system_w_singleton() { var posType = ECS_COMPONENT <Position>(world); var velType = ECS_COMPONENT <Velocity>(world); ECS_SYSTEM(world, Iter_w_singleton, SystemKind.OnUpdate, "Position, $.Velocity"); ecs.set_singleton(world, new Velocity { x = 1, y = 2 }); Assert.IsTrue(ecs.has(world, ecs.ECS_SINGLETON, velType)); var pos = new Position { x = 10, y = 20 }; var e = ecs.set(world, (EntityId)0, pos); Assert.IsTrue(!ecs.has(world, ecs.ECS_SINGLETON, posType)); var ctx = Heap.Alloc <SysTestData>(); ecs.set_context(world, (IntPtr)ctx); ecs.progress(world, 1); Assert.IsTrue(ctx->count == 1); var p = ecs.get_ptr <Position>(world, e); Assert.IsTrue(p != null); Assert.IsTrue(p->x == 11); Assert.IsTrue(p->y == 22); }
/// <summary> /// Transmit packet /// </summary> /// <param name="bytes">byte buffer</param> /// <param name="size">packet size</param> public static unsafe void Transmit(NetPacketDesc *packet) { int size = packet->end - packet->start; if (size < 0) { return; } byte *buffer = (byte *)Heap.Alloc(size); if (buffer == null) { return; } Memory.Memcpy(buffer, packet->buffer + packet->start, size); #if NETWORK_DEBUG Console.Write("[NET] Transmit packet with "); Console.WriteNum(size); Console.WriteLine(" bytes"); #endif m_dev.Transmit?.Invoke(buffer, (uint)size); Heap.Free(buffer); }
/// <summary> /// Add packet for handling /// </summary> /// <param name="buffer">The packet buffer</param> /// <param name="size">The packet buffer size</param> public static unsafe void QueueReceivePacket(byte[] buffer, int size) { if (size < sizeof(EthernetHeader)) { #if NETWORK_DEBUG Console.WriteLine("[NET] size < sizeof(EthernetHeader)"); #endif return; } NetRecBuffer *netBuf = (NetRecBuffer *)Heap.Alloc(sizeof(NetRecBuffer)); if (netBuf == null) { #if NETWORK_DEBUG Console.WriteLine("[NET] netBuf = null"); #endif return; } netBuf->Size = size; netBuf->Buffer = (byte *)Heap.Alloc(size); if (netBuf->Buffer == null) { #if NETWORK_DEBUG Console.WriteLine("[NET] netbuf->buffer == null"); #endif Heap.Free(netBuf); return; } Memory.Memcpy(netBuf->Buffer, Util.ObjectToVoidPtr(buffer), size); m_recPacketQueue.Push(netBuf); }
/// <summary> /// Generate node name from bus/slot/function /// </summary> /// <param name="bus">The bus</param> /// <param name="slot">The slot</param> /// <param name="function">The function</param> /// <returns>The node name</returns> public static unsafe string GenerateNodeName(int bus, int slot, int function) { string part1 = bus.ToString(); string part2 = slot.ToString(); string part3 = function.ToString(); char *ptr = (char *)Heap.Alloc(10); int x = 0; for (int j = 0; j < part1.Length; j++) { ptr[x++] = part1[j]; } ptr[x++] = ':'; for (int j = 0; j < part2.Length; j++) { ptr[x++] = part2[j]; } ptr[x++] = ':'; for (int j = 0; j < part3.Length; j++) { ptr[x++] = part3[j]; } ptr[x] = '\0'; Heap.Free(part1); Heap.Free(part2); Heap.Free(part3); return(Util.CharPtrToString(ptr)); }
public unsafe IUSBDriver Load(USBDevice device) { USBInterfaceDescriptor *desc = device.InterfaceDesc; if (!(desc->Class == (byte)USBClassCodes.HID && desc->SubClass == 0x01 && desc->Protocol == 0x02)) { return(null); } device.Classifier = USBDeviceClassifier.FUNCTION; mTransfer = (USBTransfer *)Heap.Alloc(sizeof(USBTransfer)); mLocation = (MouseLocation *)Heap.Alloc(sizeof(MouseLocation)); /** * Prepare poll transfer */ mTransfer->Data = (byte *)mLocation; mTransfer->Length = 3; mTransfer->Executed = false; mTransfer->Success = false; mTransfer->ID = 2; //device.PrepareInterrupt(device, mTransfer); return(this); }
private static void setConnectionForWait(TCPConnection connection) { connection.State = TCPConnectionState.TIME_WAIT; if (connection.Type == TCPConnectionType.CONNECTION) { TCPPacketDescriptor *buf = (TCPPacketDescriptor *)Heap.Alloc(sizeof(TCPPacketDescriptor)); buf->Size = 0; buf->Type = TCPPacketDescriptorTypes.CLOSE; buf->Data = null; buf->xid = 0; connection.ReceiveQueue.Push(buf); } else { /** * We need to remove the key here! */ /** * put RESET in QUEUE */ TCPPacketDescriptor *buf = (TCPPacketDescriptor *)Heap.Alloc(sizeof(TCPPacketDescriptor)); buf->Size = 0; buf->Type = TCPPacketDescriptorTypes.CLOSE; buf->Data = null; buf->xid = connection.XID; connection.BaseConnection.ReceiveQueue.Push(buf); connection.BaseConnection.Clients.Remove(connection.XID); } }
private static unsafe string subStringImpl(string str, int start, int count) { if (count <= 0) { return(Clone("")); } int stringLength = str.Length; if (start > stringLength) { return(Clone("")); } char *ch = (char *)Heap.Alloc(count + 1); int j = 0; for (int i = start; j < count; i++) { if (str[i] == '\0') { break; } ch[j++] = str[i]; } ch[j] = '\0'; return(Util.CharPtrToString(ch)); }
public void ReadWriteInt64Test() { var bh = new Heap(); bh.Alloc(32); bh.Write(0, 123L); bh.Write(8, 1234567890123L); Assert.Equal(123L, bh.ReadInt64(0)); Assert.Equal(1234567890123L, bh.ReadInt64(8)); // Big number test. bh.Write(16, 1_234_567_890_123_456_789L); Assert.Equal(1_234_567_890_123_456_789L, bh.ReadInt64(16)); // 11 22 10 F4 7D E9 81 15 Assert.Equal(0x15, bh.ReadByte(16)); Assert.Equal(0x81, bh.ReadByte(17)); Assert.Equal(0xE9, bh.ReadByte(18)); Assert.Equal(0x7D, bh.ReadByte(19)); Assert.Equal(0xF4, bh.ReadByte(20)); Assert.Equal(0x10, bh.ReadByte(21)); Assert.Equal(0x22, bh.ReadByte(22)); Assert.Equal(0x11, bh.ReadByte(23)); }
/// <summary> /// FS readdir /// </summary> /// <param name="node">The node</param> /// <param name="index">The index</param> /// <returns>The directory entry</returns> private static unsafe DirEntry *rootReadDirImpl(Node node, uint index) { Task current = Tasking.KernelTask; int i = 0; while (i < index && current.NextTask != Tasking.KernelTask) { current = current.NextTask; i++; } // Last task entry reached but index is still not reached? Stop. if (i < index && current.NextTask == Tasking.KernelTask) { return(null); } DirEntry *entry = (DirEntry *)Heap.Alloc(sizeof(DirEntry)); string name = current.PID.ToString(); String.CopyTo(entry->Name, name); Heap.Free(name); return(entry); }
/// <summary> /// Read device capacity /// </summary> /// <returns></returns> private unsafe bool ReadCapacity() { SCSIReadCapData *data = (SCSIReadCapData *)Heap.Alloc(sizeof(SCSIReadCapData)); Memory.Memclear(data, sizeof(SCSIReadCapData)); // CMD transfer SCSIReadCap *cmd = (SCSIReadCap *)Heap.Alloc(sizeof(SCSIReadCap)); Memory.Memclear(cmd, sizeof(SCSIReadCap)); cmd->Opcode = SCSI_READ_CAPACITY; cmd->LBA = 0; cmd->PMI = 0; cmd->Control = 0; if (!Command((byte *)cmd, (byte)sizeof(SCSIReadCap), (byte *)data, sizeof(SCSIReadCapData), true)) { Heap.Free(cmd); Heap.Free(data); return(false); } _BlockLength = SwapBytes((uint)data->BlockLength); _ReturnedLBA = SwapBytes((uint)data->LBA); Heap.Free(cmd); Heap.Free(data); return(true); }
/// <summary> /// FS finddir /// </summary> /// <param name="node">The node</param> /// <param name="name">The name to look for</param> /// <returns>The node</returns> private static unsafe Node findDirImpl(Node node, string name) { byte[] ip = NetworkTools.StringToIp(name); if (ip == null) { return(null); } byte *dstMac = (byte *)Heap.Alloc(6); Memory.Memset(dstMac, 0, 6); ARP.Lookup(ip, dstMac); if (dstMac[0] == 0x00 && dstMac[1] == 0x00 && dstMac[2] == 0x00 && dstMac[3] == 0x00 && dstMac[4] == 0x00 && dstMac[5] == 0x00) { byte[] mac = new byte[6]; for (int i = 0; i < 6; i++) { mac[i] = 0xFF; } ARP.ArpSend(ARP.OP_REQUEST, mac, ip); Heap.Free(mac); return(null); } Heap.Free(dstMac); return(new Node()); }
/// <summary> /// Creates a directory entry by its name /// </summary> /// <param name="str">The name</param> /// <returns>The entry</returns> private static unsafe DirEntry *makeByName(string str) { DirEntry *entry = (DirEntry *)Heap.Alloc(sizeof(DirEntry)); String.CopyTo(entry->Name, str); return(entry); }
/// <summary> /// Intializes FAT device on device node /// </summary> /// <param name="dev">Device</param> private unsafe void initFAT() { m_beginLBA = 0; byte[] bootSector = new byte[512]; _Device.Read(_Device, (uint)m_beginLBA, 512, bootSector); m_bpb = (Fat16BPB *)Util.ObjectToVoidPtr(bootSector); int fatRegionSize = m_bpb->NumFats * m_bpb->SectorsPerFat16; int rootDirSize = (512 * 32) / m_bpb->BytesPerSector; long dataRegionSize = m_bpb->LargeAmountOfSectors - (m_bpb->ReservedSectors + fatRegionSize + rootDirSize); m_fatSize = (uint)dataRegionSize / m_bpb->SectorsPerCluster; // Cache fat table table uint size = (uint)fatRegionSize * 512; m_fatTable = (byte *)Heap.Alloc((int)size); m_fatClusterSize = fatRegionSize * 256; var beginFatTable = m_beginLBA + m_bpb->ReservedSectors; _Device.Read(_Device, (uint)beginFatTable, (uint)fatRegionSize * 512, Util.PtrToArray(m_fatTable)); parseBoot(); }
/// <summary> /// Do identify and read needed shizzle /// </summary> private void DoIdentify() { void *adr = Heap.Alloc(0x1000); int status = Identify(0, 1, adr); int statusCode = (status >> COMP_STATUS_SC_SHIFT) & COMP_STATUS_SC_MASK; if (statusCode != COMP_STATUS_SC_SUC) { Panic.DoPanic("[NVMe] Couldn't identifiy"); } NVMe_ID_Ctrl *idCtrl = (NVMe_ID_Ctrl *)adr; mFirmwareRevision = new char[8]; mSerialNumber = new char[20]; mModel = new char[40]; nNumNamespaces = (int)idCtrl->Nn; Memory.Memcpy(Util.ObjectToVoidPtr(mFirmwareRevision), idCtrl->FirmwareRevision, 8); Memory.Memcpy(Util.ObjectToVoidPtr(mSerialNumber), idCtrl->SerialNumber, 20); Memory.Memcpy(Util.ObjectToVoidPtr(mModel), idCtrl->ModelNumber, 40); Heap.Free(adr); }
/// <summary> /// Send ethernet packet /// </summary> /// <param name="packet">Packet structure</param> /// <param name="destMAC">Destination MAC</param> /// <param name="protocol">Protocol</param> public static unsafe void Send(NetPacketDesc *packet, byte[] destIP, EthernetTypes protocol) { // 1 TIME PLEASE byte *srcMAC = (byte *)Heap.Alloc(6); Network.GetMac(srcMAC); // Get MAC from ARP :D byte *dstMac = (byte *)Heap.Alloc(6); bool found = Route.FindRoute(destIP, dstMac); if (!found) { Heap.Free(srcMAC); Heap.Free(dstMac); return; } addHeader(packet, Util.PtrToArray(dstMac), Util.PtrToArray(srcMAC), protocol); Network.Transmit(packet); Heap.Free(srcMAC); Heap.Free(dstMac); }
public void SystemOnFrame_1_type_1_component() { var positionTypeId = ECS_COMPONENT <Position>(world); var e_1 = ECS_ENTITY(world, "e_1", "Position"); var e_2 = ECS_ENTITY(world, "e_2", "Position"); var e_3 = ECS_ENTITY(world, "e_3", "Position"); var systemEntityId = ECS_SYSTEM(world, Iter, SystemKind.OnUpdate, "Position"); var ctx = Heap.Alloc <SysTestData>(); ecs.set_context(world, (IntPtr)ctx); ecs.progress(world, 1); Assert.IsTrue(ctx->count == 3); Assert.IsTrue(ctx->invoked == 1); Assert.IsTrue(ctx->system.Value == systemEntityId.Value); Assert.IsTrue(ctx->column_count == 1); Assert.IsTrue(ctx->e[0] == e_1.Value); Assert.IsTrue(ctx->e[1] == e_2.Value); Assert.IsTrue(ctx->e[2] == e_3.Value); var positionEntityId = ecs.type_to_entity(world, positionTypeId); Assert.IsTrue(positionEntityId.Value == ctx->c[0]); }
public void Add(UInt32 key, UInt32 value, bool SkipCheck = false) { if (!SkipCheck) { if (Contains(key)) { ExceptionMethods.Throw(new FOS_System.Exception("Cannot add duplicate key to the dictionary!")); } } KeyValuePair *newItem = null; KeyValuePair *newNext = null; KeyValuePair *newPrev = null; KeyValuePair *newListNext = null; bool Alloc = true; if (list != null) { if (list->Empty) { newItem = list; newNext = newItem->Next; newPrev = null; newListNext = newItem->Next; Alloc = false; } else if (list->Next != null) { newItem = list->Next; newNext = newItem->Next; newPrev = newItem->Prev; newListNext = newItem; Alloc = false; } else { Alloc = true; } } if (Alloc) { newItem = (KeyValuePair *)Heap.Alloc((uint)sizeof(KeyValuePair), "UInt32Dictionary.Add"); newNext = null; newPrev = list; newListNext = newItem; } newItem->Key = key; newItem->Value = value; newItem->Next = newNext; newItem->Prev = newPrev; newItem->Empty = false; if (list != null && newItem != list) { list->Next = newListNext; } list = newItem; }
internal static Cpu.Status ALLOC(Script vm) { var srx = new SRX(vm, is_jump: false); int size = vm.cpu.registers[srx.dst]; int address = Heap.Alloc(vm, size); vm.cpu.registers[Registers.EAX] = address; return(Cpu.Status.OK); }
public void AllocSetsTopTest() { var bh = new Heap(); // Top is the address of the last byte in the allocated space. bh.Alloc(10); Assert.Equal(9, bh.Top); bh.Alloc(10); Assert.Equal(19, bh.Top); bh.Alloc(-5); Assert.Equal(14, bh.Top); bh.Alloc(0); Assert.Equal(14, bh.Top); }
public void ReadWriteByteTest() { var bh = new Heap(); bh.Alloc(10); bh.Write(5, (byte)123); Assert.Equal(123, bh.ReadByte(5)); }
/// <summary> /// Initialize nvme device /// </summary> public void InitDevice() { CurrentCID = 1; mAddress = (int)mDevice.BAR0.Address; mAddressVirtual = (int)Paging.MapToVirtual(Paging.CurrentDirectory, mAddress, 0x1000, Paging.PageFlags.Present | Paging.PageFlags.Writable); mRegs = (NVMe_Registers *)mAddressVirtual; Pci.EnableBusMastering(mDevice); // TODO: Make this map the right size mTailsAndHeads = (int *)Paging.MapToVirtual(Paging.CurrentDirectory, mAddress + 0x1000, 0x4000, Paging.PageFlags.Present | Paging.PageFlags.Writable); ReadCapabilities(); ReadVersion(); ConfigureAdminQueue(); EnableController(); DoIdentify(); CreateQueues(); char *name = (char *)Heap.Alloc(6); name[0] = 'N'; name[1] = 'V'; name[2] = 'M'; name[3] = 'E'; name[4] = (char)('0' + mID++); name[5] = '\0'; string nameStr = Util.CharPtrToString(name); Node node = new Node(); node.Read = readImpl; node.Write = writeImpl; NVMeCookie cookie = new NVMeCookie(); cookie.NVMe = this; node.Cookie = cookie; Disk.InitalizeNode(node, nameStr); RootPoint dev = new RootPoint(nameStr, node); VFS.MountPointDevFS.AddEntry(dev); Console.Write("[NVME] Init with serialnumber "); for (int i = 0; i < 20; i++) { Console.Write(mSerialNumber[i]); } Console.WriteLine(""); }
/// <summary> /// Allocates a new network packet descriptor /// </summary> /// <returns>New network packet descriptor</returns> public static unsafe NetPacketDesc *Alloc() { NetPacketDesc *desc = (NetPacketDesc *)Heap.Alloc(sizeof(NetPacketDesc)); Memory.Memclear(desc->buffer, 4096); desc->start = 256; desc->end = 256; return(desc); }
/// <summary> /// Get string from descriptor /// </summary> /// <param name="languageID">Language ID</param> /// <param name="entryIndex">Index</param> /// <returns></returns> public unsafe string GetString(ushort languageID, uint entryIndex) { /** * Get length */ byte[] buf = new byte[1]; if (!Request(TYPE_DEVICETOHOST, REQ_GET_DESCRIPTOR, (ushort)((DESC_STRING << 8) | entryIndex), languageID, 1, (byte *)Util.ObjectToVoidPtr(buf))) { return(null); } int len = *(byte *)Util.ObjectToVoidPtr(buf); /** * Load full object */ byte[] buffer = new byte[len]; if (!Request(TYPE_DEVICETOHOST, REQ_GET_DESCRIPTOR, (ushort)((DESC_STRING << 8) | entryIndex), languageID, (ushort)(len), (byte *)Util.ObjectToVoidPtr(buffer))) { return(null); } if (len == 0) { return(""); } /** * Set string */ USBStringDescriptor *ptr = (USBStringDescriptor *)Util.ObjectToVoidPtr(buffer); char *entryData = (char *)ptr + 2; int size = (len - sizeof(USBStringDescriptor)) / 2; char *str = (char *)Heap.Alloc(size); int i = 0; for (; i < size; i++) { str[i] = entryData[i * 2]; } str[i] = (char)0x00; /** * Free unused shizzle */ Heap.Free(buffer); Heap.Free(buf); return(Util.CharPtrToString(str)); }
public void ReadWriteFloatTest() { var bh = new Heap(); bh.Alloc(10); bh.Write(0, 123.0f); bh.Write(4, 123456.0f); Assert.Equal(123.0f, bh.ReadFloat(0)); Assert.Equal(123456.0f, bh.ReadFloat(4)); }
public void ReadWriteDoubleTest() { var bh = new Heap(); bh.Alloc(32); bh.Write(0, 123.0); bh.Write(8, 1234567890123.0); Assert.Equal(123.0, bh.ReadDouble(0)); Assert.Equal(1234567890123.0, bh.ReadDouble(8)); }
/// <summary> /// Change power state /// </summary> /// <param name="state">Power state</param> public unsafe static void ChangePowerState(VboxDevPowerState state) { RequestPowerState *req = (RequestPowerState *)Heap.Alloc(sizeof(RequestPowerState)); req->header.Size = (uint)sizeof(RequestPowerState); req->header.Version = 0x10001; req->header.requestType = VboxDevRequestTypes.VMMDevReq_SetPowerStatus; req->header.rc = 0xFFFFF; req->PowerState = state; PortIO.Out32((ushort)m_dev.BAR0.Address, (uint)Paging.GetPhysicalFromVirtual(req)); }
private void Write() { using (var vw = File.CreateView(0, HEADER_SIZE)) { var ms = new MemoryStream(); var writer = new BinaryWriter(ms); // 幻数 writer.Write(Magic.GetBytes()); // 版本 writer.Write(Version); // 标记 Byte flag = 0; writer.Write(flag); // 如果有数据,则需要写入 var ss = Slots; var n = ss == null ? 0 : ss.Count; var len = n * BLOCK_SIZE; // 大小不同时,需要重新分配 var blk = _SlotData; if (blk.Size != len) { if (blk.Size > 0) { Heap.Free(blk); } blk = _SlotData = Heap.Alloc(len); } writer.Write(blk.Position); writer.Write(blk.Size); vw.WriteBytes(0, ms.ToArray()); // 写入数据槽 if (n > 0) { ms = new MemoryStream(); writer = new BinaryWriter(ms); for (var i = 0; i < n; i++) { writer.Write(ss[i].Position); writer.Write(ss[i].Size); } View.WriteBytes(blk.Position, ms.ToArray()); } } }
/// <summary> /// Reads directory entries from the FS /// </summary> /// <param name="node">The node</param> /// <param name="index">The current index</param> /// <returns>The directory entry</returns> private static unsafe DirEntry *readDirImpl(Node node, uint index) { if (index >= NumCommands) { return(null); } DirEntry *entry = (DirEntry *)Heap.Alloc(sizeof(DirEntry)); String.CopyTo(entry->Name, CommandNames[index]); return(entry); }