hv.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* hv.c
*
* Copyright (c) 1991-2001, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* "I sit beside the fire and think of all that I have seen." --Bilbo
*/
#include "EXTERN.h"
#define PERL_IN_HV_C
#include "perl.h"
{
if (!PL_he_root)
more_he();
he = PL_he_root;
return he;
}
STATIC void
{
PL_he_root = p;
}
STATIC void
{
PL_he_root = ++he;
he++;
}
}
#ifdef PURIFY
#else
#endif
{
char *k;
return hek;
}
void
{
}
#if defined(USE_ITHREADS)
HE *
{
if (!e)
return Nullhe;
/* look for it in the table first */
if (ret)
return ret;
/* create anew and remember what it is */
else if (shared)
else
return ret;
}
#endif /* USE_ITHREADS */
/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
* contains an SV* */
/*
=for apidoc hv_fetch
Returns the SV which corresponds to the specified key in the hash. The
C<klen> is the length of the key. If C<lval> is set then the fetch will be
part of a store. Check that the return value is non-null before
dereferencing it to a C<SV*>.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=cut
*/
SV**
{
if (!hv)
return 0;
if (SvRMAGICAL(hv)) {
sv = sv_newmortal();
PL_hv_fetch_sv = sv;
return &PL_hv_fetch_sv;
}
#ifdef ENV_IS_CASELESS
U32 i;
for (i = 0; i < klen; ++i)
return ret;
}
}
#endif
}
if (lval
#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
#endif
)
else
return 0;
}
continue;
continue;
continue;
}
#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
unsigned long len;
if (env) {
}
}
#endif
if (lval) { /* gonna assign to this, so it better be there */
}
return 0;
}
/* returns a HE * structure with the all fields set */
/* note that hent_val will be a mortal sv for MAGICAL hashes */
/*
=for apidoc hv_fetch_ent
Returns the hash entry which corresponds to the specified key in the hash.
C<hash> must be a valid precomputed hash number for the given C<key>, or 0
if you want the function to compute it. IF C<lval> is set then the fetch
will be part of a store. Make sure the return value is non-null before
accessing it. The return value when C<tb> is a tied hash is a pointer to a
static location, so be sure to make a copy of the structure if you need to
store it somewhere.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=cut
*/
HE *
{
register char *key;
if (!hv)
return 0;
if (SvRMAGICAL(hv)) {
sv = sv_newmortal();
if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
char *k;
}
return &PL_hv_fetch_ent_mh;
}
#ifdef ENV_IS_CASELESS
U32 i;
for (i = 0; i < klen; ++i)
return entry;
}
}
#endif
}
if (lval
#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
#endif
)
else
return 0;
}
if (!hash)
continue;
continue;
continue;
return entry;
}
#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
unsigned long len;
if (env) {
}
}
#endif
if (lval) { /* gonna assign to this, so it better be there */
}
return 0;
}
STATIC void
{
*needs_copy = FALSE;
*needs_store = TRUE;
while (mg) {
*needs_copy = TRUE;
case 'P':
case 'S':
*needs_store = FALSE;
}
}
}
}
/*
=for apidoc hv_store
Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
the length of the key. The C<hash> parameter is the precomputed hash
value; if it is zero then Perl will compute it. The return value will be
NULL if the operation failed or if the value did not need to be actually
stored within the hash (as in the case of tied hashes). Otherwise it can
be dereferenced to get the original C<SV*>. Note that the caller is
responsible for suitably incrementing the reference count of C<val> before
the call, and decrementing it if the function returned NULL.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=cut
*/
SV**
{
register I32 i;
if (!hv)
return 0;
bool needs_copy;
bool needs_store;
if (needs_copy) {
return 0;
#ifdef ENV_IS_CASELESS
hash = 0;
}
#endif
}
}
if (!hash)
i = 1;
continue;
continue;
continue;
}
if (HvSHAREKEYS(hv))
else /* gotta do the real thing */
if (i) { /* initial entry? */
}
}
/*
=for apidoc hv_store_ent
Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
parameter is the precomputed hash value; if it is zero then Perl will
compute it. The return value is the new hash entry so created. It will be
NULL if the operation failed or if the value did not need to be actually
stored within the hash (as in the case of tied hashes). Otherwise the
contents of the return value can be accessed using the C<He???> macros
described here. Note that the caller is responsible for suitably
incrementing the reference count of C<val> before the call, and
decrementing it if the function returned NULL.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=cut
*/
HE *
{
register char *key;
register I32 i;
if (!hv)
return 0;
bool needs_copy;
bool needs_store;
if (needs_copy) {
bool save_taint = PL_tainted;
if (PL_tainting)
return Nullhe;
#ifdef ENV_IS_CASELESS
hash = 0;
}
#endif
}
}
if (!hash)
i = 1;
continue;
continue;
continue;
return entry;
}
if (HvSHAREKEYS(hv))
else /* gotta do the real thing */
if (i) { /* initial entry? */
}
return entry;
}
/*
=for apidoc hv_delete
hash and returned to the caller. The C<klen> is the length of the key.
The C<flags> value will normally be zero; if set to G_DISCARD then NULL
will be returned.
=cut
*/
SV *
{
register I32 i;
if (!hv)
return Nullsv;
if (SvRMAGICAL(hv)) {
bool needs_copy;
bool needs_store;
if (!needs_store) {
return sv;
}
return Nullsv; /* element cannot be deleted */
}
#ifdef ENV_IS_CASELESS
}
#endif
}
}
return Nullsv;
i = 1;
continue;
continue;
continue;
if (i && !*oentry)
else {
}
else
return sv;
}
return Nullsv;
}
/*
=for apidoc hv_delete_ent
hash and returned to the caller. The C<flags> value will normally be zero;
if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
precomputed hash value, or 0 to ask for it to be computed.
=cut
*/
SV *
{
register I32 i;
register char *key;
if (!hv)
return Nullsv;
if (SvRMAGICAL(hv)) {
bool needs_copy;
bool needs_store;
if (!needs_store) {
return sv;
}
return Nullsv; /* element cannot be deleted */
}
#ifdef ENV_IS_CASELESS
hash = 0;
}
#endif
}
}
return Nullsv;
if (!hash)
i = 1;
continue;
continue;
continue;
if (i && !*oentry)
else {
}
else
return sv;
}
return Nullsv;
}
/*
=for apidoc hv_exists
Returns a boolean indicating whether the specified hash key exists. The
C<klen> is the length of the key.
=cut
*/
bool
{
if (!hv)
return 0;
if (SvRMAGICAL(hv)) {
sv = sv_newmortal();
}
#ifdef ENV_IS_CASELESS
}
#endif
}
#ifndef DYNAMIC_ENV_FETCH
return 0;
#endif
#ifdef DYNAMIC_ENV_FETCH
else
#endif
continue;
continue;
continue;
return TRUE;
}
#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
unsigned long len;
if (env) {
return TRUE;
}
}
#endif
return FALSE;
}
/*
=for apidoc hv_exists_ent
Returns a boolean indicating whether the specified hash key exists. C<hash>
can be a valid precomputed hash value, or 0 to ask for it to be
computed.
=cut
*/
bool
{
register char *key;
if (!hv)
return 0;
if (SvRMAGICAL(hv)) {
sv = sv_newmortal();
}
#ifdef ENV_IS_CASELESS
hash = 0;
}
#endif
}
#ifndef DYNAMIC_ENV_FETCH
return 0;
#endif
if (!hash)
#ifdef DYNAMIC_ENV_FETCH
else
#endif
continue;
continue;
continue;
return TRUE;
}
#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
unsigned long len;
if (env) {
return TRUE;
}
}
#endif
return FALSE;
}
STATIC void
{
register I32 i;
PL_nomemok = TRUE;
#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
if (!a) {
PL_nomemok = FALSE;
return;
}
#else
#define MALLOC_OVERHEAD 16
if (!a) {
PL_nomemok = FALSE;
return;
}
if (oldsize >= 64) {
}
else
#endif
PL_nomemok = FALSE;
if (!*aep) /* non-existent */
continue;
if (!*bep)
continue;
}
else
}
if (!*aep) /* everything moved */
}
}
void
{
register I32 i;
register I32 j;
register char *a;
return;
}
newsize *= 2;
return; /* overflow detection */
if (a) {
PL_nomemok = TRUE;
#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
if (!a) {
PL_nomemok = FALSE;
return;
}
#else
if (!a) {
PL_nomemok = FALSE;
return;
}
if (oldsize >= 64) {
}
else
#endif
PL_nomemok = FALSE;
}
else {
}
return;
if (!*aep) /* non-existent */
continue;
j -= i;
continue;
}
else
}
if (!*aep) /* everything moved */
}
}
/*
=for apidoc newHV
Creates a new HV. The reference count is set to 1.
=cut
*/
HV *
{
#ifndef NODEFAULT_SHAREKEYS
#endif
xhv->xhv_pmroot = 0;
return hv;
}
HV *
{
if (!hv_fill)
return hv;
#if 0
/* Quick way ???*/
}
else
#endif
{
/* Slow way */
}
}
return hv;
}
void
{
if (!entry)
return;
PL_sub_generation++; /* may be deletion of method from stash */
}
else if (HvSHAREKEYS(hv))
else
}
void
{
if (!entry)
return;
PL_sub_generation++; /* may be deletion of method from stash */
}
else if (HvSHAREKEYS(hv))
else
}
/*
=for apidoc hv_clear
Clears a hash, making it empty.
=cut
*/
void
{
if (!hv)
return;
if (SvRMAGICAL(hv))
}
STATIC void
{
if (!hv)
return;
return;
riter = 0;
for (;;) {
if (entry) {
}
if (!entry) {
break;
}
}
(void)hv_iterinit(hv);
}
/*
=for apidoc hv_undef
Undefines the hash.
=cut
*/
void
{
if (!hv)
return;
}
if (SvRMAGICAL(hv))
}
/*
=for apidoc hv_iterinit
Prepares a starting point to traverse a hash table. Returns the number of
keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
currently only meaningful for hashes without tie magic.
NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
hash buckets that happen to be in use. If you still need that esoteric
value, you can get it through the macro C<HvFILL(tb)>.
=cut
*/
{
if (!hv)
}
}
/*
=for apidoc hv_iternext
Returns entries from a hash iterator. See C<hv_iterinit>.
=cut
*/
HE *
{
if (!hv)
if (entry) {
}
else {
char *k;
}
/* force key to stay around until next time */
return entry; /* beware, hent_val is not set */
}
}
#ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
#endif
if (entry)
while (!entry) {
break;
}
}
}
return entry;
}
/*
=for apidoc hv_iterkey
Returns the key from the current position of the hash iterator. See
C<hv_iterinit>.
=cut
*/
char *
{
return p;
}
else {
}
}
/* unlike hv_iterval(), this always returns a mortal copy of the key */
/*
=for apidoc hv_iterkeysv
Returns the key as an C<SV*> from the current position of the hash
iterator. The return value will always be a mortal copy of the key. Also
see C<hv_iterinit>.
=cut
*/
SV *
{
else
}
/*
=for apidoc hv_iterval
Returns the value from the current position of the hash iterator. See
C<hv_iterkey>.
=cut
*/
SV *
{
if (SvRMAGICAL(hv)) {
return sv;
}
}
}
/*
=for apidoc hv_iternextsv
Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
operation.
=cut
*/
SV *
{
return NULL;
}
/*
=for apidoc hv_magic
Adds magic to a hash. See C<sv_magic>.
=cut
*/
void
{
}
char*
{
}
/* possibly free a shared string if no one has access to it
* len and hash must both be valid for str.
*/
void
{
register I32 i = 1;
/* what follows is the moral equivalent of:
if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
if (--*Svp == Nullsv)
hv_delete(PL_strtab, str, len, G_DISCARD, hash);
} */
/* assert(xhv_array != 0) */
continue;
continue;
continue;
found = 1;
if (i && !*oentry)
}
break;
}
}
/* get a (constant) string ptr from the global string table
* string will get added if it is not already there.
* len and hash must both be valid for str.
*/
HEK *
{
register I32 i = 1;
/* what follows is the moral equivalent of:
if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
hv_store(PL_strtab, str, len, Nullsv, hash);
*/
/* assert(xhv_array != 0) */
continue;
continue;
continue;
found = 1;
break;
}
if (!found) {
if (i) { /* initial entry? */
}
}
}