XMakeAssoc.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright Massachusetts Institute of Technology 1985 */
  2. #include "copyright.h"
  3. #include <config.h>
  4. #include <X11/Xlib.h>
  5. #include <X11/Xresource.h>
  6. #include "X10.h"
  7. #include <errno.h>
  8. #ifndef NULL
  9. #define NULL 0
  10. #endif
  11. extern int errno;
  12. void emacs_insque();
  13. struct qelem {
  14. struct qelem *q_forw;
  15. struct qelem *q_back;
  16. char q_data[1];
  17. };
  18. /*
  19. * XMakeAssoc - Insert data into an XAssocTable keyed on an XId.
  20. * Data is inserted into the table only once. Redundant inserts are
  21. * meaningless (but cause no problems). The queue in each association
  22. * bucket is sorted (lowest XId to highest XId).
  23. */
  24. XMakeAssoc(dpy, table, x_id, data)
  25. register Display *dpy;
  26. register XAssocTable *table;
  27. register XID x_id;
  28. register caddr_t data;
  29. {
  30. int hash;
  31. register XAssoc *bucket;
  32. register XAssoc *Entry;
  33. register XAssoc *new_entry;
  34. /* Hash the XId to get the bucket number. */
  35. hash = x_id & (table->size - 1);
  36. /* Look up the bucket to get the entries in that bucket. */
  37. bucket = &table->buckets[hash];
  38. /* Get the first entry in the bucket. */
  39. Entry = bucket->next;
  40. /* If (Entry != bucket), the bucket is empty so make */
  41. /* the new entry the first entry in the bucket. */
  42. /* if (Entry == bucket), the we have to search the */
  43. /* bucket. */
  44. if (Entry != bucket) {
  45. /* The bucket isn't empty, begin searching. */
  46. /* If we leave the for loop then we have either passed */
  47. /* where the entry should be or hit the end of the bucket. */
  48. /* In either case we should then insert the new entry */
  49. /* before the current value of "Entry". */
  50. for (; Entry != bucket; Entry = Entry->next) {
  51. if (Entry->x_id == x_id) {
  52. /* Entry has the same XId... */
  53. if (Entry->display == dpy) {
  54. /* Entry has the same Display... */
  55. /* Therefore there is already an */
  56. /* entry with this XId and Display, */
  57. /* reset its data value and return. */
  58. Entry->data = data;
  59. return;
  60. }
  61. /* We found an association with the right */
  62. /* id but the wrong display! */
  63. continue;
  64. }
  65. /* If the current entry's XId is greater than the */
  66. /* XId of the entry to be inserted then we have */
  67. /* passed the location where the new XId should */
  68. /* be inserted. */
  69. if (Entry->x_id > x_id) break;
  70. }
  71. }
  72. /* If we are here then the new entry should be inserted just */
  73. /* before the current value of "Entry". */
  74. /* Create a new XAssoc and load it with new provided data. */
  75. new_entry = (XAssoc *) malloc(sizeof(XAssoc));
  76. new_entry->display = dpy;
  77. new_entry->x_id = x_id;
  78. new_entry->data = data;
  79. /* Insert the new entry. */
  80. emacs_insque((struct qelem *)new_entry, (struct qelem *)Entry->prev);
  81. }
  82. /* arch-tag: d7e3fb8a-f3b3-4c5d-a307-75ca67ec1b49
  83. (do not change this comment) */