Пример #1
0
 public void BeginBatch()
 {
     Assert.IsTrue(currentChain == null, "Can not begin new batch write, when batch writing is started, Call EndBatch");
     Assert.IsTrue(ThreadID <= BlockChainCount, "Thread ID must be smaller than BlockChainCount");
     currentChain = BlockChains + ThreadID;
     currentBlock = currentChain->GetWritingBlock(allocator);
 }
Пример #2
0
            public static Block *Allocate(Allocator allocator)
            {
                Block *pBlock = (Block *)UnsafeUtility.Malloc(BlockSize, 32, allocator);

                UnsafeUtility.MemClear(pBlock, BlockSize);
                return(pBlock);
            }
Пример #3
0
 public Block *GetWritingBlock(Allocator allocator)
 {
     if (mCurrentWriteBlock == null)
     {
         if (mFirstBlock == null || mLastBlock == null)
         {//fresh start
             Assert.IsTrue(mFirstBlock == null || mLastBlock == null, "First/Last block has one missing while while other allocate!");
             mCurrentWriteBlock = mFirstBlock = mLastBlock = Block.Allocate(allocator);
             mBlockAllocated++;
         }
         else
         {
             var curBlk = mFirstBlock;
             while (curBlk != null)
             {
                 var nextBlk = curBlk->mNextBlock;
                 if (nextBlk->mElementCount == 0)
                 {
                     mCurrentWriteBlock = curBlk;
                     return(mCurrentWriteBlock);
                 }
                 Assert.IsFalse(nextBlk == mFirstBlock, "block loop!");
                 curBlk = nextBlk;
             }
             if (curBlk == null)//all block in use
             {
                 mLastBlock->mNextBlock = curBlk = Block.Allocate(allocator);
                 mCurrentWriteBlock     = mLastBlock = curBlk;
                 mBlockAllocated++;
             }
         }
     }
     return(mCurrentWriteBlock);
 }
Пример #4
0
        public static unsafe void Print(Block *src, TextWriter dst)
        {
            dst.WriteLine($"Hash: {Hex(src->GetHash())}");

            dst.WriteLine($"Previous: {Hex(src->GetPreviousHash())}");

            if (src->len > 0)
            {
                dst.WriteLine($"Data: {Hex(src->GetData())}");
            }

            int no = src->no;

            dst.WriteLine($"No: {no}");

            var timestamp = src->timestamp;

            dst.WriteLine($"Timestamp: {(new System.DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).AddSeconds(timestamp).ToLocalTime()}");

            var nonce = src->nonce;

            dst.WriteLine($"Nonce: {nonce}");

            dst.WriteLine($"Verified: {Database.IsValidBlock(src, null)}");

            if (Database.IsGenesis(src))
            {
                dst.WriteLine($"Genesis: {true}");
            }

            dst.WriteLine();
        }
Пример #5
0
    public static void GenerateResources(Map map)
    {
        Random seed   = new Random();
        Block *matrix = map.GetBlockMatrix();
        int    width  = map.Width;
        int    height = map.Height;


        /*gold*/
        generateResource(
            Globals.RESOURCE_GOLD,
            matrix,
            width, height,
            5,
            seed);

        /*stone*/
        generateResource(
            Globals.RESOURCE_STONE,
            matrix,
            width, height,
            5,
            seed);

        /*food*/
        generateResource(
            Globals.RESOURCE_FOOD,
            matrix,
            width, height,
            5,
            seed);
    }
Пример #6
0
        public static unsafe byte[] CreateBlock(Block *src, bool unhash)
        {
            byte[] buf = new byte[1024];

            Block tmp = *src;

            if (unhash)
            {
                byte *p = tmp.hash;

                for (int i = 0; i < 32; i++)
                {
                    p[i] = 0;
                }
            }

            fixed(byte *p = buf)
            {
                byte *s = (byte *)&tmp;

                for (int i = 0; i < 1024; i++)
                {
                    p[i] = s[i];
                }
            }

            return(buf);
        }
