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