94b2861243
Former-commit-id: 5f9c6ae75f295e057a7d2971f3a6df4656fa8850
47 lines
1.1 KiB
C#
47 lines
1.1 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)]
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|