Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rt_Memory.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_MEMORY.C
  5. * Purpose: Interface functions for Dynamic Memory Management System
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "rt_Memory.h"
  36. /* Functions */
  37. // Initialize Dynamic Memory pool
  38. // Parameters:
  39. // pool: Pointer to memory pool
  40. // size: Size of memory pool in bytes
  41. // Return: 0 - OK, 1 - Error
  42. int rt_init_mem (void *pool, U32 size) {
  43. MEMP *ptr;
  44. if ((pool == NULL) || (size < sizeof(MEMP))) return (1);
  45. ptr = (MEMP *)pool;
  46. ptr->next = (MEMP *)((U32)pool + size - sizeof(MEMP *));
  47. ptr->next->next = NULL;
  48. ptr->len = 0;
  49. return (0);
  50. }
  51. // Allocate Memory from Memory pool
  52. // Parameters:
  53. // pool: Pointer to memory pool
  54. // size: Size of memory in bytes to allocate
  55. // Return: Pointer to allocated memory
  56. void *rt_alloc_mem (void *pool, U32 size) {
  57. MEMP *p, *p_search, *p_new;
  58. U32 hole_size;
  59. if ((pool == NULL) || (size == 0)) return NULL;
  60. /* Add header offset to 'size' */
  61. size += sizeof(MEMP);
  62. /* Make sure that block is 4-byte aligned */
  63. size = (size + 3) & ~3;
  64. p_search = (MEMP *)pool;
  65. while (1) {
  66. hole_size = (U32)p_search->next - (U32)p_search;
  67. hole_size -= p_search->len;
  68. /* Check if hole size is big enough */
  69. if (hole_size >= size) break;
  70. p_search = p_search->next;
  71. if (p_search->next == NULL) {
  72. /* Failed, we are at the end of the list */
  73. return NULL;
  74. }
  75. }
  76. if (p_search->len == 0) {
  77. /* No block is allocated, set the Length of the first element */
  78. p_search->len = size;
  79. p = (MEMP *)(((U32)p_search) + sizeof(MEMP));
  80. } else {
  81. /* Insert new list element into the memory list */
  82. p_new = (MEMP *)((U32)p_search + p_search->len);
  83. p_new->next = p_search->next;
  84. p_new->len = size;
  85. p_search->next = p_new;
  86. p = (MEMP *)(((U32)p_new) + sizeof(MEMP));
  87. }
  88. return (p);
  89. }
  90. // Free Memory and return it to Memory pool
  91. // Parameters:
  92. // pool: Pointer to memory pool
  93. // mem: Pointer to memory to free
  94. // Return: 0 - OK, 1 - Error
  95. int rt_free_mem (void *pool, void *mem) {
  96. MEMP *p_search, *p_prev, *p_return;
  97. if ((pool == NULL) || (mem == NULL)) return (1);
  98. p_return = (MEMP *)((U32)mem - sizeof(MEMP));
  99. /* Set list header */
  100. p_prev = NULL;
  101. p_search = (MEMP *)pool;
  102. while (p_search != p_return) {
  103. p_prev = p_search;
  104. p_search = p_search->next;
  105. if (p_search == NULL) {
  106. /* Valid Memory block not found */
  107. return (1);
  108. }
  109. }
  110. if (p_prev == NULL) {
  111. /* First block to be released, only set length to 0 */
  112. p_search->len = 0;
  113. } else {
  114. /* Discard block from chain list */
  115. p_prev->next = p_search->next;
  116. }
  117. return (0);
  118. }