home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / oldX / XCrAssoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-23  |  2.0 KB  |  61 lines

  1. /* $XConsortium: XCrAssoc.c,v 10.17 91/01/06 12:04:57 rws Exp $ */
  2. /* Copyright    Massachusetts Institute of Technology    1985    */
  3.  
  4. /*
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation, and that the name of M.I.T. not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior permission.  M.I.T. makes no representations about the
  12. suitability of this software for any purpose.  It is provided "as is"
  13. without express or implied warranty.
  14. */
  15.  
  16. #include "Xlibint.h"
  17. #include "X10.h"
  18.  
  19. /*
  20.  * XCreateAssocTable - Create an XAssocTable.  The size argument should be
  21.  * a power of two for efficiency reasons.  Some size suggestions: use 32
  22.  * buckets per 100 objects;  a reasonable maximum number of object per
  23.  * buckets is 8.  If there is an error creating the XAssocTable, a NULL
  24.  * pointer is returned.
  25.  */
  26. XAssocTable *XCreateAssocTable(size)
  27.     register int size;        /* Desired size of the table. */
  28. {
  29.     register XAssocTable *table;    /* XAssocTable to be initialized. */
  30.     register XAssoc *buckets;    /* Pointer to the first bucket in */
  31.                     /* the bucket array. */
  32.     
  33.     /* XMalloc the XAssocTable. */
  34.     if ((table = (XAssocTable *)Xmalloc(sizeof(XAssocTable))) == NULL) {
  35.         /* XMalloc call failed! */
  36.         errno = ENOMEM;
  37.         return(NULL);
  38.     }
  39.     
  40.     /* XMalloc the buckets (actually just their headers). */
  41.     buckets = (XAssoc *)Xcalloc((unsigned)size, (unsigned)sizeof(XAssoc));
  42.     if (buckets == NULL) {
  43.         /* XCalloc call failed! */
  44.         errno = ENOMEM;
  45.         return(NULL);
  46.     }
  47.  
  48.     /* Insert table data into the XAssocTable structure. */
  49.     table->buckets = buckets;
  50.     table->size = size;
  51.  
  52.     while (--size >= 0) {
  53.         /* Initialize each bucket. */
  54.         buckets->prev = buckets;
  55.         buckets->next = buckets;
  56.         buckets++;
  57.     }
  58.  
  59.     return(table);
  60. }
  61.