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.

223 lines
6.7KB

  1. /* This module provides a set of abstract shared memory interfaces
  2. * with support using both System V and POSIX shared memory
  3. * implementations. The code is divided into three sections:
  4. *
  5. * - common (interface-independent) code
  6. * - POSIX implementation
  7. * - System V implementation
  8. * - Windows implementation
  9. *
  10. * The implementation used is determined by whether USE_POSIX_SHM was
  11. * set in the ./configure step.
  12. */
  13. /*
  14. Copyright (C) 2001-2003 Paul Davis
  15. Copyright (C) 2005-2012 Grame
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU Lesser General Public License as published by
  18. the Free Software Foundation; either version 2.1 of the License, or
  19. (at your option) any later version.
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU Lesser General Public License for more details.
  24. You should have received a copy of the GNU Lesser General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. */
  28. #ifndef __jack_shm_h__
  29. #define __jack_shm_h__
  30. #include <limits.h>
  31. #include <sys/types.h>
  32. #include "types.h"
  33. #include "JackCompilerDeps.h"
  34. #include "JackConstants.h"
  35. #define TRUE 1
  36. #define FALSE 0
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. #define MAX_SERVERS 8 /* maximum concurrent servers */
  42. #define MAX_SHM_ID 256 /* generally about 16 per server */
  43. #define JACK_SHM_MAGIC 0x4a41434b /* shm magic number: "JACK" */
  44. #define JACK_SHM_NULL_INDEX -1 /* NULL SHM index */
  45. #define JACK_SHM_REGISTRY_INDEX -2 /* pseudo SHM index for registry */
  46. /* On Mac OS X, SHM_NAME_MAX is the maximum length of a shared memory
  47. * segment name (instead of NAME_MAX or PATH_MAX as defined by the
  48. * standard).
  49. */
  50. #ifdef USE_POSIX_SHM
  51. #ifndef NAME_MAX
  52. #define NAME_MAX 255
  53. #endif
  54. #ifndef SHM_NAME_MAX
  55. #define SHM_NAME_MAX NAME_MAX
  56. #endif
  57. typedef char shm_name_t[SHM_NAME_MAX];
  58. typedef shm_name_t jack_shm_id_t;
  59. #elif WIN32
  60. #define NAME_MAX 255
  61. #ifndef SHM_NAME_MAX
  62. #define SHM_NAME_MAX NAME_MAX
  63. #endif
  64. typedef char shm_name_t[SHM_NAME_MAX];
  65. typedef shm_name_t jack_shm_id_t;
  66. #elif __ANDROID__
  67. #ifndef NAME_MAX
  68. #define NAME_MAX 255
  69. #endif
  70. #ifndef SHM_NAME_MAX
  71. #define SHM_NAME_MAX NAME_MAX
  72. #endif
  73. typedef char shm_name_t[SHM_NAME_MAX];
  74. typedef shm_name_t jack_shm_id_t;
  75. typedef int jack_shm_fd_t;
  76. #else
  77. /* System V SHM */
  78. typedef int jack_shm_id_t;
  79. #endif /* SHM type */
  80. /* shared memory type */
  81. typedef enum {
  82. shm_POSIX = 1, /* POSIX shared memory */
  83. shm_SYSV = 2, /* System V shared memory */
  84. shm_WIN32 = 3, /* Windows 32 shared memory */
  85. shm_ANDROID = 4 /* Android shared memory */
  86. } jack_shmtype_t;
  87. #ifdef __APPLE__
  88. /* we need to align and pack data to 32bit so that x86_64 and arm64 work together */
  89. typedef int32_t jack_shm_registry_index_t;
  90. #else
  91. typedef int16_t jack_shm_registry_index_t;
  92. #endif
  93. /**
  94. * A structure holding information about shared memory allocated by
  95. * JACK. this persists across invocations of JACK, and can be used by
  96. * multiple JACK servers. It contains no pointers and is valid across
  97. * address spaces.
  98. *
  99. * The registry consists of two parts: a header including an array of
  100. * server names, followed by an array of segment registry entries.
  101. */
  102. typedef struct _jack_shm_server {
  103. #ifdef WIN32
  104. int pid; /* process ID */
  105. #else
  106. pid_t pid; /* process ID */
  107. #endif
  108. char name[JACK_SERVER_NAME_SIZE+1];
  109. }
  110. jack_shm_server_t;
  111. typedef struct _jack_shm_header {
  112. uint32_t magic; /* magic number */
  113. uint16_t protocol; /* JACK protocol version */
  114. jack_shmtype_t type; /* shm type */
  115. jack_shmsize_t size; /* total registry segment size */
  116. jack_shmsize_t hdr_len; /* size of header */
  117. jack_shmsize_t entry_len; /* size of registry entry */
  118. jack_shm_server_t server[MAX_SERVERS]; /* current server array */
  119. }
  120. jack_shm_header_t;
  121. typedef struct _jack_shm_registry {
  122. jack_shm_registry_index_t index; /* offset into the registry */
  123. #ifdef WIN32
  124. int allocator; /* PID that created shm segment */
  125. #else
  126. pid_t allocator; /* PID that created shm segment */
  127. #endif
  128. jack_shmsize_t size; /* for POSIX unattach */
  129. jack_shm_id_t id; /* API specific, see above */
  130. #ifdef __ANDROID__
  131. jack_shm_fd_t fd;
  132. #endif
  133. }
  134. jack_shm_registry_t;
  135. #define JACK_SHM_REGISTRY_SIZE (sizeof (jack_shm_header_t) \
  136. + sizeof (jack_shm_registry_t) * MAX_SHM_ID)
  137. /**
  138. * a structure holding information about shared memory
  139. * allocated by JACK. this version is valid only
  140. * for a given address space. It contains a pointer
  141. * indicating where the shared memory has been
  142. * attached to the address space.
  143. */
  144. PRE_PACKED_STRUCTURE
  145. struct _jack_shm_info {
  146. jack_shm_registry_index_t index; /* offset into the registry */
  147. uint32_t size;
  148. #ifdef __ANDROID__
  149. jack_shm_fd_t fd;
  150. #endif
  151. union {
  152. void *attached_at; /* address where attached */
  153. char ptr_size[8];
  154. } ptr; /* a "pointer" that has the same 8 bytes size when compiling in 32 or 64 bits */
  155. } POST_PACKED_STRUCTURE;
  156. typedef struct _jack_shm_info jack_shm_info_t;
  157. /* utility functions used only within JACK */
  158. void jack_shm_copy_from_registry (jack_shm_info_t*,
  159. jack_shm_registry_index_t);
  160. void jack_shm_copy_to_registry (jack_shm_info_t*,
  161. jack_shm_registry_index_t*);
  162. int jack_release_shm_info (jack_shm_registry_index_t);
  163. char* jack_shm_addr (jack_shm_info_t* si);
  164. /* here begin the API */
  165. int jack_register_server (const char *server_name, int new_registry);
  166. int jack_unregister_server (const char *server_name);
  167. int jack_initialize_shm (const char *server_name);
  168. int jack_initialize_shm_server (void);
  169. int jack_initialize_shm_client (void);
  170. int jack_cleanup_shm (void);
  171. int jack_shmalloc (const char *shm_name, jack_shmsize_t size,
  172. jack_shm_info_t* result);
  173. void jack_release_shm (jack_shm_info_t*);
  174. void jack_release_lib_shm (jack_shm_info_t*);
  175. void jack_destroy_shm (jack_shm_info_t*);
  176. int jack_attach_shm (jack_shm_info_t*);
  177. int jack_attach_lib_shm (jack_shm_info_t*);
  178. int jack_attach_shm_read (jack_shm_info_t*);
  179. int jack_attach_lib_shm_read (jack_shm_info_t*);
  180. int jack_resize_shm (jack_shm_info_t*, jack_shmsize_t size);
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. #endif /* __jack_shm_h__ */