1.\"	$OpenBSD: mpool.3,v 1.14 2005/07/17 09:10:36 jaredy 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 February 25, 1999
33.Dt MPOOL 3
34.Os
35.Sh NAME
36.Nm mpool_open ,
37.Nm mpool_filter ,
38.Nm mpool_new ,
39.Nm mpool_delete ,
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.Fd #include <db.h>
47.Fd #include <mpool.h>
48.Ft MPOOL *
49.Fn mpool_open "void *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" "unsigned int flags"
55.Ft int
56.Fn mpool_delete "MPOOL *mp" "void *page"
57.Ft void *
58.Fn mpool_get "MPOOL *mp" "pgno_t pgno" "unsigned int flags"
59.Ft int
60.Fn mpool_put "MPOOL *mp" "void *pgaddr" "unsigned int flags"
61.Ft int
62.Fn mpool_sync "MPOOL *mp"
63.Ft int
64.Fn mpool_close "MPOOL *mp"
65.Sh DESCRIPTION
66.Nm mpool
67is the library interface intended to provide page-oriented buffer management
68of files.
69The buffers may be shared between processes.
70.Pp
71The function
72.Fn mpool_open
73initializes a memory pool.
74The
75.Fa key
76argument is the byte string used to negotiate between multiple
77processes wishing to share buffers.
78If the file buffers are mapped in shared memory, all processes using
79the same key will share the buffers.
80If
81.Fa key
82is
83.Dv NULL ,
84the buffers are mapped into private memory.
85The
86.Fa fd
87argument is a file descriptor for the underlying file, which must be seekable.
88If
89.Fa key
90is non-NULL and matches a file already being mapped, the
91.Fa fd
92argument is ignored.
93.Pp
94The
95.Fa pagesize
96argument is the size, in bytes, of the pages into which the file is broken up.
97The
98.Fa maxcache
99argument is the maximum number of pages from the underlying file to cache
100at any one time.
101This value is not relative to the number of processes which share a file's
102buffers, but will be the largest value specified by any of the processes
103sharing the file.
104.Pp
105The
106.Fn mpool_filter
107function is intended to make transparent input and output processing of the
108pages possible.
109If the
110.Fa pgin
111function is specified, it is called each time a buffer is read into the memory
112pool from the backing file.
113If the
114.Fa pgout
115function is specified, it is called each time a buffer is written into the
116backing file.
117Both functions are called with the
118.Fa pgcookie
119pointer, the page number, and a pointer to the page being read or written.
120.Pp
121The function
122.Fn mpool_new
123takes an
124.Dv MPOOL
125pointer, an address, and a set of flags as arguments.
126If a new page can be allocated, a pointer to the page is returned and
127the page number is stored into the
128.Fa pgnoaddr
129address.
130Otherwise,
131.Dv NULL
132is returned and
133.Va errno
134is set.
135The flags value is formed by
136.Tn OR Ns 'ing
137the following values:
138.Bl -tag -width Ds
139.It Dv MPOOL_PAGE_REQUEST
140Allocate a new page with a specific page number.
141.It Dv MPOOL_PAGE_NEXT
142Allocate a new page with the next page number.
143.El
144.Pp
145The function
146.Fn mpool_delete
147deletes the specified page from a pool and frees the page.
148It takes an
149.Dv MPOOL
150pointer and a page as arguments.
151The page must have been generated by
152.Fn mpool_new .
153.Pp
154The function
155.Fn mpool_get
156takes an
157.Dv MPOOL
158pointer and a page number as arguments.
159If the page exists, a pointer to the page is returned.
160Otherwise,
161.Dv NULL
162is returned and
163.Va errno
164is set.
165The flags parameter is not currently used.
166.Pp
167The function
168.Fn mpool_put
169unpins the page referenced by
170.Fa pgaddr .
171.Fa pgaddr
172must be an address previously returned by
173.Fn mpool_get
174or
175.Fn mpool_new .
176The flags value is formed by
177.Tn OR Ns 'ing
178the following values:
179.Bl -tag -width Ds
180.It Dv MPOOL_DIRTY
181The page has been modified and needs to be written to the backing file.
182.El
183.Pp
184.Fn mpool_put
185returns 0 on success and \-1 if an error occurs.
186.Pp
187The function
188.Fn mpool_sync
189writes all modified pages associated with the
190.Dv MPOOL
191pointer to the backing file.
192.Fn mpool_sync
193returns 0 on success and \-1 if an error occurs.
194.Pp
195The
196.Fn mpool_close
197function frees up any allocated memory associated with the memory pool
198cookie.
199Modified pages are
200.Em not
201written to the backing file.
202.Fn mpool_close
203returns 0 on success and \-1 if an error occurs.
204.Sh ERRORS
205The
206.Fn mpool_open
207function may fail and set
208.Va errno
209for any of the errors specified for the library routines
210.Xr fstat 2
211and
212.Xr malloc 3 ,
213or the following:
214.Bl -tag -width Er
215.It Bq Er ESPIPE
216The given file descriptor is a pipe.
217.El
218.Pp
219The
220.Fn mpool_get
221function may fail and set
222.Va errno
223for the following:
224.Bl -tag -width Er
225.It Bq Er EINVAL
226The requested record doesn't exist.
227.El
228.Pp
229The
230.Fn mpool_new
231and
232.Fn mpool_get
233functions may fail and set
234.Va errno
235for any of the errors specified for the library routines
236.Xr pread 2 ,
237.Xr pwrite 2 ,
238and
239.Xr malloc 3 .
240.Pp
241The
242.Fn mpool_sync
243function may fail and set
244.Va errno
245for any of the errors specified for the library routine
246.Xr pwrite 2 .
247.Pp
248The
249.Fn mpool_close
250function may fail and set
251.Va errno
252for any of the errors specified for the library routine
253.Xr free 3 .
254.Pp
255The
256.Fn mpool_delete
257always acts as if it succeeded.
258.Sh SEE ALSO
259.Xr btree 3 ,
260.Xr dbopen 3 ,
261.Xr hash 3 ,
262.Xr recno 3
263