1*f0374f15Sjmc.\" $OpenBSD: hcreate.3,v 1.8 2018/01/30 11:37:58 jmc Exp $ 27ea1c5d1Sray.\" $NetBSD: hcreate.3,v 1.8 2010/05/01 06:18:03 jruoho Exp $ 346f8fdc8Smillert.\" 446f8fdc8Smillert.\" Copyright (c) 1999 The NetBSD Foundation, Inc. 546f8fdc8Smillert.\" All rights reserved. 646f8fdc8Smillert.\" 746f8fdc8Smillert.\" This code is derived from software contributed to The NetBSD Foundation 846f8fdc8Smillert.\" by Klaus Klein. 946f8fdc8Smillert.\" 1046f8fdc8Smillert.\" Redistribution and use in source and binary forms, with or without 1146f8fdc8Smillert.\" modification, are permitted provided that the following conditions 1246f8fdc8Smillert.\" are met: 1346f8fdc8Smillert.\" 1. Redistributions of source code must retain the above copyright 1446f8fdc8Smillert.\" notice, this list of conditions and the following disclaimer. 1546f8fdc8Smillert.\" 2. Redistributions in binary form must reproduce the above copyright 1646f8fdc8Smillert.\" notice, this list of conditions and the following disclaimer in the 1746f8fdc8Smillert.\" documentation and/or other materials provided with the distribution. 1846f8fdc8Smillert.\" 1946f8fdc8Smillert.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2046f8fdc8Smillert.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2146f8fdc8Smillert.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2246f8fdc8Smillert.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2346f8fdc8Smillert.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2446f8fdc8Smillert.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2546f8fdc8Smillert.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2646f8fdc8Smillert.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2746f8fdc8Smillert.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2846f8fdc8Smillert.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2946f8fdc8Smillert.\" POSSIBILITY OF SUCH DAMAGE. 3046f8fdc8Smillert.\" 31*f0374f15Sjmc.Dd $Mdocdate: January 30 2018 $ 3246f8fdc8Smillert.Dt HCREATE 3 3346f8fdc8Smillert.Os 3446f8fdc8Smillert.Sh NAME 3546f8fdc8Smillert.Nm hcreate , 3646f8fdc8Smillert.Nm hdestroy , 3746f8fdc8Smillert.Nm hsearch 3846f8fdc8Smillert.Nd manage hash search table 3946f8fdc8Smillert.Sh SYNOPSIS 4046f8fdc8Smillert.In search.h 4146f8fdc8Smillert.Ft int 4246f8fdc8Smillert.Fn hcreate "size_t nel" 4346f8fdc8Smillert.Ft void 4446f8fdc8Smillert.Fn hdestroy "void" 4546f8fdc8Smillert.Ft ENTRY * 4646f8fdc8Smillert.Fn hsearch "ENTRY item" "ACTION action" 4746f8fdc8Smillert.Sh DESCRIPTION 4846f8fdc8SmillertThe 4946f8fdc8Smillert.Fn hcreate , 503f82fb3dSjaredy.Fn hdestroy , 5146f8fdc8Smillertand 5246f8fdc8Smillert.Fn hsearch 5346f8fdc8Smillertfunctions manage hash search tables. 5446f8fdc8Smillert.Pp 5546f8fdc8SmillertThe 5646f8fdc8Smillert.Fn hcreate 5746f8fdc8Smillertfunction allocates and initializes the table. 5846f8fdc8SmillertThe 5946f8fdc8Smillert.Fa nel 6046f8fdc8Smillertargument specifies an estimate of the maximum number of entries to be held 6146f8fdc8Smillertby the table. 6246f8fdc8SmillertUnless further memory allocation fails, supplying an insufficient 6346f8fdc8Smillert.Fa nel 6446f8fdc8Smillertvalue will not result in functional harm, although a performance degradation 6546f8fdc8Smillertmay occur. 6646f8fdc8SmillertInitialization using the 6746f8fdc8Smillert.Fn hcreate 6846f8fdc8Smillertfunction is mandatory prior to any access operations using 6946f8fdc8Smillert.Fn hsearch . 7046f8fdc8Smillert.Pp 7146f8fdc8SmillertThe 7246f8fdc8Smillert.Fn hdestroy 7346f8fdc8Smillertfunction destroys a table previously created using 7446f8fdc8Smillert.Fn hcreate . 7546f8fdc8SmillertAfter a call to 7646f8fdc8Smillert.Fn hdestroy , 7746f8fdc8Smillertthe data can no longer be accessed. 7846f8fdc8Smillert.Pp 7946f8fdc8SmillertThe 8046f8fdc8Smillert.Fn hsearch 81*f0374f15Sjmcfunction is used to search the hash table. 8246f8fdc8SmillertIt returns a pointer into the 8346f8fdc8Smillerthash table indicating the address of an item. 8446f8fdc8SmillertThe 8546f8fdc8Smillert.Fa item 8646f8fdc8Smillertargument is of type 877ea1c5d1Sray.Vt ENTRY , 887ea1c5d1Sraydefined in the 897ea1c5d1Sray.In search.h 907ea1c5d1Srayheader. 917ea1c5d1SrayThis is a structure type that contains two pointers: 923f82fb3dSjaredy.Pp 937ea1c5d1Sray.Bl -tag -compact -offset indent -width "void *data " 9446f8fdc8Smillert.It Fa char *key 957ea1c5d1Sraycomparison key 9646f8fdc8Smillert.It Fa void *data 9746f8fdc8Smillertpointer to data associated with 987ea1c5d1Sray.Fa key 9946f8fdc8Smillert.El 10046f8fdc8Smillert.Pp 10146f8fdc8SmillertThe key comparison function used by 10246f8fdc8Smillert.Fn hsearch 10346f8fdc8Smillertis 10446f8fdc8Smillert.Xr strcmp 3 . 10546f8fdc8Smillert.Pp 10646f8fdc8SmillertThe 10746f8fdc8Smillert.Fa action 10846f8fdc8Smillertargument is of type 1097ea1c5d1Sray.Vt ACTION , 11046f8fdc8Smillertan enumeration type which defines the following values: 1113f82fb3dSjaredy.Bl -tag -offset indent -width ENTERXX 11246f8fdc8Smillert.It Dv ENTER 11346f8fdc8SmillertInsert 11446f8fdc8Smillert.Fa item 11546f8fdc8Smillertinto the hash table. 11646f8fdc8SmillertIf an existing item with the same key is found, it is not replaced. 11746f8fdc8SmillertNote that the 11846f8fdc8Smillert.Fa key 11946f8fdc8Smillertand 12046f8fdc8Smillert.Fa data 12146f8fdc8Smillertelements of 12246f8fdc8Smillert.Fa item 12346f8fdc8Smillertare used directly by the new table entry. 12446f8fdc8SmillertThe storage for the 12546f8fdc8Smillertkey must not be modified during the lifetime of the hash table. 12646f8fdc8Smillert.It Dv FIND 12746f8fdc8SmillertSearch the hash table without inserting 12846f8fdc8Smillert.Fa item . 12946f8fdc8Smillert.El 1307ea1c5d1Sray.Pp 1317ea1c5d1SrayNote that the comparison 1327ea1c5d1Sray.Fa key 1337ea1c5d1Sraymust be allocated using 1347ea1c5d1Sray.Xr malloc 3 1357ea1c5d1Srayor 1367ea1c5d1Sray.Xr calloc 3 1377ea1c5d1Srayif action is 1387ea1c5d1Sray.Dv ENTER 1397ea1c5d1Srayand 1407ea1c5d1Sray.Fn hdestroy 1417ea1c5d1Sraywill be called. 1427ea1c5d1SrayThis is because 1437ea1c5d1Sray.Fn hdestroy 1447ea1c5d1Sraywill call 1457ea1c5d1Sray.Xr free 3 1467ea1c5d1Srayfor each comparison 1477ea1c5d1Sray.Fa key 1487ea1c5d1Sray(but not 1497ea1c5d1Sray.Fa data ) . 1507ea1c5d1SrayTypically the comparison 1517ea1c5d1Sray.Fa key 1527ea1c5d1Srayis allocated by using 1537ea1c5d1Sray.Xr strdup 3 . 15446f8fdc8Smillert.Sh RETURN VALUES 15546f8fdc8SmillertIf successful, the 15646f8fdc8Smillert.Fn hcreate 15746f8fdc8Smillertfunction returns a non-zero value. 15846f8fdc8SmillertOtherwise, a value of 0 is returned and 15946f8fdc8Smillert.Va errno 16046f8fdc8Smillertis set to indicate the error. 16146f8fdc8Smillert.Pp 16246f8fdc8SmillertIf successful, the 16346f8fdc8Smillert.Fn hsearch 1643f82fb3dSjaredyfunction returns a pointer to a hash table entry matching 16546f8fdc8Smillertthe provided key. 16646f8fdc8SmillertIf the action is 16746f8fdc8Smillert.Dv FIND 16846f8fdc8Smillertand the item was not found, or if the action is 16946f8fdc8Smillert.Dv ENTER 17046f8fdc8Smillertand the insertion failed, 17146f8fdc8Smillert.Dv NULL 17246f8fdc8Smillertis returned and 17346f8fdc8Smillert.Va errno 17446f8fdc8Smillertis set to indicate the error. 17546f8fdc8SmillertIf the action is 17646f8fdc8Smillert.Dv ENTER 17746f8fdc8Smillertand an entry already existed in the table matching the given 17846f8fdc8Smillertkey, the existing entry is returned and is not replaced. 17946f8fdc8Smillert.Sh ERRORS 18046f8fdc8SmillertThe 18146f8fdc8Smillert.Fn hcreate 18246f8fdc8Smillertand 18346f8fdc8Smillert.Fn hsearch 18446f8fdc8Smillertfunctions will fail if: 18546f8fdc8Smillert.Bl -tag -width Er 18646f8fdc8Smillert.It Bq Er ENOMEM 18746f8fdc8SmillertInsufficient memory is available. 18846f8fdc8Smillert.El 18946f8fdc8Smillert.Sh SEE ALSO 19046f8fdc8Smillert.Xr bsearch 3 , 19146f8fdc8Smillert.Xr lsearch 3 , 19246f8fdc8Smillert.Xr malloc 3 , 19346f8fdc8Smillert.Xr strcmp 3 19446f8fdc8Smillert.Sh STANDARDS 1957ea1c5d1SrayThe 1967ea1c5d1Sray.Fn hcreate , 1977ea1c5d1Sray.Fn hdestroy 1987ea1c5d1Srayand 1997ea1c5d1Sray.Fn hsearch 2007ea1c5d1Srayfunctions conform to 2017ea1c5d1Sray.St -xpg4.2 . 20246f8fdc8Smillert.Sh HISTORY 20346f8fdc8SmillertThe 20446f8fdc8Smillert.Fn hcreate , 20546f8fdc8Smillert.Fn hdestroy 20646f8fdc8Smillertand 20746f8fdc8Smillert.Fn hsearch 20846f8fdc8Smillertfunctions first appeared in 20946f8fdc8Smillert.At V . 2107ea1c5d1Sray.Sh CAVEATS 2117ea1c5d1SrayAt least the following limitations can be mentioned: 2127ea1c5d1Sray.Bl -bullet 2137ea1c5d1Sray.It 21446f8fdc8SmillertThe interface permits the use of only one hash table at a time. 2157ea1c5d1Sray.It 2167ea1c5d1SrayIndividual hash table entries can be added, but not deleted. 2177ea1c5d1Sray.It 2187ea1c5d1SrayThe standard is indecipherable about the 2197ea1c5d1Srayinternal memory usage of the functions, 2207ea1c5d1Sraymentioning only that 2217ea1c5d1Sray.Do 2227ea1c5d1Sray.Fn hcreate 2237ea1c5d1Srayand 2247ea1c5d1Sray.Fn hsearch 2257ea1c5d1Srayfunctions may use 2267ea1c5d1Sray.Fn malloc 2277ea1c5d1Srayto allocate space 2287ea1c5d1Sray.Dc . 2297ea1c5d1SrayThis limits the portability of the functions, 2307ea1c5d1Sraygiven that other implementations may not 2317ea1c5d1Sray.Xr free 3 2327ea1c5d1Sraythe buffer pointed by 2337ea1c5d1Sray.Fa key . 2347ea1c5d1Sray.El 235