XMakeAssoc.c 2.4 KB

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