]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-IoT-SDK/c_sdk/standard/common/taskpool/iot_taskpool_static_memory.c
Update task pool so tasks and timer are allocated statically.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-IoT-SDK / c_sdk / standard / common / taskpool / iot_taskpool_static_memory.c
1 /*\r
2  * Amazon FreeRTOS Common V1.0.0\r
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://aws.amazon.com/freertos\r
23  * http://www.FreeRTOS.org\r
24  */\r
25 \r
26 /**\r
27  * @file iot_taskpool_static_memory.c\r
28  * @brief Implementation of task pool static memory functions.\r
29  */\r
30 \r
31 /* The config header is always included first. */\r
32 #include "iot_config.h"\r
33 \r
34 /* This file should only be compiled if dynamic memory allocation is forbidden. */\r
35 #if IOT_STATIC_MEMORY_ONLY == 1\r
36 \r
37 /* Standard includes. */\r
38 #include <stdbool.h>\r
39 #include <stddef.h>\r
40 #include <string.h>\r
41 \r
42 /* Static memory include. */\r
43 #include "private/iot_static_memory.h"\r
44 \r
45 /* Task pool internal include. */\r
46 #include "private/iot_taskpool_internal.h"\r
47 \r
48 /*-----------------------------------------------------------*/\r
49 \r
50 /* Validate static memory configuration settings. */\r
51 #if IOT_TASKPOOL_JOBS_RECYCLE_LIMIT <= 0\r
52     #error "IOT_TASKPOOL_JOBS_RECYCLE_LIMIT cannot be 0 or negative."\r
53 #endif\r
54 \r
55 /*-----------------------------------------------------------*/\r
56 \r
57 /*\r
58  * Static memory buffers and flags, allocated and zeroed at compile-time.\r
59  */\r
60 static bool _pInUseTaskPools[ IOT_TASKPOOLS ] = { 0 };                                                /**< @brief Task pools in-use flags. */\r
61 static _taskPool_t _pTaskPools[ IOT_TASKPOOLS ] = { { .dispatchQueue = IOT_DEQUEUE_INITIALIZER } };   /**< @brief Task pools. */\r
62 \r
63 static bool _pInUseTaskPoolJobs[ IOT_TASKPOOL_JOBS_RECYCLE_LIMIT ] = { 0 };                                     /**< @brief Task pool jobs in-use flags. */\r
64 static _taskPoolJob_t _pTaskPoolJobs[ IOT_TASKPOOL_JOBS_RECYCLE_LIMIT ] = { { .link = IOT_LINK_INITIALIZER } }; /**< @brief Task pool jobs. */\r
65 \r
66 static bool _pInUseTaskPoolTimerEvents[ IOT_TASKPOOL_JOBS_RECYCLE_LIMIT ] = { 0 };                              /**< @brief Task pool timer event in-use flags. */\r
67 static _taskPoolTimerEvent_t _pTaskPoolTimerEvents[ IOT_TASKPOOL_JOBS_RECYCLE_LIMIT ] = { { .link = { 0 } } };  /**< @brief Task pool timer events. */\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 void * IotTaskPool_MallocTaskPool( size_t size )\r
72 {\r
73     int freeIndex = -1;\r
74     void * pNewTaskPool = NULL;\r
75 \r
76     /* Check size argument. */\r
77     if( size == sizeof( _taskPool_t ) )\r
78     {\r
79         /* Find a free task pool job. */\r
80         freeIndex = IotStaticMemory_FindFree( _pInUseTaskPools, IOT_TASKPOOLS );\r
81 \r
82         if( freeIndex != -1 )\r
83         {\r
84             pNewTaskPool = &( _pTaskPools[ freeIndex ] );\r
85         }\r
86     }\r
87 \r
88     return pNewTaskPool;\r
89 }\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void IotTaskPool_FreeTaskPool( void * ptr )\r
94 {\r
95     /* Return the in-use task pool job. */\r
96     IotStaticMemory_ReturnInUse( ptr,\r
97                                  _pTaskPools,\r
98                                  _pInUseTaskPools,\r
99                                  IOT_TASKPOOLS,\r
100                                  sizeof( _taskPool_t ) );\r
101 }\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 void * IotTaskPool_MallocJob( size_t size )\r
106 {\r
107     int32_t freeIndex = -1;\r
108     void * pNewJob = NULL;\r
109 \r
110     /* Check size argument. */\r
111     if( size == sizeof( _taskPoolJob_t ) )\r
112     {\r
113         /* Find a free task pool job. */\r
114         freeIndex = IotStaticMemory_FindFree( _pInUseTaskPoolJobs,\r
115                                               IOT_TASKPOOL_JOBS_RECYCLE_LIMIT );\r
116 \r
117         if( freeIndex != -1 )\r
118         {\r
119             pNewJob = &( _pTaskPoolJobs[ freeIndex ] );\r
120         }\r
121     }\r
122 \r
123     return pNewJob;\r
124 }\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void IotTaskPool_FreeJob( void * ptr )\r
129 {\r
130     /* Return the in-use task pool job. */\r
131     IotStaticMemory_ReturnInUse( ptr,\r
132                                  _pTaskPoolJobs,\r
133                                  _pInUseTaskPoolJobs,\r
134                                  IOT_TASKPOOL_JOBS_RECYCLE_LIMIT,\r
135                                  sizeof( _taskPoolJob_t ) );\r
136 }\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 void * IotTaskPool_MallocTimerEvent( size_t size )\r
141 {\r
142     int32_t freeIndex = -1;\r
143     void * pNewTimerEvent = NULL;\r
144 \r
145     /* Check size argument. */\r
146     if( size == sizeof( _taskPoolTimerEvent_t ) )\r
147     {\r
148         /* Find a free task pool timer event. */\r
149         freeIndex = IotStaticMemory_FindFree( _pInUseTaskPoolTimerEvents,\r
150                                               IOT_TASKPOOL_JOBS_RECYCLE_LIMIT );\r
151 \r
152         if( freeIndex != -1 )\r
153         {\r
154             pNewTimerEvent = &( _pTaskPoolTimerEvents[ freeIndex ] );\r
155         }\r
156     }\r
157 \r
158     return pNewTimerEvent;\r
159 }\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void IotTaskPool_FreeTimerEvent( void * ptr )\r
164 {\r
165     /* Return the in-use task pool timer event. */\r
166     IotStaticMemory_ReturnInUse( ptr,\r
167                                  _pTaskPoolTimerEvents,\r
168                                  _pInUseTaskPoolTimerEvents,\r
169                                  IOT_TASKPOOL_JOBS_RECYCLE_LIMIT,\r
170                                  sizeof( _taskPoolTimerEvent_t ) );\r
171 }\r
172 \r
173 /*-----------------------------------------------------------*/\r
174 \r
175 #endif\r