XMakeAssoc.c 2.4 KB

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