objarray.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. * Copyright (c) 2014 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #ifndef MICROPY_INCLUDED_PY_OBJARRAY_H
  28. #define MICROPY_INCLUDED_PY_OBJARRAY_H
  29. #include "py/obj.h"
  30. // Used only for memoryview types, set in "typecode" to indicate a writable memoryview
  31. #define MP_OBJ_ARRAY_TYPECODE_FLAG_RW (0x80)
  32. // Bit size used for mp_obj_array_t.free member.
  33. #define MP_OBJ_ARRAY_FREE_SIZE_BITS (8 * sizeof(size_t) - 8)
  34. // This structure is used for all of bytearray, array.array, memoryview
  35. // objects. Note that memoryview has different meaning for some fields,
  36. // see comment at the beginning of objarray.c.
  37. typedef struct _mp_obj_array_t {
  38. mp_obj_base_t base;
  39. size_t typecode : 8;
  40. // free is number of unused elements after len used elements
  41. // alloc size = len + free
  42. // But for memoryview, 'free' is reused as offset (in elements) into the
  43. // parent object. (Union is not used to not go into a complication of
  44. // union-of-bitfields with different toolchains). See comments in
  45. // objarray.c.
  46. size_t free : MP_OBJ_ARRAY_FREE_SIZE_BITS;
  47. size_t len; // in elements
  48. void *items;
  49. } mp_obj_array_t;
  50. #if MICROPY_PY_BUILTINS_MEMORYVIEW
  51. static inline void mp_obj_memoryview_init(mp_obj_array_t *self, size_t typecode, size_t offset, size_t len, void *items) {
  52. self->base.type = &mp_type_memoryview;
  53. self->typecode = typecode;
  54. self->free = offset;
  55. self->len = len;
  56. self->items = items;
  57. }
  58. #endif
  59. #if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY
  60. MP_DECLARE_CONST_FUN_OBJ_2(mp_obj_array_append_obj);
  61. MP_DECLARE_CONST_FUN_OBJ_2(mp_obj_array_extend_obj);
  62. #endif
  63. #endif // MICROPY_INCLUDED_PY_OBJARRAY_H