Sweden-Number/server/object.c

95 lines
2.2 KiB
C

/*
* Server-side objects
* These are the server equivalent of K32OBJ
*
* Copyright (C) 1998 Alexandre Julliard
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "server.h"
#include "object.h"
struct object_name
{
struct object_name *next;
struct object *obj;
int len;
char name[1];
};
#define NAME_HASH_SIZE 37
static struct object_name *names[NAME_HASH_SIZE];
/*****************************************************************/
static int get_name_hash( const char *name )
{
int hash = 0;
while (*name) hash ^= *name++;
return hash % NAME_HASH_SIZE;
}
static struct object_name *add_name( struct object *obj, const char *name )
{
struct object_name *ptr;
int hash = get_name_hash( name );
int len = strlen( name );
if (!(ptr = (struct object_name *)malloc( sizeof(*ptr) + len )))
return NULL;
ptr->next = names[hash];
ptr->obj = obj;
ptr->len = len;
strcpy( ptr->name, name );
names[hash] = ptr;
return ptr;
}
static void free_name( struct object *obj )
{
int hash = get_name_hash( obj->name->name );
struct object_name **pptr = &names[hash];
while (*pptr && *pptr != obj->name) pptr = &(*pptr)->next;
assert( *pptr );
*pptr = (*pptr)->next;
free( obj->name );
}
/* initialize an already allocated object */
void init_object( struct object *obj, const struct object_ops *ops,
const char *name )
{
obj->refcount = 1;
obj->ops = ops;
if (!name) obj->name = NULL;
else obj->name = add_name( obj, name );
}
/* release an object (i.e. decrement its refcount */
void release_object( void *ptr )
{
struct object *obj = (struct object *)ptr;
assert( obj->refcount );
if (!--obj->refcount)
{
if (obj->name) free_name( obj );
obj->ops->destroy( obj );
}
}
/* find an object by its name; the refcount is incremented */
struct object *find_object( const char *name )
{
int hash = get_name_hash( name );
struct object_name *ptr = names[hash];
while (ptr && strcmp( ptr->name, name )) ptr = ptr->next;
if (!ptr) return NULL;
ptr->obj->refcount++;
return ptr->obj;
}