Annotation of freem/src/shmmgr.h, revision 1.5

1.1       snw         1: /*
1.5     ! snw         2:  *   $Id: shmmgr.h,v 1.4 2025/03/26 15:17:12 snw Exp $
1.1       snw         3:  *    shared memory manager data structures
                      4:  *
                      5:  *  
1.2       snw         6:  *   Author: Serena Willis <snw@coherent-logic.com>
1.1       snw         7:  *    Copyright (C) 1998 MUG Deutschland
1.3       snw         8:  *    Copyright (C) 2021, 2025 Coherent Logic Development LLC
1.1       snw         9:  *
                     10:  *
                     11:  *   This file is part of FreeM.
                     12:  *
                     13:  *   FreeM is free software: you can redistribute it and/or modify
                     14:  *   it under the terms of the GNU Affero Public License as published by
                     15:  *   the Free Software Foundation, either version 3 of the License, or
                     16:  *   (at your option) any later version.
                     17:  *
                     18:  *   FreeM is distributed in the hope that it will be useful,
                     19:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     20:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     21:  *   GNU Affero Public License for more details.
                     22:  *
                     23:  *   You should have received a copy of the GNU Affero Public License
                     24:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
                     25:  *
1.4       snw        26:  *   $Log: shmmgr.h,v $
1.5     ! snw        27:  *   Revision 1.4  2025/03/26 15:17:12  snw
        !            28:  *   Fall back to global-backed SSVNs when memory-backed globals fail in attempt to fix Tru64
        !            29:  *
1.4       snw        30:  *   Revision 1.3  2025/03/09 19:50:47  snw
                     31:  *   Second phase of REUSE compliance and header reformat
                     32:  *
1.3       snw        33:  *
                     34:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     35:  * SPDX-License-Identifier: AGPL-3.0-or-later
1.1       snw        36:  **/
                     37: 
                     38: #if !defined(__shmmgr_h)
                     39: # define __shmmgr_h
                     40: 
                     41: #include <stdlib.h>
                     42: #if defined(HAVE_STDINT_H)
                     43: # include <stdint.h>
                     44: #endif
                     45: #include <sys/ipc.h>
                     46: #include <sys/shm.h>
                     47: #include <sys/sem.h>
                     48: #include <sys/types.h>
                     49: 
                     50: #include "locktab.h"
                     51: #include "jobtab.h"
                     52: 
                     53: /* allocation states */
                     54: #define PG_FREE     0
                     55: #define PG_ALLOC    1
                     56: 
                     57: /* size of a page */
                     58: #define PG_SIZE 1024
                     59: 
                     60: /* shm_init return values */
                     61: #define SHMS_SUCCESS 1
                     62: #define SHMS_GET_ERR 0
                     63: #define SHMS_ATTACH_ERR -1
                     64: 
                     65: #define SEMK_SHM 0xBBDD21
                     66: #define SEMK_LOCKTAB 0xBBDD22
                     67: #define SEMK_JOBTAB 0xBBDD23
                     68: #define SEMK_TP 0xBBDD24
                     69: 
                     70: #define SHMALIGN(a) (void *) (((unsigned long) a + sysconf (_SC_PAGESIZE)) & ~(sysconf (_SC_PAGESIZE) - 1))
                     71: 
1.5     ! snw        72: /* The following macros were borrowed from Reference Standard M
        !            73:  *  SOM/SOA:  reading and writing data stored in shared memory
        !            74:  *  SBM/SBA:  for modifying pointers in shared memory
        !            75:  */
        !            76: #define SOM(ptr)    ((__typeof__(ptr)) ((char *) (ptr) + ((char *) shm_config->dta - (char *) shm_config->hdr->shmad)))
        !            77: #define SOA(ptr)    ((ptr == NULL) ? NULL : SOM(ptr))
        !            78: #define SBM(ptr)    ((__typeof__(ptr)) ((char *) (ptr) - ((char *) shm_config->dta - (char *) shm_config->hdr->shmad)))
        !            79: #define SBA(ptr)    ((ptr == NULL) ? NULL : SBM(ptr))
        !            80: 
