vglite_mem.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #if defined(CONFIG_VG_LITE_MALLOC_SYS_HEAP)
  9. #include <init.h>
  10. #include <sys/sys_heap.h>
  11. __attribute__((__aligned__(4), section(".vglite.noinit.malloc")))
  12. static uint8_t vglite_malloc_mem[CONFIG_VG_LITE_MALLOC_HEAP_SIZE];
  13. static struct sys_heap vglite_heap;
  14. static struct k_spinlock vglite_heap_lock;
  15. void * vg_lite_os_malloc(size_t size)
  16. {
  17. k_spinlock_key_t key;
  18. void *ptr;
  19. key = k_spin_lock(&vglite_heap_lock);
  20. ptr = sys_heap_alloc(&vglite_heap, size);
  21. k_spin_unlock(&vglite_heap_lock, key);
  22. if (ptr == NULL) {
  23. printf("vg_lite_os_malloc %u failed\n", (unsigned int)size);
  24. }
  25. return ptr;
  26. }
  27. void * vg_lite_os_realloc(void * ptr, size_t size)
  28. {
  29. k_spinlock_key_t key;
  30. key = k_spin_lock(&vglite_heap_lock);
  31. ptr = sys_heap_realloc(&vglite_heap, ptr, size);
  32. k_spin_unlock(&vglite_heap_lock, key);
  33. if (ptr == NULL) {
  34. printf("vg_lite_os_realloc %u failed\n", (unsigned int)size);
  35. }
  36. return ptr;
  37. }
  38. void vg_lite_os_free(void *memory)
  39. {
  40. k_spinlock_key_t key;
  41. key = k_spin_lock(&vglite_heap_lock);
  42. sys_heap_free(&vglite_heap, memory);
  43. k_spin_unlock(&vglite_heap_lock, key);
  44. }
  45. static int _vg_lite_osal_init(const struct device *unused)
  46. {
  47. sys_heap_init(&vglite_heap, vglite_malloc_mem, ARRAY_SIZE(vglite_malloc_mem));
  48. return 0;
  49. }
  50. SYS_INIT(_vg_lite_osal_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
  51. #elif defined(CONFIG_VG_LITE_MALLOC_GUI_HEAP)
  52. #include <ui_mem.h>
  53. void * vg_lite_os_malloc(size_t size)
  54. {
  55. return ui_mem_alloc(MEM_GUI, size, __func__);
  56. }
  57. void * vg_lite_os_realloc(void * ptr, size_t size)
  58. {
  59. return ui_mem_realloc(MEM_GUI, ptr, size, __func__);
  60. }
  61. void vg_lite_os_free(void *memory)
  62. {
  63. ui_mem_free(MEM_GUI, memory);
  64. }
  65. #else
  66. void * vg_lite_os_malloc(size_t size)
  67. {
  68. return malloc(size);
  69. }
  70. void * vg_lite_os_realloc(void * ptr, size_t size)
  71. {
  72. return realloc(ptr, size);
  73. }
  74. void vg_lite_os_free(void *memory)
  75. {
  76. free(memory);
  77. }
  78. #endif /* CONFIG_VG_LITE_MALLOC_SYS_HEAP */