/************************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

********************************************************/

/* $Header: resource.c,v 1.56 87/06/10 19:51:05 sue Exp $ */

/*	Routines to manage various kinds of resources:
 *
 *	InitClientResources, AddResource, FreeResource, FreeClientResources,
 *	DefineSelf, ResetHosts, AddHost, RemoveHost, InvalidHost,
 *	Xalloc, Xrealloc, Xfree
 */

/* 
 *      a resource is a 32 bit quantity.  the upper 12 bits are client id.  
 *      client provides a 19 bit resource id. this is "hashed" by me by
 *      taking the 10 lower bits and xor'ing with the mid 10 bits.
 *
 *      It is sometimes necessary for the server to create an ID that looks
 *      like it belongs to a client.  This ID, however,  must not be one
 *      the client actually can create, or we have the potential for conflict.
 *      The 20th bit of the ID is resevered for the server's use for this
 *      purpose.  By setting CLIENT←ID(id) to the client, the SERVER←BIT to
 *      1, and an otherwise unused ID in the low 19 bits, we can create a
 *      resource "owned" by the client.
 *      
 *      The following IDs are currently reserved for siccing on the client:
 *      1 - allocated color to be freed when the client dies
 */

#include "X.h"
#include "misc.h"
#include "os.h"
#include "resource.h"
#include "dixstruct.h" 
#include "opaque.h"

#define CACHEDTYPES (RT←WINDOW | RT←PIXMAP | RT←GC)
#define INITBUCKETS 64
#define INITHASHSIZE 6
#define MAXHASHSIZE 11

typedef struct ←Resource {
    struct ←Resource	*next;
    XID			id;
    int			(*DeleteFunc)();
    unsigned short	type;
    unsigned short	class;
    pointer		value;
} ResourceRec, *ResourcePtr;
#define NullResource ((ResourcePtr)NULL)

typedef struct ←ClientResource {
    ResourcePtr *resources;
    int		elements;
    int		buckets;
    int		hashsize;	/* log(2)(buckets) */
    int		fakeID;
} ClientResourceRec;

static unsigned short lastResourceType;
static unsigned short lastResourceClass;

unsigned short
CreateNewResourceType()
{
    if (lastResourceType == 0x8000)	/* this is compiler dependent  XXX */
	lastResourceType = 0;
    lastResourceType <<= 1;
    return lastResourceType;
}

short
CreateNewResourceClass()
{
    return ++lastResourceClass;
}

ClientResourceRec clientTable[MAXCLIENTS];

/*****************
 * InitClientResources
 *    When a new client is created, call this to allocate space
 *    in resource table
 *****************/

InitClientResources(client)
    ClientPtr client;
{
    register int i, j;
 
    if (client == (ClientPtr)NULL)
    {
	lastResourceType = RT←LASTPREDEF;
	lastResourceClass = RC←LASTPREDEF;
    }
    clientTable[i = client->index].resources =
	(ResourcePtr *)Xalloc(INITBUCKETS*sizeof(ResourcePtr));
    clientTable[i].buckets = INITBUCKETS;
    clientTable[i].elements = 0;
    clientTable[i].hashsize = INITHASHSIZE;
    clientTable[i].fakeID = 100;
    for (j=0; j<INITBUCKETS; j++) 
    {
        clientTable[i].resources[j] = NullResource;
    }
}

static long
Hash(client, id)
    int client;
    register long id;
{
    id &= RESOURCE←ID←MASK;
    switch (clientTable[client].hashsize)
    {
	case 6:
	    return 0x03F & (id ↑ (id>>6) ↑ (id>>12));
	case 7:
	    return 0x07F & (id ↑ (id>>7) ↑ (id>>13));
	case 8:
	    return 0x0FF & (id ↑ (id>>8) ↑ (id>>16));
	case 9:
	    return 0x1FF & (id ↑ (id>>9));
	case 10:
	    return 0x3FF & (id ↑ (id>>10));
	case 11:
	    return 0x7FF & (id ↑ (id>>11));
    }
    return -1;
}

long
FakeClientID(client)
    int client;
{
	return (
	    (client<<CLIENTOFFSET) + (SERVER←BIT) +
	    ((clientTable[client].fakeID++) & RESOURCE←ID←MASK));
}

