Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.8 KiB

  1. /* hv.h
  2. *
  3. * Copyright (c) 1991-2001, Larry Wall
  4. *
  5. * You may distribute under the terms of either the GNU General Public
  6. * License or the Artistic License, as specified in the README file.
  7. *
  8. */
  9. /* typedefs to eliminate some typing */
  10. typedef struct he HE;
  11. typedef struct hek HEK;
  12. /* entry in hash value chain */
  13. struct he {
  14. HE *hent_next; /* next entry in chain */
  15. HEK *hent_hek; /* hash key */
  16. SV *hent_val; /* scalar value that was hashed */
  17. };
  18. /* hash key -- defined separately for use as shared pointer */
  19. struct hek {
  20. U32 hek_hash; /* hash of key */
  21. I32 hek_len; /* length of hash key */
  22. char hek_key[1]; /* variable-length hash key */
  23. };
  24. /* hash structure: */
  25. /* This structure must match the beginning of struct xpvmg in sv.h. */
  26. struct xpvhv {
  27. char * xhv_array; /* pointer to malloced string */
  28. STRLEN xhv_fill; /* how full xhv_array currently is */
  29. STRLEN xhv_max; /* subscript of last element of xhv_array */
  30. IV xhv_keys; /* how many elements in the array */
  31. NV xnv_nv; /* numeric value, if any */
  32. MAGIC* xmg_magic; /* magic for scalar array */
  33. HV* xmg_stash; /* class package */
  34. I32 xhv_riter; /* current root of iterator */
  35. HE *xhv_eiter; /* current entry of iterator */
  36. PMOP *xhv_pmroot; /* list of pm's for this package */
  37. char *xhv_name; /* name, if a symbol table */
  38. };
  39. /* hash a key */
  40. #define PERL_HASH(hash,str,len) \
  41. STMT_START { \
  42. register const char *s_PeRlHaSh = str; \
  43. register I32 i_PeRlHaSh = len; \
  44. register U32 hash_PeRlHaSh = 0; \
  45. while (i_PeRlHaSh--) \
  46. hash_PeRlHaSh = hash_PeRlHaSh * 33 + *s_PeRlHaSh++; \
  47. (hash) = hash_PeRlHaSh + (hash_PeRlHaSh>>5); \
  48. } STMT_END
  49. /*
  50. =for apidoc AmU||HEf_SVKEY
  51. This flag, used in the length slot of hash entries and magic structures,
  52. specifies the structure contains a C<SV*> pointer where a C<char*> pointer
  53. is to be expected. (For information only--not to be used).
  54. =for apidoc AmU||Nullhv
  55. Null HV pointer.
  56. =for apidoc Am|char*|HvNAME|HV* stash
  57. Returns the package name of a stash. See C<SvSTASH>, C<CvSTASH>.
  58. =for apidoc Am|void*|HeKEY|HE* he
  59. Returns the actual pointer stored in the key slot of the hash entry. The
  60. pointer may be either C<char*> or C<SV*>, depending on the value of
  61. C<HeKLEN()>. Can be assigned to. The C<HePV()> or C<HeSVKEY()> macros are
  62. usually preferable for finding the value of a key.
  63. =for apidoc Am|STRLEN|HeKLEN|HE* he
  64. If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
  65. holds an C<SV*> key. Otherwise, holds the actual length of the key. Can
  66. be assigned to. The C<HePV()> macro is usually preferable for finding key
  67. lengths.
  68. =for apidoc Am|SV*|HeVAL|HE* he
  69. Returns the value slot (type C<SV*>) stored in the hash entry.
  70. =for apidoc Am|U32|HeHASH|HE* he
  71. Returns the computed hash stored in the hash entry.
  72. =for apidoc Am|char*|HePV|HE* he|STRLEN len
  73. Returns the key slot of the hash entry as a C<char*> value, doing any
  74. necessary dereferencing of possibly C<SV*> keys. The length of the string
  75. is placed in C<len> (this is a macro, so do I<not> use C<&len>). If you do
  76. not care about what the length of the key is, you may use the global
  77. variable C<PL_na>, though this is rather less efficient than using a local
  78. variable. Remember though, that hash keys in perl are free to contain
  79. embedded nulls, so using C<strlen()> or similar is not a good way to find
  80. the length of hash keys. This is very similar to the C<SvPV()> macro
  81. described elsewhere in this document.
  82. =for apidoc Am|SV*|HeSVKEY|HE* he
  83. Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
  84. contain an C<SV*> key.
  85. =for apidoc Am|SV*|HeSVKEY_force|HE* he
  86. Returns the key as an C<SV*>. Will create and return a temporary mortal
  87. C<SV*> if the hash entry contains only a C<char*> key.
  88. =for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
  89. Sets the key to a given C<SV*>, taking care to set the appropriate flags to
  90. indicate the presence of an C<SV*> key, and returns the same
  91. C<SV*>.
  92. =cut
  93. */
  94. /* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
  95. #define HEf_SVKEY -2 /* hent_key is a SV* */
  96. #define Nullhv Null(HV*)
  97. #define HvARRAY(hv) ((HE**)((XPVHV*) SvANY(hv))->xhv_array)
  98. #define HvFILL(hv) ((XPVHV*) SvANY(hv))->xhv_fill
  99. #define HvMAX(hv) ((XPVHV*) SvANY(hv))->xhv_max
  100. #define HvKEYS(hv) ((XPVHV*) SvANY(hv))->xhv_keys
  101. #define HvRITER(hv) ((XPVHV*) SvANY(hv))->xhv_riter
  102. #define HvEITER(hv) ((XPVHV*) SvANY(hv))->xhv_eiter
  103. #define HvPMROOT(hv) ((XPVHV*) SvANY(hv))->xhv_pmroot
  104. #define HvNAME(hv) ((XPVHV*) SvANY(hv))->xhv_name
  105. #define HvSHAREKEYS(hv) (SvFLAGS(hv) & SVphv_SHAREKEYS)
  106. #define HvSHAREKEYS_on(hv) (SvFLAGS(hv) |= SVphv_SHAREKEYS)
  107. #define HvSHAREKEYS_off(hv) (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
  108. #define HvLAZYDEL(hv) (SvFLAGS(hv) & SVphv_LAZYDEL)
  109. #define HvLAZYDEL_on(hv) (SvFLAGS(hv) |= SVphv_LAZYDEL)
  110. #define HvLAZYDEL_off(hv) (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
  111. /* Maybe amagical: */
  112. /* #define HV_AMAGICmb(hv) (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
  113. #define HV_AMAGIC(hv) (SvFLAGS(hv) & SVpgv_AM)
  114. #define HV_AMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_AM)
  115. #define HV_AMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_AM)
  116. /*
  117. #define HV_AMAGICbad(hv) (SvFLAGS(hv) & SVpgv_badAM)
  118. #define HV_badAMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_badAM)
  119. #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
  120. */
  121. #define Nullhe Null(HE*)
  122. #define HeNEXT(he) (he)->hent_next
  123. #define HeKEY_hek(he) (he)->hent_hek
  124. #define HeKEY(he) HEK_KEY(HeKEY_hek(he))
  125. #define HeKEY_sv(he) (*(SV**)HeKEY(he))
  126. #define HeKLEN(he) HEK_LEN(HeKEY_hek(he))
  127. #define HeVAL(he) (he)->hent_val
  128. #define HeHASH(he) HEK_HASH(HeKEY_hek(he))
  129. #define HePV(he,lp) ((HeKLEN(he) == HEf_SVKEY) ? \
  130. SvPV(HeKEY_sv(he),lp) : \
  131. (((lp = HeKLEN(he)) >= 0) ? \
  132. HeKEY(he) : Nullch))
  133. #define HeSVKEY(he) ((HeKEY(he) && \
  134. HeKLEN(he) == HEf_SVKEY) ? \
  135. HeKEY_sv(he) : Nullsv)
  136. #define HeSVKEY_force(he) (HeKEY(he) ? \
  137. ((HeKLEN(he) == HEf_SVKEY) ? \
  138. HeKEY_sv(he) : \
  139. sv_2mortal(newSVpvn(HeKEY(he), \
  140. HeKLEN(he)))) : \
  141. &PL_sv_undef)
  142. #define HeSVKEY_set(he,sv) ((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
  143. #define Nullhek Null(HEK*)
  144. #define HEK_BASESIZE STRUCT_OFFSET(HEK, hek_key[0])
  145. #define HEK_HASH(hek) (hek)->hek_hash
  146. #define HEK_LEN(hek) (hek)->hek_len
  147. #define HEK_KEY(hek) (hek)->hek_key
  148. /* calculate HV array allocation */
  149. #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
  150. # define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
  151. #else
  152. # define MALLOC_OVERHEAD 16
  153. # define PERL_HV_ARRAY_ALLOC_BYTES(size) \
  154. (((size) < 64) \
  155. ? (size) * sizeof(HE*) \
  156. : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
  157. #endif