/*
 * Server-side USER handles
 *
 * Copyright (C) 2001 Alexandre Julliard
 */

#include "thread.h"
#include "user.h"
#include "server_memory.h"

struct user_handle
{
    void          *ptr;          /* pointer to object */
    unsigned short type;         /* object type (0 if free) */
    unsigned short generation;   /* generation counter */
};

START_FILE_STATIC_VARIABLES( user )
  struct user_handle *handles;
  struct user_handle *freelist;
  int nb_handles;
  int allocated_handles;
END_FILE_STATIC_VARIABLES( user )

static struct user_handle *handle_to_entry( user_handle_t handle )
{
    int index = (handle & 0xffff) - FIRST_USER_HANDLE;
    if (index < 0 || index >= SERVER_STATIC_VARIABLE( user, nb_handles )) return NULL;
    if (!SERVER_STATIC_VARIABLE( user, handles[index].type)) return NULL;
    if ((handle >> 16) && (handle >> 16 != SERVER_STATIC_VARIABLE( user, handles[index].generation ))) return NULL;
    return &SERVER_STATIC_VARIABLE( user, handles[index] );
}

inline static user_handle_t entry_to_handle( struct user_handle *ptr )
{
    int index = ptr - SERVER_STATIC_VARIABLE( user, handles );
    return (index + FIRST_USER_HANDLE) + (ptr->generation << 16);
}

inline static struct user_handle *alloc_user_entry(void)
{
    struct user_handle *handle;

    if (SERVER_STATIC_VARIABLE( user, freelist ))
    {
        handle = SERVER_STATIC_VARIABLE( user, freelist );
        SERVER_STATIC_VARIABLE( user, freelist ) = handle->ptr;
        return handle;
    }
    if (SERVER_STATIC_VARIABLE( user, nb_handles ) >= SERVER_STATIC_VARIABLE( user, allocated_handles ))  /* need to grow the array */
    {
        struct user_handle *new_handles;
        /* grow array by 50% (but at minimum 32 entries) */
        int growth = max( 32, SERVER_STATIC_VARIABLE( user, allocated_handles ) / 2 );
        int new_size = min( SERVER_STATIC_VARIABLE( user, allocated_handles ) + growth, LAST_USER_HANDLE-FIRST_USER_HANDLE+1 );
        if (new_size <= SERVER_STATIC_VARIABLE( user, allocated_handles )) return NULL;
        if (!(new_handles = mem_realloc( SERVER_STATIC_VARIABLE( user, handles ), new_size * sizeof(*SERVER_STATIC_VARIABLE( user, handles )) )))
            return NULL;
        SERVER_STATIC_VARIABLE( user, handles ) = new_handles;
        SERVER_STATIC_VARIABLE( user, allocated_handles ) = new_size;
    }
    handle = &SERVER_STATIC_VARIABLE( user, handles[SERVER_STATIC_VARIABLE( user, nb_handles )++] );
    handle->generation = 0;
    return handle;
}

inline static void *free_user_entry( struct user_handle *ptr )
{
    void *ret;
    ret = ptr->ptr;
    ptr->ptr  = SERVER_STATIC_VARIABLE( user, freelist );
    ptr->type = 0;
    SERVER_STATIC_VARIABLE( user, freelist )  = ptr;
    return ret;
}

/* allocate a user handle for a given object */
user_handle_t alloc_user_handle( void *ptr, enum user_object type )
{
    struct user_handle *entry = alloc_user_entry();
    if (!entry) return 0;
    entry->ptr  = ptr;
    entry->type = type;
    if (++entry->generation >= 0xffff) entry->generation = 1;
    return entry_to_handle( entry );
}

/* return a pointer to a user object from its handle */
void *get_user_object( user_handle_t handle, enum user_object type )
{
    struct user_handle *entry;

    if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
    return entry->ptr;
}

/* get the full handle for a possibly truncated handle */
user_handle_t get_user_full_handle( user_handle_t handle )
{
    struct user_handle *entry;

    if (handle >> 16) return handle;
    if (!(entry = handle_to_entry( handle ))) return handle;
    return entry_to_handle( entry );
}

/* same as get_user_object plus set the handle to the full 32-bit value */
void *get_user_object_handle( user_handle_t *handle, enum user_object type )
{
    struct user_handle *entry;

    if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
    *handle = entry_to_handle( entry );
    return entry->ptr;
}

/* free a user handle and return a pointer to the object */
void *free_user_handle( user_handle_t handle )
{
    struct user_handle *entry;

    if (!(entry = handle_to_entry( handle )))
    {
        set_error( STATUS_INVALID_HANDLE );
        return NULL;
    }
    return free_user_entry( entry );
}

/* return the next user handle after 'handle' that is of a given type */
void *next_user_handle( user_handle_t *handle, enum user_object type )
{
    struct user_handle *entry;

    if (!*handle) entry = SERVER_STATIC_VARIABLE( user, handles );
    else
    {
        if (!(entry = handle_to_entry( *handle ))) return NULL;
        entry++;  /* start from the next one */
    }
    while (entry < SERVER_STATIC_VARIABLE( user, handles ) + SERVER_STATIC_VARIABLE( user, nb_handles ))
    {
        if (!type || entry->type == type)
        {
            *handle = entry_to_handle( entry );
            return entry->ptr;
        }
        entry++;
    }
    return NULL;
}
