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