2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: inttab.c /main/5 1996/05/09 04:23:37 drk $ */
25 routines to implement a table of int, int value pairs.
26 the data is relocatable to allow use at various addresses
31 Tables are limited to 64K entries
37 #include <sys/types.h>
45 #define NOT_AN_INDEX 0
47 static int build_it(int * data, void * usr_arg, int key);
49 typedef struct inttab {
56 typedef struct intlist {
61 typedef struct builder {
63 intlist_t * intlist_ptr;
64 inttab_t * inttab_ptr;
69 first entry is header block;
70 contains key = size (not counting header)
74 _DtShmFindIntTabEntry(DtShmInttab inttab, unsigned int key)
76 register const inttab_t * ptr = (const inttab_t *) inttab;
83 i = ptr[key % ptr->key + 1].first;
85 while(i && key != ptr[i].key)
88 return(i?(&ptr[i].data):(const int *)NULL);
92 _DtShmProtoInitInttab(int sizeguess)
94 intlist_t * ptr = (intlist_t *)malloc(sizeof(*ptr));
96 ptr->tbl = _DtUtilMakeIHash(sizeguess);;
102 _DtShmProtoAddInttab(DtShmProtoInttab intlist, unsigned int keyin, int datain)
104 intlist_t * ptr = (intlist_t *) intlist;
107 data = (int**)_DtUtilGetHash(ptr->tbl, (unsigned char *)keyin);
109 if(!*data) /* new */ {
110 *data = (int *) malloc(sizeof(int));
119 _DtShmProtoSizeInttab(DtShmProtoInttab intlist)
121 intlist_t * ptr = (intlist_t * ) intlist;
123 return(sizeof(inttab_t) * (ptr->num_entries +1));
127 _DtShmProtoCopyInttab(DtShmProtoInttab intlist, void * destination)
132 build.intlist_ptr = (intlist_t * ) intlist;
133 build.inttab_ptr = (inttab_t *) destination;
135 memset(destination, 0, (build.intlist_ptr->num_entries+1)*sizeof(inttab_t));
137 build.inttab_ptr->key = build.intlist_ptr->num_entries;
139 _DtUtilOperateHash(build.intlist_ptr->tbl, (DtHashOperateFunc)build_it, &build);
145 _DtShmProtoDestroyInttab(DtShmProtoInttab intlist)
147 intlist_t * ptr = (intlist_t *)intlist;
148 _DtUtilDestroyHash(ptr->tbl, (DtHashDestroyFunc)free, NULL);
153 static int build_it(int * data, void * usr_arg, int key)
155 builder_t * ptr = (builder_t *) usr_arg;
158 unsigned short * add_ptr;
160 int bucket = key % (ptr->intlist_ptr->num_entries) + 1;
162 a = ptr->inttab_ptr + ptr->counter;
165 a->next = NOT_AN_INDEX;
167 b = ptr->inttab_ptr + bucket;
169 if(b->first == NOT_AN_INDEX) {
172 b = ptr->inttab_ptr + b->first;
173 while(b->next != NOT_AN_INDEX)
174 b = ptr->inttab_ptr + b->next;
178 *add_ptr = ptr->counter++;