/*
Copyright (C) 2005-2009, Parrot Foundation.
$Id: addrregistry.pmc 46300 2010-05-05 03:58:50Z jimmy $

=head1 NAME

src/pmc/addrregistry.pmc - A GC Registry PMC

=head1 DESCRIPTION

The AddrRegistry class provides the equivalence of reference counts
mainly for extenders and embedders of Parrot. The hash keys are the
addresses of the key PMC, values are reference counts, i.e. the
difference of (set_pmc_keyed - delete_pmc_keyed). If the reference
goes to zero, the entry is deleted physically.

Please note that you have to anchor an instance of AddrRegistry yourself with
C<Parrot_register_pmc> if it isn't visible to Parrot.

=head2 Functions

=over 4

=cut

*/

#include "parrot/hash.h"
#include "pmc/pmc_hash.h"

/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */

pmclass AddrRegistry extends Hash provides hash auto_attrs {
/*

=item C<void init()>

Initializes the instance.

=cut

*/

    VTABLE void init() {
        Hash  *registry = parrot_create_hash(INTERP,
                enum_type_int,
                Hash_key_type_PMC,
                int_compare,
                key_hash_int);

        SET_ATTR_hash(INTERP, SELF, registry);
        PObj_custom_mark_destroy_SETALL(SELF);

    }

/*

=item C<INTVAL get_integer_keyed(PMC *key)>

Returns the reference count for C<key> or 0 if the key doesn't exist.

=item C<INTVAL elements()>

Returns the number of elements in the hash.

=item C<INTVAL get_bool()>

Returns true if the hash size is not zero.

=cut

*/

    VTABLE INTVAL get_integer_keyed(PMC *key) {
        Hash       *hash;
        const void *value;

        GET_ATTR_hash(INTERP, SELF, hash);
        value = parrot_hash_get(INTERP, hash, key);

        if (value)
            return (INTVAL)value;

        return 0;
    }

    VTABLE INTVAL elements() {
        const Hash *hash;

        GET_ATTR_hash(INTERP, SELF, hash);

        return parrot_hash_size(INTERP, hash);
    }

    VTABLE INTVAL get_bool() {
        const Hash *hash;

        GET_ATTR_hash(INTERP, SELF, hash);

        return parrot_hash_size(INTERP, hash) != 0;
    }
/*

=item C<void set_pmc_keyed(PMC *key, PMC *value)>

Increment the reference count of C<key>. If the entry doesn't exist
create it. The C<value> is always ignored.

=item C<void set_integer_keyed(PMC *key, INTVAL value)>

Set the given value.

=item C<void delete_keyed(PMC *key)>

Decrement the reference count of C<key>. If the reference count
reaches 0, delete the entry.


=cut

*/

    VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
        const void *oldval;
        long        newval = 1;
        Hash       *hash;
        UNUSED(value);

        GET_ATTR_hash(INTERP, SELF, hash);

        oldval = parrot_hash_get(INTERP, hash, key);

        if (oldval)
            newval += (long)oldval;

        parrot_hash_put(INTERP, hash, key, (void *)newval);
    }

    VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
        Hash *hash;
        GET_ATTR_hash(INTERP, SELF, hash);
        parrot_hash_put(INTERP, hash, key, (void *)value);
    }

    VTABLE void delete_keyed(PMC *key) {
        Hash       *hash;
        void       *value;

        GET_ATTR_hash(INTERP, SELF, hash);
        value = parrot_hash_get(INTERP, hash, key);

        /* these casts look bad, but they avoid type punning warnings with -O */
        if (value) {
            long val = (long)value;
            if (val == 1L)
                parrot_hash_delete(INTERP, hash, key);
            else {
                value = (void *)(--val);
                parrot_hash_put(INTERP, hash, key, value);
            }
        }
    }

}

/*

=back

=head1 SEE ALSO

F<src/pmc.c:gc_register_pmc()>

=cut

*/

/*
 * Local variables:
 *   c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
