1.\" $NetBSD: mpool.3,v 1.10 2010/12/16 11:49:35 jruoho Exp $ 2.\" 3.\" Copyright (c) 1990, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)mpool.3 8.1 (Berkeley) 6/4/93 31.\" 32.Dd December 16, 2010 33.Dt MPOOL 3 34.Os 35.Sh NAME 36.Nm mpool , 37.Nm mpool_open , 38.Nm mpool_filter , 39.Nm mpool_new , 40.Nm mpool_get , 41.Nm mpool_put , 42.Nm mpool_sync , 43.Nm mpool_close 44.Nd shared memory buffer pool 45.Sh SYNOPSIS 46.In db.h 47.In mpool.h 48.Ft MPOOL * 49.Fn mpool_open "DBT *key" "int fd" "pgno_t pagesize" "pgno_t maxcache" 50.Ft void 51.Fn mpool_filter "MPOOL *mp" "void (*pgin)(void *, pgno_t, void *)" \ 52"void (*pgout)(void *, pgno_t, void *)" "void *pgcookie" 53.Ft void * 54.Fn mpool_new "MPOOL *mp" "pgno_t *pgnoaddr" 55.Ft void * 56.Fn mpool_get "MPOOL *mp" "pgno_t pgno" "u_int flags" 57.Ft int 58.Fn mpool_put "MPOOL *mp" "void *pgaddr" "u_int flags" 59.Ft int 60.Fn mpool_sync "MPOOL *mp" 61.Ft int 62.Fn mpool_close "MPOOL *mp" 63.Sh DESCRIPTION 64.Nm 65is the library interface intended to provide page oriented buffer 66management of files. 67The buffers may be shared between processes. 68.Pp 69The function 70.Fn mpool_open 71initializes a memory pool. 72The 73.Fa key 74argument is the byte string used to negotiate between multiple 75processes wishing to share buffers. 76If the file buffers are mapped in shared memory, all processes using 77the same key will share the buffers. 78If 79.Fa key 80is 81.Dv NULL , 82the buffers are mapped into private memory. 83The 84.Fa fd 85argument is a file descriptor for the underlying file, which must be 86seekable. 87If 88.Fa key 89is 90.No non- Ns Dv NULL 91and matches a file already being mapped, the 92.Fa fd 93argument is ignored. 94.Pp 95The 96.Fa pagesize 97argument is the size, in bytes, of the pages into which the file is 98broken up. 99The 100.Fa maxcache 101argument is the maximum number of pages from the underlying file to 102cache at any one time. 103This value is not relative to the number of processes which share a 104file's buffers, but will be the largest value specified by any of the 105processes sharing the file. 106.Pp 107The 108.Fn mpool_filter 109function is intended to make transparent input and output processing 110of the pages possible. 111If the 112.Fa pgin 113function is specified, it is called each time a buffer is read into 114the memory pool from the backing file. 115If the 116.Fa pgout 117function is specified, it is called each time a buffer is written into 118the backing file. 119Both functions are are called with the 120.Fa pgcookie 121pointer, the page number and a pointer to the page to being read or 122written. 123.Pp 124The function 125.Fn mpool_new 126takes an MPOOL pointer and an address as arguments. 127If a new page can be allocated, a pointer to the page is returned and 128the page number is stored into the 129.Fa pgnoaddr 130address. 131Otherwise, 132.Dv NULL 133is returned and errno is set. 134.Pp 135The function 136.Fn mpool_get 137takes a MPOOL pointer and a page number as arguments. 138If the page exists, a pointer to the page is returned. 139Otherwise, 140.Dv NULL 141is returned and errno is set. 142The flags parameter is not currently used. 143.Pp 144The function 145.Fn mpool_put 146unpins the page referenced by 147.Fa pgaddr . 148.Fa pgaddr 149must be an address previously returned by 150.Fn mpool_get 151or 152.Fn mpool_new . 153The flag value is specified by or'ing any of the following values: 154.Bl -tag -width MPOOL_DIRTYX -offset indent 155.It Dv MPOOL_DIRTY 156The page has been modified and needs to be written to the backing 157file. 158.El 159.Pp 160.Fn mpool_put 161returns 0 on success and \-1 if an error occurs. 162.Pp 163The function 164.Fn mpool_sync 165writes all modified pages associated with the MPOOL pointer to the 166backing file. 167.Fn mpool_sync 168returns 0 on success and \-1 if an error occurs. 169.Pp 170The 171.Fn mpool_close 172function frees up any allocated memory associated with the memory pool 173cookie. 174Modified pages are 175.Em not 176written to the backing file. 177.Fn mpool_close 178returns 0 on success and \-1 if an error occurs. 179.Sh ERRORS 180The 181.Fn mpool_open 182function may fail and set 183.Va errno 184for any of the errors specified for the library routine 185.Xr malloc 3 . 186.Pp 187The 188.Fn mpool_get 189function may fail and set 190.Va errno 191for the following: 192.Bl -tag -width Er -offset indent 193.It Er EINVAL 194The requested record doesn't exist. 195.El 196.Pp 197The 198.Fn mpool_new 199and 200.Fn mpool_get 201functions may fail and set 202.Va errno 203for any of the errors specified for the library routines 204.Xr read 2 , 205.Xr write 2 , 206and 207.Xr malloc 3 . 208.Pp 209The 210.Fn mpool_sync 211function may fail and set 212.Va errno 213for any of the errors specified for the library routine 214.Xr write 2 . 215.Pp 216The 217.Fn mpool_close 218function may fail and set 219.Va errno 220for any of the errors specified for the library routine 221.Xr free 3 . 222.Sh SEE ALSO 223.Xr btree 3 , 224.Xr dbopen 3 , 225.Xr hash 3 , 226.Xr recno 3 227