Skip to content

Instantly share code, notes, and snippets.

@ThuCommix
Created March 7, 2015 23:05
Show Gist options
  • Save ThuCommix/38fccaea26ca78928a94 to your computer and use it in GitHub Desktop.
Save ThuCommix/38fccaea26ca78928a94 to your computer and use it in GitHub Desktop.
public DataWriter(byte[] buffer)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
GCHandle handle = GCHandle.Alloc(buffer, WhateverMode.Pinned);
_buffer = (byte*)handle.AddrOfPinnedObject();
}
public void Write(int value)
{
WriteData(&value, 4);
}
private void WriteData(void* source, int length)
{
if (_buffer == null)
throw new ObjectDisposedException(this.GetType().Name);
if (_bufferLength < length)
throw new ArgumentException("Exceeding buffer boundaries.");
byte* cb = _buffer;
byte* dest = _cb + length;
byte* src = (byte*)source;
for(; cb < dest; cb++, src++)
cb = src;
}
private void Dispose(bool disposing)
{
if (_buffer != null)
{
_buffer = null;
_handle.Free();
}
}
~DataWriter()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
@ThuCommix
Copy link
Author

bei dem cb = src sollen jeweils * vor den bezeichnern stehn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment