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.

146 lines
3.6KB

  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackError.h"
  20. #include "JackShmMem.h"
  21. #include <stdio.h>
  22. namespace Jack
  23. {
  24. static unsigned int fSegmentNum = 0;
  25. static jack_shm_info_t gInfo;
  26. size_t JackMem::gSize = 0;
  27. JackShmMem::JackShmMem()
  28. {
  29. JackShmMemAble::Init();
  30. }
  31. void JackShmMemAble::Init()
  32. {
  33. fInfo.index = gInfo.index;
  34. fInfo.attached_at = gInfo.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.attached_at = info.attached_at;
  63. jack_log("JackShmMem::new index = %ld attached = %x size = %ld ", info.index, info.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.attached_at = obj->fInfo.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. void LockMemoryImp(void* ptr, size_t size)
  85. {
  86. if (CHECK_MLOCK(ptr, size)) {
  87. jack_log("Succeeded in locking %u byte memory area", size);
  88. } else {
  89. jack_error("Cannot lock down memory area (%s)", strerror(errno));
  90. }
  91. }
  92. void UnlockMemoryImp(void* ptr, size_t size)
  93. {
  94. if (CHECK_MUNLOCK(ptr, size)) {
  95. jack_log("Succeeded in unlocking %u byte memory area", size);
  96. } else {
  97. jack_error("Cannot unlock down memory area (%s)", strerror(errno));
  98. }
  99. }
  100. void LockAllMemory()
  101. {
  102. if (CHECK_MLOCKALL()) {
  103. jack_log("Succeeded in locking all memory");
  104. } else {
  105. jack_error("Cannot lock all memory (%s)", strerror(errno));
  106. }
  107. }
  108. void UnlockAllMemory()
  109. {
  110. if (CHECK_MUNLOCKALL()) {
  111. jack_log("Succeeded in unlocking all memory");
  112. } else {
  113. jack_error("Cannot unlock all memory (%s)", strerror(errno));
  114. }
  115. }
  116. } // end of namespace