void
AddResource(id, type, value, func, class)
    long id;
    unsigned short type, class;
    pointer value;
    int (* func)();
{
    int client, j;
    ResourcePtr res, next, *head;
    	
    client = CLIENT←ID(id);
    if (!clientTable[client].buckets)
    {
	ErrorF("AddResource(%x, %d, %x, %d), client=%d \n",
		id, type, value, class, client);
        FatalError("client not in use\n");
    }
    if (!func)
    {
	ErrorF("AddResource(%x, %d, %x, %d), client=%d \n",
		id, type, value, class, client);
        FatalError("No delete function given to AddResource \n");
    }
    if ((clientTable[client].elements >= 4*clientTable[client].buckets) &&
	(clientTable[client].hashsize <= MAXHASHSIZE))
    {
	register ResourcePtr *resources = (ResourcePtr *)
	    Xalloc(2*clientTable[client].buckets*sizeof(ResourcePtr));
	for (j = 0; j < clientTable[client].buckets*2; j++)
	    resources[j] = NullResource;
	clientTable[client].hashsize++;
	for (j = 0; j < clientTable[client].buckets; j++)
	{
	    for (res = clientTable[client].resources[j]; res; res = next)
	    {
		next = res->next;
		head = &resources[Hash(client, res->id)];
		res->next = *head;
		*head = res;
	    }
	}
	clientTable[client].buckets *= 2;
	Xfree(clientTable[client].resources);
	clientTable[client].resources = resources;
    }
    head = &clientTable[client].resources[Hash(client, id)];
    res = (ResourcePtr)Xalloc(sizeof(ResourceRec));
    res->next = *head;
    res->id = id;
    res->DeleteFunc = func;
    res->type = type;
    res->class = class;
    res->value = value;
    *head = res;
    clientTable[client].elements++;
}

void
FreeResource(id, skipDeleteFuncClass)
long id; 
int skipDeleteFuncClass;

{
    unsigned    cid;
    register    ResourcePtr res;
    ResourcePtr * head;
    Bool gotOne = FALSE;

    if (((cid = CLIENT←ID(id)) < MaxClients) && clientTable[cid].buckets)
    {
	head = &clientTable[cid].resources[Hash(cid, id)];

	for (res = *head; res; res = *head)
	    if (res->id == id)
	    {
		*head = res->next;
		if (res->type & CACHEDTYPES)
		    FlushClientCaches(res->id);
		if (skipDeleteFuncClass != res->class)
		    (*res->DeleteFunc) ((int *)res->value, res->id);
		Xfree(res);
		gotOne = TRUE;
		clientTable[cid].elements--;
	    }
	    else
		head = &res->next;
    }
    if (!gotOne)
	FatalError("Freeing resource id=%X which isn't there", id);
}

void
FreeClientResources(client)
    ClientPtr client;
{
    register ResourcePtr *resources;
    register ResourcePtr this, next;
    int j;

    HandleSaveSet(client);

    resources = clientTable[client->index].resources;
    for (j=0; j < clientTable[client->index].buckets; j++) 
    {
        for (this = resources[j]; this; this = next)
	{
	    next = this->next;
	    if (this->type & CACHEDTYPES)
		FlushClientCaches(this->id);
	    (*this->DeleteFunc)((int *)this->value, this->id);
	    Xfree(this);	    
	}
    }
    Xfree(clientTable[client->index].resources);
    clientTable[client->index].buckets = 0;
}

FreeAllResources()
{
    int	i;

    for (i=0; i<currentMaxClients; i++) 
    {
        if (clientTable[i].buckets) 
	    FreeClientResources(clients[i]);
    }
}

/*
 *  LookupID returns the value field in the resource or NULL
 *  if an illegal id was handed to it or given type doesn't match the
 *  type for this id and class.
 */ 
pointer
LookupID(id, rType, class)
    unsigned long id;
    unsigned short rType, class;
{
    unsigned    cid;
    register    ResourcePtr res;

    if (((cid = CLIENT←ID(id)) < MaxClients) && clientTable[cid].buckets)
    {
	res = clientTable[cid].resources[Hash(cid, id)];

	for (; res; res = res->next)
	    if ((res->id == id) && (res->class == class))
		if (res->type & rType)
		    return res->value;
		else
		    return(pointer) NULL;
    }
    return(pointer) NULL;
}