Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

Alloc.hh

Go to the documentation of this file.
00001 /*
00002 
00003     Memory allocation wrapper class
00004     Copyright (C) 2000-2003 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #include <stdlib.h>
00024 #include <sys/types.h>
00025 #include <sys/mman.h>
00026 #include <string>
00027 #include <stdexcept>
00028 
00029 
00030 #ifndef ALLOC_HH
00031     #define ALLOC_HH
00032 
00033     #ifndef DSP_IPP
00034         /*#define ALLOC_USE_IPP
00035         #include <ipp.h>*/
00036         #define ALLOC_USE_ALIGN
00037         #define ALLOC_ALIGNMENT     32
00038     #else
00039         #define ALLOC_USE_IPP
00040         #include <ipp.h>
00041     #endif
00042 
00043 
00048     class clAlloc
00049     {
00050         protected:
00051             bool bLocked;
00052             long lSize;
00053             void *vpPtr;
00054         public:
00058             clAlloc ()
00059                 {
00060                     bLocked = false;
00061                     lSize = 0l;
00062                     vpPtr = NULL;
00063                 }
00067             clAlloc (const clAlloc &CopySrc)
00068                 {
00069                     Copy(CopySrc);
00070                 }
00077             clAlloc (long lAllocSize)
00078                 {
00079                     bLocked = false;
00080                     lSize = 0l;
00081                     vpPtr = NULL;
00082                     Size(lAllocSize);
00083                 }
00087             ~clAlloc ()
00088                 { Free(); }
00097             void *Size (long lAllocSize)
00098                 {
00099                     if (lSize == lAllocSize) return vpPtr;
00100                     Free();
00101                     if (lAllocSize <= 0l) return vpPtr;
00102                     #if defined(ALLOC_USE_ALIGN)
00103                     if (posix_memalign(&vpPtr, ALLOC_ALIGNMENT, lAllocSize))
00104                         throw std::runtime_error(std::string("Out of memory!"));
00105                     #elif defined(ALLOC_USE_IPP)
00106                     vpPtr = ippMalloc(lAllocSize);
00107                     if (vpPtr == NULL)
00108                         throw std::runtime_error(std::string("Out of memory!"));
00109                     #else
00110                     vpPtr = (void *) malloc(lAllocSize);
00111                     if (vpPtr == NULL) 
00112                         throw std::runtime_error(std::string("Out of memory!"));
00113                     #endif
00114                     lSize = lAllocSize;
00115                     return vpPtr;
00116                 }
00124             void *Resize (long lAllocSize)
00125                 {
00126                     if (bLocked) UnLock();
00127                     if (lSize == lAllocSize) return vpPtr;
00128                     if (lAllocSize <= 0l)
00129                     {
00130                         Free();
00131                         return vpPtr;
00132                     }
00133                     #if defined(ALLOC_USE_ALIGN)
00134                     void *vpOldPtr = vpPtr;
00135                     vpPtr = NULL;
00136                     if (posix_memalign(&vpPtr, ALLOC_ALIGNMENT, lAllocSize))
00137                         throw std::runtime_error(std::string("Out of memory!"));
00138                     memcpy(vpPtr, vpOldPtr, 
00139                         (lSize <= lAllocSize) ? lSize : lAllocSize);
00140                     free(vpOldPtr);
00141                     #elif defined(ALLOC_USE_IPP)
00142                     void *vpOldPtr = vpPtr;
00143                     vpPtr = ippMalloc(lAllocSize);
00144                     if (vpPtr == NULL)
00145                         throw std::runtime_error(std::string("Out of memory!"));
00146                     ippsCopy_8u((Ipp8u *) vpOldPtr, (Ipp8u *) vpPtr, 
00147                         (lSize <= lAllocSize) ? lSize : lAllocSize);
00148                     ippFree(vpOldPtr);
00149                     #else
00150                     vpPtr = (void *) realloc(vpPtr, lAllocSize);
00151                     if (vpPtr == NULL)
00152                         throw std::runtime_error(std::string("Out of memory!"));
00153                     #endif
00154                     lSize = lAllocSize;
00155                     return vpPtr;
00156                 }
00161             void Free ()
00162                 {
00163                     if (bLocked) UnLock();
00164                     if (vpPtr != NULL) 
00165                     {
00166                         #if defined(ALLOC_USE_IPP)
00167                         ippFree(vpPtr);
00168                         #else
00169                         free(vpPtr);
00170                         #endif
00171                         lSize = 0l;
00172                         vpPtr = NULL;
00173                     }
00174                 }
00180             void *GetPtr () const
00181                 { return vpPtr; }
00187             long GetSize () const
00188                 { return lSize; }
00192             void Lock ()
00193                 {
00194                     bLocked = true;
00195                     #if (defined(SOLARIS))
00196                     mlock((char *) vpPtr, lSize);
00197                     #else
00198                     mlock(vpPtr, lSize);
00199                     #endif
00200                 }
00204             void UnLock ()
00205                 {
00206                     bLocked = false;
00207                     #if (defined(SOLARIS))
00208                     munlock((char *) vpPtr, lSize);
00209                     #else
00210                     munlock(vpPtr, lSize);
00211                     #endif
00212                 }
00219             void Copy (const clAlloc &Src)
00220                 {
00221                     Free();
00222                     Size(Src.lSize);
00223                     memcpy(vpPtr, Src.vpPtr, lSize);
00224                 }
00231             void CopyTo (clAlloc &Dest)
00232                 {
00233                     Dest.Copy(*this);
00234                 }
00240             clAlloc GetCopy ()
00241                 {
00242                     clAlloc Temp;
00243 
00244                     Temp.Copy(*this);
00245                     return Temp;
00246                 }
00250             operator char *() const
00251                 { return ((char *) vpPtr); }
00253             operator unsigned char *() const
00254                 { return ((unsigned char *) vpPtr); }
00256             operator short *() const
00257                 { return ((short *) vpPtr); }
00259             operator unsigned short *() const
00260                 { return ((unsigned short *) vpPtr); }
00262             operator int *() const
00263                 { return ((int *) vpPtr); }
00265             operator unsigned int *() const
00266                 { return ((unsigned int *) vpPtr); }
00268             operator long *() const
00269                 { return ((long *) vpPtr); }
00271             operator unsigned long *() const
00272                 { return ((unsigned long *) vpPtr); }
00274             operator float *() const
00275                 { return ((float *) vpPtr); }
00277             operator double *() const
00278                 { return ((double *) vpPtr); }
00280             operator long double *() const
00281                 { return ((long double *) vpPtr); }
00283             operator void *() const
00284                 { return vpPtr; }
00285             operator void *()
00286                 { return vpPtr; }
00294             clAlloc & operator = (const clAlloc &Src)
00295                 { 
00296                     Copy(Src);
00297                     return (*this);
00298                 }
00299     };
00300 
00301 #endif
00302 

Generated on Tue Mar 2 19:46:42 2004 for libDSP by doxygen 1.3.6