Пример #7
0
        public static unsafe void JSON(Block *src, TextWriter dst)
        {
            const string INDENT = "\t";

            dst.WriteLine($"{{");

            dst.WriteLine($"{INDENT}hash: \"{Hex(src->GetHash())}\",");

            dst.WriteLine($"{INDENT}previous: \"{Hex(src->GetPreviousHash())}\",");

            if (src->len > 0)
            {
                dst.WriteLine($"{INDENT}data: \"{Hex(src->GetData())}\",");
            }

            int no = src->no;

            dst.WriteLine($"{INDENT}no: {no},");

            var timestamp = src->timestamp;

            dst.WriteLine($"{INDENT}timestamp: \"{(new System.DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).AddSeconds(timestamp).ToLocalTime()}\",");

            var nonce = src->nonce;

            dst.WriteLine($"{INDENT}nonce: \"{nonce}\",");

            dst.WriteLine($"}}");
        }
Пример #8
0
    private void updateConcreteMatrix()
    {
        Lock();

        //has the concrete matrix not been allocated?
        if (p_ConcreteMatrix == (bool *)0)
        {
            p_ConcreteMatrix = (bool *)Marshal.AllocHGlobal(
                p_Width * p_Height);
        }

        //just populate the concerete matrix depending on
        //it's block counterpart having a resource state id (basically not -1)
        bool * ptr      = p_ConcreteMatrix;
        bool * ptrEnd   = ptr + (p_Width * p_Height);
        Block *blockPtr = p_Matrix;

        while (ptr != ptrEnd)
        {
            Block block = *blockPtr++;

            (*ptr++) = block.TypeID >= Globals.TERRAIN_END;
        }

        //clean up
        Unlock();
    }
Пример #9
0
    public Map(Game game, int width, int height)
    {
        p_Game   = game;
        p_Width  = width;
        p_Height = height;

        //allocate a block of memory to store the matrix
        p_Matrix = (Block *)Marshal.AllocHGlobal(
            (width * height) * sizeof(Block));

        //initialize every block
        Block *ptr    = p_Matrix;
        Block *ptrEnd = p_Matrix + (width * height);

        while (ptr != ptrEnd)
        {
            (*ptr).Selected = false;
            (*ptr).TypeID   = 0;
            ptr++;
        }

        //generate resources
        generateMap();
        updateConcreteMatrix();

        pathfindBench();
    }
Пример #10
0
            public Block *Allocate(Block *oldBlock, int threadIndex)
            {
                Assert.IsTrue(threadIndex < BlockCount && threadIndex >= 0);

                Block *block = (Block *)UnsafeUtility.Malloc(AllocationSize, 16, Allocator);

                block->Next = null;

                if (oldBlock == null)
                {
                    if (Blocks[threadIndex] == null)
                    {
                        Blocks[threadIndex] = block;
                    }
                    else
                    {
                        // Walk the linked list and append our new block to the end.
                        // Otherwise, we leak memory.
                        Block *head = Blocks[threadIndex];
                        while (head->Next != null)
                        {
                            head = head->Next;
                        }

                        head->Next = block;
                    }
                }
                else
                {
                    oldBlock->Next = block;
                }

                return(block);
            }
