public static void SetCapacity(UnsafeList *list, int capacity) { UDebug.Assert(list != null); UDebug.Assert(list->_items.Ptr != null); UDebug.Assert(capacity > 0); if (list->_items.Dynamic == 0) { throw new InvalidOperationException(ThrowHelper.InvalidOperation_CollectionFull); } // no change in capacity if (capacity == list->_items.Length) { return; } // allocate new items UnsafeBuffer newItems = default; UnsafeBuffer.InitDynamic(&newItems, capacity, list->_items.Stride); // if have anything in list, copy it if (list->_count > 0) { // also make sure that count is // not larger than the new capacity if (list->_count > capacity) { list->_count = capacity; } // copy over elements UnsafeBuffer.Copy(list->_items, 0, newItems, 0, list->_count); } // if an existing buffer was here, free it if (list->_items.Ptr != null) { UnsafeBuffer.Free(&list->_items); } // assign new buffer list->_items = newItems; }
public static void Free(UnsafeSortedSet *set) { if (set == null) { return; } if (set->_collection.Entries.Dynamic == 1) { UnsafeBuffer.Free(&set->_collection.Entries); } // clear memory *set = default; // free it Memory.Free(set); }
public static void Free(UnsafeSortedDictionary *map) { if (map == null) { return; } if (map->_collection.Entries.Dynamic == 1) { UnsafeBuffer.Free(&map->_collection.Entries); } // clear memory *map = default; // free it Memory.Free(map); }
static void Expand(UnsafeOrderedCollection *collection) { UDebug.Assert(collection->Entries.Dynamic == 1); UDebug.Assert(collection->FreeCount == 0); UDebug.Assert(collection->FreeHead == 0); var capacity = collection->Entries.Length * 2; var newEntries = default(UnsafeBuffer); UnsafeBuffer.InitDynamic(&newEntries, capacity, collection->Entries.Stride); UnsafeBuffer.Copy(collection->Entries, 0, newEntries, 0, collection->Entries.Length); // free old memory UnsafeBuffer.Free(&collection->Entries); // new storage collection->Entries = newEntries; }
public static void Free(UnsafeQueue *queue) { if (queue == null) { return; } // not fixed, we need to free items separtely if (queue->_items.Dynamic == 1) { UnsafeBuffer.Free(&queue->_items); } // clear queue memory (just in case) *queue = default; // free queue memory, if this is a fixed queue it frees the items memory at the same time Memory.Free(queue); }
public static void Free(UnsafeList *list) { if (list == null) { return; } // free dynamic items separately if (list->_items.Dynamic == 1) { UnsafeBuffer.Free(&list->_items); } // clear memory *list = default; // free list Memory.Free(list); }
public static void Free(UnsafeStack *stack) { if (stack == null) { return; } // if this is a dynamic sized stack, we need to free the buffer by hand if (stack->_items.Dynamic == 1) { UnsafeBuffer.Free(&stack->_items); } // clear stack memory just in case *stack = default; // free stack memory (if this is a fixed size stack, it frees the _items memory also) Memory.Free(stack); }
private static void Expand(UnsafeStack *stack) { // new buffer for elements UnsafeBuffer newItems = default; // initialize to double size of existing one int newSize = stack->_items.Length == 0 ? DEFAULT_CAPACITY : stack->_items.Length * 2; UnsafeBuffer.InitDynamic(&newItems, newSize, stack->_items.Stride); // copy memory over from previous items UnsafeBuffer.Copy(stack->_items, 0, newItems, 0, stack->_items.Length); // free old buffer UnsafeBuffer.Free(&stack->_items); // replace buffer with new stack->_items = newItems; }
static void Expand(UnsafeQueue *queue, int capacity) { UDebug.Assert(capacity > 0); // queue has to be dynamic and capacity we're going to have to be larger UDebug.Assert(queue->_items.Dynamic == 1); UDebug.Assert(queue->_items.Length < capacity); // new buffer for elements UnsafeBuffer newItems = default; // initialize to double size of existing one UnsafeBuffer.InitDynamic(&newItems, capacity, queue->_items.Stride); if (queue->_count > 0) { // when head is 'ahead' or at tail it means that we're wrapping around if (queue->_head >= queue->_tail) { // so we need to copy head first, from (head, length-head) into (0, length-head) UnsafeBuffer.Copy(queue->_items, queue->_head, newItems, 0, queue->_items.Length - queue->_head); // and then copy tail, from (0, tail) into (length-head, tail) UnsafeBuffer.Copy(queue->_items, 0, newItems, queue->_items.Length - queue->_head, queue->_tail); } else { // if not, we can just copy from (tail, count) into (0, count) UnsafeBuffer.Copy(queue->_items, queue->_head, newItems, 0, queue->_count); } } // free existing buffer UnsafeBuffer.Free(&queue->_items); queue->_items = newItems; queue->_head = 0; queue->_tail = queue->_count % queue->_items.Length; }