xref: /netbsd/lib/libc/stdlib/hcreate.3 (revision c4a72b64)
1.\" 	$NetBSD: hcreate.3,v 1.5 2002/10/01 17:29:23 wiz Exp $
2.\"
3.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Klaus Klein.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd February 13, 2001
38.Dt HCREATE 3
39.Os
40.Sh NAME
41.Nm hcreate ,
42.Nm hdestroy ,
43.Nm hsearch
44.Nd manage hash search table
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.Fd #include \*[Lt]search.h\*[Gt]
49.Ft int
50.Fn hcreate "size_t nel"
51.Ft void
52.Fn hdestroy "void"
53.Ft ENTRY *
54.Fn hsearch "ENTRY item" "ACTION action"
55.Sh DESCRIPTION
56The
57.Fn hcreate ,
58.Fn hdestroy
59and
60.Fn hsearch
61functions manage hash search tables.
62.Pp
63The
64.Fn hcreate
65function allocates and initializes the table.
66The
67.Fa nel
68argument specifies an estimate of the maximum number of entries to be held
69by the table.
70Unless further memory allocation fails, supplying an insufficient
71.Fa nel
72value will not result in functional harm, although a performance degradation
73may occur.
74Initialization using the
75.Fn hcreate
76function is mandatory prior to any access operations using
77.Fn hsearch .
78.Pp
79The
80.Fn hdestroy
81function destroys a table previously created using
82.Fn hcreate .
83After a call to
84.Fn hdestroy ,
85the data can no longer be accessed.
86.Pp
87The
88.Fn hsearch
89function is used to search to the hash table.
90It returns a pointer into the
91hash table indicating the address of an item.
92The
93.Fa item
94argument is of type
95.Dv ENTRY ,
96a structural type which contains the following members:
97.Bl -tag -compact -offset indent -width voidX*dataXX
98.It Fa char *key
99comparison key.
100.It Fa void *data
101pointer to data associated with
102.Fa key .
103.El
104.Pp
105The key comparison function used by
106.Fn hsearch
107is
108.Xr strcmp 3 .
109.Pp
110The
111.Fa action
112argument is of type
113.Dv ACTION ,
114an enumeration type which defines the following values:
115.Bl -tag -compact -offset indent -width ENTERXX
116.It Dv ENTER
117Insert
118.Fa item
119into the hash table.
120If an existing item with the same key is found, it is not replaced.
121Note that the
122.Fa key
123and
124.Fa data
125elements of
126.Fa item
127are used directly by the new table entry.
128The storage for the
129key must not be modified during the lifetime of the hash table.
130.It Dv FIND
131Search the hash table without inserting
132.Fa item .
133.El
134.Sh RETURN VALUES
135If successful, the
136.Fn hcreate
137function returns a non-zero value.
138Otherwise, a value of 0 is returned and
139.Va errno
140is set to indicate the error.
141.Pp
142The
143.Fn hdestroy
144functions
145returns no value.
146.Pp
147If successful, the
148.Fn hsearch
149function returns a pointer to hash table entry matching
150the provided key.
151If the action is
152.Dv FIND
153and the item was not found, or if the action is
154.Dv ENTER
155and the insertion failed,
156.Dv NULL
157is returned and
158.Va errno
159is set to indicate the error.
160If the action is
161.Dv ENTER
162and an entry already existed in the table matching the given
163key, the existing entry is returned and is not replaced.
164.Sh ERRORS
165The
166.Fn hcreate
167and
168.Fn hsearch
169functions will fail if:
170.Bl -tag -width Er
171.It Bq Er ENOMEM
172Insufficient memory is available.
173.El
174.Sh SEE ALSO
175.Xr bsearch 3 ,
176.Xr lsearch 3 ,
177.Xr malloc 3 ,
178.Xr strcmp 3
179.Sh STANDARDS
180The
181.Fn hcreate ,
182.Fn hdestroy
183and
184.Fn hsearch
185functions conform to
186.St -xpg4.2 .
187.Sh HISTORY
188The
189.Fn hcreate ,
190.Fn hdestroy
191and
192.Fn hsearch
193functions first appeared in
194.At V .
195.Sh BUGS
196The interface permits the use of only one hash table at a time.
197