dataMemory.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is part of the PikaPython project.
  3. * http://github.com/pikastech/pikapython
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is 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
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #ifndef __MEMORY_H__
  31. #define __MEMORY_H__
  32. #include "PikaPlatform.h"
  33. #include "PikaVersion.h"
  34. /*! \NOTE: Make sure #include "__pika_ooc.h" is close to the class definition
  35. */
  36. #if defined(__DATA_MEMORY_CLASS_IMPLEMENT__)
  37. #define __PLOOC_CLASS_IMPLEMENT__
  38. #endif
  39. #include "__pika_ooc.h"
  40. typedef struct {
  41. uint32_t heapUsed;
  42. uint32_t heapUsedMax;
  43. #if PIKA_ARG_CACHE_ENABLE
  44. uint8_t* cache_pool[PIKA_ARG_CACHE_POOL_SIZE];
  45. uint32_t cache_pool_top;
  46. #endif
  47. uint32_t alloc_times;
  48. uint32_t alloc_times_cache;
  49. } PikaMemInfo;
  50. typedef uint8_t* BitMap;
  51. /* clang-format off */
  52. typedef struct Pool Pool;
  53. struct Pool{
  54. private_member(
  55. BitMap bitmap;
  56. uint8_t* mem;
  57. uint8_t aline;
  58. uint32_t size;
  59. uint32_t first_free_block;
  60. uint32_t purl_free_block_start;
  61. pika_bool inited;
  62. )
  63. };
  64. /* clang-format on */
  65. #define align_by(size, align) \
  66. (((size) == 0) ? 0 : (((size)-1) / (align) + 1) * (align))
  67. void pikaFree(void* mem, uint32_t size);
  68. void* pikaMalloc(uint32_t size);
  69. uint32_t pikaMemNow(void);
  70. uint32_t pikaMemMax(void);
  71. void pikaMemMaxReset(void);
  72. BitMap bitmap_init(uint32_t size);
  73. void bitmap_set(BitMap bitmap, uint32_t index, uint8_t bit);
  74. uint8_t bitmap_get(BitMap bitmap, uint32_t index);
  75. uint8_t bitmap_getByte(BitMap bitmap, uint32_t index);
  76. void bitmap_deinit(BitMap bitmap);
  77. void mem_pool_deinit(void);
  78. void mem_pool_init(void);
  79. #define mem_align(_size) ((((_size) + 4 - 1) & ~(4 - 1)))
  80. #undef __DATA_MEMORY_CLASS_IMPLEMENT__
  81. #endif
  82. #ifdef __cplusplus
  83. }
  84. #endif