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.

140 lines
3.5KB

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