Beispiel #1
0
        unsafe void ReadHeap(IOFileStream source)
        {
            source.Position = 0;
            source.Read(_heapBytes, 0, sizeof(DeviceHeap));

            fixed(byte *src = _heapBytes)
            _heap = Marshal.PtrToStructure <DeviceHeap>(new IntPtr(src));
        }
Beispiel #2
0
        unsafe void ReadHeapOrCreate(IOFileStream source, int chunkSize, int blockSize)
        {
            if (source.Length != 0)
            {
                ReadHeap(source);
                return;
            }

            // fix up chunkSize to include fallocChunkHeader
            chunkSize = (chunkSize + sizeof(ChunkHeader) + 15) & ~15;

            // fix up blockSize to be a multiple of chunkSize
            if (blockSize == 0 || (blockSize % chunkSize) != 0)
            {
                blockSize += chunkSize - (blockSize % chunkSize);
            }

            var chunksLength = blockSize;
            var chunks       = blockSize / chunkSize;

            if (chunks == 0)
            {
                throw new InvalidOperationException();
            }

            // fix up blockSize to include DeviceHeap + freechunks
            var chunkRefsLength = chunks * sizeof(ChunkRef);

            _blockSize = (blockSize + chunkRefsLength + sizeof(DeviceHeap) + 15) & ~15;

            // transfer to heap
            var offset = sizeof(DeviceHeap);

            _heap = new DeviceHeap
            {
                chunkSize       = chunkSize,
                chunksLength    = chunksLength,
                chunkRefsLength = chunkRefsLength,
                chunkRefs       = offset,
                freeChunkPtr    = offset,
                retnChunkPtr    = offset,
                chunks          = offset + chunkRefsLength,
            };
            WriteHeap(source);
        }