private void Alloc (int length, bool realloc)
{
if ((length < 0) || (length > MaxSize))
throw new ArgumentOutOfRangeException ("length", "< 0 || > 65536");
// (size / blocksize) + 1 * blocksize
// where size = length * 2 (unicode) and blocksize == 16 (ProtectedMemory)
// length * 2 (unicode) / 16 (blocksize)
int size = (length >> 3) + (((length & 0x7) == 0) ? 0 : 1) << 4;
// is re-allocation necessary ? (i.e. grow or shrink
// but do not re-allocate the same amount of memory)
if (realloc && (data != null) && (size == data.Length))
return;
if (realloc) {
// copy, then clear
byte[] newdata = new byte[size];
Array.Copy (data, 0, newdata, 0, Math.Min (data.Length, newdata.Length));
Array.Clear (data, 0, data.Length);
data = newdata;
} else {
data = new byte[size];
}
}