mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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