Пример #11
0
    private static void generateResource(short type, Block *matrix, int width, int height, int maxCount, Random seed)
    {
        Block *ptr    = matrix;
        Block *ptrEnd = matrix + (width * height);

        //iterate through every block
        while (ptr != ptrEnd)
        {
            Block *current = ptr++;

            //eligible for a resource?
            if ((*current).TypeID != Globals.TERRAIN_GRASS)
            {
                continue;
            }
            if (seed.Next(300) != 10)
            {
                continue;
            }                                       /*1 in 100 chance of a resource*/

            Block *nextBlock       = current;
            int    currentCount    = 0;
            int    currentMaxCount = seed.Next(0, maxCount);
            while (currentCount != currentMaxCount)
            {
                //out of bounds?
                if (nextBlock < matrix ||
                    nextBlock >= ptrEnd)
                {
                    break;
                }

                //place this resource
                if ((*nextBlock).TypeID == Globals.TERRAIN_GRASS)
                {
                    (*nextBlock).TypeID = type;
                }

                //go to the next resource
                int deltaX = 0;
                int deltaY = 0;
                while (deltaX == 0 && deltaY == 0)
                {
                    deltaX = seed.Next(-1, 1);
                    deltaY = seed.Next(-1, 1);

                    //do not allow diagonals.
                    if (deltaX != 0 && deltaY != 0)
                    {
                        deltaX = 0;
                        deltaY = 0;
                    }
                }

                nextBlock += (deltaY * width) + deltaX;
                currentCount++;
            }
        }
    }
Пример #12
0
        public static unsafe bool IsGenesis(Block *src)
        {
            if (Compare(src->GetPreviousHash(), Genesis))
            {
                return(true);
            }

            return(false);
        }
Пример #13
0
 void StartNextBlock()
 {
     currentChain->mBlockUsed++;
     if (currentBlock->mNextBlock == null)
     {
         currentBlock->mNextBlock = Block.Allocate(allocator);
         currentChain->mBlockAllocated++;
     }
     currentBlock = currentBlock->mNextBlock;
 }
Пример #14
0
            public void BeginForEachIndex(int foreachIndex)
            {
                BeginForEachIndexChecks(foreachIndex);

                m_ForeachIndex   = foreachIndex;
                m_ElementCount   = 0;
                m_NumberOfBlocks = 0;
                m_FirstBlock     = m_CurrentBlock;
                m_FirstOffset    = (int)(m_CurrentPtr - (byte *)m_CurrentBlock);
            }
Пример #15
0
            public void BeginForEachIndex(int foreachIndex)
            {
                //@TODO: Check that no one writes to the same for each index multiple times...
                BeginForEachIndexChecks(foreachIndex);

                m_ForeachIndex   = foreachIndex;
                m_ElementCount   = 0;
                m_NumberOfBlocks = 0;
                m_FirstBlock     = m_CurrentBlock;
                m_FirstOffset    = (int)(m_CurrentPtr - (byte *)m_CurrentBlock);
            }
Пример #16
0
 public void EndBatch()
 {
     Assert.IsTrue(currentChain != null, "Writing not started by calling BeginBatch, target batch is not defined");
     if (currentBlock->mDataByteLen > 0)
     {
         currentChain->mBlockUsed++;
     }
     //currentChain->mLastBlock = currentBlock;
     currentChain = null;
     currentBlock = null;
 }
Пример #17
0
        /// <summary>
        /// Expands the memory block of ptr to newSize
        /// </summary>
        /// <param name="ptr">The pointer to the memory block</param>
        /// <param name="newSize">The new size of the block</param>
        /// <returns>The memory block</returns>
        public static unsafe void *Expand(void *ptr, int newSize)
        {
            Block *block = getBlockFromPtr(ptr);

            int   size   = (block->Size < newSize) ? block->Size : newSize;
            void *newPtr = AlignedAlloc(4, newSize);

            Memory.Memcpy(newPtr, ptr, size);
            Free(ptr);

            return(newPtr);
        }
Пример #18
0
        /// <summary>
        /// Gets the block from the pointer
        /// </summary>
        /// <param name="ptr">The pointer</param>
        /// <returns>The block</returns>
        private static unsafe Block *getBlockFromPtr(void *ptr)
        {
            Block *block = (Block *)((int)ptr - sizeof(Block));

#if HEAP_USE_MAGIC
            if (block->Magic != HEAP_MAGIC)
            {
                Panic.DoPanic("[HEAP] block->Magic != HEAP_MAGIC");
            }
#endif
            return(block);
        }
