/* * Revision Control Information * * /projects/hsis/CVS/utilities/st/st.c,v * serdar * 1.1 * 1993/07/29 01:00:13 * */ #include #include "misc/extra/extra.h" #include "stmm.h" ABC_NAMESPACE_IMPL_START #define STMM_NUMCMP(x,y) ((x) != (y)) #define STMM_NUMHASH(x,size) (Abc_AbsInt((long)x)%(size)) //#define STMM_PTRHASH(x,size) ((int)((ABC_PTRUINT_T)(x)>>2)%size) // 64-bit bug fix 9/17/2007 #define STMM_PTRHASH(x,size) ((int)(((ABC_PTRUINT_T)(x)>>2)%size)) #define EQUAL(func, x, y) \ ((((func) == stmm_numcmp) || ((func) == stmm_ptrcmp)) ?\ (STMM_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0)) #define do_hash(key, table)\ ((table->hash == stmm_ptrhash) ? STMM_PTRHASH((key),(table)->num_bins) :\ (table->hash == stmm_numhash) ? STMM_NUMHASH((key), (table)->num_bins) :\ (*table->hash)((key), (table)->num_bins)) static int rehash (stmm_table *table); //int stmm_numhash (), stmm_ptrhash (), stmm_numcmp (), stmm_ptrcmp (); stmm_table * stmm_init_table_with_params (stmm_compare_func_type compare, stmm_hash_func_type hash, int size, int density, double grow_factor, int reorder_flag) { int i; stmm_table *newTable; newTable = ABC_ALLOC(stmm_table, 1); if (newTable == NULL) { return NULL; } newTable->compare = compare; newTable->hash = hash; newTable->num_entries = 0; newTable->max_density = density; newTable->grow_factor = grow_factor; newTable->reorder_flag = reorder_flag; if (size <= 0) { size = 1; } newTable->num_bins = size; newTable->bins = ABC_ALLOC(stmm_table_entry *, size); if (newTable->bins == NULL) { ABC_FREE(newTable); return NULL; } for (i = 0; i < size; i++) { newTable->bins[i] = 0; } // added by alanmi newTable->pMemMan = Extra_MmFixedStart(sizeof (stmm_table_entry)); return newTable; } stmm_table * stmm_init_table (stmm_compare_func_type compare, stmm_hash_func_type hash) { return stmm_init_table_with_params (compare, hash, STMM_DEFAULT_INIT_TABLE_SIZE, STMM_DEFAULT_MAX_DENSITY, STMM_DEFAULT_GROW_FACTOR, STMM_DEFAULT_REORDER_FLAG); } void stmm_free_table (stmm_table *table) { /* stmm_table_entry *ptr, *next; int i; for ( i = 0; i < table->num_bins; i++ ) { ptr = table->bins[i]; while ( ptr != NULL ) { next = ptr->next; ABC_FREE( ptr ); ptr = next; } } */ // no need to deallocate entries because they are in the memory manager now // added by alanmi if ( table->pMemMan ) Extra_MmFixedStop ((Extra_MmFixed_t *)table->pMemMan); ABC_FREE(table->bins); ABC_FREE(table); } // this function recycles all the bins void stmm_clean (stmm_table *table) { int i; // clean the bins for (i = 0; i < table->num_bins; i++) table->bins[i] = NULL; // reset the parameters table->num_entries = 0; // restart the memory manager Extra_MmFixedRestart ((Extra_MmFixed_t *)table->pMemMan); } #define PTR_NOT_EQUAL(table, ptr, user_key)\ (ptr != NULL && !EQUAL(table->compare, user_key, (ptr)->key)) #define FIND_ENTRY(table, hash_val, key, ptr, last) \ (last) = &(table)->bins[hash_val];\ (ptr) = *(last);\ while (PTR_NOT_EQUAL((table), (ptr), (key))) {\ (last) = &(ptr)->next; (ptr) = *(last);\ }\ if ((ptr) != NULL && (table)->reorder_flag) {\ *(last) = (ptr)->next;\ (ptr)->next = (table)->bins[hash_val];\ (table)->bins[hash_val] = (ptr);\ } int stmm_lookup (stmm_table *table, char *key, char **value) { int hash_val; stmm_table_entry *ptr, **last; hash_val = do_hash (key, table); FIND_ENTRY (table, hash_val, key, ptr, last); if (ptr == NULL) { return 0; } else { if (value != NULL) { *value = ptr->record; } return 1; } } int stmm_lookup_int (stmm_table *table, char *key, int *value) { int hash_val; stmm_table_entry *ptr, **last; hash_val = do_hash (key, table); FIND_ENTRY (table, hash_val, key, ptr, last); if (ptr == NULL) { return 0; } else { if (value != 0) { *value = (long) ptr->record; } return 1; } } // This macro contained a line // new = ABC_ALLOC(stmm_table_entry, 1); // which was modified by alanmi /* This macro does not check if memory allocation fails. Use at you own risk */ #define ADD_DIRECT(table, key, value, hash_val, new)\ {\ if (table->num_entries/table->num_bins >= table->max_density) {\ rehash(table);\ hash_val = do_hash(key,table);\ }\ \ new = (stmm_table_entry *)Extra_MmFixedEntryFetch( (Extra_MmFixed_t *)table->pMemMan );\ \ new->key = key;\ new->record = value;\ new->next = table->bins[hash_val];\ table->bins[hash_val] = new;\ table->num_entries++;\ } int stmm_insert (stmm_table *table, char *key, char *value) { int hash_val; stmm_table_entry *newEntry; stmm_table_entry *ptr, **last; hash_val = do_hash (key, table); FIND_ENTRY (table, hash_val, key, ptr, last); if (ptr == NULL) { if (table->num_entries / table->num_bins >= table->max_density) { if (rehash (table) == STMM_OUT_OF_MEM) { return STMM_OUT_OF_MEM; } hash_val = do_hash (key, table); } // newEntry = ABC_ALLOC( stmm_table_entry, 1 ); newEntry = (stmm_table_entry *) Extra_MmFixedEntryFetch ((Extra_MmFixed_t *)table->pMemMan); if (newEntry == NULL) { return STMM_OUT_OF_MEM; } newEntry->key = key; newEntry->record = value; newEntry->next = table->bins[hash_val]; table->bins[hash_val] = newEntry; table->num_entries++; return 0; } else { ptr->record = value; return 1; } } int stmm_add_direct (stmm_table *table, char *key, char *value) { int hash_val; stmm_table_entry *newEntry; hash_val = do_hash (key, table); if (table->num_entries / table->num_bins >= table->max_density) { if (rehash (table) == STMM_OUT_OF_MEM) { return STMM_OUT_OF_MEM; } } hash_val = do_hash (key, table); // newEntry = ABC_ALLOC( stmm_table_entry, 1 ); newEntry = (stmm_table_entry *) Extra_MmFixedEntryFetch ((Extra_MmFixed_t *)table->pMemMan); if (newEntry == NULL) { return STMM_OUT_OF_MEM; } newEntry->key = key; newEntry->record = value; newEntry->next = table->bins[hash_val]; table->bins[hash_val] = newEntry; table->num_entries++; return 1; } int stmm_find_or_add (stmm_table *table, char *key, char ***slot) { int hash_val; stmm_table_entry *newEntry, *ptr, **last; hash_val = do_hash (key, table); FIND_ENTRY (table, hash_val, key, ptr, last); if (ptr == NULL) { if (table->num_entries / table->num_bins >= table->max_density) { if (rehash (table) == STMM_OUT_OF_MEM) { return STMM_OUT_OF_MEM; } hash_val = do_hash (key, table); } // newEntry = ABC_ALLOC( stmm_table_entry, 1 ); newEntry = (stmm_table_entry *) Extra_MmFixedEntryFetch ((Extra_MmFixed_t *)table->pMemMan); if (newEntry == NULL) { return STMM_OUT_OF_MEM; } newEntry->key = key; newEntry->record = (char *) 0; newEntry->next = table->bins[hash_val]; table->bins[hash_val] = newEntry; table->num_entries++; if (slot != NULL) *slot = &newEntry->record; return 0; } else { if (slot != NULL) *slot = &ptr->record; return 1; } } int stmm_find (stmm_table *table, char *key, char ***slot) { int hash_val; stmm_table_entry *ptr, **last; hash_val = do_hash (key, table); FIND_ENTRY (table, hash_val, key, ptr, last); if (ptr == NULL) { return 0; } else { if (slot != NULL) { *slot = &ptr->record; } return 1; } } static int rehash (stmm_table *table) { stmm_table_entry *ptr, *next, **old_bins; int i, old_num_bins, hash_val, old_num_entries; /* save old values */ old_bins = table->bins; old_num_bins = table->num_bins; old_num_entries = table->num_entries; /* rehash */ table->num_bins = (int) (table->grow_factor * old_num_bins); if (table->num_bins % 2 == 0) { table->num_bins += 1; } table->num_entries = 0; table->bins = ABC_ALLOC(stmm_table_entry *, table->num_bins); if (table->bins == NULL) { table->bins = old_bins; table->num_bins = old_num_bins; table->num_entries = old_num_entries; return STMM_OUT_OF_MEM; } /* initialize */ for (i = 0; i < table->num_bins; i++) { table->bins[i] = 0; } /* copy data over */ for (i = 0; i < old_num_bins; i++) { ptr = old_bins[i]; while (ptr != NULL) { next = ptr->next; hash_val = do_hash (ptr->key, table); ptr->next = table->bins[hash_val]; table->bins[hash_val] = ptr; table->num_entries++; ptr = next; } } ABC_FREE(old_bins); return 1; } stmm_table * stmm_copy (stmm_table *old_table) { stmm_table *newEntry_table; stmm_table_entry *ptr, /* *newEntryptr, *next, */ *newEntry; int i, /*j, */ num_bins = old_table->num_bins; newEntry_table = ABC_ALLOC(stmm_table, 1); if (newEntry_table == NULL) { return NULL; } *newEntry_table = *old_table; newEntry_table->bins = ABC_ALLOC(stmm_table_entry *, num_bins); if (newEntry_table->bins == NULL) { ABC_FREE(newEntry_table); return NULL; } // allocate the memory manager for the newEntry table newEntry_table->pMemMan = Extra_MmFixedStart (sizeof (stmm_table_entry)); for (i = 0; i < num_bins; i++) { newEntry_table->bins[i] = NULL; ptr = old_table->bins[i]; while (ptr != NULL) { // newEntry = ABC_ALLOC( stmm_table_entry, 1 ); newEntry = (stmm_table_entry *)Extra_MmFixedEntryFetch ((Extra_MmFixed_t *)newEntry_table->pMemMan); if (newEntry == NULL) { /* for ( j = 0; j <= i; j++ ) { newEntryptr = newEntry_table->bins[j]; while ( newEntryptr != NULL ) { next = newEntryptr->next; ABC_FREE( newEntryptr ); newEntryptr = next; } } */ Extra_MmFixedStop ((Extra_MmFixed_t *)newEntry_table->pMemMan); ABC_FREE(newEntry_table->bins); ABC_FREE(newEntry_table); return NULL; } *newEntry = *ptr; newEntry->next = newEntry_table->bins[i]; newEntry_table->bins[i] = newEntry; ptr = ptr->next; } } return newEntry_table; } int stmm_delete (stmm_table *table, char **keyp, char **value) { int hash_val; char *key = *keyp; stmm_table_entry *ptr, **last; hash_val = do_hash (key, table); FIND_ENTRY (table, hash_val, key, ptr, last); if (ptr == NULL) { return 0; } *last = ptr->next; if (value != NULL) *value = ptr->record; *keyp = ptr->key; // ABC_FREE( ptr ); Extra_MmFixedEntryRecycle ((Extra_MmFixed_t *)table->pMemMan, (char *) ptr); table->num_entries--; return 1; } int stmm_delete_int (stmm_table *table, long *keyp, char **value) { int hash_val; char *key = (char *) *keyp; stmm_table_entry *ptr, **last; hash_val = do_hash (key, table); FIND_ENTRY (table, hash_val, key
import pytest

