root/trunk/AprSharp/dev/src/AprAllocator.cs

Revision 59, 5.2 kB (checked in by DenisG, 4 years ago)

[AprSharp] Cosmetic. License region.

  • Property svn:eol-style set to native
Line 
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         ///<summary>Embeds an opaque apr_allocator</summary>
35     public struct AprAllocator : IAprUnmanaged
36     {
37         private IntPtr mAllocator;
38
39         #region Generic embedding functions of an IntPtr
40         public AprAllocator(IntPtr ptr)
41         {
42             mAllocator = ptr;
43         }
44        
45         public bool IsNull
46         {
47                 get
48                 {
49                 return( mAllocator == IntPtr.Zero );
50             }
51         }
52
53         private void CheckPtr()
54         {
55             if( IsNull )
56                 throw new AprNullReferenceException();
57         }
58
59         public void ClearPtr()
60         {
61             mAllocator = IntPtr.Zero;
62         }
63
64         public IntPtr ToIntPtr()
65         {
66             return mAllocator;
67         }
68
69                 public bool ReferenceEquals(IAprUnmanaged obj)
70                 {
71                         return(obj.ToIntPtr() == ToIntPtr());
72                 }
73                
74         public static implicit operator IntPtr(AprAllocator allocator)
75         {
76             return allocator.mAllocator;
77         }
78        
79         public static implicit operator AprAllocator(IntPtr ptr)
80         {
81             return new AprAllocator(ptr);
82         }
83        
84         public override string ToString()
85         {
86             return("[apr_allocator_t:"+mAllocator.ToInt32().ToString("X")+"]");
87         }
88         #endregion
89
90         #region Wrapper methods
91         public static AprAllocator Create()
92         {
93             IntPtr ptr;
94
95             Debug.Write("apr_allocator_create...");
96             int res = Apr.apr_allocator_create(out ptr);
97            
98             if(res != 0 )
99                 throw new AprException(res);
100             Debug.WriteLine(String.Format("Done({0:X})",(Int32)ptr));
101
102             return((AprAllocator) ptr);
103         }
104        
105         public void Destroy()
106         {
107             CheckPtr();
108             Debug.Write(String.Format("apr_allocator_destroy({0:X})...",(Int32)mAllocator));
109             Apr.apr_allocator_destroy(mAllocator);
110             Debug.WriteLine("Done");
111             ClearPtr();
112         }
113        
114         public AprMemNode Alloc(int size)
115         {
116             return(Alloc(unchecked((uint)size)));
117         }
118
119         [CLSCompliant(false)]
120         public AprMemNode Alloc(uint size)
121         {
122             CheckPtr();
123             Debug.WriteLine(String.Format("apr_allocator_alloc({0:X},{1})",mAllocator.ToInt32(),size));
124             return(new AprMemNode(Apr.apr_allocator_alloc(mAllocator,size)));
125         }
126
127         public void Free(AprMemNode memnode)
128         {
129             CheckPtr();
130             Debug.Write(String.Format("apr_allocator_free({0:X},{1:X})...",mAllocator.ToInt32(),(Int32)((IntPtr)memnode)));
131             Apr.apr_allocator_free(mAllocator,memnode);
132             Debug.WriteLine("Done");
133         }
134         #endregion
135
136         #region Wrapper Properties
137         public AprPool Owner
138         {
139             get {
140                 Debug.WriteLine(String.Format("apr_allocator_owner_get({0:X})",mAllocator.ToInt32()));
141                 return(Apr.apr_allocator_owner_get(mAllocator));
142             }
143            
144             set {
145                 Debug.Write(String.Format("apr_allocator_owner_set({0:X},{1})...",mAllocator.ToInt32(),value));
146                 Apr.apr_allocator_owner_set(mAllocator,value);
147                 Debug.WriteLine("Done");
148             }
149         }       
150
151         public AprThreadMutex Mutex
152         {
153             get {
154                 Debug.WriteLine(String.Format("apr_allocator_mutex_get({0:X})",mAllocator.ToInt32()));
155                 return(Apr.apr_allocator_mutex_get(mAllocator));
156             }
157            
158             set {
159                 Debug.Write(String.Format("apr_allocator_mutex_set({0:X},{1})...",mAllocator.ToInt32(),value));
160                 Apr.apr_allocator_mutex_set(mAllocator,value);
161                 Debug.WriteLine("Done");
162             }
163         } 
164        
165         public int MaxFree
166         {
167             set
168             {
169                 NativeMaxFree = unchecked((uint)value);
170             }
171         }
172
173             [CLSCompliant(false)]
174         public uint NativeMaxFree
175         {
176             set
177             {
178                 Apr.apr_allocator_max_free_set(mAllocator, value);
179             }
180         }
181         #endregion       
182     }
183 }
Note: See TracBrowser for help on using the browser.