Jack2  1.9.10
shm.h
00001 /* This module provides a set of abstract shared memory interfaces
00002  * with support using both System V and POSIX shared memory
00003  * implementations.  The code is divided into three sections:
00004  *
00005  *      - common (interface-independent) code
00006  *      - POSIX implementation
00007  *      - System V implementation
00008  *  - Windows implementation
00009  *
00010  * The implementation used is determined by whether USE_POSIX_SHM was
00011  * set in the ./configure step.
00012  */
00013 
00014 /*
00015  Copyright (C) 2001-2003 Paul Davis
00016  Copyright (C) 2005-2012 Grame
00017 
00018  This program is free software; you can redistribute it and/or modify
00019  it under the terms of the GNU Lesser General Public License as published by
00020  the Free Software Foundation; either version 2.1 of the License, or
00021  (at your option) any later version.
00022 
00023  This program is distributed in the hope that it will be useful,
00024  but WITHOUT ANY WARRANTY; without even the implied warranty of
00025  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026  GNU Lesser General Public License for more details.
00027 
00028  You should have received a copy of the GNU Lesser General Public License
00029  along with this program; if not, write to the Free Software
00030  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00031 
00032  */
00033 
00034 #ifndef __jack_shm_h__
00035 #define __jack_shm_h__
00036 
00037 #include <limits.h>
00038 #include <sys/types.h>
00039 #include "types.h"
00040 #include "JackCompilerDeps.h"
00041 #include "JackConstants.h"
00042 
00043 #define TRUE 1
00044 #define FALSE 0
00045 
00046 #ifdef __cplusplus
00047 extern "C"
00048 {
00049 #endif
00050 
00051 #define MAX_SERVERS 8               /* maximum concurrent servers */
00052 #define MAX_SHM_ID 256              /* generally about 16 per server */
00053 #define JACK_SHM_MAGIC 0x4a41434b       /* shm magic number: "JACK" */
00054 #define JACK_SHM_NULL_INDEX -1          /* NULL SHM index */
00055 #define JACK_SHM_REGISTRY_INDEX -2      /* pseudo SHM index for registry */
00056 
00057 
00058     /* On Mac OS X, SHM_NAME_MAX is the maximum length of a shared memory
00059      * segment name (instead of NAME_MAX or PATH_MAX as defined by the
00060      * standard).
00061      */
00062 #ifdef USE_POSIX_SHM
00063 
00064 #ifndef NAME_MAX
00065 #define NAME_MAX 255
00066 #endif
00067 
00068 #ifndef SHM_NAME_MAX
00069 #define SHM_NAME_MAX NAME_MAX
00070 #endif
00071     typedef char shm_name_t[SHM_NAME_MAX];
00072     typedef shm_name_t jack_shm_id_t;
00073 
00074 #elif WIN32  
00075 #define NAME_MAX 255
00076 #ifndef SHM_NAME_MAX
00077 #define SHM_NAME_MAX NAME_MAX
00078 #endif
00079     typedef char shm_name_t[SHM_NAME_MAX];
00080     typedef shm_name_t jack_shm_id_t;
00081 
00082 #elif __ANDROID__
00083 
00084 #ifndef NAME_MAX
00085 #define NAME_MAX 255
00086 #endif
00087 
00088 #ifndef SHM_NAME_MAX
00089 #define SHM_NAME_MAX NAME_MAX
00090 #endif
00091     typedef char shm_name_t[SHM_NAME_MAX];
00092     typedef shm_name_t jack_shm_id_t;
00093     typedef int jack_shm_fd_t;
00094 
00095 #else
00096     /* System V SHM */
00097     typedef int jack_shm_id_t;
00098 #endif /* SHM type */
00099 
00100     /* shared memory type */
00101     typedef enum {
00102         shm_POSIX = 1,                  /* POSIX shared memory */
00103         shm_SYSV = 2,                   /* System V shared memory */
00104         shm_WIN32 = 3,                  /* Windows 32 shared memory */
00105         shm_ANDROID = 4                 /* Android shared memory */
00106     } jack_shmtype_t;
00107 
00108     typedef int16_t jack_shm_registry_index_t;
00109 
00119     typedef struct _jack_shm_server {
00120 #ifdef WIN32
00121         int     pid;    /* process ID */
00122 #else
00123         pid_t pid;      /* process ID */
00124 #endif
00125 
00126         char    name[JACK_SERVER_NAME_SIZE];
00127     }
00128     jack_shm_server_t;
00129 
00130     typedef struct _jack_shm_header {
00131         uint32_t        magic;  /* magic number */
00132         uint16_t        protocol;       /* JACK protocol version */
00133         jack_shmtype_t  type;   /* shm type */
00134         jack_shmsize_t  size;   /* total registry segment size */
00135         jack_shmsize_t  hdr_len;        /* size of header */
00136         jack_shmsize_t  entry_len; /* size of registry entry */
00137         jack_shm_server_t server[MAX_SERVERS]; /* current server array */
00138     }
00139     jack_shm_header_t;
00140 
00141     typedef struct _jack_shm_registry {
00142         jack_shm_registry_index_t index;     /* offset into the registry */
00143 
00144 #ifdef WIN32
00145         int     allocator; /* PID that created shm segment */
00146 #else
00147         pid_t allocator; /* PID that created shm segment */
00148 #endif
00149 
00150         jack_shmsize_t size;      /* for POSIX unattach */
00151         jack_shm_id_t id;        /* API specific, see above */
00152 #ifdef __ANDROID__
00153         jack_shm_fd_t fd;
00154 #endif
00155     }
00156     jack_shm_registry_t;
00157 
00158 #define JACK_SHM_REGISTRY_SIZE (sizeof (jack_shm_header_t) \
00159                                 + sizeof (jack_shm_registry_t) * MAX_SHM_ID)
00160 
00169     PRE_PACKED_STRUCTURE
00170     struct _jack_shm_info {
00171         jack_shm_registry_index_t index;       /* offset into the registry */
00172         uint32_t size;
00173 #ifdef __ANDROID__
00174         jack_shm_fd_t fd;
00175 #endif
00176         union {
00177             void *attached_at;  /* address where attached */
00178             char ptr_size[8];
00179         } ptr;  /* a "pointer" that has the same 8 bytes size when compling in 32 or 64 bits */
00180     } POST_PACKED_STRUCTURE;
00181     
00182     typedef struct _jack_shm_info jack_shm_info_t;
00183     
00184         /* utility functions used only within JACK */
00185 
00186     void jack_shm_copy_from_registry (jack_shm_info_t*,
00187                 jack_shm_registry_index_t);
00188     void jack_shm_copy_to_registry (jack_shm_info_t*,
00189                                                jack_shm_registry_index_t*);
00190     int jack_release_shm_info (jack_shm_registry_index_t);
00191     char* jack_shm_addr (jack_shm_info_t* si);
00192 
00193     // here begin the API
00194     int jack_register_server (const char *server_name, int new_registry);
00195     int jack_unregister_server (const char *server_name);
00196 
00197     int jack_initialize_shm (const char *server_name);
00198     int jack_initialize_shm_server (void);
00199     int jack_initialize_shm_client (void);
00200     int jack_cleanup_shm (void);
00201 
00202     int jack_shmalloc (const char *shm_name, jack_shmsize_t size,
00203                                   jack_shm_info_t* result);
00204     void jack_release_shm (jack_shm_info_t*);
00205     void jack_release_lib_shm (jack_shm_info_t*);
00206     void jack_destroy_shm (jack_shm_info_t*);
00207     int jack_attach_shm (jack_shm_info_t*);
00208     int jack_attach_lib_shm (jack_shm_info_t*);
00209     int jack_attach_shm_read (jack_shm_info_t*);
00210     int jack_attach_lib_shm_read (jack_shm_info_t*);
00211     int jack_resize_shm (jack_shm_info_t*, jack_shmsize_t size);
00212 
00213 #ifdef __cplusplus
00214 }
00215 #endif
00216 
00217 #endif /* __jack_shm_h__ */