xref: /netbsd-src/lib/libc/stdlib/hcreate.3 (revision 17dd36da8292193180754d5047c0926dbb56818c)
1.\" 	$NetBSD: hcreate.3,v 1.2 2001/02/14 05:59:56 cgd 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 <search.h>
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.  Unless further memory allocation fails, supplying an
70insufficient
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.  It returns a pointer into the
90hash table indicating the address of an item.  The
91.Fa item
92argument is of type
93.Dv ENTRY ,
94a structural type which contains the following members:
95.Bl -tag -compact -offset indent -width voidX*dataXX
96.It Fa char *key
97comparison key.
98.It Fa void *data
99pointer to data associated with
100.Fa key .
101.El
102.Pp
103The key comparison function used by
104.Fn hsearch
105is
106.Xr strcmp 3 .
107.Pp
108The
109.Fa action
110argument is of type
111.Dv ACTION ,
112an enumeration type which defines the following values:
113.Bl -tag -compact -offset indent -width ENTERXX
114.It Dv ENTER
115Insert
116.Fa item
117into the hash table.
118If an existing item with the same key is found, it is not replaced.
119Note that the
120.Fa key
121and
122.Fa data
123elements of
124.Fa item
125are used directly by the new table entry.  The storage for the
126key must not be modified during the lifetime of the hash table.
127.It Dv FIND
128Search the hash table without inserting
129.Fa item .
130.El
131.Pp
132.Sh RETURN VALUES
133If successful, the
134.Fn hcreate
135function returns a non-zero value.  Otherwise, a value of 0 is returned and
136.Va errno
137is set to indicate the error.
138.Pp
139The
140.Fn hdestroy
141functions
142returns no value.
143.Pp
144If successful, the
145.Fn hsearch
146function returns a pointer to hash table entry matching
147the provided key.
148If the action is
149.Dv FIND
150and the item was not found, or if the action is
151.Dv ENTER
152and the insertion failed,
153.Dv NULL
154is returned and
155.Va errno
156is set to indicate the error.
157If the action is
158.Dv ENTER
159and an entry already existed in the table matching the given
160key, the existing entry is returned and is not replaced.
161.Sh ERRORS
162The
163.Fn hcreate
164and
165.Fn hsearch
166functions will fail if:
167.Bl -tag -width Er
168.It Bq Er ENOMEM
169Insufficient memory is available.
170.El
171.Sh SEE ALSO
172.Xr bsearch 3 ,
173.Xr lsearch 3 ,
174.Xr malloc 3 ,
175.Xr strcmp 3
176.Sh STANDARDS
177The
178.Fn hcreate ,
179.Fn hdestroy
180and
181.Fn hsearch
182functions conform to
183.St -xpg4.2 .
184.Sh BUGS
185.Pp
186The interface permits the use of only one hash table at a time.
187.Sh HISTORY
188The
189.Fn hcreate ,
190.Fn hdestroy
191and
192.Fn hsearch
193functions first appeared in
194.At V .
195