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.

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