import env  # noqa: F401
from pybind11_tests import ConstructorStats
from pybind11_tests import call_policies as m


@pytest.mark.xfail("env.PYPY", reason="sometimes comes out 1 off on PyPy", strict=False)
def test_keep_alive_argument(capture):
    n_inst = ConstructorStats.detail_reg_inst()
    with capture:
        p = m.Parent()
    assert capture == "Allocating parent."
    with capture:
        p.addChild(m.Child())
        assert ConstructorStats.detail_reg_inst() == n_inst + 1
    assert (
        capture
        == """
        Allocating child.
        Releasing child.
    """
    )
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == "Releasing parent."

    with capture:
        p = m.Parent()
    assert capture == "Allocating parent."
    with capture:
        p.addChildKeepAlive(m.Child())
        assert ConstructorStats.detail_reg_inst() == n_inst + 2
    assert capture == "Allocating child."
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert (
        capture
        == """
        Releasing parent.
        Releasing child.
    """
    )

    p = m.Parent()
    c = m.Child()
    assert ConstructorStats.detail_reg_inst() == n_inst + 2
    m.free_function(p, c)
    del c
    assert ConstructorStats.detail_reg_inst() == n_inst + 2
    del p
    assert ConstructorStats.detail_reg_inst() == n_inst

    with pytest.raises(RuntimeError) as excinfo:
        m.invalid_arg_index()
    assert str(excinfo.value) == "Could not activate keep_alive!"


