pycore_dict.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef Py_INTERNAL_DICT_H
  2. #define Py_INTERNAL_DICT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. #include "pycore_dict_state.h"
  10. #include "pycore_runtime.h" // _PyRuntime
  11. /* runtime lifecycle */
  12. extern void _PyDict_Fini(PyInterpreterState *interp);
  13. /* other API */
  14. typedef struct {
  15. /* Cached hash code of me_key. */
  16. Py_hash_t me_hash;
  17. PyObject *me_key;
  18. PyObject *me_value; /* This field is only meaningful for combined tables */
  19. } PyDictKeyEntry;
  20. typedef struct {
  21. PyObject *me_key; /* The key must be Unicode and have hash. */
  22. PyObject *me_value; /* This field is only meaningful for combined tables */
  23. } PyDictUnicodeEntry;
  24. extern PyDictKeysObject *_PyDict_NewKeysForClass(void);
  25. extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
  26. /* Gets a version number unique to the current state of the keys of dict, if possible.
  27. * Returns the version number, or zero if it was not possible to get a version number. */
  28. extern uint32_t _PyDictKeys_GetVersionForCurrentState(
  29. PyInterpreterState *interp, PyDictKeysObject *dictkeys);
  30. extern size_t _PyDict_KeysSize(PyDictKeysObject *keys);
  31. /* _Py_dict_lookup() returns index of entry which can be used like DK_ENTRIES(dk)[index].
  32. * -1 when no entry found, -3 when compare raises error.
  33. */
  34. extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
  35. extern Py_ssize_t _PyDict_LookupIndex(PyDictObject *, PyObject *);
  36. extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key);
  37. extern PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);
  38. /* Consumes references to key and value */
  39. extern int _PyDict_SetItem_Take2(PyDictObject *op, PyObject *key, PyObject *value);
  40. extern int _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, PyObject *name, PyObject *value);
  41. extern PyObject *_PyDict_Pop_KnownHash(PyObject *, PyObject *, Py_hash_t, PyObject *);
  42. #define DKIX_EMPTY (-1)
  43. #define DKIX_DUMMY (-2) /* Used internally */
  44. #define DKIX_ERROR (-3)
  45. #define DKIX_KEY_CHANGED (-4) /* Used internally */
  46. typedef enum {
  47. DICT_KEYS_GENERAL = 0,
  48. DICT_KEYS_UNICODE = 1,
  49. DICT_KEYS_SPLIT = 2
  50. } DictKeysKind;
  51. /* See dictobject.c for actual layout of DictKeysObject */
  52. struct _dictkeysobject {
  53. Py_ssize_t dk_refcnt;
  54. /* Size of the hash table (dk_indices). It must be a power of 2. */
  55. uint8_t dk_log2_size;
  56. /* Size of the hash table (dk_indices) by bytes. */
  57. uint8_t dk_log2_index_bytes;
  58. /* Kind of keys */
  59. uint8_t dk_kind;
  60. /* Version number -- Reset to 0 by any modification to keys */
  61. uint32_t dk_version;
  62. /* Number of usable entries in dk_entries. */
  63. Py_ssize_t dk_usable;
  64. /* Number of used entries in dk_entries. */
  65. Py_ssize_t dk_nentries;
  66. /* Actual hash table of dk_size entries. It holds indices in dk_entries,
  67. or DKIX_EMPTY(-1) or DKIX_DUMMY(-2).
  68. Indices must be: 0 <= indice < USABLE_FRACTION(dk_size).
  69. The size in bytes of an indice depends on dk_size:
  70. - 1 byte if dk_size <= 0xff (char*)
  71. - 2 bytes if dk_size <= 0xffff (int16_t*)
  72. - 4 bytes if dk_size <= 0xffffffff (int32_t*)
  73. - 8 bytes otherwise (int64_t*)
  74. Dynamically sized, SIZEOF_VOID_P is minimum. */
  75. char dk_indices[]; /* char is required to avoid strict aliasing. */
  76. /* "PyDictKeyEntry or PyDictUnicodeEntry dk_entries[USABLE_FRACTION(DK_SIZE(dk))];" array follows:
  77. see the DK_ENTRIES() macro */
  78. };
  79. /* This must be no more than 250, for the prefix size to fit in one byte. */
  80. #define SHARED_KEYS_MAX_SIZE 30
  81. #define NEXT_LOG2_SHARED_KEYS_MAX_SIZE 6
  82. /* Layout of dict values:
  83. *
  84. * The PyObject *values are preceded by an array of bytes holding
  85. * the insertion order and size.
  86. * [-1] = prefix size. [-2] = used size. size[-2-n...] = insertion order.
  87. */
  88. struct _dictvalues {
  89. PyObject *values[1];
  90. };
  91. #define DK_LOG_SIZE(dk) _Py_RVALUE((dk)->dk_log2_size)
  92. #if SIZEOF_VOID_P > 4
  93. #define DK_SIZE(dk) (((int64_t)1)<<DK_LOG_SIZE(dk))
  94. #else
  95. #define DK_SIZE(dk) (1<<DK_LOG_SIZE(dk))
  96. #endif
  97. static inline void* _DK_ENTRIES(PyDictKeysObject *dk) {
  98. int8_t *indices = (int8_t*)(dk->dk_indices);
  99. size_t index = (size_t)1 << dk->dk_log2_index_bytes;
  100. return (&indices[index]);
  101. }
  102. static inline PyDictKeyEntry* DK_ENTRIES(PyDictKeysObject *dk) {
  103. assert(dk->dk_kind == DICT_KEYS_GENERAL);
  104. return (PyDictKeyEntry*)_DK_ENTRIES(dk);
  105. }
  106. static inline PyDictUnicodeEntry* DK_UNICODE_ENTRIES(PyDictKeysObject *dk) {
  107. assert(dk->dk_kind != DICT_KEYS_GENERAL);
  108. return (PyDictUnicodeEntry*)_DK_ENTRIES(dk);
  109. }
  110. #define DK_IS_UNICODE(dk) ((dk)->dk_kind != DICT_KEYS_GENERAL)
  111. #define DICT_VERSION_INCREMENT (1 << DICT_MAX_WATCHERS)
  112. #define DICT_VERSION_MASK (DICT_VERSION_INCREMENT - 1)
  113. #define DICT_NEXT_VERSION(INTERP) \
  114. ((INTERP)->dict_state.global_version += DICT_VERSION_INCREMENT)
  115. void
  116. _PyDict_SendEvent(int watcher_bits,
  117. PyDict_WatchEvent event,
  118. PyDictObject *mp,
  119. PyObject *key,
  120. PyObject *value);
  121. static inline uint64_t
  122. _PyDict_NotifyEvent(PyInterpreterState *interp,
  123. PyDict_WatchEvent event,
  124. PyDictObject *mp,
  125. PyObject *key,
  126. PyObject *value)
  127. {
  128. assert(Py_REFCNT((PyObject*)mp) > 0);
  129. int watcher_bits = mp->ma_version_tag & DICT_VERSION_MASK;
  130. if (watcher_bits) {
  131. _PyDict_SendEvent(watcher_bits, event, mp, key, value);
  132. return DICT_NEXT_VERSION(interp) | watcher_bits;
  133. }
  134. return DICT_NEXT_VERSION(interp);
  135. }
  136. extern PyObject *_PyObject_MakeDictFromInstanceAttributes(PyObject *obj, PyDictValues *values);
  137. extern PyObject *_PyDict_FromItems(
  138. PyObject *const *keys, Py_ssize_t keys_offset,
  139. PyObject *const *values, Py_ssize_t values_offset,
  140. Py_ssize_t length);
  141. static inline void
  142. _PyDictValues_AddToInsertionOrder(PyDictValues *values, Py_ssize_t ix)
  143. {
  144. assert(ix < SHARED_KEYS_MAX_SIZE);
  145. uint8_t *size_ptr = ((uint8_t *)values)-2;
  146. int size = *size_ptr;
  147. assert(size+2 < ((uint8_t *)values)[-1]);
  148. size++;
  149. size_ptr[-size] = (uint8_t)ix;
  150. *size_ptr = size;
  151. }
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif /* !Py_INTERNAL_DICT_H */