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.

93 lines
2.6KB

  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. 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%ld", JackShmMem::fSegmentNum++);
  30. if (JackShmMem::fSegmentCount++ == 0) {
  31. JackLog("jack_initialize_shm\n");
  32. if (jack_initialize_shm_server() < 0) {
  33. jack_error("cannot initialize shm", strerror(errno));
  34. goto error;
  35. }
  36. }
  37. if (jack_shmalloc(name, size, &info)) {
  38. jack_error("cannot create shared memory segment of size = %d", size, strerror(errno));
  39. goto error;
  40. }
  41. if (jack_attach_shm(&info)) {
  42. jack_error("cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
  43. jack_destroy_shm(&info);
  44. goto error;
  45. }
  46. obj = (JackShmMem*)jack_shm_addr(&info);
  47. // It is unsafe to set object fields directly (may be overwritten during object initialization),
  48. // so use an intermediate global data
  49. gInfo.index = info.index;
  50. gInfo.attached_at = info.attached_at;
  51. JackLog("JackShmMem::new index = %ld attached = %x size = %ld \n", info.index, info.attached_at, size);
  52. return obj;
  53. error:
  54. jack_error("JackShmMem::new bad alloc", size);
  55. throw new std::bad_alloc;
  56. }
  57. void JackShmMem::operator delete(void* p, size_t size)
  58. {
  59. jack_shm_info_t info;
  60. JackShmMem* obj = (JackShmMem*)p;
  61. info.index = obj->fInfo.index;
  62. info.attached_at = obj->fInfo.attached_at;
  63. JackLog("JackShmMem::delete size = %ld index = %ld\n", size, info.index);
  64. jack_release_shm(&info);
  65. jack_destroy_shm(&info);
  66. if (--JackShmMem::fSegmentCount == 0) {
  67. JackLog("jack_cleanup_shm\n");
  68. jack_cleanup_shm();
  69. }
  70. }
  71. } // end of namespace