/// <summary> /// Searches the hash map and returns the area element for the given pointer. /// </summary> /// <param name="pointer"></param> /// <returns></returns> private HeapAreaElement GetAreaElement(long pointer) { lock (this) { // Find the pointer in the hash int hash_pos = (int)(pointer % area_map.Length); HeapAreaElement prev = null; HeapAreaElement element = area_map[hash_pos]; // Search for this pointer while (element != null && element.Id != pointer) { prev = element; element = element.next_hash_element; } // If not found if (element == null) { throw new IOException("Pointer " + pointer + " is invalid."); } // Move the element to the start of the list. if (prev != null) { prev.next_hash_element = element.next_hash_element; element.next_hash_element = area_map[hash_pos]; area_map[hash_pos] = element; } // Return the element return(element); } }
/// <inheritdoc/> public void DeleteArea(long pointer) { lock (this) { // Find the pointer in the hash int hash_pos = (int)(pointer % area_map.Length); HeapAreaElement prev = null; HeapAreaElement element = area_map[hash_pos]; // Search for this pointer while (element != null && element.Id != pointer) { prev = element; element = element.next_hash_element; } // If not found if (element == null) { throw new IOException("Pointer " + pointer + " is invalid."); } // Remove if (prev == null) { area_map[hash_pos] = element.next_hash_element; } else { prev.next_hash_element = element.next_hash_element; } // Garbage collector should do the rest... } }
/// <inheritdoc/> public IAreaWriter CreateArea(long size) { if (size > Int32.MaxValue) { throw new IOException("'size' is too large."); } lock (this) { // Generate a unique id for this area. long id = unique_id_key; ++unique_id_key; // Create the element. HeapAreaElement element = new HeapAreaElement(id, (int)size); // The position in the hash map int hash_pos = (int)(id % area_map.Length); // Add to the chain element.next_hash_element = area_map[hash_pos]; // Set the element in the chain area_map[hash_pos] = element; // And return the object return(element.GetAreaWriter()); } }
/// <inheritdoc/> public IAreaWriter CreateArea(long size) { if (size > Int32.MaxValue) { throw new IOException("'size' is too large."); } lock (this) { // Generate a unique id for this area. long id = unique_id_key; ++unique_id_key; // Create the element. HeapAreaElement element = new HeapAreaElement(id, (int)size); // The position in the hash map int hash_pos = (int)(id % area_map.Length); // Add to the chain element.next_hash_element = area_map[hash_pos]; // Set the element in the chain area_map[hash_pos] = element; // And return the object return element.GetAreaWriter(); } }