def test_keep_alive_return_value(capture):
    n_inst = ConstructorStats.detail_reg_inst()
    with capture:
        p = m.Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnChild()
        assert ConstructorStats.detail_reg_inst() == n_inst + 1
    assert (
        capture
        == """
        Allocating child.
        Releasing child.
    """
    )
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == "Releasing parent."

    with capture:
        p = m.Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnChildKeepAlive()
        assert ConstructorStats.detail_reg_inst() == n_inst + 2
    assert capture == "Allocating child."
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert (
        capture
        == """
        Releasing parent.
        Releasing child.
    """
    )

    p = m.Parent()
    assert ConstructorStats.detail_reg_inst() == n_inst + 1
    with capture:
        m.Parent.staticFunction(p)
        assert ConstructorStats.detail_reg_inst() == n_inst + 2
    assert capture == "Allocating child."
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert (
        capture
        == """
        Releasing parent.
        Releasing child.
    """
    )


# https://foss.heptapod.net/pypy/pypy/-/issues/2447
@pytest.mark.xfail("env.PYPY", reason="_PyObject_GetDictPtr is unimplemented")
def test_alive_gc(capture):
    n_inst = ConstructorStats.detail_reg_inst()
    p = m.ParentGC()
    p.addChildKeepAlive(m.Child())
    assert ConstructorStats.detail_reg_inst() == n_inst + 2
    lst = [p]
    lst.append(lst)  # creates a circular reference
    with capture:
        del p, lst
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert (
        capture
        == """
        Releasing parent.
        Releasing child.
    """
    )


