Main Page | User's Guide | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

Allocator.hxx

00001 // Copyright (C) 2001-2004 Vivien Mallet
00002 //
00003 // This file is part of Seldon library.
00004 // Seldon library provides matrices and vectors structures for
00005 // linear algebra.
00006 // 
00007 // Seldon is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // Seldon is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License (file "license") for more details.
00016 //
00017 // For more information, please see the Seldon home page:
00018 //     http://spacetown.free.fr/lib/seldon/
00019 
00020 #ifndef SELDON_FILE_ALLOCATOR_HXX
00021 
00022 namespace Seldon
00023 {
00024 
00025 
00027   // MALLOCALLOC //
00029 
00030 
00031   template <class T>
00032   class MallocAlloc
00033   {
00034   public:
00035     typedef T value_type;
00036     typedef T* pointer;
00037     typedef const T* const_pointer;
00038     typedef T& reference;
00039     typedef const T& const_reference;
00040 
00041   public:
00042 
00043     pointer allocate(int num, void* h = 0)
00044     {
00045       return static_cast<pointer>( malloc(num * sizeof(T)) );
00046     }
00047 
00048     void deallocate(pointer data, int num, void* h = 0)
00049     {
00050       free(data);
00051     }
00052 
00053     void* reallocate(pointer data, int num, void* h = 0)
00054     {
00055       return realloc(reinterpret_cast<void*>(data), num * sizeof(T));
00056     }
00057 
00058     void memoryset(pointer data, char c, size_t num)
00059     {
00060       memset(reinterpret_cast<void*>(data), c, num);
00061     }
00062 
00063     void memorycpy(pointer datat, pointer datas, size_t num)
00064     {
00065       memcpy(reinterpret_cast<void*>(datat), reinterpret_cast<void*>(datas),
00066              num * sizeof(T));
00067     }
00068   };
00069 
00070 
00072   // CALLOCALLOC //
00074 
00075 
00076   template <class T>
00077   class CallocAlloc
00078   {
00079   public:
00080     typedef T value_type;
00081     typedef T* pointer;
00082     typedef const T* const_pointer;
00083     typedef T& reference;
00084     typedef const T& const_reference;
00085 
00086   public:
00087 
00088     pointer allocate(int num, void* h = 0)
00089     {
00090       return static_cast<pointer>( calloc(num, sizeof(T)) );
00091     }
00092 
00093     void deallocate(pointer data, int num, void* h = 0)
00094     {
00095       free(data);
00096     }
00097 
00098     void* reallocate(pointer data, int num, void* h = 0)
00099     {
00100       return realloc(reinterpret_cast<void*>(data), num * sizeof(T));
00101     }
00102 
00103     void memoryset(pointer data, char c, size_t num)
00104     {
00105       memset(reinterpret_cast<void*>(data), c, num);
00106     }
00107 
00108     void memorycpy(pointer datat, pointer datas, size_t num)
00109     {
00110       memcpy(reinterpret_cast<void*>(datat), reinterpret_cast<void*>(datas),
00111              num * sizeof(T));
00112     }
00113   };
00114 
00115 
00117   // NEWALLOC //
00119 
00120 
00121   template <class T>
00122   class NewAlloc
00123   {
00124   public:
00125     typedef T value_type;
00126     typedef T* pointer;
00127     typedef const T* const_pointer;
00128     typedef T& reference;
00129     typedef const T& const_reference;
00130 
00131   public:
00132 
00133     pointer allocate(int num, void* h = 0)
00134     {
00135       return static_cast<pointer>(new T[num]);
00136     }
00137 
00138     void deallocate(pointer data, int num, void* h = 0)
00139     {
00140       delete [] data;
00141     }
00142 
00143     void* reallocate(pointer data, int num, void* h = 0)
00144     {
00145       if (data != NULL)
00146         delete [] data;
00147       return (new T[num]);
00148     }
00149 
00150     void memoryset(pointer data, char c, size_t num)
00151     {
00152       memset(reinterpret_cast<void*>(data), c, num);
00153     }
00154 
00155     void memorycpy(pointer datat, pointer datas, size_t num)
00156     {
00157       for (size_t i = 0; i < num; i++)
00158         datat[i] = datas[i];
00159     }
00160   };
00161 
00162 
00164   // NANALLOC //
00166 
00167 
00168   template <class T>
00169   class NaNAlloc
00170   {
00171   public:
00172     typedef T value_type;
00173     typedef T* pointer;
00174     typedef const T* const_pointer;
00175     typedef T& reference;
00176     typedef const T& const_reference;
00177 
00178   public:
00179 
00180     pointer allocate(int num, void* h = 0)
00181     {
00182       pointer data = static_cast<pointer>( malloc(num * sizeof(T)) );
00183       if (numeric_limits<value_type>::has_signaling_NaN)
00184         for (int i = 0; i < num; i++)
00185           data[i] = numeric_limits<value_type>::signaling_NaN();
00186       else if (numeric_limits<value_type>::has_quiet_NaN)
00187         for (int i = 0; i < num; i++)
00188           data[i] = numeric_limits<value_type>::quiet_NaN();
00189       else if  (numeric_limits<value_type>::has_infinity)
00190         for (int i = 0; i < num; i++)
00191           data[i] = numeric_limits<value_type>::infinity();
00192       return data;
00193     }
00194 
00195     void deallocate(pointer data, int num, void* h = 0)
00196     {
00197       free(data);
00198     }
00199 
00200     void* reallocate(pointer data, int num, void* h = 0)
00201     {
00202       void* datav = realloc(reinterpret_cast<void*>(data), num * sizeof(T));
00203       pointer datap = reinterpret_cast<pointer>(datav);
00204       if (numeric_limits<value_type>::has_signaling_NaN)
00205         for (int i = 0; i < num; i++)
00206           datap[i] = numeric_limits<value_type>::signaling_NaN();
00207       else if (numeric_limits<value_type>::has_quiet_NaN)
00208         for (int i = 0; i < num; i++)
00209           datap[i] = numeric_limits<value_type>::quiet_NaN();
00210       else if  (numeric_limits<value_type>::has_infinity)
00211         for (int i = 0; i < num; i++)
00212           datap[i] = numeric_limits<value_type>::infinity();
00213       return datav;
00214     }
00215 
00216     void memoryset(pointer data, char c, size_t num)
00217     {
00218       memset(reinterpret_cast<void*>(data), c, num);
00219     }
00220 
00221     void memorycpy(pointer datat, pointer datas, size_t num)
00222     {
00223       memcpy(reinterpret_cast<void*>(datat), reinterpret_cast<void*>(datas),
00224              num * sizeof(T));
00225     }
00226   };
00227 
00228 
00229 } // namespace Seldon.
00230 
00231 #define SELDON_FILE_ALLOCATOR_HXX
00232 #endif

Generated on Sun Jan 16 23:37:57 2005 for Multivac by  doxygen 1.4.0