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.

159 lines
4.0KB

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