| 1 |
// AprSharp, a wrapper library around the Apache Portable Runtime Library |
|---|
| 2 |
#region Copyright (C) 2004 SOFTEC sa. |
|---|
| 3 |
// |
|---|
| 4 |
// This library is free software; you can redistribute it and/or |
|---|
| 5 |
// modify it under the terms of the GNU Lesser General Public |
|---|
| 6 |
// License as published by the Free Software Foundation; either |
|---|
| 7 |
// version 2.1 of the License, or (at your option) any later version. |
|---|
| 8 |
// |
|---|
| 9 |
// This library is distributed in the hope that it will be useful, |
|---|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 |
// Lesser General Public License for more details. |
|---|
| 13 |
// |
|---|
| 14 |
// You should have received a copy of the GNU Lesser General Public |
|---|
| 15 |
// License along with this library; if not, write to the Free Software |
|---|
| 16 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 |
// |
|---|
| 18 |
// Sources, support options and lastest version of the complete library |
|---|
| 19 |
// is available from: |
|---|
| 20 |
// http://www.softec.st/AprSharp |
|---|
| 21 |
// |
|---|
| 22 |
// |
|---|
| 23 |
// Initial authors : |
|---|
| 24 |
// Denis Gervalle |
|---|
| 25 |
// Olivier Desaive |
|---|
| 26 |
#endregion |
|---|
| 27 |
// |
|---|
| 28 |
using System; |
|---|
| 29 |
using System.Diagnostics; |
|---|
| 30 |
|
|---|
| 31 |
namespace Softec.AprSharp |
|---|
| 32 |
{ |
|---|
| 33 |
|
|---|
| 34 |
public struct AprPool : IAprUnmanaged |
|---|
| 35 |
{ |
|---|
| 36 |
IntPtr mPool; |
|---|
| 37 |
|
|---|
| 38 |
#region Generic embedding functions of an IntPtr |
|---|
| 39 |
public AprPool(IntPtr ptr) |
|---|
| 40 |
{ |
|---|
| 41 |
mPool = ptr; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
public bool IsNull |
|---|
| 45 |
{ |
|---|
| 46 |
get |
|---|
| 47 |
{ |
|---|
| 48 |
return( mPool == IntPtr.Zero ); |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
private void CheckPtr() |
|---|
| 53 |
{ |
|---|
| 54 |
if( IsNull ) |
|---|
| 55 |
throw new AprNullReferenceException(); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
public void ClearPtr() |
|---|
| 59 |
{ |
|---|
| 60 |
mPool = IntPtr.Zero; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
public IntPtr ToIntPtr() |
|---|
| 64 |
{ |
|---|
| 65 |
return mPool; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
public bool ReferenceEquals(IAprUnmanaged obj) |
|---|
| 69 |
{ |
|---|
| 70 |
return(obj.ToIntPtr() == ToIntPtr()); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
public static implicit operator IntPtr(AprPool pool) |
|---|
| 74 |
{ |
|---|
| 75 |
return pool.mPool; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
public static implicit operator AprPool(IntPtr ptr) |
|---|
| 79 |
{ |
|---|
| 80 |
return new AprPool(ptr); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
public override string ToString() |
|---|
| 84 |
{ |
|---|
| 85 |
return("[apr_pool_t:"+mPool.ToInt32().ToString("X")+"]"); |
|---|
| 86 |
} |
|---|
| 87 |
#endregion |
|---|
| 88 |
|
|---|
| 89 |
#region Wrapper methods |
|---|
| 90 |
public static AprPool Create() |
|---|
| 91 |
{ |
|---|
| 92 |
return(Create(IntPtr.Zero, IntPtr.Zero)); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
public static AprPool Create(AprPool pool) |
|---|
| 96 |
{ |
|---|
| 97 |
return(Create(pool, IntPtr.Zero)); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
public static AprPool Create(AprAllocator allocator) |
|---|
| 101 |
{ |
|---|
| 102 |
return(Create(IntPtr.Zero, allocator)); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
public static AprPool Create(AprPool pool, AprAllocator allocator) |
|---|
| 106 |
{ |
|---|
| 107 |
IntPtr ptr; |
|---|
| 108 |
|
|---|
| 109 |
Debug.Write(String.Format("apr_pool_create_ex({0},{1})...",pool,allocator)); |
|---|
| 110 |
int res = Apr.apr_pool_create_ex(out ptr, pool, |
|---|
| 111 |
IntPtr.Zero, allocator); |
|---|
| 112 |
if(res != 0 ) |
|---|
| 113 |
throw new AprException(res); |
|---|
| 114 |
Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); |
|---|
| 115 |
|
|---|
| 116 |
return(ptr); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
public void Destroy() |
|---|
| 120 |
{ |
|---|
| 121 |
CheckPtr(); |
|---|
| 122 |
Debug.Write(String.Format("apr_pool_destroy({0:X})...",this)); |
|---|
| 123 |
Apr.apr_pool_destroy(mPool); |
|---|
| 124 |
Debug.WriteLine("Done"); |
|---|
| 125 |
ClearPtr(); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
public void Clear() |
|---|
| 129 |
{ |
|---|
| 130 |
CheckPtr(); |
|---|
| 131 |
Debug.Write(String.Format("apr_pool_clear({0:X})...",this)); |
|---|
| 132 |
Apr.apr_pool_clear(mPool); |
|---|
| 133 |
Debug.WriteLine("Done"); |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
public unsafe IntPtr Alloc(int size) |
|---|
| 137 |
{ |
|---|
| 138 |
return((IntPtr)Alloc(unchecked((uint)size))); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
[CLSCompliant(false)] |
|---|
| 142 |
public unsafe byte *Alloc(uint size) |
|---|
| 143 |
{ |
|---|
| 144 |
CheckPtr(); |
|---|
| 145 |
Debug.WriteLine(String.Format("apr_palloc({0:X})",this)); |
|---|
| 146 |
return((byte *)Apr.apr_palloc(mPool, size)); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
public unsafe IntPtr CAlloc(int size) |
|---|
| 150 |
{ |
|---|
| 151 |
return((IntPtr)CAlloc(unchecked((uint)size))); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
[CLSCompliant(false)] |
|---|
| 155 |
public unsafe byte *CAlloc(uint size) |
|---|
| 156 |
{ |
|---|
| 157 |
CheckPtr(); |
|---|
| 158 |
Debug.WriteLine(String.Format("apr_pcalloc({0:X})",this)); |
|---|
| 159 |
return((byte *)Apr.apr_pcalloc(mPool, size)); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
public bool IsAncestor(AprPool pool) |
|---|
| 163 |
{ |
|---|
| 164 |
CheckPtr(); |
|---|
| 165 |
Debug.WriteLine(String.Format("apr_pool_is_ancestor({0:X},{1:X})",this,pool)); |
|---|
| 166 |
return(!(Apr.apr_pool_is_ancestor(mPool,pool) == 0)); |
|---|
| 167 |
} |
|---|
| 168 |
#endregion |
|---|
| 169 |
|
|---|
| 170 |
#region Wrapper properties |
|---|
| 171 |
public AprAllocator Allocator |
|---|
| 172 |
{ |
|---|
| 173 |
get { |
|---|
| 174 |
Debug.WriteLine(String.Format("apr_pool_allocator_get({0:X})",this)); |
|---|
| 175 |
return(Apr.apr_pool_allocator_get(mPool)); |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
public AprPool Parent |
|---|
| 180 |
{ |
|---|
| 181 |
get { |
|---|
| 182 |
Debug.WriteLine(String.Format("apr_pool_parent_get({0:X})",this)); |
|---|
| 183 |
return(Apr.apr_pool_parent_get(mPool)); |
|---|
| 184 |
} |
|---|
| 185 |
} |
|---|
| 186 |
#endregion |
|---|
| 187 |
} |
|---|
| 188 |
} |
|---|