/// <summary> /// WriteDataRecord - writes a data record in the form of "size - id - data" /// The Length of the data packed is "size" - (2 * sizeof(int)). /// Note that the cbRecordSize param is *only* the size of the record itself. The Size /// written to the stream will be larger (by sizeof(RecordHeader)) because it includes the size /// itself and the id. /// </summary> /// <param name="id"> MILCMD - the record id </param> /// <param name="pbRecord"> /// byte* pointing to at least cbRecordSize bytes which will be copied to the stream. /// </param> /// <param name="cbRecordSize"> int - the size, in bytes, of pbRecord. Must be >= 0. </param> public unsafe void WriteDataRecord(MILCMD id, byte *pbRecord, int cbRecordSize) { Debug.Assert(cbRecordSize >= 0); // The records must always be padded to be QWORD aligned. Debug.Assert((_curOffset % 8) == 0); Debug.Assert((cbRecordSize % 8) == 0); Debug.Assert((sizeof(RecordHeader) % 8) == 0); int totalSize, newOffset; checked { totalSize = cbRecordSize + sizeof(RecordHeader); newOffset = _curOffset + totalSize; } // Do we need to increase the buffer size? // Yes, if there's no buffer or if the buffer is too small. if ((_buffer == null) || (newOffset > _buffer.Length)) { EnsureBuffer(newOffset); } // At this point, _buffer must be non-null and // _buffer.Length must be >= newOffset Debug.Assert((_buffer != null) && (_buffer.Length >= newOffset)); // Also, because pinning a 0-length buffer fails, we assert this too. Debug.Assert(_buffer.Length > 0); RecordHeader header; header.Size = totalSize; header.Id = id; Marshal.Copy((IntPtr)(&header), this._buffer, _curOffset, sizeof(RecordHeader)); Marshal.Copy((IntPtr)pbRecord, this._buffer, _curOffset + sizeof(RecordHeader), cbRecordSize); _curOffset += totalSize; }
public unsafe void WriteDataRecord(MILCMD id, byte* pbRecord, int cbRecordSize) { Debug.Assert(cbRecordSize >= 0); // The records must always be padded to be QWORD aligned. Debug.Assert((_curOffset % 8) == 0); Debug.Assert((cbRecordSize % 8) == 0); Debug.Assert((sizeof(RecordHeader) % 8) == 0); int totalSize, newOffset; checked { totalSize = cbRecordSize + sizeof(RecordHeader); newOffset = _curOffset + totalSize; } // Do we need to increase the buffer size? // Yes, if there's no buffer or if the buffer is too small. if ((_buffer == null) || (newOffset > _buffer.Length)) { EnsureBuffer(newOffset); } // At this point, _buffer must be non-null and // _buffer.Length must be >= newOffset Debug.Assert((_buffer != null) && (_buffer.Length >= newOffset)); // Also, because pinning a 0-length buffer fails, we assert this too. Debug.Assert(_buffer.Length > 0); RecordHeader header; header.Size = totalSize; header.Id = id; Marshal.Copy((IntPtr)(&header), this._buffer, _curOffset, sizeof(RecordHeader)); Marshal.Copy((IntPtr)pbRecord, this._buffer, _curOffset + sizeof(RecordHeader), cbRecordSize); _curOffset += totalSize; }