diff --git a/common/JackClientControl.h b/common/JackClientControl.h index e9e9ce5a..98cd89d4 100644 --- a/common/JackClientControl.h +++ b/common/JackClientControl.h @@ -56,9 +56,6 @@ struct JackClientControl : public JackShmMem Init("", -1); } - virtual ~JackClientControl() - {} - void Init(const char* name, int refnum) { strcpy(fName, name); diff --git a/common/JackEngine.cpp b/common/JackEngine.cpp index d70bca93..ac64e49e 100644 --- a/common/JackEngine.cpp +++ b/common/JackEngine.cpp @@ -33,7 +33,15 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. namespace Jack { -JackEngine::JackEngine(JackGraphManager* manager, JackSynchro** table, JackEngineControl* control, JackSyncInterface* signal, bool sync, long time_out_ms, bool rt, long priority, bool ve) +JackEngine::JackEngine(JackGraphManager* manager, + JackSynchro** table, + JackEngineControl* control, + JackSyncInterface* signal, + bool sync, + long time_out_ms, + bool rt, + long priority, + bool ve) { fGraphManager = manager; fSynchroTable = table; diff --git a/common/JackShmMem.cpp b/common/JackShmMem.cpp index 5fe9826f..29b2b9c6 100644 --- a/common/JackShmMem.cpp +++ b/common/JackShmMem.cpp @@ -29,7 +29,7 @@ unsigned long JackShmMem::fSegmentNum = 0; unsigned long JackShmMem::fSegmentCount = 0; jack_shm_info_t JackShmMem::gInfo; -size_t JackLockMem::gSize = 0; +size_t JackMem::gSize = 0; void* JackShmMem::operator new(size_t size) { @@ -62,7 +62,9 @@ void* JackShmMem::operator new(size_t size) // It is unsafe to set object fields directly (may be overwritten during object initialization), // so use an intermediate global data gInfo.index = info.index; + gInfo.size = size; gInfo.attached_at = info.attached_at; + JackLog("JackShmMem::new index = %ld attached = %x size = %ld \n", info.index, info.attached_at, size); return obj; @@ -89,5 +91,38 @@ void JackShmMem::operator delete(void* p, size_t size) } } +void LockMemoryImp(void* ptr, size_t size) +{ +#ifdef __APPLE__ + int res = mlock(ptr, size); +#elif linux_ + int res = mlock(ptr, size); +#elif WIN32 + int res = VirtualLock(ptr, size); +#endif + if (res != 0) { + jack_error("Cannot lock down memory area (%s)", strerror(errno)); + } else { + JackLog("Succeeded in locking %u byte memory area\n", size); + } +} + +void UnlockMemoryImp(void* ptr, size_t size) +{ +#ifdef __APPLE__ + int res = munlock(ptr, size); +#elif linux_ + int res = munlock(ptr, size); +#elif WIN32 + int res = VirtualUnlock(ptr, size); +#endif + if (res != 0) { + jack_error("Cannot unlock down memory area (%s)", strerror(errno)); + } else { + JackLog("Succeeded in unlocking %u byte memory area\n", size); + } +} + + } // end of namespace diff --git a/common/JackShmMem.h b/common/JackShmMem.h index 5ebb55d0..9b51e8cc 100644 --- a/common/JackShmMem.h +++ b/common/JackShmMem.h @@ -38,7 +38,10 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. namespace Jack { -class JackLockMem +void LockMemoryImp(void* ptr, size_t size); +void UnlockMemoryImp(void* ptr, size_t size); + +class JackMem { private: @@ -50,7 +53,7 @@ class JackLockMem void* operator new(size_t size) { gSize = size; - return malloc(size); + return calloc(1, size); } void operator delete(void* ptr, size_t size) @@ -58,49 +61,36 @@ class JackLockMem free(ptr); } - JackLockMem():fSize(gSize) + JackMem():fSize(gSize) {} - virtual ~JackLockMem() + virtual ~JackMem() { - UnlockMemory(); + UnlockMemoryImp(this, fSize); } - void LockMemory() + void LockMemory() { - #ifdef __APPLE__ - mlock(this, fSize); - #elif linux_ - mlock(this, fSize); - #elif WIN32 - VirtualLock(this, fSize); - #endif + LockMemoryImp(this, fSize); } - void UnlockMemory() + void UnlockMemory() { - #ifdef __APPLE__ - munlock(this, fSize); - #elif linux_ - munlock(this, fSize); - #elif WIN32 - VirtualUnlock(this, fSize); - #endif + UnlockMemoryImp(this, fSize); } }; - /*! \brief The base class for shared memory management. A class which objects need to be allocated in shared memory derives from this class. */ -class JackShmMem +class JackShmMem { - private: + protected: jack_shm_info_t fInfo; static unsigned long fSegmentNum; @@ -115,11 +105,14 @@ class JackShmMem JackShmMem() { fInfo.index = gInfo.index; - fInfo.attached_at = gInfo.attached_at; - } + fInfo.attached_at = gInfo.attached_at; + fInfo.size = gInfo.size; + } virtual ~JackShmMem() - {} + { + UnlockMemoryImp(this, fInfo.size); + } int GetShmIndex() { @@ -130,6 +123,16 @@ class JackShmMem { return (char*)fInfo.attached_at; } + + void LockMemory() + { + LockMemoryImp(this, fInfo.size); + } + + void UnlockMemory() + { + UnlockMemoryImp(this, fInfo.size); + } }; diff --git a/common/shm.h b/common/shm.h index 2ef29fc4..d8256e0d 100644 --- a/common/shm.h +++ b/common/shm.h @@ -117,7 +117,8 @@ extern "C" */ typedef struct _jack_shm_info { jack_shm_registry_index_t index; /* offset into the registry */ - void *attached_at; /* address where attached */ + size_t size; + void *attached_at; /* address where attached */ } jack_shm_info_t;