This function allocates a continuous memory space that contains a TfLiteDelegateParams followed by a several TfLiteIntArray. When calling `free` at TfLiteDelegateParams*, all the allocated space will be freed together. +-----------------------------------+ | TfLiteDelegateParams | | TfLiteDelegate* delegate; | | TfLiteIntArray* nodes_to_replace; |--\ // | TfLiteIntArray* inpu
| 252 | // | TfLiteIntArray (variable size) |<-------/ |
| 253 | // +-----------------------------------+ |
| 254 | TfLiteDelegateParams* CreateDelegateParams(TfLiteDelegate* delegate, |
| 255 | const NodeSubset& node_subset) { |
| 256 | // Step 1: Calculate the allocation size. |
| 257 | int allocation_size = sizeof(TfLiteDelegateParams); |
| 258 | |
| 259 | int nodes_to_replace_size = |
| 260 | TfLiteIntArrayGetSizeInBytes(node_subset.nodes.size()); |
| 261 | allocation_size += nodes_to_replace_size; |
| 262 | |
| 263 | int input_tensors_size = |
| 264 | TfLiteIntArrayGetSizeInBytes(node_subset.input_tensors.size()); |
| 265 | allocation_size += input_tensors_size; |
| 266 | |
| 267 | int output_tensors_size = |
| 268 | TfLiteIntArrayGetSizeInBytes(node_subset.output_tensors.size()); |
| 269 | allocation_size += output_tensors_size; |
| 270 | |
| 271 | // Step 2: Allocate the memory. |
| 272 | // Use `char*` for conveniently step through the allocated space by bytes. |
| 273 | char* allocation = reinterpret_cast<char*>(malloc(allocation_size)); |
| 274 | |
| 275 | // Step 3: Fill all data structures structures. |
| 276 | TfLiteDelegateParams* params = |
| 277 | reinterpret_cast<TfLiteDelegateParams*>(allocation); |
| 278 | params->delegate = delegate; |
| 279 | allocation += sizeof(TfLiteDelegateParams); |
| 280 | |
| 281 | params->nodes_to_replace = reinterpret_cast<TfLiteIntArray*>(allocation); |
| 282 | CopyVectorToTfLiteIntArray(node_subset.nodes, params->nodes_to_replace); |
| 283 | allocation += nodes_to_replace_size; |
| 284 | |
| 285 | params->input_tensors = reinterpret_cast<TfLiteIntArray*>(allocation); |
| 286 | CopyVectorToTfLiteIntArray(node_subset.input_tensors, params->input_tensors); |
| 287 | allocation += input_tensors_size; |
| 288 | |
| 289 | params->output_tensors = reinterpret_cast<TfLiteIntArray*>(allocation); |
| 290 | CopyVectorToTfLiteIntArray(node_subset.output_tensors, |
| 291 | params->output_tensors); |
| 292 | allocation += output_tensors_size; |
| 293 | |
| 294 | return params; |
| 295 | } |
| 296 | |
| 297 | } // namespace |
| 298 |
no test coverage detected