xref: /aosp_15_r20/external/llvm-libc/src/spawn/posix_spawn_file_actions_destroy.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Implementation of posix_spawn_file_actions_destroy ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "posix_spawn_file_actions_destroy.h"
10 
11 #include "file_actions.h"
12 
13 #include "src/__support/CPP/new.h"
14 #include "src/__support/common.h"
15 #include "src/__support/macros/config.h"
16 #include "src/errno/libc_errno.h"
17 
18 #include <spawn.h>
19 
20 namespace LIBC_NAMESPACE_DECL {
21 
22 LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_destroy,
23                    (posix_spawn_file_actions_t * actions)) {
24   if (actions == nullptr)
25     return EINVAL;
26   if (actions->__front == nullptr)
27     return 0;
28 
29   auto *act = reinterpret_cast<BaseSpawnFileAction *>(actions->__front);
30   actions->__front = nullptr;
31   actions->__back = nullptr;
32   if (act == nullptr)
33     return 0;
34 
35   while (act != nullptr) {
36     auto *temp = act;
37     act = act->next;
38     switch (temp->type) {
39     case BaseSpawnFileAction::OPEN:
40       delete reinterpret_cast<SpawnFileOpenAction *>(temp);
41       break;
42     case BaseSpawnFileAction::CLOSE:
43       delete reinterpret_cast<SpawnFileCloseAction *>(temp);
44       break;
45     case BaseSpawnFileAction::DUP2:
46       delete reinterpret_cast<SpawnFileDup2Action *>(temp);
47       break;
48     }
49   }
50 
51   return 0;
52 }
53 
54 } // namespace LIBC_NAMESPACE_DECL
55