/* -*- mode: c ; c-file-style: "canonware-c-style" -*-
 ******************************************************************************
 *
 * Copyright (C) 1996-2004 Jason Evans <jasone@canonware.com>.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice(s), this list of conditions and the following disclaimer
 *    unmodified other than the allowable addition of one or more
 *    copyright notices.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice(s), this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 *
 * Version: Onyx 5.1.1
 *
 ******************************************************************************/

typedef struct cw_nxoe_name_s cw_nxoe_name_t;

struct cw_nxoe_name_s
{
    cw_nxoe_t nxoe;

    /* str is not required to be NULL-terminated, so we keep track of the
     * length. */
    const char *str;
    uint32_t len;

    cw_chi_t chi;
    ql_elm(cw_nxoe_name_t) link;
};

/* Global variables. */
#ifdef CW_THREADS
extern cw_mtx_t cw_g_name_lock;
#endif
extern cw_dch_t cw_g_name_hash;
typedef ql_head(cw_nxoe_name_t) cw_name_list_t;
extern cw_name_list_t cw_g_name_list;

uint32_t
nxo_l_name_hash(const void *a_key);

bool
nxo_l_name_key_comp(const void *a_k1, const void *a_k2);

#ifndef CW_USE_INLINES
#ifdef CW_THREADS
cw_mtx_t *
nxo_l_name_lock_get(void);
#endif

void
nxo_l_name_list_prune(bool a_white);

bool
nxoe_l_name_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter);

cw_nxoe_t *
nxoe_l_name_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset);
#endif

#if (defined(CW_USE_INLINES) || defined(CW_NXO_NAME_C_))
#ifdef CW_THREADS
CW_INLINE cw_mtx_t *
nxo_l_name_lock_get(void)
{
    cw_assert(cw_g_nxa_initialized);

    return &cw_g_name_lock;
}
#endif

CW_INLINE void
nxo_l_name_list_prune(bool a_white)
{
    cw_nxoe_name_t *name;

    for (name = ql_last(&cw_g_name_list, link);
	 name != NULL && nxoe_l_color_get(&name->nxoe) == a_white;
	 name = ql_last(&cw_g_name_list, link))
    {
	dch_chi_remove(&cw_g_name_hash, &name->chi);
	ql_remove(&cw_g_name_list, name, link);
    }
}

CW_INLINE bool
nxoe_l_name_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter)
{
    cw_nxoe_name_t *name;

    name = (cw_nxoe_name_t *) a_nxoe;

    cw_check_ptr(name);
    cw_dassert(name->nxoe.magic == CW_NXOE_MAGIC);
    cw_assert(name->nxoe.type == NXOT_NAME);

    if (name->nxoe.name_static == false)
    {
	/* Cast away the const here; it's one of two places that the string is
	 * allowed to be modified, and this cast is better than dropping the
	 * const altogether. */
	nxa_free((char *) name->str, name->len);
    }

    nxa_free(name, sizeof(cw_nxoe_name_t));

    return false;
}

CW_INLINE cw_nxoe_t *
nxoe_l_name_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset)
{
    cw_nxoe_name_t *name;

    name = (cw_nxoe_name_t *) a_nxoe;

    cw_check_ptr(name);
    cw_dassert(name->nxoe.magic == CW_NXOE_MAGIC);
    cw_assert(name->nxoe.type == NXOT_NAME);

    /* Move this name to the beginning of the name list, in order to facilitate
     * removal of all garbage names from the name hash at the end of the mark
     * phase. */
    ql_remove(&cw_g_name_list, name, link);
    ql_head_insert(&cw_g_name_list, name, link);

    return NULL;
}
#endif /* (defined(CW_USE_INLINES) || defined(CW_NXO_NAME_C_)) */
