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.12 (Sleepycat) 7/6/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 LOCKINIT(dbmp, &dbmp->mutex);
60 LIST_INIT(&dbmp->dbregq);
61 TAILQ_INIT(&dbmp->dbmfq);
65 /* Decide if it's possible for anyone else to access the pool. */
66 if ((dbenv == NULL && path == NULL) ||
67 (dbenv != NULL && F_ISSET(dbenv, DB_MPOOL_PRIVATE)))
68 F_SET(dbmp, MP_ISPRIVATE);
71 * Map in the region. We do locking regardless, as portions of it are
72 * implemented in common code (if we put the region in a file, that is).
74 F_SET(dbmp, MP_LOCKREGION);
75 if ((ret = __memp_ropen(dbmp, path, cachesize, mode, flags)) != 0)
77 F_CLR(dbmp, MP_LOCKREGION);
80 * If there's concurrent access, then we have to lock the region.
81 * If it's threaded, then we have to lock both the handles and the
84 if (!F_ISSET(dbmp, MP_ISPRIVATE))
85 F_SET(dbmp, MP_LOCKREGION);
86 if (LF_ISSET(DB_THREAD))
87 F_SET(dbmp, MP_LOCKHANDLE | MP_LOCKREGION);
92 err: if (dbmp != NULL)
93 FREE(dbmp, sizeof(DB_MPOOL));
99 * Close a memory pool.
111 /* Discard DB_MPREGs. */
112 while ((mpreg = LIST_FIRST(&dbmp->dbregq)) != NULL) {
113 LIST_REMOVE(mpreg, q);
114 FREE(mpreg, sizeof(DB_MPREG));
117 /* Discard DB_MPOOLFILEs. */
118 while ((dbmfp = TAILQ_FIRST(&dbmp->dbmfq)) != NULL)
119 if ((t_ret = memp_fclose(dbmfp)) != 0 && ret == 0)
122 /* Close the region. */
123 if ((t_ret = __memp_rclose(dbmp)) && ret == 0)
126 /* Free the structure. */
127 FREE(dbmp, sizeof(DB_MPOOL));
134 * Exit a memory pool.
137 memp_unlink(path, force, dbenv)
142 return (__db_runlink(dbenv,
143 DB_APP_NONE, path, DB_DEFAULT_MPOOL_FILE, force));
148 * Register a file type's pgin, pgout routines.
151 memp_register(dbmp, ftype, pgin, pgout)
154 int (*pgin) __P((db_pgno_t, void *, DBT *));
155 int (*pgout) __P((db_pgno_t, void *, DBT *));
159 if ((mpr = (DB_MPREG *)malloc(sizeof(DB_MPREG))) == NULL)
167 * Insert at the head. Because we do a linear walk, we'll find
168 * the most recent registry in the case of multiple entries, so
169 * we don't have to check for multiple registries.
171 LOCKHANDLE(dbmp, &dbmp->mutex);
172 LIST_INSERT_HEAD(&dbmp->dbregq, mpr, q);
173 UNLOCKHANDLE(dbmp, &dbmp->mutex);