6
// Marek Safar (marek.safar@gmail.com)
8
// Copyright 2012 Xamarin Inc
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:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
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.
31
using System.Runtime.InteropServices;
32
using MonoMac.Foundation;
33
using MonoMac.ObjCRuntime;
35
namespace MonoMac.CoreFoundation {
36
public class CFAllocator : INativeObject, IDisposable
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;
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;
55
var handle = Dlfcn.dlopen (Constants.CoreFoundationLibrary, 0);
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");
64
Dlfcn.dlclose (handle);
68
public CFAllocator (IntPtr handle)
73
public CFAllocator (IntPtr handle, bool owns)
76
CFObject.CFRetain (handle);
85
public void Dispose ()
88
GC.SuppressFinalize (this);
91
public IntPtr Handle {
92
get { return handle; }
95
protected virtual void Dispose (bool disposing)
97
if (handle != IntPtr.Zero){
98
CFObject.CFRelease (handle);
103
public static CFAllocator Default {
105
return Default_cf ?? (Default_cf = new CFAllocator (default_ptr));
109
public static CFAllocator SystemDefault {
111
return SystemDefault_cf ?? (SystemDefault_cf = new CFAllocator (system_default_ptr));
115
public static CFAllocator Malloc {
117
return Malloc_cf ?? (Malloc_cf = new CFAllocator (malloc_ptr));
121
public static CFAllocator MallocZone {
123
return MallocZone_cf ?? (MallocZone_cf = new CFAllocator (malloc_zone_ptr));
127
public static CFAllocator Null {
129
return Null_cf ?? (Null_cf = new CFAllocator (null_ptr));
133
[DllImport (Constants.CoreFoundationLibrary)]
134
static extern IntPtr CFAllocatorAllocate (IntPtr allocator, /*CFIndex*/ long size, CFAllocatorFlags hint);
136
public IntPtr Allocate (long size, CFAllocatorFlags hint = 0)
138
return CFAllocatorAllocate (handle, size, hint);
141
[DllImport (Constants.CoreFoundationLibrary)]
142
static extern void CFAllocatorDeallocate(IntPtr allocator, IntPtr ptr);
144
public void Deallocate (IntPtr ptr)
146
CFAllocatorDeallocate (handle, ptr);
149
// TODO: Implement more methods
152
// Seems to be some sort of secret values
154
public enum CFAllocatorFlags : ulong
156
GCScannedMemory = 0x200,
157
GCObjectMemory = 0x400,
b'\\ No newline at end of file'