1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
//
// Safe wrapper for a string and its UTF8 encoding
//
// Authors:
// Aleksey Kliger <aleksey@xamarin.com>
// Rodrigo Kumpera <kumpera@xamarin.com>
//
// Copyright 2016 Dot net foundation.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Runtime.CompilerServices;
namespace Mono {
internal struct SafeStringMarshal : IDisposable {
readonly string str;
IntPtr marshaled_string;
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern static IntPtr StringToUtf8 (string str);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern static void GFree (IntPtr ptr);
public SafeStringMarshal (string str) {
this.str = str;
this.marshaled_string = IntPtr.Zero;
}
public IntPtr Value {
get {
if (marshaled_string == IntPtr.Zero && str != null)
marshaled_string = StringToUtf8 (str);
return marshaled_string;
}
}
public void Dispose () {
if (marshaled_string != IntPtr.Zero) {
GFree (marshaled_string);
marshaled_string = IntPtr.Zero;
}
}
}
}
|