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.

118 lines
3.2KB

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