├── .clang-format ├── .gitignore ├── LICENSE ├── README.md ├── chry_mempool.c ├── chry_mempool.h ├── chry_mempool_osal_freertos.c ├── chry_mempool_osal_nonos.c └── chry_mempool_osal_rtthread.c /.clang-format: -------------------------------------------------------------------------------- 1 | # clang-format configuration file. Intended for clang-format >= 11.0 2 | # 3 | # For more information, see: 4 | # 5 | # https://clang.llvm.org/docs/ClangFormat.html 6 | # https://clang.llvm.org/docs/ClangFormatStyleOptions.html 7 | # 8 | --- 9 | # 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto 10 | Language: Cpp 11 | # BasedOnStyle: LLVM 12 | # 访问说明符(public、private等)的偏移 13 | AccessModifierOffset: -4 14 | # 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行) 15 | AlignAfterOpenBracket: Align 16 | # 连续赋值时,对齐所有等号 17 | AlignConsecutiveAssignments: false 18 | # 对齐位域 19 | AlignConsecutiveBitFields: true 20 | # 连续声明时,对齐所有声明的变量名 21 | AlignConsecutiveDeclarations: false 22 | # 连续宏时,进行对齐 23 | AlignConsecutiveMacros: true 24 | # 左对齐逃脱换行(使用反斜杠换行)的反斜杠 25 | AlignEscapedNewlines: Left 26 | # 水平对齐二元和三元表达式的操作数 27 | AlignOperands: true 28 | # 对齐连续的尾随的注释 29 | AlignTrailingComments: true 30 | # 允许函数声明的所有参数在放在下一行 31 | AllowAllParametersOfDeclarationOnNextLine: false 32 | # 允许短的块放在同一行 33 | AllowShortBlocksOnASingleLine: false 34 | # 允许短的case标签放在同一行 35 | AllowShortCaseLabelsOnASingleLine: false 36 | # 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All 37 | AllowShortFunctionsOnASingleLine: None 38 | # 允许短的if语句保持在同一行 39 | AllowShortIfStatementsOnASingleLine: false 40 | # 允许短的循环保持在同一行 41 | AllowShortLoopsOnASingleLine: false 42 | # 总是在定义返回类型后换行(deprecated) 43 | AlwaysBreakAfterDefinitionReturnType: None 44 | # 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数), 45 | # AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义) 46 | AlwaysBreakAfterReturnType: None 47 | # 总是在多行string字面量前换行 48 | AlwaysBreakBeforeMultilineStrings: false 49 | # 总是在template声明后换行 50 | AlwaysBreakTemplateDeclarations: false 51 | # false表示函数实参要么都在同一行,要么都各自一行 52 | BinPackArguments: true 53 | # false表示所有形参要么都在同一行,要么都各自一行 54 | BinPackParameters: true 55 | # 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效 56 | BraceWrapping: 57 | AfterClass: false 58 | AfterControlStatement: false 59 | AfterEnum: false 60 | AfterFunction: true 61 | AfterNamespace: false 62 | AfterObjCDeclaration: false 63 | AfterStruct: false 64 | AfterUnion: false 65 | AfterExternBlock: false # Unknown to clang-format-5.0 66 | BeforeCatch: false 67 | BeforeElse: false 68 | IndentBraces: false 69 | SplitEmptyFunction: true # Unknown to clang-format-4.0 70 | SplitEmptyRecord: true # Unknown to clang-format-4.0 71 | SplitEmptyNamespace: true # Unknown to clang-format-4.0 72 | # 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行) 73 | BreakBeforeBinaryOperators: None 74 | BreakBeforeBraces: Custom 75 | #BreakBeforeInheritanceComma: false # Unknown to clang-format-4.0 76 | # 在三元运算符前换行 77 | BreakBeforeTernaryOperators: false 78 | # 在构造函数的初始化列表的逗号前换行 79 | BreakConstructorInitializersBeforeComma: false 80 | BreakAfterJavaFieldAnnotations: false 81 | BreakStringLiterals: false 82 | # 每行字符的限制,0表示没有限制 83 | ColumnLimit: 0 84 | # 描述具有特殊意义的注释的正则表达式,它不应该被分割为多行或以其它方式改变 85 | CommentPragmas: '^ IWYU pragma:' 86 | CompactNamespaces: false # Unknown to clang-format-4.0 87 | # 构造函数的初始化列表要么都在同一行,要么都各自一行 88 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 89 | # 构造函数的初始化列表的缩进宽度 90 | ConstructorInitializerIndentWidth: 4 91 | # 延续的行的缩进宽度 92 | ContinuationIndentWidth: 4 93 | # 去除C++11的列表初始化的大括号{后和}前的空格 94 | Cpp11BracedListStyle: false 95 | # 继承最常用的指针和引用的对齐方式 96 | DerivePointerAlignment: false 97 | # 关闭格式化 98 | DisableFormat: false 99 | ForEachMacros: 100 | - 'SHELL_EXPORT_CMD' 101 | 102 | # 自动检测函数的调用和定义是否被格式为每行一个参数(Experimental) 103 | ExperimentalAutoDetectBinPacking: false 104 | # 缩进case标签 105 | IndentCaseLabels: true 106 | # 缩进宽度 107 | IndentWidth: 4 108 | # 函数返回类型换行时,缩进函数声明或函数定义的函数名 109 | IndentWrappedFunctionNames: false 110 | # 保留在块开始处的空行 111 | KeepEmptyLinesAtTheStartOfBlocks: false 112 | # 开始一个块的宏的正则表达式 113 | MacroBlockBegin: '' 114 | # 结束一个块的宏的正则表达式 115 | MacroBlockEnd: '' 116 | # 连续空行的最大数量 117 | MaxEmptyLinesToKeep: 1 118 | # 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All 119 | NamespaceIndentation: None 120 | # 使用ObjC块时缩进宽度 121 | ObjCBlockIndentWidth: 4 122 | # 在ObjC的@property后添加一个空格 123 | ObjCSpaceAfterProperty: false 124 | # 在ObjC的protocol列表前添加一个空格 125 | ObjCSpaceBeforeProtocolList: true 126 | # 在call(后对函数调用换行的penalty 127 | PenaltyBreakBeforeFirstCallParameter: 30 128 | # 在一个注释中引入换行的penalty 129 | PenaltyBreakComment: 10 130 | # 第一次在<<前换行的penalty 131 | PenaltyBreakFirstLessLess: 0 132 | # 在一个字符串字面量中引入换行的penalty 133 | PenaltyBreakString: 10 134 | # 对于每个在行字符数限制之外的字符的penalty 135 | PenaltyExcessCharacter: 100 136 | # 将函数的返回类型放到它自己的行的penalty 137 | PenaltyReturnTypeOnItsOwnLine: 60 138 | # 指针和引用的对齐: Left, Right, Middle 139 | PointerAlignment: Right 140 | # 允许重新排版注释 141 | ReflowComments: false 142 | # 允许排序#include 143 | SortIncludes: false 144 | # 在C风格类型转换后添加空格 145 | SpaceAfterCStyleCast: false 146 | # 在赋值运算符之前添加空格 147 | SpaceBeforeAssignmentOperators: true 148 | # 开圆括号之前添加一个空格: Never, ControlStatements, Always 149 | SpaceBeforeParens: ControlStatements 150 | # 在空的圆括号中添加空格 151 | SpaceInEmptyParentheses: false 152 | # 在尾随的评论前添加的空格数(只适用于//) 153 | SpacesBeforeTrailingComments: 1 154 | # 在尖括号的<后和>前添加空格 155 | SpacesInAngles: false 156 | # 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格 157 | SpacesInContainerLiterals: false 158 | # 在C风格类型转换的括号中添加空格 159 | SpacesInCStyleCastParentheses: false 160 | # 在圆括号的(后和)前添加空格 161 | SpacesInParentheses: false 162 | # 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响 163 | SpacesInSquareBrackets: false 164 | # 标准: Cpp03, Cpp11, Auto 165 | Standard: Cpp03 166 | # tab宽度 167 | TabWidth: 4 168 | # 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always 169 | UseTab: Never 170 | ... 171 | 172 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CherryMempool 2 | 3 | CherryMempool is a tiny block memory pool based on CherryRB, support nonos or os(but we suggest you use in os). 4 | -------------------------------------------------------------------------------- /chry_mempool.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024, sakumisu 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | #include "chry_mempool.h" 7 | 8 | /***************************************************************************** 9 | * @brief init ringbuffer 10 | * 11 | * @param[in] rb ringbuffer instance 12 | * @param[in] pool memory pool address 13 | * @param[in] size memory size in byte, 14 | * must be power of 2 !!! 15 | * 16 | * @retval int 0:Success -1:Error 17 | *****************************************************************************/ 18 | static int __chry_ringbuffer_init(chry_mempool_ringbuffer_t *rb, void *pool, uint32_t size) 19 | { 20 | if (NULL == rb) { 21 | return -1; 22 | } 23 | 24 | if (NULL == pool) { 25 | return -1; 26 | } 27 | 28 | if ((size < 2) || (size & (size - 1))) { 29 | return -1; 30 | } 31 | 32 | rb->in = 0; 33 | rb->out = 0; 34 | rb->mask = size - 1; 35 | rb->pool = pool; 36 | 37 | return 0; 38 | } 39 | 40 | /***************************************************************************** 41 | * @brief reset ringbuffer, clean all data, 42 | * should be add lock in multithread 43 | * 44 | * @param[in] rb ringbuffer instance 45 | * 46 | *****************************************************************************/ 47 | static void __chry_ringbuffer_reset(chry_mempool_ringbuffer_t *rb) 48 | { 49 | rb->in = 0; 50 | rb->out = 0; 51 | } 52 | 53 | /***************************************************************************** 54 | * @brief write data to ringbuffer, 55 | * should be add lock in multithread, 56 | * in single write thread not need lock 57 | * 58 | * @param[in] rb ringbuffer instance 59 | * @param[in] data data pointer 60 | * @param[in] size size in byte 61 | * 62 | * @retval uint32_t actual write size in byte 63 | *****************************************************************************/ 64 | static uint32_t __chry_ringbuffer_write(chry_mempool_ringbuffer_t *rb, void *data, uint32_t size) 65 | { 66 | uint32_t unused; 67 | uint32_t offset; 68 | uint32_t remain; 69 | 70 | unused = (rb->mask + 1) - (rb->in - rb->out); 71 | 72 | if (size > unused) { 73 | size = unused; 74 | } 75 | 76 | offset = rb->in & rb->mask; 77 | 78 | remain = rb->mask + 1 - offset; 79 | remain = remain > size ? size : remain; 80 | 81 | memcpy(((uint8_t *)(rb->pool)) + offset, data, remain); 82 | memcpy(rb->pool, (uint8_t *)data + remain, size - remain); 83 | 84 | rb->in += size; 85 | 86 | return size; 87 | } 88 | 89 | /***************************************************************************** 90 | * @brief peek data from ringbuffer 91 | * should be add lock in multithread, 92 | * in single read thread not need lock 93 | * 94 | * @param[in] rb ringbuffer instance 95 | * @param[in] data data pointer 96 | * @param[in] size size in byte 97 | * 98 | * @retval uint32_t actual peek size in byte 99 | *****************************************************************************/ 100 | static uint32_t __chry_ringbuffer_peek(chry_mempool_ringbuffer_t *rb, void *data, uint32_t size) 101 | { 102 | uint32_t used; 103 | uint32_t offset; 104 | uint32_t remain; 105 | 106 | used = rb->in - rb->out; 107 | if (size > used) { 108 | size = used; 109 | } 110 | 111 | offset = rb->out & rb->mask; 112 | 113 | remain = rb->mask + 1 - offset; 114 | remain = remain > size ? size : remain; 115 | 116 | memcpy(data, ((uint8_t *)(rb->pool)) + offset, remain); 117 | memcpy((uint8_t *)data + remain, rb->pool, size - remain); 118 | 119 | return size; 120 | } 121 | 122 | /***************************************************************************** 123 | * @brief read data from ringbuffer 124 | * should be add lock in multithread, 125 | * in single read thread not need lock 126 | * 127 | * @param[in] rb ringbuffer instance 128 | * @param[in] data data pointer 129 | * @param[in] size size in byte 130 | * 131 | * @retval uint32_t actual read size in byte 132 | *****************************************************************************/ 133 | static uint32_t __chry_ringbuffer_read(chry_mempool_ringbuffer_t *rb, void *data, uint32_t size) 134 | { 135 | size = __chry_ringbuffer_peek(rb, data, size); 136 | rb->out += size; 137 | return size; 138 | } 139 | 140 | int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count) 141 | { 142 | uintptr_t *item; 143 | 144 | if (block_count > CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT) { 145 | return -1; 146 | } 147 | 148 | if (pool->block_size % 4) { 149 | return -1; 150 | } 151 | 152 | if (__chry_ringbuffer_init(&pool->in, pool->in_buf, sizeof(uintptr_t) * block_count) == -1) { 153 | return -1; 154 | } 155 | 156 | if (__chry_ringbuffer_init(&pool->out, pool->out_buf, sizeof(uintptr_t) * block_count) == -1) { 157 | return -1; 158 | } 159 | 160 | pool->out_sem = chry_mempool_osal_sem_create(block_count); 161 | if (pool->out_sem == NULL) { 162 | return -1; 163 | } 164 | 165 | pool->block = block; 166 | pool->block_size = block_size; 167 | pool->block_count = block_count; 168 | 169 | for (uint32_t i = 0; i < pool->block_count; i++) { 170 | item = (uintptr_t *)((uint8_t *)pool->block + i * pool->block_size); 171 | chry_mempool_free(pool, item); 172 | } 173 | 174 | return 0; 175 | } 176 | 177 | void chry_mempool_delete(struct chry_mempool *pool) 178 | { 179 | chry_mempool_osal_sem_delete(pool->out_sem); 180 | __chry_ringbuffer_reset(&pool->in); 181 | __chry_ringbuffer_reset(&pool->out); 182 | } 183 | 184 | uintptr_t *chry_mempool_alloc(struct chry_mempool *pool) 185 | { 186 | uintptr_t *addr; 187 | uint32_t len; 188 | 189 | len = __chry_ringbuffer_read(&pool->in, (uintptr_t *)&addr, sizeof(uintptr_t)); 190 | if (len == 0) { 191 | return NULL; 192 | } else { 193 | return addr; 194 | } 195 | } 196 | 197 | int chry_mempool_free(struct chry_mempool *pool, uintptr_t *item) 198 | { 199 | uintptr_t addr; 200 | 201 | addr = (uintptr_t)item; 202 | return __chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t)); 203 | } 204 | 205 | int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item) 206 | { 207 | uintptr_t addr; 208 | 209 | addr = (uintptr_t)item; 210 | __chry_ringbuffer_write(&pool->out, &addr, sizeof(uintptr_t)); 211 | return chry_mempool_osal_sem_give(pool->out_sem); 212 | } 213 | 214 | int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t timeout) 215 | { 216 | uint32_t len; 217 | int ret; 218 | 219 | ret = chry_mempool_osal_sem_take(pool->out_sem, timeout); 220 | if (ret < 0) { 221 | return -1; 222 | } 223 | 224 | len = __chry_ringbuffer_read(&pool->out, (uintptr_t *)item, sizeof(uintptr_t)); 225 | if (len == 0) { 226 | return -1; 227 | } else { 228 | return 0; 229 | } 230 | } 231 | 232 | void chry_mempool_reset(struct chry_mempool *pool) 233 | { 234 | uintptr_t *item; 235 | 236 | __chry_ringbuffer_reset(&pool->in); 237 | __chry_ringbuffer_reset(&pool->out); 238 | 239 | for (uint32_t i = 0; i < pool->block_count; i++) { 240 | item = (uintptr_t *)((uint8_t *)pool->block + i * pool->block_size); 241 | chry_mempool_free(pool, item); 242 | } 243 | } -------------------------------------------------------------------------------- /chry_mempool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024, sakumisu 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | #ifndef CHRY_MEMPOOL_H 7 | #define CHRY_MEMPOOL_H 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | typedef void *chry_mempool_osal_sem_t; 14 | 15 | #ifndef CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT 16 | #define CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT 128 17 | #endif 18 | 19 | typedef struct { 20 | uint32_t in; /*!< Define the write pointer. */ 21 | uint32_t out; /*!< Define the read pointer. */ 22 | uint32_t mask; /*!< Define the write and read pointer mask. */ 23 | void *pool; /*!< Define the memory pointer. */ 24 | } chry_mempool_ringbuffer_t; 25 | 26 | struct chry_mempool { 27 | chry_mempool_ringbuffer_t in; 28 | chry_mempool_ringbuffer_t out; 29 | chry_mempool_osal_sem_t out_sem; 30 | 31 | void *block; 32 | uint32_t block_size; 33 | uint32_t block_count; 34 | uint8_t in_buf[sizeof(uintptr_t) * CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT]; 35 | uint8_t out_buf[sizeof(uintptr_t) * CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT]; 36 | }; 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count); 43 | void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem); 44 | int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout); 45 | int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem); 46 | 47 | int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count); 48 | uintptr_t *chry_mempool_alloc(struct chry_mempool *pool); 49 | int chry_mempool_free(struct chry_mempool *pool, uintptr_t *item); 50 | int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item); 51 | int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t timeout); 52 | void chry_mempool_reset(struct chry_mempool *pool); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif -------------------------------------------------------------------------------- /chry_mempool_osal_freertos.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024, sakumisu 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | #include "chry_mempool.h" 7 | #include 8 | #include "semphr.h" 9 | 10 | chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count) 11 | { 12 | return (chry_mempool_osal_sem_t)xSemaphoreCreateCounting(max_count, 0); 13 | } 14 | 15 | void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem) 16 | { 17 | vSemaphoreDelete((SemaphoreHandle_t)sem); 18 | } 19 | 20 | int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout) 21 | { 22 | BaseType_t xHigherPriorityTaskWoken = pdFALSE; 23 | int ret; 24 | 25 | if (xPortIsInsideInterrupt()) { 26 | ret = xSemaphoreTakeFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken); 27 | if (ret == pdPASS) { 28 | portYIELD_FROM_ISR(xHigherPriorityTaskWoken); 29 | } 30 | return (ret == pdPASS) ? 0 : -1; 31 | } else { 32 | return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -1; 33 | } 34 | } 35 | 36 | int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem) 37 | { 38 | BaseType_t xHigherPriorityTaskWoken = pdFALSE; 39 | int ret; 40 | 41 | if (xPortIsInsideInterrupt()) { 42 | ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken); 43 | if (ret == pdPASS) { 44 | portYIELD_FROM_ISR(xHigherPriorityTaskWoken); 45 | } 46 | } else { 47 | ret = xSemaphoreGive((SemaphoreHandle_t)sem); 48 | } 49 | 50 | return (ret == pdPASS) ? 0 : -1; 51 | } -------------------------------------------------------------------------------- /chry_mempool_osal_nonos.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024, sakumisu 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | #include "chry_mempool.h" 7 | #include "stdlib.h" 8 | 9 | chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count) 10 | { 11 | return (chry_mempool_osal_sem_t)1; 12 | } 13 | 14 | void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem) 15 | { 16 | } 17 | 18 | int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout) 19 | { 20 | return 0; 21 | } 22 | 23 | int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem) 24 | { 25 | return 0; 26 | } -------------------------------------------------------------------------------- /chry_mempool_osal_rtthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024, sakumisu 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | #include "chry_mempool.h" 7 | #include 8 | #include 9 | 10 | chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count) 11 | { 12 | return (chry_mempool_osal_sem_t)rt_sem_create("chry_mempoolh_sem", max_count, RT_IPC_FLAG_FIFO); 13 | } 14 | 15 | void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem) 16 | { 17 | rt_sem_delete((rt_sem_t)sem); 18 | } 19 | 20 | int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout) 21 | { 22 | int ret = 0; 23 | rt_err_t result = RT_EOK; 24 | 25 | if (timeout == 0xfffffff) { 26 | result = rt_sem_take((rt_sem_t)sem, RT_WAITING_FOREVER); 27 | } else { 28 | result = rt_sem_take((rt_sem_t)sem, rt_tick_from_millisecond(timeout)); 29 | } 30 | if (result == -RT_ETIMEOUT) { 31 | ret = -1; 32 | } else if (result == -RT_ERROR) { 33 | ret = -1; 34 | } else { 35 | ret = 0; 36 | } 37 | 38 | return (int)ret; 39 | } 40 | 41 | int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem) 42 | { 43 | return (int)rt_sem_release((rt_sem_t)sem); 44 | } --------------------------------------------------------------------------------