1*17359e5aSrillig.\" $NetBSD: hcreate.3,v 1.15 2023/02/21 19:27:14 rillig Exp $ 266678fd1Scgd.\" 366678fd1Scgd.\" Copyright (c) 1999 The NetBSD Foundation, Inc. 466678fd1Scgd.\" All rights reserved. 566678fd1Scgd.\" 666678fd1Scgd.\" This code is derived from software contributed to The NetBSD Foundation 766678fd1Scgd.\" by Klaus Klein. 866678fd1Scgd.\" 966678fd1Scgd.\" Redistribution and use in source and binary forms, with or without 1066678fd1Scgd.\" modification, are permitted provided that the following conditions 1166678fd1Scgd.\" are met: 1266678fd1Scgd.\" 1. Redistributions of source code must retain the above copyright 1366678fd1Scgd.\" notice, this list of conditions and the following disclaimer. 1466678fd1Scgd.\" 2. Redistributions in binary form must reproduce the above copyright 1566678fd1Scgd.\" notice, this list of conditions and the following disclaimer in the 1666678fd1Scgd.\" documentation and/or other materials provided with the distribution. 1766678fd1Scgd.\" 1866678fd1Scgd.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1966678fd1Scgd.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2066678fd1Scgd.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2166678fd1Scgd.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2266678fd1Scgd.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2366678fd1Scgd.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2466678fd1Scgd.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2566678fd1Scgd.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2666678fd1Scgd.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2766678fd1Scgd.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2866678fd1Scgd.\" POSSIBILITY OF SUCH DAMAGE. 2966678fd1Scgd.\" 304251f87dSchristos.Dd February 7, 2017 3166678fd1Scgd.Dt HCREATE 3 3266678fd1Scgd.Os 3366678fd1Scgd.Sh NAME 3466678fd1Scgd.Nm hcreate , 3505845f98Schristos.Nm hcreate_r , 3666678fd1Scgd.Nm hdestroy , 37842ee049Schristos.Nm hdestroy1 , 3805845f98Schristos.Nm hdestroy_r , 39842ee049Schristos.Nm hdestroy1_r , 4005845f98Schristos.Nm hsearch , 4105845f98Schristos.Nm hsearch_r 4266678fd1Scgd.Nd manage hash search table 4366678fd1Scgd.Sh LIBRARY 4466678fd1Scgd.Lb libc 4566678fd1Scgd.Sh SYNOPSIS 46472351e1Swiz.In search.h 4766678fd1Scgd.Ft int 4866678fd1Scgd.Fn hcreate "size_t nel" 4905845f98Schristos.Ft int 5005845f98Schristos.Fn hcreate_r "size_t nel" "struct hsearch_data *table" 5166678fd1Scgd.Ft void 5266678fd1Scgd.Fn hdestroy "void" 5305845f98Schristos.Ft void 546030f04aSchristos.Fn hdestroy1 "void (*freekey)(void *)" "void (*freedata)(void *)" 55842ee049Schristos.Ft void 5605845f98Schristos.Fn hdestroy_r "struct hsearch_data *table" 57842ee049Schristos.Ft void 586030f04aSchristos.Fn hdestroy1_r "struct hsearch_data *table" "void (*freekey)(void *)" "void (*freedata)(void *)" 5966678fd1Scgd.Ft ENTRY * 6066678fd1Scgd.Fn hsearch "ENTRY item" "ACTION action" 6105845f98Schristos.Ft int 6205845f98Schristos.Fn hsearch_r "ENTRY item" "ACTION action" "ENTRY ** itemp" "struct hsearch_data *table" 6366678fd1Scgd.Sh DESCRIPTION 6466678fd1ScgdThe 6566678fd1Scgd.Fn hcreate , 6605845f98Schristos.Fn hcreate_r , 6705845f98Schristos.Fn hdestroy , 6805845f98Schristos.Fn hdestroy_r 69842ee049Schristos.Fn hdestroy1 , 70842ee049Schristos.Fn hdestroy1_r 7105845f98Schristos.Fn hsearch , 7219ea2ccdSwizand 7305845f98Schristos.Fn hsearch_r 7466678fd1Scgdfunctions manage hash search tables. 7566678fd1Scgd.Pp 7666678fd1ScgdThe 7766678fd1Scgd.Fn hcreate 7866678fd1Scgdfunction allocates and initializes the table. 7966678fd1ScgdThe 8066678fd1Scgd.Fa nel 8166678fd1Scgdargument specifies an estimate of the maximum number of entries to be held 826569c5c5Swizby the table. 836569c5c5SwizUnless further memory allocation fails, supplying an insufficient 8466678fd1Scgd.Fa nel 8566678fd1Scgdvalue will not result in functional harm, although a performance degradation 8666678fd1Scgdmay occur. 8766678fd1ScgdInitialization using the 8866678fd1Scgd.Fn hcreate 8966678fd1Scgdfunction is mandatory prior to any access operations using 9066678fd1Scgd.Fn hsearch . 9166678fd1Scgd.Pp 9266678fd1ScgdThe 9366678fd1Scgd.Fn hdestroy 9466678fd1Scgdfunction destroys a table previously created using 9566678fd1Scgd.Fn hcreate . 9666678fd1ScgdAfter a call to 9766678fd1Scgd.Fn hdestroy , 9866678fd1Scgdthe data can no longer be accessed. 9966678fd1Scgd.Pp 10066678fd1ScgdThe 10166678fd1Scgd.Fn hsearch 102*17359e5aSrilligfunction is used to search the hash table. 1036569c5c5SwizIt returns a pointer into the 1046569c5c5Swizhash table indicating the address of an item. 1056569c5c5SwizThe 10666678fd1Scgd.Fa item 10766678fd1Scgdargument is of type 10845192286Sjruoho.Vt ENTRY , 10945192286Sjruohodefined in the 11045192286Sjruoho.In search.h 11145192286Sjruohoheader. 11245192286SjruohoThis is a structure type that contains two pointers: 11345192286Sjruoho.Pp 11445192286Sjruoho.Bl -tag -compact -offset indent -width "void *data " 11566678fd1Scgd.It Fa char *key 11645192286Sjruohocomparison key 11766678fd1Scgd.It Fa void *data 11866678fd1Scgdpointer to data associated with 11945192286Sjruoho.Fa key 12066678fd1Scgd.El 12166678fd1Scgd.Pp 12266678fd1ScgdThe key comparison function used by 12366678fd1Scgd.Fn hsearch 12466678fd1Scgdis 12566678fd1Scgd.Xr strcmp 3 . 12666678fd1Scgd.Pp 12766678fd1ScgdThe 12866678fd1Scgd.Fa action 12966678fd1Scgdargument is of type 13045192286Sjruoho.Vt ACTION , 13166678fd1Scgdan enumeration type which defines the following values: 13245192286Sjruoho.Bl -tag -offset indent -width ENTERXX 13366678fd1Scgd.It Dv ENTER 134d563d62fScgdInsert 13566678fd1Scgd.Fa item 136d563d62fScgdinto the hash table. 137d563d62fScgdIf an existing item with the same key is found, it is not replaced. 138d563d62fScgdNote that the 139d563d62fScgd.Fa key 140d563d62fScgdand 141d563d62fScgd.Fa data 142d563d62fScgdelements of 143d563d62fScgd.Fa item 1446569c5c5Swizare used directly by the new table entry. 1456569c5c5SwizThe storage for the 146d563d62fScgdkey must not be modified during the lifetime of the hash table. 14766678fd1Scgd.It Dv FIND 148d563d62fScgdSearch the hash table without inserting 14966678fd1Scgd.Fa item . 15066678fd1Scgd.El 15145192286Sjruoho.Pp 152842ee049SchristosThe traditional 153842ee049Schristos.Fn hdestroy 15445192286Sjruohoand 155842ee049Schristos.Fn hdestroy_r 156842ee049Schristosfunctions don't 15745192286Sjruoho.Xr free 3 158842ee049Schristosthe data associated with the 15945192286Sjruoho.Fa key 160842ee049Schristosand 161842ee049Schristos.Fa value 162842ee049Schristosof each entry, because they did not allocate them. 163842ee049SchristosSince there is no 164842ee049Schristos.Dq iterator 165842ee049Schristosfunction provided, the 166842ee049Schristos.Fn hdestroy1 167842ee049Schristosand 168842ee049Schristos.Fn hdestroy1_r 1696030f04aSchristosallow controlling how the 17045192286Sjruoho.Fa key 171842ee049Schristosor 172842ee049Schristos.Fa value 173842ee049Schristoswill be freed using the 1746030f04aSchristosprovided functions in the 1756030f04aSchristos.Fa freekey 1766030f04aSchristosand 1776030f04aSchristos.Fa freedata 1786030f04aSchristosarguments. 1796030f04aSchristosIf they are 1806030f04aSchristos.Dv NULL , 1816030f04aSchristosthen 182842ee049Schristos.Fa key 1836030f04aSchristosand 184842ee049Schristos.Fa value 1856030f04aSchristosare not freed. 18605845f98Schristos.Pp 18705845f98SchristosThe 18805845f98Schristos.Fn hcreate_r , 18905845f98Schristos.Fn hdestroy_r , 190842ee049Schristos.Fn hdestroy1_r , 19105845f98Schristosand 19205845f98Schristos.Fn hsearch_r 19319ea2ccdSwizfunctions are re-entrant versions of the above functions that can 19419ea2ccdSwizoperate on a table supplied by the user. 19505845f98SchristosThe 19605845f98Schristos.Fn hsearch_r 19705845f98Schristosfunction returns 19805845f98Schristos.Dv 0 19905845f98Schristosif the action is 20005845f98Schristos.Dv ENTER 20105845f98Schristosand the element cannot be created, 20205845f98Schristos.Dv 1 20305845f98Schristosotherwise. 20405845f98SchristosIf the element exists or can be created, it will be placed in 20505845f98Schristos.Fa itemp , 20605845f98Schristosotherwise 20705845f98Schristos.Fa itemp 20805845f98Schristoswill be set to 20905845f98Schristos.Dv NULL . 21066678fd1Scgd.Sh RETURN VALUES 21166678fd1ScgdIf successful, the 21266678fd1Scgd.Fn hcreate 21305845f98Schristosand 21405845f98Schristos.Fn hcreate_r 21519ea2ccdSwizfunctions return a non-zero value. 21605845f98SchristosOtherwise, a value of 21705845f98Schristos.Dv 0 21805845f98Schristosis returned and 21966678fd1Scgd.Va errno 22066678fd1Scgdis set to indicate the error. 22166678fd1Scgd.Pp 22266678fd1ScgdThe 22366678fd1Scgd.Fn hdestroy 22405845f98Schristosand 22505845f98Schristos.Fn hdestroy_r 22605845f98Schristosfunctions return no value. 22766678fd1Scgd.Pp 22866678fd1ScgdIf successful, the 22966678fd1Scgd.Fn hsearch 230d563d62fScgdfunction returns a pointer to hash table entry matching 231d563d62fScgdthe provided key. 23266678fd1ScgdIf the action is 23366678fd1Scgd.Dv FIND 23466678fd1Scgdand the item was not found, or if the action is 23566678fd1Scgd.Dv ENTER 23666678fd1Scgdand the insertion failed, 23766678fd1Scgd.Dv NULL 23866678fd1Scgdis returned and 23966678fd1Scgd.Va errno 24066678fd1Scgdis set to indicate the error. 241d563d62fScgdIf the action is 242d563d62fScgd.Dv ENTER 243d563d62fScgdand an entry already existed in the table matching the given 244d563d62fScgdkey, the existing entry is returned and is not replaced. 24505845f98Schristos.Pp 24605845f98SchristosThe 24705845f98Schristos.Fn hsearch_r 24805845f98Schristosfunction returns 24905845f98Schristos.Dv 1 25005845f98Schristosunless the table is full, when it returns 25105845f98Schristos.Dv 0 . 25219ea2ccdSwizIf 25305845f98Schristos.Fn hsearch 25405845f98Schristosreturns 25505845f98Schristos.Dv 0 25605845f98Schristosor the element is not found, 25705845f98Schristos.Va errno 25805845f98Schristosis set to indicate the error. 25966678fd1Scgd.Sh ERRORS 26066678fd1ScgdThe 26105845f98Schristos.Fn hcreate , 26205845f98Schristos.Fn hcreate_r , 26366678fd1Scgd.Fn hsearch 26405845f98Schristosand 26505845f98Schristos.Fn hsearch_r 26666678fd1Scgdfunctions will fail if: 26766678fd1Scgd.Bl -tag -width Er 26866678fd1Scgd.It Bq Er ENOMEM 26966678fd1ScgdInsufficient memory is available. 27066678fd1Scgd.El 27105845f98Schristos.Pp 27205845f98SchristosThe 27305845f98Schristos.Fn hsearch 27405845f98Schristosand 27505845f98Schristos.Fn hsearch_r 27605845f98Schristosfunctions will also fail if the action is 2774251f87dSchristos.Dv FIND 27819ea2ccdSwizand the element is not found: 27905845f98Schristos.Bl -tag -width Er 28005845f98Schristos.It Bq Er ESRCH 28105845f98SchristosThe 28205845f98Schristos.Fa item 28305845f98Schristosgiven is not found. 28405845f98Schristos.El 28566678fd1Scgd.Sh SEE ALSO 28666678fd1Scgd.Xr bsearch 3 , 2876ce80a18Swiz.Xr free 3 , 28866678fd1Scgd.Xr lsearch 3 , 28966678fd1Scgd.Xr malloc 3 , 29066678fd1Scgd.Xr strcmp 3 29166678fd1Scgd.Sh STANDARDS 29266678fd1ScgdThe 29366678fd1Scgd.Fn hcreate , 29466678fd1Scgd.Fn hdestroy 29566678fd1Scgdand 29666678fd1Scgd.Fn hsearch 29766678fd1Scgdfunctions conform to 29866678fd1Scgd.St -xpg4.2 . 29966678fd1Scgd.Sh HISTORY 30066678fd1ScgdThe 30166678fd1Scgd.Fn hcreate , 30266678fd1Scgd.Fn hdestroy 30366678fd1Scgdand 30466678fd1Scgd.Fn hsearch 30566678fd1Scgdfunctions first appeared in 30666678fd1Scgd.At V . 30705845f98SchristosThe 30805845f98Schristos.Fn hcreate_r , 3096ce80a18Swiz.Fn hdestroy_r , 31005845f98Schristosand 31105845f98Schristos.Fn hsearch_r 31219ea2ccdSwizfunctions are 31305845f98Schristos.Tn GNU 31405845f98Schristosextensions. 315842ee049SchristosThe 316842ee049Schristos.Fn hdestroy1 317842ee049Schristosand 318842ee049Schristos.Fn hdestroy1_r 319842ee049Schristosare 320842ee049Schristos.Nx 321842ee049Schristosextensions. 32245192286Sjruoho.Sh CAVEATS 32345192286SjruohoAt least the following limitations can be mentioned: 32445192286Sjruoho.Bl -bullet 32545192286Sjruoho.It 32619ea2ccdSwizThe original, 32719ea2ccdSwiz.Pf non- Tn GNU 32805845f98Schristosinterface permits the use of only one hash table at a time. 32945192286Sjruoho.It 33045192286SjruohoIndividual hash table entries can be added, but not deleted. 33145192286Sjruoho.It 332842ee049SchristosThere is no iterator to scan for all entries in the table. 33345192286Sjruoho.El 334