mem_slab.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2016 Wind River Systems, Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <kernel_structs.h>
  8. #include <toolchain.h>
  9. #include <linker/sections.h>
  10. #include <wait_q.h>
  11. #include <sys/dlist.h>
  12. #include <ksched.h>
  13. #include <init.h>
  14. #include <sys/check.h>
  15. /**
  16. * @brief Initialize kernel memory slab subsystem.
  17. *
  18. * Perform any initialization of memory slabs that wasn't done at build time.
  19. * Currently this just involves creating the list of free blocks for each slab.
  20. *
  21. * @return N/A
  22. */
  23. static int create_free_list(struct k_mem_slab *slab)
  24. {
  25. uint32_t j;
  26. char *p;
  27. /* blocks must be word aligned */
  28. CHECKIF(((slab->block_size | (uintptr_t)slab->buffer) &
  29. (sizeof(void *) - 1)) != 0U) {
  30. return -EINVAL;
  31. }
  32. slab->free_list = NULL;
  33. p = slab->buffer;
  34. for (j = 0U; j < slab->num_blocks; j++) {
  35. *(char **)p = slab->free_list;
  36. slab->free_list = p;
  37. p += slab->block_size;
  38. }
  39. return 0;
  40. }
  41. /**
  42. * @brief Complete initialization of statically defined memory slabs.
  43. *
  44. * Perform any initialization that wasn't done at build time.
  45. *
  46. * @return N/A
  47. */
  48. static int init_mem_slab_module(const struct device *dev)
  49. {
  50. int rc = 0;
  51. ARG_UNUSED(dev);
  52. STRUCT_SECTION_FOREACH(k_mem_slab, slab) {
  53. rc = create_free_list(slab);
  54. if (rc < 0) {
  55. goto out;
  56. }
  57. z_object_init(slab);
  58. }
  59. out:
  60. return rc;
  61. }
  62. SYS_INIT(init_mem_slab_module, PRE_KERNEL_1,
  63. CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
  64. int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
  65. size_t block_size, uint32_t num_blocks)
  66. {
  67. int rc = 0;
  68. slab->num_blocks = num_blocks;
  69. slab->block_size = block_size;
  70. slab->buffer = buffer;
  71. slab->num_used = 0U;
  72. slab->lock = (struct k_spinlock) {};
  73. #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
  74. slab->max_used = 0U;
  75. #endif
  76. rc = create_free_list(slab);
  77. if (rc < 0) {
  78. goto out;
  79. }
  80. z_waitq_init(&slab->wait_q);
  81. z_object_init(slab);
  82. out:
  83. SYS_PORT_TRACING_OBJ_INIT(k_mem_slab, slab, rc);
  84. return rc;
  85. }
  86. int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
  87. {
  88. k_spinlock_key_t key = k_spin_lock(&slab->lock);
  89. int result;
  90. SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, alloc, slab, timeout);
  91. if (slab->free_list != NULL) {
  92. /* take a free block */
  93. *mem = slab->free_list;
  94. slab->free_list = *(char **)(slab->free_list);
  95. slab->num_used++;
  96. #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
  97. slab->max_used = MAX(slab->num_used, slab->max_used);
  98. #endif
  99. result = 0;
  100. } else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT) ||
  101. !IS_ENABLED(CONFIG_MULTITHREADING)) {
  102. /* don't wait for a free block to become available */
  103. *mem = NULL;
  104. result = -ENOMEM;
  105. } else {
  106. SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mem_slab, alloc, slab, timeout);
  107. /* wait for a free block or timeout */
  108. result = z_pend_curr(&slab->lock, key, &slab->wait_q, timeout);
  109. if (result == 0) {
  110. *mem = _current->base.swap_data;
  111. }
  112. SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
  113. return result;
  114. }
  115. SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
  116. k_spin_unlock(&slab->lock, key);
  117. return result;
  118. }
  119. void k_mem_slab_free(struct k_mem_slab *slab, void **mem)
  120. {
  121. k_spinlock_key_t key = k_spin_lock(&slab->lock);
  122. SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, free, slab);
  123. if (slab->free_list == NULL && IS_ENABLED(CONFIG_MULTITHREADING)) {
  124. struct k_thread *pending_thread = z_unpend_first_thread(&slab->wait_q);
  125. if (pending_thread != NULL) {
  126. SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
  127. z_thread_return_value_set_with_data(pending_thread, 0, *mem);
  128. z_ready_thread(pending_thread);
  129. z_reschedule(&slab->lock, key);
  130. return;
  131. }
  132. }
  133. **(char ***) mem = slab->free_list;
  134. slab->free_list = *(char **) mem;
  135. slab->num_used--;
  136. SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
  137. k_spin_unlock(&slab->lock, key);
  138. }