XCrAssoc.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright Massachusetts Institute of Technology 1985 */
  2. #include "copyright.h"
  3. #include <config.h>
  4. #include <X11/Xlib.h>
  5. #include <errno.h>
  6. #include "X10.h"
  7. #ifndef NULL
  8. #define NULL 0
  9. #endif
  10. /*
  11. * XCreateAssocTable - Create an XAssocTable. The size argument should be
  12. * a power of two for efficiency reasons. Some size suggestions: use 32
  13. * buckets per 100 objects; a reasonable maximum number of object per
  14. * buckets is 8. If there is an error creating the XAssocTable, a NULL
  15. * pointer is returned.
  16. */
  17. XAssocTable *XCreateAssocTable(register int size)
  18. /* Desired size of the table. */
  19. {
  20. register XAssocTable *table; /* XAssocTable to be initialized. */
  21. register XAssoc *buckets; /* Pointer to the first bucket in */
  22. /* the bucket array. */
  23. /* Malloc the XAssocTable. */
  24. if ((table = (XAssocTable *)malloc(sizeof(XAssocTable))) == NULL) {
  25. /* malloc call failed! */
  26. errno = ENOMEM;
  27. return(NULL);
  28. }
  29. /* calloc the buckets (actually just their headers). */
  30. buckets = (XAssoc *)calloc((unsigned)size, (unsigned)sizeof(XAssoc));
  31. if (buckets == NULL) {
  32. /* calloc call failed! */
  33. errno = ENOMEM;
  34. return(NULL);
  35. }
  36. /* Insert table data into the XAssocTable structure. */
  37. table->buckets = buckets;
  38. table->size = size;
  39. while (--size >= 0) {
  40. /* Initialize each bucket. */
  41. buckets->prev = buckets;
  42. buckets->next = buckets;
  43. buckets++;
  44. }
  45. return(table);
  46. }