JackShmMem.h

00001 /*
00002 Copyright (C) 2001 Paul Davis 
00003 Copyright (C) 2004-2006 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 #ifndef __JackShmMem__
00022 #define __JackShmMem__
00023 
00024 #include "shm.h"
00025 #include "JackError.h"
00026 
00027 #include <new>  // GCC 4.0
00028 #include <errno.h> 
00029 #include <stdlib.h>
00030 
00031 #ifdef WIN32
00032         #include <windows.h>
00033         #define CHECK_MLOCK(ptr, size) (VirtualLock((ptr), (size)) != 0)
00034         #define CHECK_MUNLOCK(ptr, size) (VirtualUnlock((ptr), (size)) != 0)
00035         #define CHECK_MLOCKALL()(false)
00036         #define CHECK_MUNLOCKALL()(false)
00037 #else
00038         #include <sys/types.h>
00039         #include <sys/mman.h>
00040         #define CHECK_MLOCK(ptr, size) (mlock((ptr), (size)) == 0)
00041         #define CHECK_MUNLOCK(ptr, size) (munlock((ptr), (size)) == 0)
00042         #define CHECK_MLOCKALL() (mlockall(MCL_CURRENT | MCL_FUTURE) == 0)
00043         #define CHECK_MUNLOCKALL() (munlockall() == 0)
00044 #endif
00045   
00046 namespace Jack
00047 {
00048 
00049 void LockMemoryImp(void* ptr, size_t size);
00050 void UnlockMemoryImp(void* ptr, size_t size);
00051 
00052 class JackMem
00053 {
00054         private:
00055 
00056                 size_t fSize;
00057                 static size_t gSize;
00058         
00059         public:
00060 
00061         void* operator new(size_t size)
00062                 {       
00063                         gSize = size;
00064                         return calloc(1, size);
00065                 }
00066                 
00067         void operator delete(void* ptr, size_t size)
00068                 {       
00069                         free(ptr);
00070                 }
00071 
00072         JackMem():fSize(gSize)
00073         {}
00074 
00075         virtual ~JackMem()
00076         {}
00077                 
00078                 void LockMemory()
00079                 {
00080                         LockMemoryImp(this, fSize);
00081                 }
00082                 
00083                 void UnlockMemory()
00084                 {
00085                         UnlockMemoryImp(this, fSize);
00086                 }
00087 
00088 };
00089 
00096 class JackShmMem 
00097 {
00098 
00099     protected:
00100 
00101         jack_shm_info_t fInfo;
00102         static unsigned long fSegmentNum;
00103         static unsigned long fSegmentCount;
00104         static jack_shm_info_t gInfo;
00105 
00106     public:
00107 
00108         void* operator new(size_t size);
00109         void operator delete(void* p, size_t size);
00110 
00111         JackShmMem()
00112         {
00113             fInfo.index = gInfo.index;
00114                         fInfo.attached_at = gInfo.attached_at;
00115                         fInfo.size = gInfo.size;
00116                 }
00117 
00118         virtual ~JackShmMem()
00119         {}
00120 
00121         int GetShmIndex()
00122         {
00123             return fInfo.index;
00124         }
00125 
00126         char* GetShmAddress()
00127         {
00128             return (char*)fInfo.attached_at;
00129         }
00130                 
00131                 void LockMemory()
00132                 {
00133                         LockMemoryImp(this, fInfo.size);
00134                 }
00135                 
00136                 void UnlockMemory()
00137                 {
00138                         UnlockMemoryImp(this, fInfo.size);
00139                 }
00140 
00141 };
00142 
00147 template <class T>
00148 class JackShmReadWritePtr
00149 {
00150 
00151     private:
00152 
00153         jack_shm_info_t fInfo;
00154 
00155         void Init(int index)
00156         {
00157             if (fInfo.index < 0 && index >= 0) {
00158                 JackLog("JackShmReadWritePtr::Init %ld %ld\n", index, fInfo.index);
00159                 if (jack_initialize_shm_client() < 0)
00160                     throw - 1;
00161                 fInfo.index = index;
00162                 if (jack_attach_shm(&fInfo)) {
00163                     //jack_error("cannot attach shared memory segment", strerror(errno));
00164                     throw - 2;
00165                 }
00166             }
00167         }
00168 
00169     public:
00170 
00171         JackShmReadWritePtr()
00172         {
00173             fInfo.index = -1;
00174             fInfo.attached_at = NULL;
00175         }
00176 
00177         JackShmReadWritePtr(int index)
00178         {
00179             Init(index);
00180         }
00181 
00182         virtual ~JackShmReadWritePtr()
00183         {
00184             if (fInfo.index >= 0) {
00185                 JackLog("JackShmReadWritePtr::~JackShmReadWritePtr %ld\n", fInfo.index);
00186                 jack_release_shm(&fInfo);
00187                 fInfo.index = -1;
00188             }
00189         }
00190 
00191         T* operator->() const
00192         {
00193             return (T*)fInfo.attached_at;
00194         }
00195 
00196         operator T*() const
00197         {
00198             return (T*)fInfo.attached_at;
00199         }
00200 
00201         JackShmReadWritePtr& operator=(int index)
00202         {
00203             Init(index);
00204             return *this;
00205         }
00206 
00207         int GetShmIndex()
00208         {
00209             return fInfo.index;
00210         }
00211 
00212         T* GetShmAddress()
00213         {
00214             return (T*)fInfo.attached_at;
00215         }
00216 };
00217 
00222 template <class T>
00223 class JackShmReadWritePtr1
00224 {
00225 
00226     private:
00227 
00228         jack_shm_info_t fInfo;
00229 
00230         void Init(int index)
00231         {
00232             if (fInfo.index < 0 && index >= 0) {
00233                 JackLog("JackShmReadWritePtr1::Init %ld %ld\n", index, fInfo.index);
00234                 if (jack_initialize_shm_client() < 0)
00235                     throw - 1;
00236                 fInfo.index = index;
00237                 if (jack_attach_shm(&fInfo)) {
00238                     //jack_error("cannot attach shared memory segment", strerror(errno));
00239                     throw - 2;
00240                 }
00241                 /*
00242                 nobody else needs to access this shared memory any more, so
00243                 destroy it. because we have our own attachment to it, it won't
00244                 vanish till we exit (and release it).
00245                 */
00246                 jack_destroy_shm(&fInfo);
00247             }
00248         }
00249 
00250     public:
00251 
00252         JackShmReadWritePtr1()
00253         {
00254             fInfo.index = -1;
00255             fInfo.attached_at = NULL;
00256         }
00257 
00258         JackShmReadWritePtr1(int index)
00259         {
00260             Init(index);
00261         }
00262 
00263         virtual ~JackShmReadWritePtr1()
00264         {
00265             if (fInfo.index >= 0) {
00266                 JackLog("JackShmReadWritePtr1::~JackShmReadWritePtr1 %ld\n", fInfo.index);
00267                 jack_release_shm(&fInfo);
00268                 fInfo.index = -1;
00269             }
00270         }
00271 
00272         T* operator->() const
00273         {
00274             return (T*)fInfo.attached_at;
00275         }
00276 
00277         operator T*() const
00278         {
00279             return (T*)fInfo.attached_at;
00280         }
00281 
00282         JackShmReadWritePtr1& operator=(int index)
00283         {
00284             Init(index);
00285             return *this;
00286         }
00287 
00288         int GetShmIndex()
00289         {
00290             return fInfo.index;
00291         }
00292 
00293         T* GetShmAddress()
00294         {
00295             return (T*)fInfo.attached_at;
00296         }
00297 };
00298 
00303 template <class T>
00304 class JackShmReadPtr
00305 {
00306 
00307     private:
00308 
00309         jack_shm_info_t fInfo;
00310 
00311         void Init(int index)
00312         {
00313             if (fInfo.index < 0 && index >= 0) {
00314                 JackLog("JackShmPtrRead::Init %ld %ld\n", index, fInfo.index);
00315                 if (jack_initialize_shm_client() < 0)
00316                     throw - 1;
00317                 fInfo.index = index;
00318                 if (jack_attach_shm_read(&fInfo)) {
00319                     //jack_error("cannot attach shared memory segment", strerror(errno));
00320                     throw - 2;
00321                 }
00322             }
00323         }
00324 
00325     public:
00326 
00327         JackShmReadPtr()
00328         {
00329             fInfo.index = -1;
00330             fInfo.attached_at = NULL;
00331         }
00332 
00333         JackShmReadPtr(int index)
00334         {
00335             Init(index);
00336         }
00337 
00338         virtual ~JackShmReadPtr()
00339         {
00340             if (fInfo.index >= 0) {
00341                 JackLog("JackShmPtrRead::~JackShmPtrRead %ld\n", fInfo.index);
00342                 jack_release_shm(&fInfo);
00343                 fInfo.index = -1;
00344             }
00345         }
00346 
00347         T* operator->() const
00348         {
00349             return (T*)fInfo.attached_at;
00350         }
00351 
00352         operator T*() const
00353         {
00354             return (T*)fInfo.attached_at;
00355         }
00356 
00357         JackShmReadPtr& operator=(int index)
00358         {
00359             Init(index);
00360             return *this;
00361         }
00362 
00363         int GetShmIndex()
00364         {
00365             return fInfo.index;
00366         }
00367 
00368         T* GetShmAddress()
00369         {
00370             return (T*)fInfo.attached_at;
00371         }
00372 
00373 };
00374 
00375 } // end of namespace
00376 
00377 #endif

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1