| 1 |
// |
|---|
| 2 |
// Softec |
|---|
| 3 |
// |
|---|
| 4 |
// Contact: |
|---|
| 5 |
// |
|---|
| 6 |
// Designed by Denis Gervalle and Olivier Desaive |
|---|
| 7 |
// Written by Denis Gervalle |
|---|
| 8 |
// |
|---|
| 9 |
// Copyright 2004 by SOFTEC. All rights reserved. |
|---|
| 10 |
// |
|---|
| 11 |
using System; |
|---|
| 12 |
using System.Diagnostics; |
|---|
| 13 |
|
|---|
| 14 |
namespace Softec.AprSharp |
|---|
| 15 |
{ |
|---|
| 16 |
|
|---|
| 17 |
///<summary>Embeds an opaque apr_allocator</summary> |
|---|
| 18 |
public struct AprAllocator |
|---|
| 19 |
{ |
|---|
| 20 |
private IntPtr mAllocator; |
|---|
| 21 |
|
|---|
| 22 |
#region Generic embedding functions of an IntPtr |
|---|
| 23 |
private AprAllocator(IntPtr ptr) |
|---|
| 24 |
{ |
|---|
| 25 |
mAllocator = ptr; |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
public bool IsNull() |
|---|
| 29 |
{ |
|---|
| 30 |
return( mAllocator == IntPtr.Zero ); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
private void CheckPtr() |
|---|
| 34 |
{ |
|---|
| 35 |
if( IsNull() ) |
|---|
| 36 |
throw new AprNullReferenceException(); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
public void ClearPtr() |
|---|
| 40 |
{ |
|---|
| 41 |
mAllocator = IntPtr.Zero; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
public static implicit operator IntPtr(AprAllocator allocator) |
|---|
| 45 |
{ |
|---|
| 46 |
return allocator.mAllocator; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
public static implicit operator AprAllocator(IntPtr ptr) |
|---|
| 50 |
{ |
|---|
| 51 |
return new AprAllocator(ptr); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
public override string ToString() |
|---|
| 55 |
{ |
|---|
| 56 |
return("[apr_allocator_t:"+mAllocator.ToInt32().ToString("X")+"]"); |
|---|
| 57 |
} |
|---|
| 58 |
#endregion |
|---|
| 59 |
|
|---|
| 60 |
#region Wrapper methods |
|---|
| 61 |
public static AprAllocator Create() |
|---|
| 62 |
{ |
|---|
| 63 |
IntPtr ptr; |
|---|
| 64 |
|
|---|
| 65 |
Debug.Write("apr_allocator_create..."); |
|---|
| 66 |
int res = Apr.apr_allocator_create(out ptr); |
|---|
| 67 |
|
|---|
| 68 |
if(res != 0 ) |
|---|
| 69 |
throw new AprException(res); |
|---|
| 70 |
Debug.WriteLine(String.Format("Done({0:X})",(Int32)ptr)); |
|---|
| 71 |
|
|---|
| 72 |
return((AprAllocator) ptr); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
public void Destroy() |
|---|
| 76 |
{ |
|---|
| 77 |
CheckPtr(); |
|---|
| 78 |
Debug.Write(String.Format("apr_allocator_destroy({0:X})...",(Int32)mAllocator)); |
|---|
| 79 |
Apr.apr_allocator_destroy(mAllocator); |
|---|
| 80 |
Debug.WriteLine("Done"); |
|---|
| 81 |
ClearPtr(); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
public AprMemNode Alloc(int size) |
|---|
| 85 |
{ |
|---|
| 86 |
return(Alloc((uint)size)); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
[CLSCompliant(false)] |
|---|
| 90 |
public AprMemNode Alloc(uint size) |
|---|
| 91 |
{ |
|---|
| 92 |
CheckPtr(); |
|---|
| 93 |
Debug.WriteLine(String.Format("apr_allocator_alloc({0:X},{1})",mAllocator.ToInt32(),size)); |
|---|
| 94 |
return(new AprMemNode(Apr.apr_allocator_alloc(mAllocator,size))); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
public void Free(AprMemNode memnode) |
|---|
| 98 |
{ |
|---|
| 99 |
CheckPtr(); |
|---|
| 100 |
Debug.Write(String.Format("apr_allocator_free({0:X},{1:X})...",mAllocator.ToInt32(),(Int32)((IntPtr)memnode))); |
|---|
| 101 |
Apr.apr_allocator_free(mAllocator,memnode); |
|---|
| 102 |
Debug.WriteLine("Done"); |
|---|
| 103 |
} |
|---|
| 104 |
#endregion |
|---|
| 105 |
|
|---|
| 106 |
#region Wrapper Properties |
|---|
| 107 |
public AprPool Owner |
|---|
| 108 |
{ |
|---|
| 109 |
get { |
|---|
| 110 |
Debug.WriteLine(String.Format("apr_allocator_owner_get({0:X})",mAllocator.ToInt32())); |
|---|
| 111 |
return(Apr.apr_allocator_owner_get(mAllocator)); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
set { |
|---|
| 115 |
Debug.Write(String.Format("apr_allocator_owner_set({0:X},{1})...",mAllocator.ToInt32(),value)); |
|---|
| 116 |
Apr.apr_allocator_owner_set(mAllocator,value); |
|---|
| 117 |
Debug.WriteLine("Done"); |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
public AprThreadMutex Mutex |
|---|
| 122 |
{ |
|---|
| 123 |
get { |
|---|
| 124 |
Debug.WriteLine(String.Format("apr_allocator_mutex_get({0:X})",mAllocator.ToInt32())); |
|---|
| 125 |
return(Apr.apr_allocator_mutex_get(mAllocator)); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
set { |
|---|
| 129 |
Debug.Write(String.Format("apr_allocator_mutex_set({0:X},{1})...",mAllocator.ToInt32(),value)); |
|---|
| 130 |
Apr.apr_allocator_mutex_set(mAllocator,value); |
|---|
| 131 |
Debug.WriteLine("Done"); |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
public int MaxFree |
|---|
| 136 |
{ |
|---|
| 137 |
set |
|---|
| 138 |
{ |
|---|
| 139 |
NativeMaxFree = unchecked((uint)value); |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
[CLSCompliant(false)] |
|---|
| 144 |
public uint NativeMaxFree |
|---|
| 145 |
{ |
|---|
| 146 |
set |
|---|
| 147 |
{ |
|---|
| 148 |
Apr.apr_allocator_max_free_set(mAllocator, value); |
|---|
| 149 |
} |
|---|
| 150 |
} |
|---|
| 151 |
#endregion |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|