File:  [Coherent Logic Development] / freem / src / shmmgr.h
Revision 1.7: download - view: text, annotated - select for diffs
Thu May 15 04:14:20 2025 UTC (2 months, 2 weeks ago) by snw
Branches: MAIN
CVS tags: HEAD
Remove old SEMK_ macros since ftok is now used throughout to determine semaphore keys

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

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