You've already forked linux-packaging-mono
Imported Upstream version 4.8.0.309
Former-commit-id: 5f9c6ae75f295e057a7d2971f3a6df4656fa8850
This commit is contained in:
parent
ee1447783b
commit
94b2861243
46
mcs/class/corlib/Mono/SafeStringMarshal.cs
Normal file
46
mcs/class/corlib/Mono/SafeStringMarshal.cs
Normal file
@ -0,0 +1,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user