Пример #1
0
static void ht_insert(void *ptr, unsigned long size, char *at)
{
	int idx;
	
	/* ht too small? */
	if (ht_count >= ht_size / 2)
		ht_grow();
	
	/* find position */
	idx = (unsigned long) ptr % ht_size;
	while (ht[idx].active && !ht[idx].removed)
		idx = (idx + 1) % ht_size;
	
	/* insert item */
	ht[idx].ptr = ptr;
	ht[idx].size = size;
	ht[idx].at = at;
	ht[idx].active = 1;
	ht[idx].removed = 0;
	mem_busy += size;
	ht_count++;
}
Пример #2
0
ht_entry *ht_put(htab_desc *table, mname *target, char *stash)
{

	/* stash set to 0 if already present, else 1 */

	ht_entry 	*p;
	int4 		*pt0, *pt1, *pt2;
	unsigned int	prev_size;

	pt0 = pt1 = (int4 *)target;
	p = (ht_entry *)(table->base + hash(target, table->size));
	pt2 = (int4 *)p;
	if (*pt1++ == *pt2++ && *pt1 == *pt2)
		*stash = 0;
	else if ((p->nb.val.i1 == 0) && (p->nb.val.i2 ==0))
	{
		if (++table->count >= table->trigger_size)
		{
			prev_size = table->size;
			ht_grow(table);
#ifdef HT_TRACE
			FPRINTF(stderr, "\nHT TRACE: Hash table %lx grew from %d to %d elements\n", (caddr_t)table,
				prev_size, table->size);
			fflush(stderr);
#endif
			if (prev_size != table->size)
				return(ht_put(table, target, stash));
		}
		*stash = 1;
		p->nb = *target;
	} else
	{
		for (;;)
		{
			pt1 = pt0;
			pt2 = (int4 *)p;
			if (*pt1++ == *pt2++ && *pt1 == *pt2)
			{
				*stash = 0;
				break;
			} else if (p->link)
			{
				p = p->link;
			} else
			{
				for (;;)
				{
					--(table->r);
					assert(table->r >= table->base);
					if ((table->r->nb.val.i1 == 0) && (table->r->nb.val.i2 ==0))
					{
						if (++table->count >= table->trigger_size)
						{
							prev_size = table->size;
							ht_grow(table);
#ifdef HT_TRACE
							FPRINTF(stderr,
								"HT TRACE: Hash table %lx grew from %d to %d elements\n",
								(caddr_t)table, prev_size, table->size);
							fflush(stderr);
#endif
							if (prev_size != table->size)
								return(ht_put(table, target, stash));
						}
						p->link = table->r;
						p = p->link;
						p->nb = *target;
						*stash = 1;
						break;
					}
				}
				break;
			}
		}
	}
	return(p);
}