1*0a6a1f1dSLionel Sambuc.\" $NetBSD: hcreate.3,v 1.13 2014/07/20 20:17:21 christos Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1999 The NetBSD Foundation, Inc. 42fe8fb19SBen Gras.\" All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" This code is derived from software contributed to The NetBSD Foundation 72fe8fb19SBen Gras.\" by Klaus Klein. 82fe8fb19SBen Gras.\" 92fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 102fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 112fe8fb19SBen Gras.\" are met: 122fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 132fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 142fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 152fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 162fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 192fe8fb19SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 202fe8fb19SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 212fe8fb19SBen Gras.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 222fe8fb19SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 232fe8fb19SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 242fe8fb19SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 252fe8fb19SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 262fe8fb19SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 272fe8fb19SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 282fe8fb19SBen Gras.\" POSSIBILITY OF SUCH DAMAGE. 292fe8fb19SBen Gras.\" 30*0a6a1f1dSLionel Sambuc.Dd July 20, 2014 312fe8fb19SBen Gras.Dt HCREATE 3 322fe8fb19SBen Gras.Os 332fe8fb19SBen Gras.Sh NAME 342fe8fb19SBen Gras.Nm hcreate , 35f14fb602SLionel Sambuc.Nm hcreate_r , 362fe8fb19SBen Gras.Nm hdestroy , 37*0a6a1f1dSLionel Sambuc.Nm hdestroy1 , 38f14fb602SLionel Sambuc.Nm hdestroy_r , 39*0a6a1f1dSLionel Sambuc.Nm hdestroy1_r , 40f14fb602SLionel Sambuc.Nm hsearch , 41f14fb602SLionel Sambuc.Nm hsearch_r 422fe8fb19SBen Gras.Nd manage hash search table 432fe8fb19SBen Gras.Sh LIBRARY 442fe8fb19SBen Gras.Lb libc 452fe8fb19SBen Gras.Sh SYNOPSIS 462fe8fb19SBen Gras.In search.h 472fe8fb19SBen Gras.Ft int 482fe8fb19SBen Gras.Fn hcreate "size_t nel" 49f14fb602SLionel Sambuc.Ft int 50f14fb602SLionel Sambuc.Fn hcreate_r "size_t nel" "struct hsearch_data *table" 512fe8fb19SBen Gras.Ft void 522fe8fb19SBen Gras.Fn hdestroy "void" 53f14fb602SLionel Sambuc.Ft void 54*0a6a1f1dSLionel Sambuc.Fn hdestroy1 "void (*freekey)(void *)" "void (*freedata)(void *)" 55*0a6a1f1dSLionel Sambuc.Ft void 56f14fb602SLionel Sambuc.Fn hdestroy_r "struct hsearch_data *table" 57*0a6a1f1dSLionel Sambuc.Ft void 58*0a6a1f1dSLionel Sambuc.Fn hdestroy1_r "struct hsearch_data *table" "void (*freekey)(void *)" "void (*freedata)(void *)" 592fe8fb19SBen Gras.Ft ENTRY * 602fe8fb19SBen Gras.Fn hsearch "ENTRY item" "ACTION action" 61f14fb602SLionel Sambuc.Ft int 62f14fb602SLionel Sambuc.Fn hsearch_r "ENTRY item" "ACTION action" "ENTRY ** itemp" "struct hsearch_data *table" 632fe8fb19SBen Gras.Sh DESCRIPTION 642fe8fb19SBen GrasThe 652fe8fb19SBen Gras.Fn hcreate , 66f14fb602SLionel Sambuc.Fn hcreate_r , 67f14fb602SLionel Sambuc.Fn hdestroy , 68f14fb602SLionel Sambuc.Fn hdestroy_r 69*0a6a1f1dSLionel Sambuc.Fn hdestroy1 , 70*0a6a1f1dSLionel Sambuc.Fn hdestroy1_r 71f14fb602SLionel Sambuc.Fn hsearch , 722fe8fb19SBen Grasand 73f14fb602SLionel Sambuc.Fn hsearch_r 742fe8fb19SBen Grasfunctions manage hash search tables. 752fe8fb19SBen Gras.Pp 762fe8fb19SBen GrasThe 772fe8fb19SBen Gras.Fn hcreate 782fe8fb19SBen Grasfunction allocates and initializes the table. 792fe8fb19SBen GrasThe 802fe8fb19SBen Gras.Fa nel 812fe8fb19SBen Grasargument specifies an estimate of the maximum number of entries to be held 822fe8fb19SBen Grasby the table. 832fe8fb19SBen GrasUnless further memory allocation fails, supplying an insufficient 842fe8fb19SBen Gras.Fa nel 852fe8fb19SBen Grasvalue will not result in functional harm, although a performance degradation 862fe8fb19SBen Grasmay occur. 872fe8fb19SBen GrasInitialization using the 882fe8fb19SBen Gras.Fn hcreate 892fe8fb19SBen Grasfunction is mandatory prior to any access operations using 902fe8fb19SBen Gras.Fn hsearch . 912fe8fb19SBen Gras.Pp 922fe8fb19SBen GrasThe 932fe8fb19SBen Gras.Fn hdestroy 942fe8fb19SBen Grasfunction destroys a table previously created using 952fe8fb19SBen Gras.Fn hcreate . 962fe8fb19SBen GrasAfter a call to 972fe8fb19SBen Gras.Fn hdestroy , 982fe8fb19SBen Grasthe data can no longer be accessed. 992fe8fb19SBen Gras.Pp 1002fe8fb19SBen GrasThe 1012fe8fb19SBen Gras.Fn hsearch 1022fe8fb19SBen Grasfunction is used to search to the hash table. 1032fe8fb19SBen GrasIt returns a pointer into the 1042fe8fb19SBen Grashash table indicating the address of an item. 1052fe8fb19SBen GrasThe 1062fe8fb19SBen Gras.Fa item 1072fe8fb19SBen Grasargument is of type 1082fe8fb19SBen Gras.Vt ENTRY , 1092fe8fb19SBen Grasdefined in the 1102fe8fb19SBen Gras.In search.h 1112fe8fb19SBen Grasheader. 1122fe8fb19SBen GrasThis is a structure type that contains two pointers: 1132fe8fb19SBen Gras.Pp 1142fe8fb19SBen Gras.Bl -tag -compact -offset indent -width "void *data " 1152fe8fb19SBen Gras.It Fa char *key 1162fe8fb19SBen Grascomparison key 1172fe8fb19SBen Gras.It Fa void *data 1182fe8fb19SBen Graspointer to data associated with 1192fe8fb19SBen Gras.Fa key 1202fe8fb19SBen Gras.El 1212fe8fb19SBen Gras.Pp 1222fe8fb19SBen GrasThe key comparison function used by 1232fe8fb19SBen Gras.Fn hsearch 1242fe8fb19SBen Grasis 1252fe8fb19SBen Gras.Xr strcmp 3 . 1262fe8fb19SBen Gras.Pp 1272fe8fb19SBen GrasThe 1282fe8fb19SBen Gras.Fa action 1292fe8fb19SBen Grasargument is of type 1302fe8fb19SBen Gras.Vt ACTION , 1312fe8fb19SBen Grasan enumeration type which defines the following values: 1322fe8fb19SBen Gras.Bl -tag -offset indent -width ENTERXX 1332fe8fb19SBen Gras.It Dv ENTER 1342fe8fb19SBen GrasInsert 1352fe8fb19SBen Gras.Fa item 1362fe8fb19SBen Grasinto the hash table. 1372fe8fb19SBen GrasIf an existing item with the same key is found, it is not replaced. 1382fe8fb19SBen GrasNote that the 1392fe8fb19SBen Gras.Fa key 1402fe8fb19SBen Grasand 1412fe8fb19SBen Gras.Fa data 1422fe8fb19SBen Graselements of 1432fe8fb19SBen Gras.Fa item 1442fe8fb19SBen Grasare used directly by the new table entry. 1452fe8fb19SBen GrasThe storage for the 1462fe8fb19SBen Graskey must not be modified during the lifetime of the hash table. 1472fe8fb19SBen Gras.It Dv FIND 1482fe8fb19SBen GrasSearch the hash table without inserting 1492fe8fb19SBen Gras.Fa item . 1502fe8fb19SBen Gras.El 1512fe8fb19SBen Gras.Pp 152*0a6a1f1dSLionel SambucThe traditional 153*0a6a1f1dSLionel Sambuc.Fn hdestroy 1542fe8fb19SBen Grasand 155*0a6a1f1dSLionel Sambuc.Fn hdestroy_r 156*0a6a1f1dSLionel Sambucfunctions don't 1572fe8fb19SBen Gras.Xr free 3 158*0a6a1f1dSLionel Sambucthe data associated with the 1592fe8fb19SBen Gras.Fa key 160*0a6a1f1dSLionel Sambucand 161*0a6a1f1dSLionel Sambuc.Fa value 162*0a6a1f1dSLionel Sambucof each entry, because they did not allocate them. 163*0a6a1f1dSLionel SambucSince there is no 164*0a6a1f1dSLionel Sambuc.Dq iterator 165*0a6a1f1dSLionel Sambucfunction provided, the 166*0a6a1f1dSLionel Sambuc.Fn hdestroy1 167*0a6a1f1dSLionel Sambucand 168*0a6a1f1dSLionel Sambuc.Fn hdestroy1_r 169*0a6a1f1dSLionel Sambucallow controlling how the 1702fe8fb19SBen Gras.Fa key 171*0a6a1f1dSLionel Sambucor 172*0a6a1f1dSLionel Sambuc.Fa value 173*0a6a1f1dSLionel Sambucwill be freed using the 174*0a6a1f1dSLionel Sambucprovided functions in the 175*0a6a1f1dSLionel Sambuc.Fa freekey 176*0a6a1f1dSLionel Sambucand 177*0a6a1f1dSLionel Sambuc.Fa freedata 178*0a6a1f1dSLionel Sambucarguments. 179*0a6a1f1dSLionel SambucIf they are 180*0a6a1f1dSLionel Sambuc.Dv NULL , 181*0a6a1f1dSLionel Sambucthen 182*0a6a1f1dSLionel Sambuc.Fa key 183*0a6a1f1dSLionel Sambucand 184*0a6a1f1dSLionel Sambuc.Fa value 185*0a6a1f1dSLionel Sambucare not freed. 186f14fb602SLionel Sambuc.Pp 187f14fb602SLionel SambucThe 188f14fb602SLionel Sambuc.Fn hcreate_r , 189f14fb602SLionel Sambuc.Fn hdestroy_r , 190*0a6a1f1dSLionel Sambuc.Fn hdestroy1_r , 191f14fb602SLionel Sambucand 192f14fb602SLionel Sambuc.Fn hsearch_r 193f14fb602SLionel Sambucfunctions are re-entrant versions of the above functions that can 194f14fb602SLionel Sambucoperate on a table supplied by the user. 195f14fb602SLionel SambucThe 196f14fb602SLionel Sambuc.Fn hsearch_r 197f14fb602SLionel Sambucfunction returns 198f14fb602SLionel Sambuc.Dv 0 199f14fb602SLionel Sambucif the action is 200f14fb602SLionel Sambuc.Dv ENTER 201f14fb602SLionel Sambucand the element cannot be created, 202f14fb602SLionel Sambuc.Dv 1 203f14fb602SLionel Sambucotherwise. 204f14fb602SLionel SambucIf the element exists or can be created, it will be placed in 205f14fb602SLionel Sambuc.Fa itemp , 206f14fb602SLionel Sambucotherwise 207f14fb602SLionel Sambuc.Fa itemp 208f14fb602SLionel Sambucwill be set to 209f14fb602SLionel Sambuc.Dv NULL . 2102fe8fb19SBen Gras.Sh RETURN VALUES 2112fe8fb19SBen GrasIf successful, the 2122fe8fb19SBen Gras.Fn hcreate 213f14fb602SLionel Sambucand 214f14fb602SLionel Sambuc.Fn hcreate_r 215f14fb602SLionel Sambucfunctions return a non-zero value. 216f14fb602SLionel SambucOtherwise, a value of 217f14fb602SLionel Sambuc.Dv 0 218f14fb602SLionel Sambucis returned and 2192fe8fb19SBen Gras.Va errno 2202fe8fb19SBen Grasis set to indicate the error. 2212fe8fb19SBen Gras.Pp 2222fe8fb19SBen GrasThe 2232fe8fb19SBen Gras.Fn hdestroy 224f14fb602SLionel Sambucand 225f14fb602SLionel Sambuc.Fn hdestroy_r 226f14fb602SLionel Sambucfunctions return no value. 2272fe8fb19SBen Gras.Pp 2282fe8fb19SBen GrasIf successful, the 2292fe8fb19SBen Gras.Fn hsearch 2302fe8fb19SBen Grasfunction returns a pointer to hash table entry matching 2312fe8fb19SBen Grasthe provided key. 2322fe8fb19SBen GrasIf the action is 2332fe8fb19SBen Gras.Dv FIND 2342fe8fb19SBen Grasand the item was not found, or if the action is 2352fe8fb19SBen Gras.Dv ENTER 2362fe8fb19SBen Grasand the insertion failed, 2372fe8fb19SBen Gras.Dv NULL 2382fe8fb19SBen Grasis returned and 2392fe8fb19SBen Gras.Va errno 2402fe8fb19SBen Grasis set to indicate the error. 2412fe8fb19SBen GrasIf the action is 2422fe8fb19SBen Gras.Dv ENTER 2432fe8fb19SBen Grasand an entry already existed in the table matching the given 2442fe8fb19SBen Graskey, the existing entry is returned and is not replaced. 245f14fb602SLionel Sambuc.Pp 246f14fb602SLionel SambucThe 247f14fb602SLionel Sambuc.Fn hsearch_r 248f14fb602SLionel Sambucfunction returns 249f14fb602SLionel Sambuc.Dv 1 250f14fb602SLionel Sambucunless the table is full, when it returns 251f14fb602SLionel Sambuc.Dv 0 . 252f14fb602SLionel SambucIf 253f14fb602SLionel Sambuc.Fn hsearch 254f14fb602SLionel Sambucreturns 255f14fb602SLionel Sambuc.Dv 0 256f14fb602SLionel Sambucor the element is not found, 257f14fb602SLionel Sambuc.Va errno 258f14fb602SLionel Sambucis set to indicate the error. 2592fe8fb19SBen Gras.Sh ERRORS 2602fe8fb19SBen GrasThe 261f14fb602SLionel Sambuc.Fn hcreate , 262f14fb602SLionel Sambuc.Fn hcreate_r , 2632fe8fb19SBen Gras.Fn hsearch 264f14fb602SLionel Sambucand 265f14fb602SLionel Sambuc.Fn hsearch_r 2662fe8fb19SBen Grasfunctions will fail if: 2672fe8fb19SBen Gras.Bl -tag -width Er 2682fe8fb19SBen Gras.It Bq Er ENOMEM 2692fe8fb19SBen GrasInsufficient memory is available. 2702fe8fb19SBen Gras.El 271f14fb602SLionel Sambuc.Pp 272f14fb602SLionel SambucThe 273f14fb602SLionel Sambuc.Fn hsearch 274f14fb602SLionel Sambucand 275f14fb602SLionel Sambuc.Fn hsearch_r 276f14fb602SLionel Sambucfunctions will also fail if the action is 277f14fb602SLionel Sambuc.Dv SEARCH 278f14fb602SLionel Sambucand the element is not found: 279f14fb602SLionel Sambuc.Bl -tag -width Er 280f14fb602SLionel Sambuc.It Bq Er ESRCH 281f14fb602SLionel SambucThe 282f14fb602SLionel Sambuc.Fa item 283f14fb602SLionel Sambucgiven is not found. 284f14fb602SLionel Sambuc.El 2852fe8fb19SBen Gras.Sh SEE ALSO 2862fe8fb19SBen Gras.Xr bsearch 3 , 287*0a6a1f1dSLionel Sambuc.Xr free 3 , 2882fe8fb19SBen Gras.Xr lsearch 3 , 2892fe8fb19SBen Gras.Xr malloc 3 , 2902fe8fb19SBen Gras.Xr strcmp 3 2912fe8fb19SBen Gras.Sh STANDARDS 2922fe8fb19SBen GrasThe 2932fe8fb19SBen Gras.Fn hcreate , 2942fe8fb19SBen Gras.Fn hdestroy 2952fe8fb19SBen Grasand 2962fe8fb19SBen Gras.Fn hsearch 2972fe8fb19SBen Grasfunctions conform to 2982fe8fb19SBen Gras.St -xpg4.2 . 2992fe8fb19SBen Gras.Sh HISTORY 3002fe8fb19SBen GrasThe 3012fe8fb19SBen Gras.Fn hcreate , 3022fe8fb19SBen Gras.Fn hdestroy 3032fe8fb19SBen Grasand 3042fe8fb19SBen Gras.Fn hsearch 3052fe8fb19SBen Grasfunctions first appeared in 3062fe8fb19SBen Gras.At V . 307f14fb602SLionel SambucThe 308f14fb602SLionel Sambuc.Fn hcreate_r , 309*0a6a1f1dSLionel Sambuc.Fn hdestroy_r , 310f14fb602SLionel Sambucand 311f14fb602SLionel Sambuc.Fn hsearch_r 312f14fb602SLionel Sambucfunctions are 313f14fb602SLionel Sambuc.Tn GNU 314f14fb602SLionel Sambucextensions. 315*0a6a1f1dSLionel SambucThe 316*0a6a1f1dSLionel Sambuc.Fn hdestroy1 317*0a6a1f1dSLionel Sambucand 318*0a6a1f1dSLionel Sambuc.Fn hdestroy1_r 319*0a6a1f1dSLionel Sambucare 320*0a6a1f1dSLionel Sambuc.Nx 321*0a6a1f1dSLionel Sambucextensions. 3222fe8fb19SBen Gras.Sh CAVEATS 3232fe8fb19SBen GrasAt least the following limitations can be mentioned: 3242fe8fb19SBen Gras.Bl -bullet 3252fe8fb19SBen Gras.It 326f14fb602SLionel SambucThe original, 327f14fb602SLionel Sambuc.Pf non- Tn GNU 328f14fb602SLionel Sambucinterface permits the use of only one hash table at a time. 3292fe8fb19SBen Gras.It 3302fe8fb19SBen GrasIndividual hash table entries can be added, but not deleted. 3312fe8fb19SBen Gras.It 332*0a6a1f1dSLionel SambucThere is no iterator to scan for all entries in the table. 3332fe8fb19SBen Gras.El 334