JBBoardに接続したモーター2つをRCBControllerでコントロールするテストです。
Dependencies: FatFileSystem TB6612FNG2 mbed
Fork of BTstackLE by
BTstack/memory_pool.c@7:3ed1e36587d4, 2014-05-12 (annotated)
- Committer:
- jksoft
- Date:
- Mon May 12 14:24:35 2014 +0000
- Revision:
- 7:3ed1e36587d4
- Parent:
- 1:6078e430af82
First edition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
todotani | 1:6078e430af82 | 1 | /* |
todotani | 1:6078e430af82 | 2 | * Copyright (C) 2011 by Matthias Ringwald |
todotani | 1:6078e430af82 | 3 | * |
todotani | 1:6078e430af82 | 4 | * Redistribution and use in source and binary forms, with or without |
todotani | 1:6078e430af82 | 5 | * modification, are permitted provided that the following conditions |
todotani | 1:6078e430af82 | 6 | * are met: |
todotani | 1:6078e430af82 | 7 | * |
todotani | 1:6078e430af82 | 8 | * 1. Redistributions of source code must retain the above copyright |
todotani | 1:6078e430af82 | 9 | * notice, this list of conditions and the following disclaimer. |
todotani | 1:6078e430af82 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
todotani | 1:6078e430af82 | 11 | * notice, this list of conditions and the following disclaimer in the |
todotani | 1:6078e430af82 | 12 | * documentation and/or other materials provided with the distribution. |
todotani | 1:6078e430af82 | 13 | * 3. Neither the name of the copyright holders nor the names of |
todotani | 1:6078e430af82 | 14 | * contributors may be used to endorse or promote products derived |
todotani | 1:6078e430af82 | 15 | * from this software without specific prior written permission. |
todotani | 1:6078e430af82 | 16 | * |
todotani | 1:6078e430af82 | 17 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS |
todotani | 1:6078e430af82 | 18 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
todotani | 1:6078e430af82 | 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
todotani | 1:6078e430af82 | 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS |
todotani | 1:6078e430af82 | 21 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
todotani | 1:6078e430af82 | 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
todotani | 1:6078e430af82 | 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
todotani | 1:6078e430af82 | 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
todotani | 1:6078e430af82 | 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
todotani | 1:6078e430af82 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
todotani | 1:6078e430af82 | 27 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
todotani | 1:6078e430af82 | 28 | * SUCH DAMAGE. |
todotani | 1:6078e430af82 | 29 | * |
todotani | 1:6078e430af82 | 30 | */ |
todotani | 1:6078e430af82 | 31 | |
todotani | 1:6078e430af82 | 32 | /* |
todotani | 1:6078e430af82 | 33 | * memory_pool.c |
todotani | 1:6078e430af82 | 34 | * |
todotani | 1:6078e430af82 | 35 | * Fixed-size block allocation |
todotani | 1:6078e430af82 | 36 | * |
todotani | 1:6078e430af82 | 37 | * Free blocks are kept in singly linked list |
todotani | 1:6078e430af82 | 38 | * |
todotani | 1:6078e430af82 | 39 | */ |
todotani | 1:6078e430af82 | 40 | |
todotani | 1:6078e430af82 | 41 | #include "btstack/memory_pool.h" |
todotani | 1:6078e430af82 | 42 | #include <stddef.h> |
todotani | 1:6078e430af82 | 43 | |
todotani | 1:6078e430af82 | 44 | typedef struct node { |
todotani | 1:6078e430af82 | 45 | struct node * next; |
todotani | 1:6078e430af82 | 46 | } node_t; |
todotani | 1:6078e430af82 | 47 | |
todotani | 1:6078e430af82 | 48 | void memory_pool_create(memory_pool_t *pool, void * storage, int count, int block_size){ |
todotani | 1:6078e430af82 | 49 | node_t *free_blocks = (node_t*) pool; |
todotani | 1:6078e430af82 | 50 | char *mem_ptr = (char *) storage; |
todotani | 1:6078e430af82 | 51 | int i; |
todotani | 1:6078e430af82 | 52 | |
todotani | 1:6078e430af82 | 53 | // create singly linked list of all available blocks |
todotani | 1:6078e430af82 | 54 | free_blocks->next = NULL; |
todotani | 1:6078e430af82 | 55 | for (i = 0 ; i < count ; i++){ |
todotani | 1:6078e430af82 | 56 | memory_pool_free(pool, mem_ptr); |
todotani | 1:6078e430af82 | 57 | mem_ptr += block_size; |
todotani | 1:6078e430af82 | 58 | } |
todotani | 1:6078e430af82 | 59 | } |
todotani | 1:6078e430af82 | 60 | |
todotani | 1:6078e430af82 | 61 | void * memory_pool_get(memory_pool_t *pool){ |
todotani | 1:6078e430af82 | 62 | node_t *free_blocks = (node_t*) pool; |
todotani | 1:6078e430af82 | 63 | |
todotani | 1:6078e430af82 | 64 | if (!free_blocks->next) return NULL; |
todotani | 1:6078e430af82 | 65 | |
todotani | 1:6078e430af82 | 66 | // remove first |
todotani | 1:6078e430af82 | 67 | node_t *node = free_blocks->next; |
todotani | 1:6078e430af82 | 68 | free_blocks->next = node->next; |
todotani | 1:6078e430af82 | 69 | |
todotani | 1:6078e430af82 | 70 | return (void*) node; |
todotani | 1:6078e430af82 | 71 | } |
todotani | 1:6078e430af82 | 72 | |
todotani | 1:6078e430af82 | 73 | void memory_pool_free(memory_pool_t *pool, void * block){ |
todotani | 1:6078e430af82 | 74 | node_t *free_blocks = (node_t*) pool; |
todotani | 1:6078e430af82 | 75 | node_t *node = (node_t*) block; |
todotani | 1:6078e430af82 | 76 | // add block as node to list |
todotani | 1:6078e430af82 | 77 | node->next = free_blocks->next; |
todotani | 1:6078e430af82 | 78 | free_blocks->next = node; |
todotani | 1:6078e430af82 | 79 | } |