00001 /******************************************************************************* 00002 00003 @file ArrayAllocator.d 00004 00005 Copyright (C) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, October 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.io.ArrayAllocator; 00037 00038 private import mango.io.Reader; 00039 00040 private import mango.utils.HeapSlice; 00041 00042 private import mango.io.model.IBuffer, 00043 mango.io.model.IReader; 00044 00045 /******************************************************************************* 00046 00047 *******************************************************************************/ 00048 00049 class SimpleAllocator : IArrayAllocator 00050 { 00051 private IReader reader; 00052 00053 /*********************************************************************** 00054 00055 ***********************************************************************/ 00056 00057 this (IReader reader) 00058 { 00059 this.reader = reader; 00060 } 00061 00062 /*********************************************************************** 00063 00064 ***********************************************************************/ 00065 00066 void reset () 00067 { 00068 } 00069 00070 /*********************************************************************** 00071 00072 ***********************************************************************/ 00073 00074 bool isReadOnly (void* x) 00075 { 00076 return false; 00077 } 00078 00079 /*********************************************************************** 00080 00081 ***********************************************************************/ 00082 00083 void allocate (void[]* x, BufferConverter decoder) 00084 { 00085 uint len = reader.length; 00086 *x = new void [len]; 00087 //printf ("allocated %d bytes\n", len); 00088 decoder (*x, len); 00089 } 00090 } 00091 00092 00093 /******************************************************************************* 00094 00095 *******************************************************************************/ 00096 00097 class BufferAllocator : IArrayAllocator 00098 { 00099 private BufferConverter raw; 00100 private uint width; 00101 private IReader reader; 00102 00103 /*********************************************************************** 00104 00105 ***********************************************************************/ 00106 00107 this (IReader reader, int width = 0) 00108 { 00109 this.width = width; 00110 this.reader = reader; 00111 raw = &(cast(Reader) reader).read; 00112 } 00113 00114 /*********************************************************************** 00115 00116 ***********************************************************************/ 00117 00118 void reset () 00119 { 00120 IBuffer buffer = reader.getBuffer; 00121 00122 // ensure there's enough room for another record 00123 if (buffer.writable < width) 00124 buffer.compress (); 00125 } 00126 00127 /*********************************************************************** 00128 00129 ***********************************************************************/ 00130 00131 bool isReadOnly (void* x) 00132 { 00133 void[] buffer = reader.getBuffer.getContent; 00134 return (x >= buffer && x <= cast(void*)buffer+buffer.length); 00135 } 00136 00137 /*********************************************************************** 00138 00139 ***********************************************************************/ 00140 00141 void allocate (void[]* x, BufferConverter decoder) 00142 { 00143 uint len = reader.length; 00144 00145 if (decoder == raw) 00146 { 00147 *x = reader.getBuffer.get (len); 00148 //printf ("sliced %d bytes from buffer\n", len); 00149 } 00150 else 00151 { 00152 *x = new void [len]; 00153 //printf ("allocated %d bytes\n", len); 00154 decoder (*x, len); 00155 } 00156 } 00157 } 00158 00159 00160 /******************************************************************************* 00161 00162 *******************************************************************************/ 00163 00164 class SliceAllocator : HeapSlice, IArrayAllocator 00165 { 00166 private IReader reader; 00167 00168 /*********************************************************************** 00169 00170 ***********************************************************************/ 00171 00172 this (IReader reader, int width) 00173 { 00174 super (width); 00175 this.reader = reader; 00176 } 00177 00178 /*********************************************************************** 00179 00180 ***********************************************************************/ 00181 00182 void reset () 00183 { 00184 super.reset (); 00185 } 00186 00187 /*********************************************************************** 00188 00189 ***********************************************************************/ 00190 00191 bool isReadOnly (void* x) 00192 { 00193 return true; 00194 } 00195 00196 /*********************************************************************** 00197 00198 ***********************************************************************/ 00199 00200 void allocate (void[]* x, BufferConverter decoder) 00201 { 00202 uint len = reader.length; 00203 00204 expand (len); 00205 *x = slice (len); 00206 //printf ("sliced %d bytes from heap\n", len); 00207 decoder (*x, len); 00208 } 00209 } 00210 00211