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 void allocate (void[]* x, BufferConverter decoder) 00075 { 00076 uint len = reader.length; 00077 *x = new void [len]; 00078 printf ("allocated %d bytes\n", len); 00079 decoder (*x, len); 00080 } 00081 } 00082 00083 00084 /******************************************************************************* 00085 00086 *******************************************************************************/ 00087 00088 class BufferAllocator : IArrayAllocator 00089 { 00090 private BufferConverter raw; 00091 private uint width; 00092 private IReader reader; 00093 00094 /*********************************************************************** 00095 00096 ***********************************************************************/ 00097 00098 this (IReader reader, int width = 0) 00099 { 00100 this.width = width; 00101 this.reader = reader; 00102 raw = &(cast(Reader) reader).read; 00103 } 00104 00105 /*********************************************************************** 00106 00107 ***********************************************************************/ 00108 00109 void reset () 00110 { 00111 IBuffer buffer = reader.getBuffer; 00112 00113 // ensure there's enough room for another record 00114 if (buffer.writable < width) 00115 buffer.compress (); 00116 } 00117 00118 /*********************************************************************** 00119 00120 ***********************************************************************/ 00121 00122 void allocate (void[]* x, BufferConverter decoder) 00123 { 00124 uint len = reader.length; 00125 00126 if (decoder == raw) 00127 { 00128 *x = reader.getBuffer.get (len); 00129 printf ("sliced %d bytes from buffer\n", len); 00130 } 00131 else 00132 { 00133 *x = new void [len]; 00134 printf ("allocated %d bytes\n", len); 00135 decoder (*x, len); 00136 } 00137 } 00138 } 00139 00140 00141 /******************************************************************************* 00142 00143 *******************************************************************************/ 00144 00145 class SliceAllocator : HeapSlice, IArrayAllocator 00146 { 00147 private IReader reader; 00148 00149 /*********************************************************************** 00150 00151 ***********************************************************************/ 00152 00153 this (IReader reader, int width) 00154 { 00155 super (width); 00156 this.reader = reader; 00157 } 00158 00159 /*********************************************************************** 00160 00161 ***********************************************************************/ 00162 00163 void reset () 00164 { 00165 super.reset (); 00166 } 00167 00168 /*********************************************************************** 00169 00170 ***********************************************************************/ 00171 00172 void allocate (void[]* x, BufferConverter decoder) 00173 { 00174 uint len = reader.length; 00175 00176 expand (len); 00177 *x = slice (len); 00178 printf ("sliced %d bytes from heap\n", len); 00179 decoder (*x, len); 00180 } 00181 } 00182 00183