Merge pull request #4248 from Unity-Technologies/boehm-gc-alloc-fixed
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsError.cs
1 //
2 // MonoBtlsError.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
27 using System;
28 using System.IO;
29 using System.Text;
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
32
33 #if MONOTOUCH
34 using MonoTouch;
35 #endif
36
37 namespace Mono.Btls
38 {
39         static class MonoBtlsError
40         {
41                 [DllImport (MonoBtlsObject.BTLS_DYLIB)]
42                 extern static int mono_btls_error_peek_error ();
43
44                 [DllImport (MonoBtlsObject.BTLS_DYLIB)]
45                 extern static int mono_btls_error_get_error ();
46
47                 [DllImport (MonoBtlsObject.BTLS_DYLIB)]
48                 extern static void mono_btls_error_clear_error ();
49
50                 [DllImport (MonoBtlsObject.BTLS_DYLIB)]
51                 extern static int mono_btls_error_peek_error_line (out IntPtr file, out int line);
52
53                 [DllImport (MonoBtlsObject.BTLS_DYLIB)]
54                 extern static int mono_btls_error_get_error_line (out IntPtr file, out int line);
55
56                 [DllImport (MonoBtlsObject.BTLS_DYLIB)]
57                 extern static void mono_btls_error_get_error_string_n (int error, IntPtr buf, int len);
58
59                 public static int PeekError ()
60                 {
61                         return mono_btls_error_peek_error ();
62                 }
63
64                 public static int GetError ()
65                 {
66                         return mono_btls_error_get_error ();
67                 }
68
69                 public static void ClearError ()
70                 {
71                         mono_btls_error_clear_error ();
72                 }
73
74                 public static string GetErrorString (int error)
75                 {
76                         var size = 1024;
77                         var buffer = Marshal.AllocHGlobal (size);
78                         if (buffer == IntPtr.Zero)
79                                 throw new OutOfMemoryException ();
80                         try {
81                                 mono_btls_error_get_error_string_n (error, buffer, size);
82                                 return Marshal.PtrToStringAnsi (buffer);
83                         } finally {
84                                 Marshal.FreeHGlobal (buffer);
85                         }
86                 }
87
88                 public static int PeekError (out string file, out int line)
89                 {
90                         IntPtr filePtr;
91                         var error = mono_btls_error_peek_error_line (out filePtr, out line);
92                         if (filePtr != IntPtr.Zero)
93                                 file = Marshal.PtrToStringAnsi (filePtr);
94                         else
95                                 file = null;
96                         return error;
97                 }
98
99                 public static int GetError (out string file, out int line)
100                 {
101                         IntPtr filePtr;
102                         var error = mono_btls_error_get_error_line (out filePtr, out line);
103                         if (filePtr != IntPtr.Zero)
104                                 file = Marshal.PtrToStringAnsi (filePtr);
105                         else
106                                 file = null;
107                         return error;
108                 }
109         }
110 }
111 #endif