jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.0KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackError.h"
  16. #include "JackShmMem.h"
  17. #include <stdio.h>
  18. namespace Jack
  19. {
  20. static unsigned int fSegmentNum = 0;
  21. static jack_shm_info_t gInfo;
  22. JackShmMem::JackShmMem()
  23. {
  24. JackShmMemAble::Init();
  25. LockMemory();
  26. }
  27. JackShmMem::~JackShmMem()
  28. {
  29. UnlockMemory();
  30. }
  31. void JackShmMemAble::Init()
  32. {
  33. fInfo.index = gInfo.index;
  34. fInfo.ptr.attached_at = gInfo.ptr.attached_at;
  35. fInfo.size = gInfo.size;
  36. }
  37. void* JackShmMem::operator new(size_t size, void* memory)
  38. {
  39. jack_log("JackShmMem::new placement size = %ld", size);
  40. return memory;
  41. }
  42. void* JackShmMem::operator new(size_t size)
  43. {
  44. jack_shm_info_t info;
  45. JackShmMem* obj;
  46. char name[64];
  47. snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
  48. if (jack_shmalloc(name, size, &info)) {
  49. jack_error("Cannot create shared memory segment of size = %d", size, strerror(errno));
  50. goto error;
  51. }
  52. if (jack_attach_shm(&info)) {
  53. jack_error("Cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
  54. jack_destroy_shm(&info);
  55. goto error;
  56. }
  57. obj = (JackShmMem*)jack_shm_addr(&info);
  58. // It is unsafe to set object fields directly (may be overwritten during object initialization),
  59. // so use an intermediate global data
  60. gInfo.index = info.index;
  61. gInfo.size = size;
  62. gInfo.ptr.attached_at = info.ptr.attached_at;
  63. jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.ptr.attached_at, size);
  64. return obj;
  65. error:
  66. jack_error("JackShmMem::new bad alloc", size);
  67. throw std::bad_alloc();
  68. }
  69. void JackShmMem::operator delete(void* p, size_t size)
  70. {
  71. jack_shm_info_t info;
  72. JackShmMem* obj = (JackShmMem*)p;
  73. info.index = obj->fInfo.index;
  74. info.ptr.attached_at = obj->fInfo.ptr.attached_at;
  75. jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
  76. jack_release_shm(&info);
  77. jack_destroy_shm(&info);
  78. }
  79. void JackShmMem::operator delete(void* obj)
  80. {
  81. if (obj) {
  82. JackShmMem::operator delete(obj, 0);
  83. }
  84. }
  85. void LockMemoryImp(void* ptr, size_t size)
  86. {
  87. if (CHECK_MLOCK((char*)ptr, size)) {
  88. jack_log("Succeeded in locking %u byte memory area", size);
  89. } else {
  90. jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
  91. }
  92. }
  93. void InitLockMemoryImp(void* ptr, size_t size)
  94. {
  95. if (CHECK_MLOCK((char*)ptr, size)) {
  96. memset(ptr, 0, size);
  97. jack_log("Succeeded in locking %u byte memory area", size);
  98. } else {
  99. jack_error("Cannot lock down %u byte memory area (%s)", size, strerror(errno));
  100. }
  101. }
  102. void UnlockMemoryImp(void* ptr, size_t size)
  103. {
  104. if (CHECK_MUNLOCK((char*)ptr, size)) {
  105. jack_log("Succeeded in unlocking %u byte memory area", size);
  106. } else {
  107. jack_error("Cannot unlock down %u byte memory area (%s)", size, strerror(errno));
  108. }
  109. }
  110. void LockAllMemory()
  111. {
  112. if (CHECK_MLOCKALL()) {
  113. jack_log("Succeeded in locking all memory");
  114. } else {
  115. jack_error("Cannot lock all memory (%s)", strerror(errno));
  116. }
  117. }
  118. void UnlockAllMemory()
  119. {
  120. if (CHECK_MUNLOCKALL()) {
  121. jack_log("Succeeded in unlocking all memory");
  122. } else {
  123. jack_error("Cannot unlock all memory (%s)", strerror(errno));
  124. }
  125. }
  126. } // end of namespace