def test_alive_gc_derived(capture):
    class Derived(m.Parent):
        pass

    n_inst = ConstructorStats.detail_reg_inst()
    p = Derived()
    p.addChildKeepAlive(m.Child())
    assert ConstructorStats.detail_reg_inst() == n_inst + 2
    lst = [p]
    lst.append(lst)  # creates a circular reference
    with capture:
        del p, lst
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert (
        capture
        == """
        Releasing parent.
        Releasing child.
    """
    )


def test_alive_gc_multi_derived(capture):
    class Derived(m.Parent, m.Child):
        def __init__(self):
            m.Parent.__init__(self)
            m.Child.__init__(self)

    n_inst = ConstructorStats.detail_reg_inst()
    p = Derived()
    p.addChildKeepAlive(m.Child())
    # +3 rather than +2 because Derived corresponds to two registered instances
    assert ConstructorStats.detail_reg_inst() == n_inst + 3
    lst = [p]
    lst.append(lst)  # creates a circular reference
    with capture:
        del p, lst
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert (
        capture
        == """
        Releasing parent.
        Releasing child.
        Releasing child.
    """
    )


def test_return_none(capture):
    n_inst = ConstructorStats.detail_reg_inst()
    with capture:
        p = m.Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnNullChildKeepAliveChild()
        assert ConstructorStats.detail_reg_inst() == n_inst + 1
    assert capture == ""
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == "Releasing parent."

    with capture:
        p = m.Parent()
    assert capture == "Allocating parent."
    with capture:
        p.returnNullChildKeepAliveParent()
        assert ConstructorStats.detail_reg_inst() == n_inst + 1
    assert capture == ""
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert capture == "Releasing parent."


def test_keep_alive_constructor(capture):
    n_inst = ConstructorStats.detail_reg_inst()

    with capture:
        p = m.Parent(m.Child())
        assert ConstructorStats.detail_reg_inst() == n_inst + 2
    assert (
        capture
        == """
        Allocating child.
        Allocating parent.
    """
    )
    with capture:
        del p
        assert ConstructorStats.detail_reg_inst() == n_inst
    assert (
        capture
        == """
        Releasing parent.
        Releasing child.
    """
    )


def test_call_guard():
    assert m.unguarded_call() == "unguarded"
    assert m.guarded_call() == "guarded"

    assert m.multiple_guards_correct_order() == "guarded & guarded"
    assert m.multiple_guards_wrong_order() == "unguarded & guarded"

    if hasattr(m, "with_gil"):
        assert m.with_gil() == "GIL held"
        assert m.without_gil() == "GIL released"