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