///
/// This file is part of ILNumerics Community Edition.
///
/// ILNumerics Community Edition - high performance computing for applications.
/// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
///
/// ILNumerics Community Edition is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License version 3 as published by
/// the Free Software Foundation.
///
/// ILNumerics Community Edition is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with ILNumerics Community Edition. See the file License.txt in the root
/// of your distribution package. If not, see .
///
/// In addition this software uses the following components and/or licenses:
///
/// =================================================================================
/// The Open Toolkit Library License
///
/// Copyright (c) 2006 - 2009 the Open Toolkit library.
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights to
/// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
/// the Software, and to permit persons to whom the Software is furnished to do
/// so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// =================================================================================
///
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using ILNumerics.Exceptions;
using ILNumerics.Misc;
namespace ILNumerics {
///
/// The ILNumerics Memory Pool is the heart of the ILNumerics memory management.
///
/// The pool reduces the pressure on the systems memory done by larger objects.
/// Arrays created in ILNumerics will try to reclaim its memory from this pool. If attempt fails, the memory is gathered from the managed heap normally.
/// Disposed array objects register their underlying storage in the pool for later reusing. The process is triggered by a deterministic disposal
/// pattern in conjunction with ILNumerics array types and ILNumerics Function Rules.
///
public sealed class ILMemoryPool {
///
/// The only global ILMemoryPool instance
///
public static readonly ILMemoryPool Pool = new ILMemoryPool();
internal static readonly Dictionary Pools = new Dictionary();
/////
///// todo: remove!!
/////
/////
/////
//public void SetIncrementLenghtPercent(double value) {
// ILMemoryPoolInternal.Pool.MaxRequestedLengthIncrease = value;
//}
//public double GetIncrementLenghtPercent()
//{
// return ILMemoryPoolInternal.Pool.MaxRequestedLengthIncrease;
//}
internal long MinArrayLength()
{
return ILMemoryPoolInternal.Pool.MinArrayLength;
}
//public void SetMinArrayLength(long value) {
// ILMemoryPoolInternal.Pool.MinArrayLength = value;
//}
///
/// Get new array of type T from memory pool.
///
/// Element type
/// Size of T[]
/// If true, set the elements in T[] to default(T)
/// Always true if was set. Otherwise this will be true if the array was newly created instead of being recycled from the pool. False otherwise.
/// If the pool contains an unused matching element of sufficient size this element will be returned. If was false,
/// the result might still contain the old data.
/// If the pool does not contain a matching element a new one is created.
/// Note: The returned array may be larger than requested if it was recycled from the pool.
/// An array of type T of at least length
public T[] New(long length, bool clear, out bool cleared) {
return ILMemoryPoolInternal.Pool.New(length, clear, out cleared);
}
///
/// Get a new array of type T from the memory pool
///
/// Element type
/// Required minimum length
/// The returned array is only guaranteed to have at least length . If it is recycled from the pool it may be longer.
/// There is no guarantee on the values contained in the returned array. To force these to have a default value use
/// An array of type T of at least length .
public T[] New(long length)
{
return ILMemoryPoolInternal.Pool.New(length);
}
///
/// Return a array of type T that is not needed anymore to the pool.
///
/// Element type
/// The array to add to the pool
/// You may also "free" objects not retrieved from the pool, in which case they are registered in the pool.
public void Free(T[] array) {
ILMemoryPoolInternal.Pool.Free(array);
}
///
/// Get maximal potential size of the memory pool for objects of type T
///
/// Element type
/// Maximal number of bytes in pool
public long MaxBytes() {
return ILMemoryPoolInternal.Pool.MaxBytes;
}
///
/// Reset & reconfigure the pool
///
/// Minimum length for array object to be stored inside the pool
/// Overall size the memory pool consumes at most
/// Reset will dispose all objects currently hold in the pool!
public void Reset(long MinPoolArrayLen, int maxSizeMB) {
ILMemoryPoolInternal.Pool.Reset(MinPoolArrayLen, maxSizeMB);
}
///
/// Give information about pool state
///
/// true (default): abbreviate infos to: current MB in Pool, reclaimed MB for livetime, reclaimed objects for livetime. False: give full info
/// Infos about current pool state
public string Info(bool shortVersion = true) {
if (Pools.ContainsKey(typeof(double))) {
return Pools[typeof(double)].Info(shortVersion);
} else if (Pools.Count > 0) {
foreach (IILMemoryPool pool in Pools.Values) {
return pool.Info(shortVersion);
}
}
return "no pool found";
}
}
}