StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
package com.tvd12.example.common; public class MemoryManagement { public static class Space { public int startIndex; public int position; public byte[] bytes; public int size; public Space prev; public Space next; public Space(int size) { this.bytes = new byte[size]; this.size = size; } public Space(int startIndex, int position, byte[] bytes) { this.startIndex = startIndex; this.position = position; this.bytes = bytes; } public boolean enoughSize(int requiredSize) { return position + requiredSize <= size; } public Space splitIfNeed(int requiredSize) { Space newSpace = this; if (startIndex != position) { newSpace = new Space( position, position + requiredSize, bytes ); newSpace.prev = this; newSpace.next = next; this.next = newSpace; this.next.prev = newSpace; this.size = position; } else { newSpace.position = requiredSize; } return newSpace; } public void free() { if (this.next == null) { if (this.prev != null) { this.prev.next = null; } } else { if (this.prev != null) { this.prev.next = this.next; this.next.prev = this.prev; } } } public int countSpaces() { int count = 1; Space space = this; while ((space = space.next) != null) { ++count; } return count; } } public static class Memory { public int maxSize; public Space root = new Space(1024); public Space allocate(int size) { Space availableSpace; Space node = root; while (true) { if (node.enoughSize(size)) { availableSpace = node.splitIfNeed(size); break; } if (node.next == null) { availableSpace = new Space(size); availableSpace.position = size; availableSpace.prev = node; node.next = availableSpace; break; } node = node.next; } if (availableSpace == null) { throw new OutOfMemoryError("not enough"); } return availableSpace; } public void retrieve(Space freeSpace) { freeSpace.free(); } } public static void main(String[] args) { Memory memory = new Memory(); Space space1 = memory.allocate(100); System.out.println(memory.root.countSpaces()); Space space2 = memory.allocate(1024); System.out.println(memory.root.countSpaces()); Space space3 = memory.allocate(1024); System.out.println(memory.root.countSpaces()); memory.retrieve(space3); System.out.println(memory.root.countSpaces()); memory.retrieve(space1); System.out.println(memory.root.countSpaces()); memory.retrieve(space2); System.out.println(memory.root.countSpaces()); } }
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Your account is signed in on the device(s) below. Sign out of at least one of them to continue signing in on this device.
Your account is signed in on the device(s) below. Sign out directly on one of those devices, or wait until the sign-in again time shown below.
If you cant access those devices, please contact the administrator to sign them out for you.
Input your email to receive reset password link, or if you remember your password, you can click