/* -*- mode: c ; c-file-style: "canonware-c-style" -*-
 ******************************************************************************
 *
 * Copyright (C) 1996-2002 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 4.1.0
 *
 ******************************************************************************/

/* Memory allocator. */
struct cw_nxa_s
{
#ifdef CW_DBG
    cw_uint32_t magic;
#define CW_NXA_MAGIC 0x63935743
#endif
#ifdef CW_THREADS
    /* Protects the gcdict_* fields and gc_pending. */
    cw_mtx_t lock;
#endif

    /* Dictionary that contains stats and flags, available from within the
     * interpreter. */
    cw_nxo_t gcdict;

    /* Actual state of gcdict. */
    cw_bool_t gcdict_active;
#ifdef CW_PTHREADS
    cw_nxoi_t gcdict_period;
#endif
    cw_nxoi_t gcdict_threshold;
    cw_nxoi_t gcdict_collections;
    cw_nxoi_t gcdict_count;
    cw_nxoi_t gcdict_current[3];
    cw_nxoi_t gcdict_maximum[3];
    cw_nxoi_t gcdict_sum[3];

    /* Sequence set. */
#ifdef CW_THREADS
    cw_mtx_t seq_mtx;
#endif
    ql_head(cw_nxoe_t) seq_set;
    cw_bool_t white; /* Current value for white (alternates). */

#ifdef CW_PTHREADS
    cw_mq_t gc_mq;
#endif
    cw_bool_t gc_pending;
    cw_bool_t gc_allocated;

    cw_nx_t *nx;
#ifdef CW_PTHREADS
    cw_thd_t *gc_thd;
#endif
};

void *
nxa_malloc_e(cw_nxa_t *a_nxa, size_t a_size, const char *a_filename,
	     cw_uint32_t a_line_num);

void *
nxa_realloc_e(cw_nxa_t *a_nxa, void *a_ptr, size_t a_size, size_t
	      a_old_size, const char *a_filename, cw_uint32_t a_line_num);

void
nxa_free_e(cw_nxa_t *a_nxa, void *a_ptr, size_t a_size, const char *a_filename,
	   cw_uint32_t a_line_num);

#ifdef CW_DBG
#define nxa_malloc(a_nxa, a_size)					\
    nxa_malloc_e((a_nxa), (a_size), __FILE__, __LINE__)
#define nxa_free(a_nxa, a_ptr, a_size)					\
    nxa_free_e((a_nxa), (a_ptr), (a_size), __FILE__, __LINE__)
#else
#define nxa_malloc(a_nxa, a_size)					\
    nxa_malloc_e((a_nxa), (a_size), NULL, 0)
#define nxa_free(a_nxa, a_ptr, a_size)					\
    nxa_free_e((a_nxa), (a_ptr), (a_size), NULL, 0)
#endif

void
nxa_collect(cw_nxa_t *a_nxa);

cw_bool_t
nxa_active_get(cw_nxa_t *a_nxa);

void
nxa_active_set(cw_nxa_t *a_nxa, cw_bool_t a_active);

#ifdef CW_PTHREADS
cw_nxoi_t
nxa_period_get(cw_nxa_t *a_nxa);

void
nxa_period_set(cw_nxa_t *a_nxa, cw_nxoi_t a_period);
#endif
cw_nxoi_t
nxa_threshold_get(cw_nxa_t *a_nxa);

void
nxa_threshold_set(cw_nxa_t *a_nxa, cw_nxoi_t a_threshold);

void
nxa_stats_get(cw_nxa_t *a_nxa, cw_nxoi_t *r_collections, cw_nxoi_t *r_count,
	      cw_nxoi_t *r_ccount, cw_nxoi_t *r_cmark, cw_nxoi_t *r_csweep,
	      cw_nxoi_t *r_mcount, cw_nxoi_t *r_mmark, cw_nxoi_t *r_msweep,
	      cw_nxoi_t *r_scount, cw_nxoi_t *r_smark, cw_nxoi_t *r_ssweep);

#ifndef CW_USE_INLINES
cw_nx_t *
nxa_nx_get(cw_nxa_t *a_nxa);

cw_nxo_t *
nxa_gcdict_get(cw_nxa_t *a_nxa);
#endif

#if (defined(CW_USE_INLINES) || defined(CW_NXA_C_))
CW_INLINE cw_nx_t *
nxa_nx_get(cw_nxa_t *a_nxa)
{
    cw_check_ptr(a_nxa);
    cw_dassert(a_nxa->magic == CW_NXA_MAGIC);

    return a_nxa->nx;
}

CW_INLINE cw_nxo_t *
nxa_gcdict_get(cw_nxa_t *a_nxa)
{
    cw_check_ptr(a_nxa);
    cw_dassert(a_nxa->magic == CW_NXA_MAGIC);

    return &a_nxa->gcdict;
}
#endif /* (defined(CW_USE_INLINES) || defined(CW_NXA_C_)) */
