qstr.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef MICROPY_INCLUDED_PY_QSTR_H
  27. #define MICROPY_INCLUDED_PY_QSTR_H
  28. #include "py/mpconfig.h"
  29. #include "py/misc.h"
  30. // See qstrdefs.h for a list of qstr's that are available as constants.
  31. // Reference them as MP_QSTR_xxxx.
  32. //
  33. // Note: it would be possible to define MP_QSTR_xxx as qstr_from_str("xxx")
  34. // for qstrs that are referenced this way, but you don't want to have them in ROM.
  35. // first entry in enum will be MP_QSTRnull=0, which indicates invalid/no qstr
  36. enum {
  37. #ifndef NO_QSTR
  38. #define QDEF0(id, hash, len, str) id,
  39. #define QDEF1(id, hash, len, str)
  40. #include "genhdr/qstrdefs.generated.h"
  41. #undef QDEF0
  42. #undef QDEF1
  43. #endif
  44. MP_QSTRnumber_of_static,
  45. MP_QSTRstart_of_main = MP_QSTRnumber_of_static - 1, // unused but shifts the enum counter back one
  46. #ifndef NO_QSTR
  47. #define QDEF0(id, hash, len, str)
  48. #define QDEF1(id, hash, len, str) id,
  49. #include "genhdr/qstrdefs.generated.h"
  50. #undef QDEF0
  51. #undef QDEF1
  52. #endif
  53. MP_QSTRnumber_of, // no underscore so it can't clash with any of the above
  54. };
  55. typedef size_t qstr;
  56. typedef uint16_t qstr_short_t;
  57. #if MICROPY_QSTR_BYTES_IN_HASH == 0
  58. // No qstr_hash_t type needed.
  59. #elif MICROPY_QSTR_BYTES_IN_HASH == 1
  60. typedef uint8_t qstr_hash_t;
  61. #elif MICROPY_QSTR_BYTES_IN_HASH == 2
  62. typedef uint16_t qstr_hash_t;
  63. #else
  64. #error unimplemented qstr hash decoding
  65. #endif
  66. #if MICROPY_QSTR_BYTES_IN_LEN == 1
  67. typedef uint8_t qstr_len_t;
  68. #elif MICROPY_QSTR_BYTES_IN_LEN == 2
  69. typedef uint16_t qstr_len_t;
  70. #else
  71. #error unimplemented qstr length decoding
  72. #endif
  73. typedef struct _qstr_pool_t {
  74. const struct _qstr_pool_t *prev;
  75. size_t total_prev_len : (8 * sizeof(size_t) - 1);
  76. size_t is_sorted : 1;
  77. size_t alloc;
  78. size_t len;
  79. #if MICROPY_QSTR_BYTES_IN_HASH
  80. qstr_hash_t *hashes;
  81. #endif
  82. qstr_len_t *lengths;
  83. const char *qstrs[];
  84. } qstr_pool_t;
  85. #define QSTR_TOTAL() (MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len)
  86. void qstr_init(void);
  87. size_t qstr_compute_hash(const byte *data, size_t len);
  88. qstr qstr_find_strn(const char *str, size_t str_len); // returns MP_QSTRnull if not found
  89. qstr qstr_from_str(const char *str);
  90. qstr qstr_from_strn(const char *str, size_t len);
  91. mp_uint_t qstr_hash(qstr q);
  92. const char *qstr_str(qstr q);
  93. size_t qstr_len(qstr q);
  94. const byte *qstr_data(qstr q, size_t *len);
  95. void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes);
  96. void qstr_dump_data(void);
  97. #if MICROPY_ROM_TEXT_COMPRESSION
  98. void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src);
  99. #define MP_IS_COMPRESSED_ROM_STRING(s) (*(byte *)(s) == 0xff)
  100. #endif
  101. #endif // MICROPY_INCLUDED_PY_QSTR_H