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.

124 lines
3.3KB

  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 "JackShmMem.h"
  17. #include "JackError.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 LockMemoryImp(void* ptr, size_t size)
  67. {
  68. if (CHECK_MLOCK(ptr, size)) {
  69. jack_log("Succeeded in locking %u byte memory area", size);
  70. } else {
  71. jack_error("Cannot lock down memory area (%s)", strerror(errno));
  72. }
  73. }
  74. void UnlockMemoryImp(void* ptr, size_t size)
  75. {
  76. if (CHECK_MUNLOCK(ptr, size)) {
  77. jack_log("Succeeded in unlocking %u byte memory area", size);
  78. } else {
  79. jack_error("Cannot unlock down memory area (%s)", strerror(errno));
  80. }
  81. }
  82. void LockAllMemory()
  83. {
  84. if (CHECK_MLOCKALL()) {
  85. jack_log("Succeeded in locking all memory");
  86. } else {
  87. jack_error("Cannot lock all memory (%s)", strerror(errno));
  88. }
  89. }
  90. void UnlockAllMemory()
  91. {
  92. if (CHECK_MUNLOCKALL()) {
  93. jack_log("Succeeded in unlocking all memory");
  94. } else {
  95. jack_error("Cannot unlock all memory (%s)", strerror(errno));
  96. }
  97. }
  98. } // end of namespace