public void Alloc(byte order) { var size = (1u << order) * 4096; fixed(mem_zone *zone = &global_mem_block.zone) { var data = new byte[size]; rnd.NextBytes(data); var ptr = (byte *)Allo.page_to_virt(zone, Allo.buddy_get_pages(zone, order)); if (ptr == null) { var s = ""; } for (var i = 0; i < size; i++) { ptr[i] = data[i]; } hash.Add((UIntPtr)ptr, new AllocEnry { Data = data }); //ptr[10] = 0; Check(); } }
public void run() { uint pages_size; uint start_addr; const uint _NPAGES = 8192; // init global memory block // all pages area pages_size = _NPAGES * (uint)sizeof(page); global_mem_block.pages = (page *)Marshal.AllocHGlobal((int)pages_size); start_addr = (uint)Marshal.AllocHGlobal((int)(_NPAGES * Allo.BUDDY_PAGE_SIZE)); global_mem_block.zone.free_area = (Allo.free_area *)Marshal.AllocHGlobal(Allo.BUDDY_MAX_ORDER * (int)sizeof(Allo.free_area)); fixed(mem_zone *zone = &global_mem_block.zone) { Allo.buddy_system_init(zone, global_mem_block.pages, start_addr, _NPAGES); } for (var i = 0; i < 10000; i++) { if (hash.Count > 3 && rnd.Next(2) == 0) { RandomFree(); } else { var order = (byte)((uint)rnd.Next(4) + 1); Alloc(order); } } System.Console.WriteLine(hash.Count); }
private void RandomFree() { fixed(mem_zone *zone = &global_mem_block.zone) { var keys = hash.Keys.ToList(); var ptr = keys[rnd.Next(keys.Count - 1)]; hash.Remove(ptr); var page = Allo.virt_to_page(zone, (void *)ptr); Allo.buddy_free_pages(zone, page); } }