inttab.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. const inttab_t * ptr = (const inttab_t *) inttab;
  65. 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. int _DtShmProtoAddInttab(DtShmProtoInttab intlist, unsigned int keyin,
  82. int datain)
  83. {
  84. intlist_t * ptr = (intlist_t *) intlist;
  85. int ** data;
  86. data = (int**)_DtUtilGetHash(ptr->tbl, (unsigned char *) (intptr_t) keyin);
  87. if(!*data) /* new */ {
  88. *data = (int *) malloc(sizeof(int));
  89. ptr->num_entries++;
  90. }
  91. **data = datain;
  92. return(0);
  93. }
  94. int
  95. _DtShmProtoSizeInttab(DtShmProtoInttab intlist)
  96. {
  97. intlist_t * ptr = (intlist_t * ) intlist;
  98. return(sizeof(inttab_t) * (ptr->num_entries +1));
  99. }
  100. DtShmInttab
  101. _DtShmProtoCopyInttab(DtShmProtoInttab intlist, void * destination)
  102. {
  103. builder_t build;
  104. build.counter = 1;
  105. build.intlist_ptr = (intlist_t * ) intlist;
  106. build.inttab_ptr = (inttab_t *) destination;
  107. memset(destination, 0, (build.intlist_ptr->num_entries+1)*sizeof(inttab_t));
  108. build.inttab_ptr->key = build.intlist_ptr->num_entries;
  109. _DtUtilOperateHash(build.intlist_ptr->tbl, (DtHashOperateFunc)build_it, &build);
  110. return(0);
  111. }
  112. int
  113. _DtShmProtoDestroyInttab(DtShmProtoInttab intlist)
  114. {
  115. intlist_t * ptr = (intlist_t *)intlist;
  116. _DtUtilDestroyHash(ptr->tbl, (DtHashDestroyFunc)free, NULL);
  117. free(intlist);
  118. return(0);
  119. }
  120. static int build_it(int * data, void * usr_arg, int key)
  121. {
  122. builder_t * ptr = (builder_t *) usr_arg;
  123. inttab_t * a;
  124. inttab_t * b;
  125. unsigned short * add_ptr;
  126. int bucket = key % (ptr->intlist_ptr->num_entries) + 1;
  127. a = ptr->inttab_ptr + ptr->counter;
  128. a->key = key;
  129. a->data = *data;
  130. a->next = NOT_AN_INDEX;
  131. b = ptr->inttab_ptr + bucket;
  132. if(b->first == NOT_AN_INDEX) {
  133. add_ptr = &b->first;
  134. } else {
  135. b = ptr->inttab_ptr + b->first;
  136. while(b->next != NOT_AN_INDEX)
  137. b = ptr->inttab_ptr + b->next;
  138. add_ptr = &b->next;
  139. }
  140. *add_ptr = ptr->counter++;
  141. return(0);
  142. }