0

So, I am trying to write a new linked list in c# that will behave properly in an unsafe context so I can pass it off to a multithreaded process. Unfortunately, even though all I need is a constant size pointer, this is creating a circular dependency. I would rather not make this a class, and I'm not sure how that would behave in an unsafe context. I also don't think that the interface solution will work in my case as it is literally a pointer to itself. Also, when I try to write it it complains about some of the code being managed yet... how do I fix that? Here is the code snippet:

struct UnsafeLinkedListNode<T>
{
    public T value;
    unsafe public UnsafeLinkedListNode<T>* next;
    unsafe public UnsafeLinkedListNode<T>* prev;
}

class UnsafeLinkedList<T>
{

    unsafe public UnsafeLinkedListNode<T>* head = null;
    unsafe public UnsafeLinkedListNode<T>* tail = null;
    public ulong count;
    public void AddAfter(T value)
    {
        UnsafeLinkedList<T> temp = new UnsafeLinkedList<T>();
        temp.value = value;
        AddAfter(temp);
    }
    public void AddAfter(UnsafeLinkedListNode<T>* value)
    {
        unsafe
        {
            if(head !=null)
            {
                value.prev = tail;
                value.next = null;
                tail.next = value;
                count++;

            }
            else
            {
                value.next = null;
                value.prev = null;
                head = value;
                tail = value;
                count++;
            }
        }
    }
}
Firestar9114
  • 432
  • 4
  • 9
  • I don't see any circular dependency (except the self-referencing ``UnsafeLinkListNode`` but that is normal for a linked list). However, I doubt this code will actually compile: you are calling ``AddAfter()`` with an ``UnsafeLinkedList`` value but it expects a pointer to an ``UnsafeLinkListNode`` value. – dumetrulo Mar 02 '18 at 06:34
  • The circular dependency is in that first struct, also thanks, I completely missed that I already grabbed the address. I've updated that. How do I fix the circular dependency though? – Firestar9114 Mar 02 '18 at 06:41
  • Actuall, not using a generic does fix the managed code error, and I am no longer getting the error, but how do I make this work with a generic type then? struct UnsafeLinkedListNode { public int value; unsafe public UnsafeLinkedListNode* next; unsafe public UnsafeLinkedListNode* prev; } – Firestar9114 Mar 02 '18 at 06:44
  • I am actually curious, why can't you use array list / list for passing around data? It's both more memory efficient as well as more performant. Plus its easier to pass around as well. – Grimson Mar 02 '18 at 06:48
  • Also, you are not assigning `value.value = something` so that could be the reason – Grimson Mar 02 '18 at 06:51

1 Answers1

0

This fixes the circular dependency and code management issues. Finally solved it after 3 days, and then within minutes of posting. Unfortunately there is still an issue trying to get the address of the node for some reason, but here is the solution to THIS problem:

struct UnsafeLinkedListNode
{
    public EncodedData value;
    unsafe public UnsafeLinkedListNode* next;
    unsafe public UnsafeLinkedListNode* prev;
}

class UnsafeLinkedList
{

    unsafe public UnsafeLinkedListNode *head = null;
    unsafe public UnsafeLinkedListNode *tail = null;
    public ulong count { get; private set; }
    unsafe public void AddAfter(EncodedData value)
    {
        UnsafeLinkedListNode temp = new UnsafeLinkedListNode();
        temp.value = value;
        AddAfter(&temp);
    }
    unsafe public void AddAfter(UnsafeLinkedListNode* value)
    {
        unsafe
        {
            if(head !=null)
            {
                value->prev = tail;
                value->next = null;
                tail->next = value;
                count++;

            }
            else
            {
                value->next = null;
                value->prev = null;
                head = value;
                tail = value;
                count++;
            }
        }
    }
}
Firestar9114
  • 432
  • 4
  • 9