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.

218 lines
6.6KB

  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. typedef int16_t jack_shm_registry_index_t;
  88. /**
  89. * A structure holding information about shared memory allocated by
  90. * JACK. this persists across invocations of JACK, and can be used by
  91. * multiple JACK servers. It contains no pointers and is valid across
  92. * address spaces.
  93. *
  94. * The registry consists of two parts: a header including an array of
  95. * server names, followed by an array of segment registry entries.
  96. */
  97. typedef struct _jack_shm_server {
  98. #ifdef WIN32
  99. int pid; /* process ID */
  100. #else
  101. pid_t pid; /* process ID */
  102. #endif
  103. char name[JACK_SERVER_NAME_SIZE+1];
  104. }
  105. jack_shm_server_t;
  106. typedef struct _jack_shm_header {
  107. uint32_t magic; /* magic number */
  108. uint16_t protocol; /* JACK protocol version */
  109. jack_shmtype_t type; /* shm type */
  110. jack_shmsize_t size; /* total registry segment size */
  111. jack_shmsize_t hdr_len; /* size of header */
  112. jack_shmsize_t entry_len; /* size of registry entry */
  113. jack_shm_server_t server[MAX_SERVERS]; /* current server array */
  114. }
  115. jack_shm_header_t;
  116. typedef struct _jack_shm_registry {
  117. jack_shm_registry_index_t index; /* offset into the registry */
  118. #ifdef WIN32
  119. int allocator; /* PID that created shm segment */
  120. #else
  121. pid_t allocator; /* PID that created shm segment */
  122. #endif
  123. jack_shmsize_t size; /* for POSIX unattach */
  124. jack_shm_id_t id; /* API specific, see above */
  125. #ifdef __ANDROID__
  126. jack_shm_fd_t fd;
  127. #endif
  128. }
  129. jack_shm_registry_t;
  130. #define JACK_SHM_REGISTRY_SIZE (sizeof (jack_shm_header_t) \
  131. + sizeof (jack_shm_registry_t) * MAX_SHM_ID)
  132. /**
  133. * a structure holding information about shared memory
  134. * allocated by JACK. this version is valid only
  135. * for a given address space. It contains a pointer
  136. * indicating where the shared memory has been
  137. * attached to the address space.
  138. */
  139. PRE_PACKED_STRUCTURE
  140. struct _jack_shm_info {
  141. jack_shm_registry_index_t index; /* offset into the registry */
  142. uint32_t size;
  143. #ifdef __ANDROID__
  144. jack_shm_fd_t fd;
  145. #endif
  146. union {
  147. void *attached_at; /* address where attached */
  148. char ptr_size[8];
  149. } ptr; /* a "pointer" that has the same 8 bytes size when compling in 32 or 64 bits */
  150. } POST_PACKED_STRUCTURE;
  151. typedef struct _jack_shm_info jack_shm_info_t;
  152. /* utility functions used only within JACK */
  153. void jack_shm_copy_from_registry (jack_shm_info_t*,
  154. jack_shm_registry_index_t);
  155. void jack_shm_copy_to_registry (jack_shm_info_t*,
  156. jack_shm_registry_index_t*);
  157. int jack_release_shm_info (jack_shm_registry_index_t);
  158. char* jack_shm_addr (jack_shm_info_t* si);
  159. // here begin the API
  160. int jack_register_server (const char *server_name, int new_registry);
  161. int jack_unregister_server (const char *server_name);
  162. int jack_initialize_shm (const char *server_name);
  163. int jack_initialize_shm_server (void);
  164. int jack_initialize_shm_client (void);
  165. int jack_cleanup_shm (void);
  166. int jack_shmalloc (const char *shm_name, jack_shmsize_t size,
  167. jack_shm_info_t* result);
  168. void jack_release_shm (jack_shm_info_t*);
  169. void jack_release_lib_shm (jack_shm_info_t*);
  170. void jack_destroy_shm (jack_shm_info_t*);
  171. int jack_attach_shm (jack_shm_info_t*);
  172. int jack_attach_lib_shm (jack_shm_info_t*);
  173. int jack_attach_shm_read (jack_shm_info_t*);
  174. int jack_attach_lib_shm_read (jack_shm_info_t*);
  175. int jack_resize_shm (jack_shm_info_t*, jack_shmsize_t size);
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* __jack_shm_h__ */