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