//---------------------------------------------------------------------------- // Copyright (C) 2013-2015 Fabrice HARROUET (ENIB) // // Permission to use, copy, modify, distribute and sell this software // and its documentation for any purpose is hereby granted without fee, // provided that the above copyright notice appear in all copies and // that both that copyright notice and this permission notice appear // in supporting documentation. // The author makes no representations about the suitability of this // software for any purpose. // It is provided "as is" without express or implied warranty. //---------------------------------------------------------------------------- using System; public class Point { public int x,y; public Point(int x, int y) { this.x=x; this.y=y; } } public unsafe struct TinyFifo { fixed int d[16]; int r,w; public void put(int v) { fixed(int *p=d) { p[w]=v; } w=(w+1)&15; } public int get() { int v; fixed(int *p=d) { v=p[r]; } r=(r+1)&15; return v; } public bool full() { return r==((w+1)&15); } public bool empty() { return r==w; } } public class Test { public static void Main(string[] args) { int value=1000; unsafe { int *p1=&value; // value is local so won't move int *p2=p1; Console.WriteLine("value={0}, p1={1}, p2={2}", value,(IntPtr)p1,(IntPtr)p2); *p2=20; } Console.WriteLine("value={0}",value); unsafe { Point p=new Point(10,20); fixed(int *ptr1 = &p.x) // prevent object from moving fixed(int *ptr2 = &p.y) { long delta=ptr2-ptr1; Console.WriteLine("&p.x={0} p.x={1}",(IntPtr)ptr1,*ptr1); Console.WriteLine("&p.y={0} p.y={1}",(IntPtr)ptr2,*ptr2); Console.WriteLine("delta={0}",delta); ptr1[0]+=100; ptr1[delta]+=200; Console.WriteLine("&p.x={0} p.x={1}",(IntPtr)ptr1,*ptr1); Console.WriteLine("&p.y={0} p.y={1}",(IntPtr)ptr2,*ptr2); } } TinyFifo[] t=new TinyFifo[5]; unsafe { fixed(TinyFifo *p=t) { for(int i=0;i<100;++i) { TinyFifo *f=p+(i%5); if(!f->full()) f->put(i); } for(int i=0;i<5;++i) { TinyFifo *f=p+i; while(!f->empty()) Console.Write("{0} ",f->get()); Console.WriteLine(""); } } } unsafe { int sz=20; int* prime=stackalloc int[sz]; prime[0]=1; for(int count=1,candidate=2;count