<commit_before>/* $Id$ */

/*
 * TODO
 *
 * An object reference has the following memory layout:
 *
 *	+-----------------------------------------------+
 *	| struct psc_objref				|
 *	+-----------------------------------------------+
 *	| struct psc_refmgr * if PRMF_TREE		|
 *	| struct psclist_head if PRMF_LIST and PRMF_LRU	|
 *	| struct psclist_head if (LIST or LRU) and HASH	|
 *	| SPLAY_ENTRY() if PRMF_TREE			|
 *	+-----------------------------------------------+
 *	| private object-specific data			|
 *	+-----------------------------------------------+
 *
 * XXX need a poolmgr pointer to avoid returning to different poolmgrs.
 */

void
psc_refmgr_init(struct psc_refmgr *prm, int flags, int privsiz, int nobjs,
    int min, int max, int (*initf)(struct psc_poolmgr *, void *),
    void (*destroyf)(void *), const char *namefmt, ...)
{
	int off = 0, pool_lentry_avail = 1;
	va_list ap;

	memset(prm, 0, sizeof(*prm));
	prm->prm_flags = flags;

	off = sizeof(struct psc_objref);
	if (prm->prm_flags & PRMF_TREE) {
		prm->prm_refmgr_offset = off;
		off += sizeof(void *);
	}
	if (prm->prm_flags & PRMF_LIST) {
		prm->prm_list_offset = offsetof(struct psc_objref,
		    pobj_pool_lentry);
		pool_lentry_avail = 0;
	}
	if (prm->prm_flags & PRMF_LRU) {
		if (pool_lentry_avail) {
			prm->prm_lru_offset = offsetof(struct psc_objref,
			    pobj_pool_lentry);
			pool_lentry_avail = 0;
		} else {
			prm->prm_lru_offset = off;
			off += sizeof(struct psclist_head);
		}
	}
	if (prm->prm_flags & PRMF_HASH) {
		if (pool_lentry_avail) {
			prm->prm_hash_offset = offsetof(struct psc_objref,
			    pobj_pool_lentry);
			pool_lentry_avail = 0;
		} else {
			prm->prm_hash_offset = off;
			off += sizeof(struct psc_hashent);
		}
	}
	if (prm->prm_flags & PRMF_TREE) {
		prm->prm_tree_offset = off;
		off += sizeof(SPLAY_ENTRY(psc_objref));
	}
	prm->prm_private_offset = off;

	flags = 0;
	if (prm->prm_flags & PRMF_AUTOSIZE)
		flags |= PPMF_AUTO;
	if (prm->prm_flags & PRMF_NOMEMPIN)
		flags |= PPMF_NOLOCK;

	va_start(ap, namefmt);
	_psc_poolmaster_initv(&prm->prm_pms, off + privsiz, off, flags,
	    nobjs, min, max, initf, destroyf, NULL,
	    psc_refmgr_reclaimcb, namefmt, ap);
	va_end(ap);

	if (prm->prm_flags & PRMF_LIST)
		lc_reginit(&prm->prm_list, prm->prm_pms->pms_name, NULL);
	if (prm->prm_flags & PRMF_LRU)
		lc_reginit(&prm->prm_lru);
	if (prm->prm_flags & PRMF_TREE) {
		SPLAY_INIT(psc_objref_tree, &prm->prm_tree);
		LOCK_INIT(&prm->prm_lock);
	}
	if (prm->prm_flags & PRMF_HASH) {
		flags = 0;
		if (prm->prm_flags & PRMF_HASHSTR)
			flags |= PHTF_STR;

		_psc_hashtbl_init(&prm->prm_hashtbl, flags, hashidmemboff,
		    prm->prm_hash_offset, nbuckets, hashcmpf, "%s",
		    prm->prm_pms->pms_name);
	}
}

/*
 * psc_refmgr_lock - Lock a reference manager for a write operation.
 */
