ソースの整理中ですが、利用はできます。

Dependencies:   EthernetInterface HttpServer TextLCD mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
yueee_yt
Date:
Wed Mar 12 04:39:15 2014 +0000
Revision:
2:14b689a85306
Parent:
0:7766f6712673
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:7766f6712673 1 /* mbed Microcontroller Library
yueee_yt 0:7766f6712673 2 * Copyright (c) 2006-2012 ARM Limited
yueee_yt 0:7766f6712673 3 *
yueee_yt 0:7766f6712673 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
yueee_yt 0:7766f6712673 5 * of this software and associated documentation files (the "Software"), to deal
yueee_yt 0:7766f6712673 6 * in the Software without restriction, including without limitation the rights
yueee_yt 0:7766f6712673 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yueee_yt 0:7766f6712673 8 * copies of the Software, and to permit persons to whom the Software is
yueee_yt 0:7766f6712673 9 * furnished to do so, subject to the following conditions:
yueee_yt 0:7766f6712673 10 *
yueee_yt 0:7766f6712673 11 * The above copyright notice and this permission notice shall be included in
yueee_yt 0:7766f6712673 12 * all copies or substantial portions of the Software.
yueee_yt 0:7766f6712673 13 *
yueee_yt 0:7766f6712673 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yueee_yt 0:7766f6712673 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yueee_yt 0:7766f6712673 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yueee_yt 0:7766f6712673 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yueee_yt 0:7766f6712673 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yueee_yt 0:7766f6712673 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
yueee_yt 0:7766f6712673 20 * SOFTWARE.
yueee_yt 0:7766f6712673 21 */
yueee_yt 0:7766f6712673 22 #ifndef MEMORYPOOL_H
yueee_yt 0:7766f6712673 23 #define MEMORYPOOL_H
yueee_yt 0:7766f6712673 24
yueee_yt 0:7766f6712673 25 #include <stdint.h>
yueee_yt 0:7766f6712673 26 #include <string.h>
yueee_yt 0:7766f6712673 27
yueee_yt 0:7766f6712673 28 #include "cmsis_os.h"
yueee_yt 0:7766f6712673 29
yueee_yt 0:7766f6712673 30 namespace rtos {
yueee_yt 0:7766f6712673 31
yueee_yt 0:7766f6712673 32 /** Define and manage fixed-size memory pools of objects of a given type.
yueee_yt 0:7766f6712673 33 @tparam T data type of a single object (element).
yueee_yt 0:7766f6712673 34 @tparam queue_sz maximum number of objects (elements) in the memory pool.
yueee_yt 0:7766f6712673 35 */
yueee_yt 0:7766f6712673 36 template<typename T, uint32_t pool_sz>
yueee_yt 0:7766f6712673 37 class MemoryPool {
yueee_yt 0:7766f6712673 38 public:
yueee_yt 0:7766f6712673 39 /** Create and Initialize a memory pool. */
yueee_yt 0:7766f6712673 40 MemoryPool() {
yueee_yt 0:7766f6712673 41 #ifdef CMSIS_OS_RTX
yueee_yt 0:7766f6712673 42 memset(_pool_m, 0, sizeof(_pool_m));
yueee_yt 0:7766f6712673 43 _pool_def.pool = _pool_m;
yueee_yt 0:7766f6712673 44
yueee_yt 0:7766f6712673 45 _pool_def.pool_sz = pool_sz;
yueee_yt 0:7766f6712673 46 _pool_def.item_sz = sizeof(T);
yueee_yt 0:7766f6712673 47 #endif
yueee_yt 0:7766f6712673 48 _pool_id = osPoolCreate(&_pool_def);
yueee_yt 0:7766f6712673 49 }
yueee_yt 0:7766f6712673 50
yueee_yt 0:7766f6712673 51 /** Allocate a memory block of type T from a memory pool.
yueee_yt 0:7766f6712673 52 @return address of the allocated memory block or NULL in case of no memory available.
yueee_yt 0:7766f6712673 53 */
yueee_yt 0:7766f6712673 54 T* alloc(void) {
yueee_yt 0:7766f6712673 55 return (T*)osPoolAlloc(_pool_id);
yueee_yt 0:7766f6712673 56 }
yueee_yt 0:7766f6712673 57
yueee_yt 0:7766f6712673 58 /** Allocate a memory block of type T from a memory pool and set memory block to zero.
yueee_yt 0:7766f6712673 59 @return address of the allocated memory block or NULL in case of no memory available.
yueee_yt 0:7766f6712673 60 */
yueee_yt 0:7766f6712673 61 T* calloc(void) {
yueee_yt 0:7766f6712673 62 return (T*)osPoolCAlloc(_pool_id);
yueee_yt 0:7766f6712673 63 }
yueee_yt 0:7766f6712673 64
yueee_yt 0:7766f6712673 65 /** Return an allocated memory block back to a specific memory pool.
yueee_yt 0:7766f6712673 66 @param address of the allocated memory block that is returned to the memory pool.
yueee_yt 0:7766f6712673 67 @return status code that indicates the execution status of the function.
yueee_yt 0:7766f6712673 68 */
yueee_yt 0:7766f6712673 69 osStatus free(T *block) {
yueee_yt 0:7766f6712673 70 return osPoolFree(_pool_id, (void*)block);
yueee_yt 0:7766f6712673 71 }
yueee_yt 0:7766f6712673 72
yueee_yt 0:7766f6712673 73 private:
yueee_yt 0:7766f6712673 74 osPoolId _pool_id;
yueee_yt 0:7766f6712673 75 osPoolDef_t _pool_def;
yueee_yt 0:7766f6712673 76 #ifdef CMSIS_OS_RTX
yueee_yt 0:7766f6712673 77 uint32_t _pool_m[3+((sizeof(T)+3)/4)*(pool_sz)];
yueee_yt 0:7766f6712673 78 #endif
yueee_yt 0:7766f6712673 79 };
yueee_yt 0:7766f6712673 80
yueee_yt 0:7766f6712673 81 }
yueee_yt 0:7766f6712673 82 #endif