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.

94 lines
2.7KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 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 long JackShmMem::fSegmentNum = 0;
  22. unsigned long JackShmMem::fSegmentCount = 0;
  23. jack_shm_info_t JackShmMem::gInfo;
  24. size_t JackLockMem::gSize = 0;
  25. void* JackShmMem::operator new(size_t size)
  26. {
  27. jack_shm_info_t info;
  28. JackShmMem* obj;
  29. char name[64];
  30. snprintf(name, sizeof(name), "/jack_shared%ld", JackShmMem::fSegmentNum++);
  31. if (JackShmMem::fSegmentCount++ == 0) {
  32. JackLog("jack_initialize_shm\n");
  33. if (jack_initialize_shm_server() < 0) {
  34. jack_error("cannot initialize shm", strerror(errno));
  35. goto error;
  36. }
  37. }
  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.attached_at = info.attached_at;
  52. JackLog("JackShmMem::new index = %ld attached = %x size = %ld \n", info.index, info.attached_at, size);
  53. return obj;
  54. error:
  55. jack_error("JackShmMem::new bad alloc", size);
  56. throw new std::bad_alloc;
  57. }
  58. void JackShmMem::operator delete(void* p, size_t size)
  59. {
  60. jack_shm_info_t info;
  61. JackShmMem* obj = (JackShmMem*)p;
  62. info.index = obj->fInfo.index;
  63. info.attached_at = obj->fInfo.attached_at;
  64. JackLog("JackShmMem::delete size = %ld index = %ld\n", size, info.index);
  65. jack_release_shm(&info);
  66. jack_destroy_shm(&info);
  67. if (--JackShmMem::fSegmentCount == 0) {
  68. JackLog("jack_cleanup_shm\n");
  69. jack_cleanup_shm();
  70. }
  71. }
  72. } // end of namespace