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.

133 lines
3.4KB

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