inttab.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  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
  22. */
  23. /* $XConsortium: inttab.c /main/5 1996/05/09 04:23:37 drk $ */
  24. /*
  25. routines to implement a table of int, int value pairs.
  26. the data is relocatable to allow use at various addresses
  27. (eg shared memory)
  28. -1 is an illegal key
  29. Tables are limited to 64K entries
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdint.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <sys/mman.h>
  37. #include <string.h>
  38. #include "DtHash.h"
  39. #include "DtShmDb.h"
  40. #define NOT_AN_INDEX 0
  41. static int build_it(int * data, void * usr_arg, int key);
  42. typedef struct inttab {
  43. int key;
  44. int data;
  45. unsigned short first;
  46. unsigned short next;
  47. } inttab_t;
  48. typedef struct intlist {
  49. int num_entries;
  50. void * tbl;
  51. } intlist_t;
  52. typedef struct builder {
  53. int counter;
  54. intlist_t * intlist_ptr;
  55. inttab_t * inttab_ptr;
  56. } builder_t;
  57. /*
  58. first entry is header block;
  59. contains key = size (not counting header)
  60. */
  61. const int *
  62. _DtShmFindIntTabEntry(DtShmInttab inttab, unsigned int key)
  63. {
  64. register const inttab_t * ptr = (const inttab_t *) inttab;
  65. register int i;
  66. if ( !ptr->key)
  67. return(0);
  68. i = ptr[key % ptr->key + 1].first;
  69. while(i && key != ptr[i].key)
  70. i = ptr[i].next;
  71. return(i?(&ptr[i].data):(const int *)NULL);
  72. }
  73. DtShmProtoInttab
  74. _DtShmProtoInitInttab(int sizeguess)
  75. {
  76. intlist_t * ptr = (intlist_t *)malloc(sizeof(*ptr));
  77. ptr->tbl = _DtUtilMakeIHash(sizeguess);;
  78. ptr->num_entries = 0;
  79. return((void*)ptr);
  80. }
  81. _DtShmProtoAddInttab(DtShmProtoInttab intlist, unsigned int keyin, int datain)
  82. {
  83. intlist_t * ptr = (intlist_t *) intlist;
  84. int ** data;
  85. data = (int**)_DtUtilGetHash(ptr->tbl, (unsigned char *) (intptr_t) keyin);
  86. if(!*data) /* new */ {
  87. *data = (int *) malloc(sizeof(int));
  88. ptr->num_entries++;
  89. }
  90. **data = datain;
  91. return(0);
  92. }
  93. int
  94. _DtShmProtoSizeInttab(DtShmProtoInttab intlist)
  95. {
  96. intlist_t * ptr = (intlist_t * ) intlist;
  97. return(sizeof(inttab_t) * (ptr->num_entries +1));
  98. }
  99. DtShmInttab
  100. _DtShmProtoCopyInttab(DtShmProtoInttab intlist, void * destination)
  101. {
  102. builder_t build;
  103. build.counter = 1;
  104. build.intlist_ptr = (intlist_t * ) intlist;
  105. build.inttab_ptr = (inttab_t *) destination;
  106. memset(destination, 0, (build.intlist_ptr->num_entries+1)*sizeof(inttab_t));
  107. build.inttab_ptr->key = build.intlist_ptr->num_entries;
  108. _DtUtilOperateHash(build.intlist_ptr->tbl, (DtHashOperateFunc)build_it, &build);
  109. return(0);
  110. }
  111. int
  112. _DtShmProtoDestroyInttab(DtShmProtoInttab intlist)
  113. {
  114. intlist_t * ptr = (intlist_t *)intlist;
  115. _DtUtilDestroyHash(ptr->tbl, (DtHashDestroyFunc)free, NULL);
  116. free(intlist);
  117. return(0);
  118. }
  119. static int build_it(int * data, void * usr_arg, int key)
  120. {
  121. builder_t * ptr = (builder_t *) usr_arg;
  122. inttab_t * a;
  123. inttab_t * b;
  124. unsigned short * add_ptr;
  125. int bucket = key % (ptr->intlist_ptr->num_entries) + 1;
  126. a = ptr->inttab_ptr + ptr->counter;
  127. a->key = key;
  128. a->data = *data;
  129. a->next = NOT_AN_INDEX;
  130. b = ptr->inttab_ptr + bucket;
  131. if(b->first == NOT_AN_INDEX) {
  132. add_ptr = &b->first;
  133. } else {
  134. b = ptr->inttab_ptr + b->first;
  135. while(b->next != NOT_AN_INDEX)
  136. b = ptr->inttab_ptr + b->next;
  137. add_ptr = &b->next;
  138. }
  139. *add_ptr = ptr->counter++;
  140. return(0);
  141. }