heap_private.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <soc/soc_memory_layout.h>
  17. #include "multi_heap.h"
  18. #include "multi_heap_platform.h"
  19. #include "sys/queue.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Some common heap registration data structures used
  24. for heap_caps_init.c to share heap information with heap_caps.c
  25. */
  26. #define HEAP_SIZE_MAX (SOC_MAX_CONTIGUOUS_RAM_SIZE)
  27. /* Type for describing each registered heap */
  28. typedef struct heap_t_ {
  29. uint32_t caps[SOC_MEMORY_TYPE_NO_PRIOS]; ///< Capabilities for the type of memory in this heap (as a prioritised set). Copied from soc_memory_types so it's in RAM not flash.
  30. intptr_t start;
  31. intptr_t end;
  32. multi_heap_lock_t heap_mux;
  33. multi_heap_handle_t heap;
  34. SLIST_ENTRY(heap_t_) next;
  35. } heap_t;
  36. /* All registered heaps.
  37. Forms a single linked list, even though most entries are contiguous.
  38. This means at the expense of 4 bytes per heap, new heaps can be
  39. added at runtime in a fast & thread-safe way.
  40. */
  41. extern SLIST_HEAD(registered_heap_ll, heap_t_) registered_heaps;
  42. bool heap_caps_match(const heap_t *heap, uint32_t caps);
  43. /* return all possible capabilities (across all priorities) for a given heap */
  44. inline static IRAM_ATTR uint32_t get_all_caps(const heap_t *heap)
  45. {
  46. if (heap->heap == NULL) {
  47. return 0;
  48. }
  49. uint32_t all_caps = 0;
  50. for (int prio = 0; prio < SOC_MEMORY_TYPE_NO_PRIOS; prio++) {
  51. all_caps |= heap->caps[prio];
  52. }
  53. return all_caps;
  54. }
  55. /*
  56. Because we don't want to add _another_ known allocation method to the stack of functions to trace wrt memory tracing,
  57. these are declared private. The newlib malloc()/realloc() implementation also calls these, so they are declared
  58. separately in newlib/syscalls.c.
  59. */
  60. void *heap_caps_realloc_default(void *p, size_t size);
  61. void *heap_caps_malloc_default(size_t size);
  62. #ifdef __cplusplus
  63. }
  64. #endif