Пример #19
0
        public int CompareTo(Sha1 other)
        {
            unsafe
            {
                fixed(Block *src = &_block)
                {
                    Block *dst = &other._block;

                    int cmp = ShaUtil.NativeMethods.MemCompare((byte *)src, (byte *)dst, ByteLength);

                    return(cmp);
                }
            }
        }
Пример #20
0
    public static void GenerateTerrain(Map map)
    {
        Block *matrix = map.GetBlockMatrix();
        int    width  = map.Width;
        int    height = map.Height;


        //define the noise generators for the terrain/resource
        Random      seed         = new Random();
        PerlinNoise terrainNoise = new PerlinNoise(seed);

        //use perlin noise to generate terrain (check if
        //the heighmap for each pixel is within a range for a resource
        Block *ptr = matrix;
        Block *ptrEnd = matrix + (width * height);
        int    x = 0, y = 0;

        while (ptr != ptrEnd)
        {
            Block *block = (ptr++);

            //get the heightmap value for this pixel
            //within a 1-100 scale
            double gen   = terrainNoise.GetHeight(x, y, 50, 50);
            int    value = (int)(Math.Abs(gen * 100));

            //deturmine the terrain block
            short blockType = Globals.TERRAIN_GRASS;
            if (value < 15)
            {
                blockType       = Globals.TERRAIN_WATER;
                (*block).Height = (byte)(value * 1.0f * 2);
            }
            else
            {
                blockType = Globals.TERRAIN_GRASS;
            }

            //set the block type
            (*block).TypeID = blockType;

            //update x/y
            x++;
            if (x == width)
            {
                x = 0;
                y++;
            }
        }
    }
Пример #21
0
        /// <summary>
        /// Creates a block descriptor with given minimal size
        /// </summary>
        /// <param name="size">The minimal size</param>
        /// <returns>The block descriptor</returns>
        private static unsafe BlockDescriptor *createBlockDescriptor(int size)
        {
#if HEAP_DEBUG_DESCRIPTOR
            Console.WriteLine("[HEAP] Creating a new block descriptor");
#endif

            // Add size of Block and BlockDescriptor
            size += sizeof(Block) + sizeof(BlockDescriptor);

            // Allocate descriptor
            size = getRequiredPageCount(size) * 0x1000;
            BlockDescriptor *descriptor = (BlockDescriptor *)Paging.AllocateVirtual(size);

#if HEAP_DEBUG_DESCRIPTOR
            Console.Write("[HEAP] New descriptor is at 0x");
            Console.WriteHex((int)descriptor);
            Console.Write(", physical: 0x");
            Console.WriteHex((int)Paging.GetPhysicalFromVirtual(descriptor));
            Console.Write('\n');
#endif

            if (descriptor == null)
            {
                Panic.DoPanic("descriptor == null");
            }

            // Setup block
            Block *first = (Block *)((int)descriptor + sizeof(BlockDescriptor));
            first->Prev       = null;
            first->Next       = null;
            first->Size       = size - sizeof(BlockDescriptor);
            first->Used       = false;
            first->Descriptor = descriptor;
#if HEAP_USE_MAGIC
            first->Magic = HEAP_MAGIC;
#endif

            // Setup descriptor
            descriptor->FreeSpace = size - sizeof(BlockDescriptor);
            descriptor->First     = first;
            descriptor->FirstFree = first;
            descriptor->Next      = null;
#if HEAP_USE_MAGIC
            descriptor->Magic = HEAP_MAGIC;
#endif

            return(descriptor);
        }