1.1       snw        81: /* data structures */
                     82: 
                     83: /* alloc map entry */
                     84: typedef struct shm_page_t {
                     85: 
                     86:     /* is this the first entry for this allocation? */
                     87:     short is_first;
                     88: 
                     89:     /* can be PG_FREE or PG_ALLOC */
                     90:     short pg_state;
                     91: 
                     92:     /* to which pid does this page belong? */
                     93:     pid_t pid;
                     94: 
                     95:     /* is this the last entry for this allocation? */
                     96:     short is_last;
                     97:     
                     98: } shm_page_t;
                     99: 
                    100: 
                    101: /* segment header */
                    102: typedef struct shm_hdr_t {
                    103: 
                    104:     /* will be set to shm_key to identify whether or not the segment is initialized */
                    105:     key_t magic;
                    106: 
                    107:     /* pid of the first process that attached to the segment */
                    108:     pid_t first_process;
                    109: 
                    110:     /* shm address */
                    111:     void *shmad;
                    112: 
                    113:     /* HEAD of lock table linked list */
                    114:     locktab_ent_t *locktab_head;    
                    115: 
                    116:     /* HEAD of job table linked list */
                    117:     job_slot_t *jobtab_head;
                    118: 
                    119:     /* locks out non-fmadm processes when set to 1 */
                    120:     short maintenance_mode;
                    121: 
                    122:     unsigned long max_locks;
                    123:     unsigned long max_jobs;
                    124:     unsigned long max_ipcs;
                    125: 
                    126:     /* shared symbol table */
                    127:     char *partition; 
                    128:     unsigned long alphptr[128];
                    129:     char *s;
                    130:     char *argptr;
                    131:     long PSIZE;
                    132:     long symlen;
1.4       snw       133:     short use_mb_globals;
1.1       snw       134:     
                    135:     /* pid of process currently in transaction */
                    136:     /* zero if nobody owns these exclusive rights */
                    137:     pid_t tp_owner;
                    138: 
                    139:     /* counter for the tp semaphore */
                    140:     unsigned long long tp_semctr;
                    141:     
                    142:     /* monotonically-incrementing serial number for DB operations:
                    143:      * all ops inside of transactions have the same tp_serial_number.
                    144:      *
                    145:      * used in journaling.
                    146:      */
                    147:     unsigned long long tp_serial_number;
                    148: 
                    149:     /* allocation map */
                    150:     shm_page_t alloc_map[1];
                    151:     
                    152: } shm_hdr_t;
                    153: 
                    154: typedef struct shm_config_t {
                    155: 
                    156:     /* segment size */
                    157:     size_t segsiz;
                    158: 
                    159:     /* page size */
                    160:     size_t pgsiz;
                    161: 
                    162:     /* page count */
                    163:     int pgct;
                    164: 
                    165:     /* shared memory key */
                    166:     key_t key;
                    167: 
                    168:     /* segment ID */
                    169:     int seg_id;
                    170: 
                    171:     /* segment data area */
                    172:     void *dta;
                    173: 
                    174:     /* pointer to the header */
                    175:     shm_hdr_t *hdr;
                    176:     
                    177:     /* actual buffer */
                    178:     void *buf;
                    179:     
                    180: } shm_config_t;
                    181: 
                    182: /* global variables */
                    183: extern shm_config_t *shm_config;
                    184: 
                    185: /* function prototypes */
                    186: extern short shm_init(const size_t seg_size);
                    187: extern short shm_exit(void);
                    188: extern short shm_get_sem(void);
                    189: extern short shm_release_sem(void);
                    190: extern shm_page_t *shm_get_alloc_map_entry(const int page_number);
                    191: extern void *shm_page_num_to_address(const int page_num);
                    192: extern int shm_address_to_page_num(const void *address);
                    193: extern void *shm_alloc_pages(const int page_count);
                    194: extern void *shm_alloc(const size_t bytes);
                    195: extern void shm_free_page(const int page_number);
                    196: extern void shm_free(const void *addr);
                    197: extern void shm_dump(void);
                    198: extern void shm_dump_pages(void);
                    199: #endif
                    200: 
                    201:    

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>