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.

152 lines
3.9KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 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. }
  28. void JackShmMemAble::Init()
  29. {
  30. fInfo.index = gInfo.index;
  31. fInfo.attached_at = gInfo.attached_at;
  32. fInfo.size = gInfo.size;
  33. }
  34. void* JackShmMem::operator new(size_t size, void* memory)
  35. {
  36. jack_log("JackShmMem::new placement size = %ld", size);
  37. return memory;
  38. }
  39. void* JackShmMem::operator new(size_t size)
  40. {
  41. jack_shm_info_t info;
  42. JackShmMem* obj;
  43. char name[64];
  44. snprintf(name, sizeof(name), "/jack_shared%d", fSegmentNum++);
  45. if (jack_shmalloc(name, size, &info)) {
  46. jack_error("cannot create shared memory segment of size = %d", size, strerror(errno));
  47. goto error;
  48. }
  49. if (jack_attach_shm(&info)) {
  50. jack_error("cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
  51. jack_destroy_shm(&info);
  52. goto error;
  53. }
  54. obj = (JackShmMem*)jack_shm_addr(&info);
  55. // It is unsafe to set object fields directly (may be overwritten during object initialization),
  56. // so use an intermediate global data
  57. gInfo.index = info.index;
  58. gInfo.size = size;
  59. gInfo.attached_at = info.attached_at;
  60. jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.attached_at, size);
  61. return obj;
  62. error:
  63. jack_error("JackShmMem::new bad alloc", size);
  64. throw std::bad_alloc();
  65. }
  66. void JackShmMem::operator delete(void* p, size_t size)
  67. {
  68. jack_shm_info_t info;
  69. JackShmMem* obj = (JackShmMem*)p;
  70. info.index = obj->fInfo.index;
  71. info.attached_at = obj->fInfo.attached_at;
  72. jack_log("JackShmMem::delete size = %ld index = %ld", size, info.index);
  73. jack_release_shm(&info);
  74. jack_destroy_shm(&info);
  75. }
  76. void JackShmMem::operator delete(void* obj)
  77. {
  78. if (obj)
  79. JackShmMem::operator delete(obj, 0);
  80. }
  81. void LockMemoryImp(void* ptr, size_t size)
  82. {
  83. if (CHECK_MLOCK((char*)ptr, size)) {
  84. jack_log("Succeeded in locking %u byte memory area", size);
  85. } else {
  86. jack_error("Cannot lock down memory area (%s)", strerror(errno));
  87. }
  88. }
  89. void InitLockMemoryImp(void* ptr, size_t size)
  90. {
  91. if (CHECK_MLOCK((char*)ptr, size)) {
  92. memset(ptr, 0, size);
  93. jack_log("Succeeded in locking %u byte memory area", size);
  94. } else {
  95. jack_error("Cannot lock down memory area (%s)", strerror(errno));
  96. }
  97. }
  98. void UnlockMemoryImp(void* ptr, size_t size)
  99. {
  100. if (CHECK_MUNLOCK((char*)ptr, size)) {
  101. jack_log("Succeeded in unlocking %u byte memory area", size);
  102. } else {
  103. jack_error("Cannot unlock down memory area (%s)", strerror(errno));
  104. }
  105. }
  106. void LockAllMemory()
  107. {
  108. if (CHECK_MLOCKALL()) {
  109. jack_log("Succeeded in locking all memory");
  110. } else {
  111. jack_error("Cannot lock all memory (%s)", strerror(errno));
  112. }
  113. }
  114. void UnlockAllMemory()
  115. {
  116. if (CHECK_MUNLOCKALL()) {
  117. jack_log("Succeeded in unlocking all memory");
  118. } else {
  119. jack_error("Cannot unlock all memory (%s)", strerror(errno));
  120. }
  121. }
  122. } // end of namespace