Пример #22
0
    private void test()
    {
        Point st = Point.Empty;
        Point en = Point.Empty;

        bool[,] m = load("paths/maze.astar", out st, out en);


        //st = new Point(200, 0);
        //en = new Point(999, 999);

        //st = new Point(3, 0);
        //en = new Point(7, 9);

        for (int x = 0; x < 1000; x++)
        {
            for (int y = 0; y < 1000; y++)
            {
                Block *b = translateToPointer(x, y);
                (*b).TypeID = (short)(m[x, y] ? Globals.RESOURCE_WOOD : Globals.TERRAIN_GRASS);
            }
        }

        updateConcreteMatrix();

        IPathfinderContext ctx = Pathfinder.ASCreateContext(p_Width, p_Height);

        while (true)
        {
            int          time = Environment.TickCount;
            List <Point> path = Pathfinder.ASSearch(
                ctx,
                st,
                en,
                p_ConcreteMatrix);

            Console.WriteLine((Environment.TickCount - time) + "ms for " + path.Count + " items");

            for (int c = 0; c < path.Count; c++)
            {
                Point  p  = path[c];
                Block *bl = translateToPointer(p.X, p.Y);
                (*bl).Selected = true;
            }
            break;
        }
    }
Пример #23
0
            public void BeginForEachIndex(int foreachIndex)
            {
#if BLOCK_STREAM_DEBUG
                if (foreachIndex < m_MinIndex || foreachIndex > m_MaxIndex)
                {
                    throw new ArgumentException($"Index {foreachIndex} is out of restricted IJobParallelFor range [{m_MinIndex}...{m_MaxIndex}] in BlockStream.");
                }
                Assert.IsTrue(foreachIndex >= 0 && foreachIndex < m_BlockStream->RangeCount);
                Assert.AreEqual(-1, m_ForeachIndex);
                Assert.AreEqual(0, m_BlockStream->Ranges[foreachIndex].Count);
#endif

                m_ForeachIndex = foreachIndex;
                m_Count        = 0;
                m_FirstBlock   = m_CurrentBlock;
                m_FirstOffset  = (int)(m_CurrentPtr - (byte *)m_CurrentBlock);
            }
Пример #24
0
            static public short[] DecodeBlocks(Block *Blocks, int BlockCount)
            {
                var Data          = new short[28 * BlockCount];
                var Decoder       = new Decoder();
                int SamplesOffset = 0;

                for (int n = 0; n < BlockCount; n++)
                {
                    if (Decoder.DecodeBlock(Blocks[n], Data, ref SamplesOffset))
                    {
                        //Console.WriteLine("STOP: {0}/{1}", n, BlockCount);
                        Data = Data.Slice(0, SamplesOffset);
                        break;
                    }
                }
                return(Data);
            }
Пример #25
0
            public void AppendForEachIndex(int foreachIndex)
            {
                CheckAccess();
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (foreachIndex < m_MinIndex || foreachIndex > m_MaxIndex)
                {
                    throw new System.ArgumentException($"forEachIndex {foreachIndex} is not in the allowed range");
                }
#endif

#if BLOCK_STREAM_DEBUG
                Assert.IsTrue(foreachIndex >= 0 && foreachIndex < m_BlockStream->RangeCount);
                Assert.AreEqual(-1, m_ForeachIndex);
#endif

                m_ForeachIndex = foreachIndex;
                m_Count        = m_BlockStream->Ranges[foreachIndex].Count;
                m_FirstOffset  = m_BlockStream->Ranges[foreachIndex].Offset;
                m_FirstBlock   = m_BlockStream->Ranges[foreachIndex].Block;

                if (m_Count > 0)
                {
                    m_CurrentBlock = m_FirstBlock;
                    while (m_CurrentBlock->Next != null)
                    {
                        m_CurrentBlock = m_CurrentBlock->Next;
                    }

                    m_CurrentPtr      = (byte *)m_CurrentBlock + m_BlockStream->Ranges[foreachIndex].LastOffset;
                    m_CurrentBlockEnd = (byte *)m_CurrentBlock + BlockStreamData.AllocationSize;
                }
                else
                {
                    Block *allocatedBlock = m_BlockStream->Blocks[m_ThreadIndex];
                    if (allocatedBlock != null)
                    {
                        // This can happen if a job was generated for this work item but didn't produce any
                        // contacts - the range will be empty, but a block will have been preemptively allocated
                        m_FirstBlock      = allocatedBlock;
                        m_CurrentBlock    = m_FirstBlock;
                        m_CurrentPtr      = m_CurrentBlock->Data;
                        m_FirstOffset     = (int)(m_CurrentPtr - (byte *)m_CurrentBlock);
                        m_CurrentBlockEnd = (byte *)m_CurrentBlock + BlockStreamData.AllocationSize;
                    }
                }
            }