void
psc_refmgr_lock_generic(struct psc_refmgr *prm)
{
	struct psc_hashbkt *b;

	if (prm->prm_flags & PRMF_LIST)<commit_msg>more progress<commit_after>/* $Id$ */

/*
 * An object reference has the following memory layout:
 *
 *	+-------------------------------------------------------+
 *	| struct psc_objref					|
 *	+-------------------------------------------------------+
 *	| SPLAY_ENTRY() if PRMF_TREE				|
 *	| struct psc_refmgr * if PRMF_TREE			|
 *	| struct psclist_head (for pool and (LIST|LRU|HASH))	|
 *	| struct psclist_head (if LIST and LRU)			|
 *	| struct psclist_head (if (LIST or LRU) and HASH)	|
 *	+-------------------------------------------------------+
 *	| private object-specific data				|
 *	+-------------------------------------------------------+
 *
 * XXX need a poolmgr pointer to avoid returning to different poolmgrs.
 */

#include <stdarg.h>
#include <string.h>

#include "psc_ds/dynarray.h"
#include "psc_ds/hash2.h"
#include "psc_ds/list.h"
#include "psc_ds/lockedlist.h"
#include "psc_ds/pool.h"
#include "psc_ds/tree.h"
#include "psc_util/lock.h"
#include "psc_util/log.h"
#include "psc_util/refmgr.h"

void
psc_refmgr_init(struct psc_refmgr *prm, int flags, int privsiz, int nobjs,
    int min, int max, int (*initf)(struct psc_poolmgr *, void *),
    void (*destroyf)(void *), const char *namefmt, ...)
{
	int off = 0, pool_lentry_avail = 1;
	va_list ap;

	memset(prm, 0, sizeof(*prm));
	prm->prm_flags = flags;

	off = sizeof(struct psc_objref);
	if (prm->prm_flags & PRMF_TREE) {
		prm->prm_tree_offset = off;
		off += sizeof(SPLAY_ENTRY(psc_objref));

		prm->prm_refmgr_offset = off;
		off += sizeof(void *);
	}
	if (prm->prm_flags & PRMF_LIST) {
		prm->prm_list_offset = offsetof(struct psc_objref,
		    pobj_pool_lentry);
		pool_lentry_avail = 0;
	}
	if (prm->prm_flags & PRMF_LRU) {
		if (pool_lentry_avail) {
			prm->prm_lru_offset = offsetof(struct psc_objref,
			    pobj_pool_lentry);
			pool_lentry_avail = 0;
		} else {
			prm->prm_lru_offset = off;
			off += sizeof(struct psclist_head);
		}
	}
	if (prm->prm_flags & PRMF_HASH) {
		if (pool_lentry_avail) {
			prm->prm_hash_offset = offsetof(struct psc_objref,
			    pobj_pool_lentry);
			pool_lentry_avail = 0;
		} else {
			prm->prm_hash_offset = off;
			off += sizeof(struct psc_hashent);
		}
	}
	prm->prm_private_offset = off;

	flags = 0;
	if (prm->prm_flags & PRMF_AUTOSIZE)
		flags |= PPMF_AUTO;
	if (prm->prm_flags & PRMF_NOMEMPIN)
		flags |= PPMF_NOLOCK;

	va_start(ap, namefmt);
	_psc_poolmaster_initv(&prm->prm_pms, off + privsiz, off, flags,
	    nobjs, min, max, initf, destroyf, NULL,
	    psc_refmgr_reclaimcb, namefmt, ap);
	va_end(ap);

	if (prm->prm_flags & PRMF_LIST)
		pll_init(&prm->prm_list, struct psc_objref,
		    prm->prm_list_offset);
	if (prm->prm_flags & PRMF_LRU)
		pll_init(&prm->prm_lru, struct psc_objref,
		    prm->prm_lru_offset);
	if (prm->prm_flags & PRMF_TREE) {
		SPLAY_INIT(psc_objref_tree, &prm->prm_tree);
		LOCK_INIT(&prm->prm_trlock);
	}
	if (prm->prm_flags & PRMF_HASH) {
		flags = 0;
		if (prm->prm_flags & PRMF_HASHSTR)
			flags |= PHTF_STR;

		_psc_hashtbl_init(&prm->prm_hashtbl, flags, hashidmemboff,
		    prm->prm_hash_offset, nbuckets, hashcmpf, "%s",
		    prm->prm_pms->pms_name);
	}
}

/*
 * psc_refmgr_lock - Lock a reference manager for a write operation.
 */
void
psc_refmgr_lock_generic(struct psc_refmgr *prm)
{
	struct psc_hashbkt *b;

	if (prm->prm_flags & PRMF_LIST)
{'fraction_non_alphanumeric': 0.19547787042976267, 'fraction_numerical': 0.0027261064785118667, 'mean_word_length': 2.885981308411215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6625590', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2643, 'n_words': 482}