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