Пример #26
0
        public unsafe static Task GetBlocks(Microsoft.Owin.IOwinContext ctx)
        {
            Cors(ctx);

            var plain = new StringBuilder();

            using (System.IO.TextWriter dst = new StringWriter(plain))
            {
                Database.Map(App.FILE, (i) =>
                {
                    Block *block = (Block *)i;

                    Utils.Print(
                        block,
                        dst);
                });
            }

            return(OK(ctx, plain.ToString()));
        }
Пример #27
0
        private void _Dispose()
        {
            if (m_Block == null)
            {
                return;
            }

            for (int i = 0; i != m_Block->BlockCount; i++)
            {
                Block *block = m_Block->Blocks[i];
                while (block != null)
                {
                    Block *next = block->Next;
                    UnsafeUtility.Free(block, m_AllocatorLabel);
                    block = next;
                }
            }

            UnsafeUtility.Free(m_Block, m_AllocatorLabel);
            m_Block = null;
        }
Пример #28
0
            internal Writer(ref NativeStream stream)
            {
                m_BlockStream     = stream.m_Block;
                m_ForeachIndex    = int.MinValue;
                m_ElementCount    = -1;
                m_CurrentBlock    = null;
                m_CurrentBlockEnd = null;
                m_CurrentPtr      = null;
                m_FirstBlock      = null;
                m_NumberOfBlocks  = 0;
                m_FirstOffset     = 0;
                m_ThreadIndex     = 0;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                m_Safety         = stream.m_Safety;
                m_Length         = int.MaxValue;
                m_MinIndex       = int.MinValue;
                m_MaxIndex       = int.MinValue;
                m_PassByRefCheck = null;
#endif
            }
Пример #29
0
    private bool isCoastline(Block *ptr, Block *matrix, out Direction direction, int x, int y, int width, int height)
    {
        Block *ptrEnd = matrix + (width * height);

        //get adjacent blocks
        Block *top    = ptr - width;
        Block *left   = ptr - 1;
        Block *right  = ptr + 1;
        Block *bottom = ptr + width;

        //check
        int typeID = Globals.TERRAIN_WATER;

        direction = Direction.NONE;

        if ((*ptr).TypeID == Globals.TERRAIN_WATER)
        {
            return(false);
        }

        if (y > 0 && (*top).TypeID == typeID)
        {
            direction |= Direction.NORTH;
        }
        if (x > 0 && (*left).TypeID == typeID)
        {
            direction |= Direction.WEST;
        }
        if (y < height && bottom < ptrEnd && (*bottom).TypeID == typeID)
        {
            direction |= Direction.SOUTH;
        }
        if (x < width && (*right).TypeID == typeID)
        {
            direction |= Direction.EAST;
        }


        return(direction != Direction.NONE);
    }
Пример #30
0
        void Deallocate()
        {
            if (m_Block == null)
            {
                return;
            }

            for (int i = 0; i != m_Block->BlockCount; i++)
            {
                Block *block = m_Block->Blocks[i];
                while (block != null)
                {
                    Block *next = block->Next;
                    UnsafeUtility.Free(block, m_AllocatorLabel);
                    block = next;
                }
            }

            UnsafeUtility.Free(m_Block->Ranges, m_AllocatorLabel);
            UnsafeUtility.Free(m_Block, m_AllocatorLabel);
            m_Block          = null;
            m_AllocatorLabel = Allocator.Invalid;
        }
Пример #31
0
 public Decoder(Block* BlockPointer, int BlockTotalCount)
 {
     this.BlockPointer = BlockPointer;
     this.BlockTotalCount = BlockTotalCount;
     Reset();
 }