2 * Amazon FreeRTOS Common V1.0.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://aws.amazon.com/freertos
\r
23 * http://www.FreeRTOS.org
\r
27 * @file iot_taskpool_static_memory.c
\r
28 * @brief Implementation of task pool static memory functions.
\r
31 /* The config header is always included first. */
\r
32 #include "iot_config.h"
\r
34 /* This file should only be compiled if dynamic memory allocation is forbidden. */
\r
35 #if IOT_STATIC_MEMORY_ONLY == 1
\r
37 /* Standard includes. */
\r
38 #include <stdbool.h>
\r
42 /* Static memory include. */
\r
43 #include "private/iot_static_memory.h"
\r
45 /* Task pool internal include. */
\r
46 #include "private/iot_taskpool_internal.h"
\r
48 /*-----------------------------------------------------------*/
\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
55 /*-----------------------------------------------------------*/
\r
58 * Static memory buffers and flags, allocated and zeroed at compile-time.
\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
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
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
69 /*-----------------------------------------------------------*/
\r
71 void * IotTaskPool_MallocTaskPool( size_t size )
\r
74 void * pNewTaskPool = NULL;
\r
76 /* Check size argument. */
\r
77 if( size == sizeof( _taskPool_t ) )
\r
79 /* Find a free task pool job. */
\r
80 freeIndex = IotStaticMemory_FindFree( _pInUseTaskPools, IOT_TASKPOOLS );
\r
82 if( freeIndex != -1 )
\r
84 pNewTaskPool = &( _pTaskPools[ freeIndex ] );
\r
88 return pNewTaskPool;
\r
91 /*-----------------------------------------------------------*/
\r
93 void IotTaskPool_FreeTaskPool( void * ptr )
\r
95 /* Return the in-use task pool job. */
\r
96 IotStaticMemory_ReturnInUse( ptr,
\r
100 sizeof( _taskPool_t ) );
\r
103 /*-----------------------------------------------------------*/
\r
105 void * IotTaskPool_MallocJob( size_t size )
\r
107 int32_t freeIndex = -1;
\r
108 void * pNewJob = NULL;
\r
110 /* Check size argument. */
\r
111 if( size == sizeof( _taskPoolJob_t ) )
\r
113 /* Find a free task pool job. */
\r
114 freeIndex = IotStaticMemory_FindFree( _pInUseTaskPoolJobs,
\r
115 IOT_TASKPOOL_JOBS_RECYCLE_LIMIT );
\r
117 if( freeIndex != -1 )
\r
119 pNewJob = &( _pTaskPoolJobs[ freeIndex ] );
\r
126 /*-----------------------------------------------------------*/
\r
128 void IotTaskPool_FreeJob( void * ptr )
\r
130 /* Return the in-use task pool job. */
\r
131 IotStaticMemory_ReturnInUse( ptr,
\r
133 _pInUseTaskPoolJobs,
\r
134 IOT_TASKPOOL_JOBS_RECYCLE_LIMIT,
\r
135 sizeof( _taskPoolJob_t ) );
\r
138 /*-----------------------------------------------------------*/
\r
140 void * IotTaskPool_MallocTimerEvent( size_t size )
\r
142 int32_t freeIndex = -1;
\r
143 void * pNewTimerEvent = NULL;
\r
145 /* Check size argument. */
\r
146 if( size == sizeof( _taskPoolTimerEvent_t ) )
\r
148 /* Find a free task pool timer event. */
\r
149 freeIndex = IotStaticMemory_FindFree( _pInUseTaskPoolTimerEvents,
\r
150 IOT_TASKPOOL_JOBS_RECYCLE_LIMIT );
\r
152 if( freeIndex != -1 )
\r
154 pNewTimerEvent = &( _pTaskPoolTimerEvents[ freeIndex ] );
\r
158 return pNewTimerEvent;
\r
161 /*-----------------------------------------------------------*/
\r
163 void IotTaskPool_FreeTimerEvent( void * ptr )
\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
173 /*-----------------------------------------------------------*/
\r