2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
10 static const char sccsid[] = "@(#)mp_open.c 10.13 (Sleepycat) 9/23/97";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
27 #include "common_ext.h"
31 * Initialize and/or join a memory pool.
34 memp_open(path, flags, mode, dbenv, retp)
44 /* Validate arguments. */
46 #define OKFLAGS (DB_CREATE | DB_MPOOL_PRIVATE | DB_NOMMAP | DB_THREAD)
48 #define OKFLAGS (DB_CREATE | DB_MPOOL_PRIVATE | DB_NOMMAP)
50 if ((ret = __db_fchk(dbenv, "memp_open", flags, OKFLAGS)) != 0)
53 /* Extract fields from DB_ENV structure. */
54 cachesize = dbenv == NULL ? 0 : dbenv->mp_size;
56 /* Create and initialize the DB_MPOOL structure. */
57 if ((dbmp = (DB_MPOOL *)calloc(1, sizeof(DB_MPOOL))) == NULL)
59 LIST_INIT(&dbmp->dbregq);
60 TAILQ_INIT(&dbmp->dbmfq);
64 /* Decide if it's possible for anyone else to access the pool. */
65 if ((dbenv == NULL && path == NULL) ||
66 (dbenv != NULL && F_ISSET(dbenv, DB_MPOOL_PRIVATE)))
67 F_SET(dbmp, MP_ISPRIVATE);
71 * HP-UX won't permit mutexes to live in anything but shared memory.
72 * So, we have to instantiate the shared mpool region file on that
73 * architecture, regardless. If this turns out to be a performance
74 * problem, we could probably use anonymous memory instead.
77 F_CLR(dbmp, MP_ISPRIVATE);
81 * Map in the region. We do locking regardless, as portions of it are
82 * implemented in common code (if we put the region in a file, that is).
84 F_SET(dbmp, MP_LOCKREGION);
85 if ((ret = __memp_ropen(dbmp, path, cachesize, mode, flags)) != 0)
87 F_CLR(dbmp, MP_LOCKREGION);
90 * If there's concurrent access, then we have to lock the region.
91 * If it's threaded, then we have to lock both the handles and the
92 * region, and we need to allocate a mutex for that purpose.
94 if (!F_ISSET(dbmp, MP_ISPRIVATE))
95 F_SET(dbmp, MP_LOCKREGION);
96 if (LF_ISSET(DB_THREAD)) {
97 F_SET(dbmp, MP_LOCKHANDLE | MP_LOCKREGION);
99 ret = __memp_ralloc(dbmp,
100 sizeof(db_mutex_t), NULL, &dbmp->mutexp);
103 (void)memp_close(dbmp);
106 LOCKINIT(dbmp, dbmp->mutexp);
112 err: if (dbmp != NULL)
113 FREE(dbmp, sizeof(DB_MPOOL));
119 * Close a memory pool.
131 /* Discard DB_MPREGs. */
132 while ((mpreg = LIST_FIRST(&dbmp->dbregq)) != NULL) {
133 LIST_REMOVE(mpreg, q);
134 FREE(mpreg, sizeof(DB_MPREG));
137 /* Discard DB_MPOOLFILEs. */
138 while ((dbmfp = TAILQ_FIRST(&dbmp->dbmfq)) != NULL)
139 if ((t_ret = memp_fclose(dbmfp)) != 0 && ret == 0)
142 /* Discard thread mutex. */
143 if (F_ISSET(dbmp, MP_LOCKHANDLE)) {
145 __db_shalloc_free(dbmp->addr, dbmp->mutexp);
149 /* Close the region. */
150 if ((t_ret = __memp_rclose(dbmp)) && ret == 0)
153 /* Discard the structure. */
154 FREE(dbmp, sizeof(DB_MPOOL));
161 * Exit a memory pool.
164 memp_unlink(path, force, dbenv)
169 return (__db_runlink(dbenv,
170 DB_APP_NONE, path, DB_DEFAULT_MPOOL_FILE, force));
175 * Register a file type's pgin, pgout routines.
178 memp_register(dbmp, ftype, pgin, pgout)
181 int (*pgin) __P((db_pgno_t, void *, DBT *));
182 int (*pgout) __P((db_pgno_t, void *, DBT *));
186 if ((mpr = (DB_MPREG *)malloc(sizeof(DB_MPREG))) == NULL)
194 * Insert at the head. Because we do a linear walk, we'll find
195 * the most recent registry in the case of multiple entries, so
196 * we don't have to check for multiple registries.
198 LOCKHANDLE(dbmp, dbmp->mutexp);
199 LIST_INSERT_HEAD(&dbmp->dbregq, mpr, q);
200 UNLOCKHANDLE(dbmp, dbmp->mutexp);