/* static DispoIntPtr() { // Platform.SetupPlatformImplementation(typeof(DispoIntPtr)); } */ public static DispoIntPtr Alloc(int size) { var dispPtr = new DispoIntPtr(); dispPtr._ptr = Marshal.AllocHGlobal(size); dispPtr.isAllocated = true; return dispPtr; }
/* static DispoIntPtr() { * // Platform.SetupPlatformImplementation(typeof(DispoIntPtr)); * } */ public static DispoIntPtr Alloc(int size) { var dispPtr = new DispoIntPtr(); dispPtr._ptr = Marshal.AllocHGlobal(size); dispPtr.isAllocated = true; return(dispPtr); }
private static DispoIntPtr AllocStringNative(string str, out int byteCount) { // use builtin allocation var dispPtr = new DispoIntPtr(); dispPtr._ptr = Marshal.StringToHGlobalAnsi(str); dispPtr.isAllocated = true; byteCount = Encoding.Default.GetByteCount(str); return(dispPtr); }
unsafe internal static DispoIntPtr AllocStringNative(string str, out int byteCount) { // use builtin allocation var dispPtr = new DispoIntPtr(); dispPtr._ptr = Marshal.StringToHGlobalAnsi(str); dispPtr.isAllocated = true; byteCount = Encoding.Default.GetByteCount(str); return(dispPtr); /**/ /* use encoding or Encoding.Default ( system codepage of ANSI ) * var enc = Encoding.Default.GetEncoder(); * * // var encoded = new byte[length]; * // Marshal.Copy(encoded, 0, dispPtr._ptr, length); * * IntPtr ptr; * int charCount = str.Length; * * fixed (char* strP = str) * { * byteCount = enc.GetByteCount(strP, charCount, false); * * ptr = Marshal.AllocHGlobal(byteCount + 1); * * enc.GetBytes(strP, charCount, (byte*)ptr, byteCount, true); * *((byte*)ptr + byteCount) = 0x00; * } * * var dispPtr = new DispoIntPtr (); * dispPtr._ptr = ptr; * dispPtr.isAllocated = true; * * // and a C char 0x00 terminator * // Marshal.WriteByte(dispPtr._ptr + length, byte.MinValue); * // *((byte*)dispPtr._ptr + length) = 0x00; * * return dispPtr; /**/ }
unsafe internal static DispoIntPtr AllocStringNative(string str, out int byteCount) { // use builtin allocation var dispPtr = new DispoIntPtr(); dispPtr._ptr = Marshal.StringToHGlobalAnsi(str); dispPtr.isAllocated = true; byteCount = Encoding.Default.GetByteCount(str); return dispPtr; /**/ /* use encoding or Encoding.Default ( system codepage of ANSI ) var enc = Encoding.Default.GetEncoder(); // var encoded = new byte[length]; // Marshal.Copy(encoded, 0, dispPtr._ptr, length); IntPtr ptr; int charCount = str.Length; fixed (char* strP = str) { byteCount = enc.GetByteCount(strP, charCount, false); ptr = Marshal.AllocHGlobal(byteCount + 1); enc.GetBytes(strP, charCount, (byte*)ptr, byteCount, true); *((byte*)ptr + byteCount) = 0x00; } var dispPtr = new DispoIntPtr (); dispPtr._ptr = ptr; dispPtr.isAllocated = true; // and a C char 0x00 terminator // Marshal.WriteByte(dispPtr._ptr + length, byte.MinValue); // *((byte*)dispPtr._ptr + length) = 0x00; return dispPtr; /**/ }
unsafe internal void WriteStringNative(string str, Encoding encoding, bool create) { if (str == null) { throw new ArgumentNullException("str"); } if (str == string.Empty) { if (create) { this._framePtr = CreateNative(0); this._capacity = 0; this._position = 0; } return; } int charCount = str.Length; Encoder enc = encoding.GetEncoder(); fixed (char* strP = str) { int byteCount = enc.GetByteCount(strP, charCount, false); if (create) { this._framePtr = CreateNative(byteCount); this._capacity = byteCount; this._position = 0; } else if (this._position + byteCount > this.Length) { // fail if frame is too small throw new InvalidOperationException(); } byteCount = enc.GetBytes(strP, charCount, (byte*)(this.DataPtr() + this._position), byteCount, true); this._position += byteCount; } }
public void Dismiss() { if (_framePtr != null) { _framePtr.Dispose(); _framePtr = null; } }
/* protected ZFrame(IntPtr data, int size) : this(Alloc(data, size), size) { } */ protected ZFrame(DispoIntPtr framePtr, int size) : base() { _framePtr = framePtr; _capacity = size; _position = 0; }
internal void WriteStringNative(string str, Encoding encoding, bool create) { if (str == null) { throw new ArgumentNullException("str"); } if (str == string.Empty) { if (create) { this._framePtr = CreateNative(0); this._capacity = 0; this._position = 0; } return; } byte[] bytes = encoding.GetBytes(str); if (create) { this._framePtr = CreateNative(bytes.Length); this._capacity = bytes.Length; this._position = 0; } Write(bytes, 0, bytes.Length); }