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 libraries 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
38 #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 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 int _DtShmProtoAddInttab(DtShmProtoInttab intlist, unsigned int keyin,
105 intlist_t * ptr = (intlist_t *) intlist;
108 data = (int**)_DtUtilGetHash(ptr->tbl, (unsigned char *) (intptr_t) keyin);
110 if(!*data) /* new */ {
111 *data = (int *) malloc(sizeof(int));
120 _DtShmProtoSizeInttab(DtShmProtoInttab intlist)
122 intlist_t * ptr = (intlist_t * ) intlist;
124 return(sizeof(inttab_t) * (ptr->num_entries +1));
128 _DtShmProtoCopyInttab(DtShmProtoInttab intlist, void * destination)
133 build.intlist_ptr = (intlist_t * ) intlist;
134 build.inttab_ptr = (inttab_t *) destination;
136 memset(destination, 0, (build.intlist_ptr->num_entries+1)*sizeof(inttab_t));
138 build.inttab_ptr->key = build.intlist_ptr->num_entries;
140 _DtUtilOperateHash(build.intlist_ptr->tbl, (DtHashOperateFunc)build_it, &build);
146 _DtShmProtoDestroyInttab(DtShmProtoInttab intlist)
148 intlist_t * ptr = (intlist_t *)intlist;
149 _DtUtilDestroyHash(ptr->tbl, (DtHashDestroyFunc)free, NULL);
154 static int build_it(int * data, void * usr_arg, int key)
156 builder_t * ptr = (builder_t *) usr_arg;
159 unsigned short * add_ptr;
161 int bucket = key % (ptr->intlist_ptr->num_entries) + 1;
163 a = ptr->inttab_ptr + ptr->counter;
166 a->next = NOT_AN_INDEX;
168 b = ptr->inttab_ptr + bucket;
170 if(b->first == NOT_AN_INDEX) {
173 b = ptr->inttab_ptr + b->first;
174 while(b->next != NOT_AN_INDEX)
175 b = ptr->inttab_ptr + b->next;
179 *add_ptr = ptr->counter++;