linux-packaging-mono/mcs/class/corlib/Mono/SafeStringMarshal.cs
Xamarin Public Jenkins (auto-signing) 468663ddbb Imported Upstream version 6.10.0.49
Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
2020-01-16 16:38:04 +00:00

52 lines
1.2 KiB
C#

//
// 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)]
extern static IntPtr StringToUtf8_icall (ref string str);
public static IntPtr StringToUtf8 (string str)
{
return StringToUtf8_icall (ref 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;
}
}
}
}