~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/CoreFoundation/CFAllocator.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// CFAllocator.cs
 
3
//
 
4
// Authors:
 
5
//    Rolf Bjarne Kvinge
 
6
//    Marek Safar (marek.safar@gmail.com)
 
7
//     
 
8
// Copyright 2012 Xamarin Inc
 
9
//
 
10
// Permission is hereby granted, free of charge, to any person obtaining
 
11
// a copy of this software and associated documentation files (the
 
12
// "Software"), to deal in the Software without restriction, including
 
13
// without limitation the rights to use, copy, modify, merge, publish,
 
14
// distribute, sublicense, and/or sell copies of the Software, and to
 
15
// permit persons to whom the Software is furnished to do so, subject to
 
16
// the following conditions:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
// 
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
28
//
 
29
 
 
30
using System;
 
31
using System.Runtime.InteropServices;
 
32
using MonoMac.Foundation;
 
33
using MonoMac.ObjCRuntime;
 
34
 
 
35
namespace MonoMac.CoreFoundation {      
 
36
        public class CFAllocator : INativeObject, IDisposable 
 
37
        {
 
38
                static CFAllocator Default_cf;
 
39
                static CFAllocator SystemDefault_cf;
 
40
                static CFAllocator Malloc_cf;
 
41
                static CFAllocator MallocZone_cf;
 
42
                static CFAllocator Null_cf;
 
43
 
 
44
                static readonly IntPtr default_ptr;
 
45
                static readonly IntPtr system_default_ptr;
 
46
                static readonly IntPtr malloc_ptr;
 
47
                static readonly IntPtr malloc_zone_ptr;
 
48
                internal static readonly IntPtr null_ptr;
 
49
                //static readonly IntPtr UseContextFlag;
 
50
 
 
51
                IntPtr handle;
 
52
                
 
53
                static CFAllocator ()
 
54
                {
 
55
                        var handle = Dlfcn.dlopen (Constants.CoreFoundationLibrary, 0);
 
56
                        try {
 
57
                                default_ptr = Dlfcn.GetIntPtr (handle, "kCFAllocatorDefault");
 
58
                                system_default_ptr = Dlfcn.GetIntPtr (handle, "kCFAllocatorSystemDefault");
 
59
                                malloc_ptr = Dlfcn.GetIntPtr (handle, "kCFAllocatorMalloc");
 
60
                                malloc_zone_ptr = Dlfcn.GetIntPtr (handle, "kCFAllocatorMallocZone");
 
61
                                null_ptr = Dlfcn.GetIntPtr (handle, "kCFAllocatorNull");
 
62
                        //      UseContextFlag = Dlfcn.GetIntPtr (handle, "kCFAllocatorUseContext");
 
63
                        } finally {
 
64
                                Dlfcn.dlclose (handle);
 
65
                        }
 
66
                }
 
67
 
 
68
                public CFAllocator (IntPtr handle)
 
69
                {
 
70
                        this.handle = handle;
 
71
                }
 
72
 
 
73
                public CFAllocator (IntPtr handle, bool owns)
 
74
                {
 
75
                        if (!owns)
 
76
                                CFObject.CFRetain (handle);
 
77
                        this.handle = handle;
 
78
                }
 
79
 
 
80
                ~CFAllocator ()
 
81
                {
 
82
                        Dispose (false);
 
83
                }
 
84
                
 
85
                public void Dispose ()
 
86
                {
 
87
                        Dispose (true);
 
88
                        GC.SuppressFinalize (this);
 
89
                }
 
90
 
 
91
                public IntPtr Handle {
 
92
                        get { return handle; }
 
93
                }
 
94
                
 
95
                protected virtual void Dispose (bool disposing)
 
96
                {
 
97
                        if (handle != IntPtr.Zero){
 
98
                                CFObject.CFRelease (handle);
 
99
                                handle = IntPtr.Zero;
 
100
                        }
 
101
                }
 
102
 
 
103
                public static CFAllocator Default {
 
104
                        get {
 
105
                                return Default_cf ?? (Default_cf = new CFAllocator (default_ptr)); 
 
106
                        }
 
107
                }
 
108
 
 
109
                public static CFAllocator SystemDefault {
 
110
                        get {
 
111
                                return SystemDefault_cf ?? (SystemDefault_cf = new CFAllocator (system_default_ptr)); 
 
112
                        }
 
113
                }
 
114
                
 
115
                public static CFAllocator Malloc {
 
116
                        get {
 
117
                                return Malloc_cf ?? (Malloc_cf = new CFAllocator (malloc_ptr)); 
 
118
                        }
 
119
                }
 
120
 
 
121
                public static CFAllocator MallocZone {
 
122
                        get {
 
123
                                return MallocZone_cf ?? (MallocZone_cf = new CFAllocator (malloc_zone_ptr)); 
 
124
                        }
 
125
                }
 
126
 
 
127
                public static CFAllocator Null {
 
128
                        get {
 
129
                                return Null_cf ?? (Null_cf = new CFAllocator (null_ptr)); 
 
130
                        }
 
131
                }
 
132
 
 
133
                [DllImport (Constants.CoreFoundationLibrary)]
 
134
                static extern IntPtr CFAllocatorAllocate (IntPtr allocator, /*CFIndex*/ long size, CFAllocatorFlags hint);
 
135
 
 
136
                public IntPtr Allocate (long size, CFAllocatorFlags hint = 0)
 
137
                {
 
138
                        return CFAllocatorAllocate (handle, size, hint);
 
139
                }
 
140
 
 
141
                [DllImport (Constants.CoreFoundationLibrary)]
 
142
                static extern void CFAllocatorDeallocate(IntPtr allocator, IntPtr ptr);
 
143
 
 
144
                public void Deallocate (IntPtr ptr)
 
145
                {
 
146
                        CFAllocatorDeallocate (handle, ptr);
 
147
                }
 
148
 
 
149
                // TODO: Implement more methods
 
150
        }
 
151
 
 
152
        // Seems to be some sort of secret values
 
153
        [Flags]
 
154
        public enum CFAllocatorFlags : ulong
 
155
        {
 
156
                GCScannedMemory = 0x200,
 
157
                GCObjectMemory  = 0x400,
 
158
        }
 
159
}
 
 
b'\\ No newline at end of file'