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