[libc-commits] [libc] f112960 - [libc][NFC] Use operator new and operator delete in POSIX file actions API.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Thu Dec 22 21:58:38 PST 2022
Author: Siva Chandra Reddy
Date: 2022-12-23T05:58:25Z
New Revision: f1129601900162aa25f6d2586901a22c3956b333
URL: https://github.com/llvm/llvm-project/commit/f1129601900162aa25f6d2586901a22c3956b333
DIFF: https://github.com/llvm/llvm-project/commit/f1129601900162aa25f6d2586901a22c3956b333.diff
LOG: [libc][NFC] Use operator new and operator delete in POSIX file actions API.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D140597
Added:
Modified:
libc/src/spawn/CMakeLists.txt
libc/src/spawn/file_actions.h
libc/src/spawn/posix_spawn_file_actions_addclose.cpp
libc/src/spawn/posix_spawn_file_actions_adddup2.cpp
libc/src/spawn/posix_spawn_file_actions_addopen.cpp
libc/src/spawn/posix_spawn_file_actions_destroy.cpp
Removed:
################################################################################
diff --git a/libc/src/spawn/CMakeLists.txt b/libc/src/spawn/CMakeLists.txt
index fd929fa0cee16..4fbbaacb10d26 100644
--- a/libc/src/spawn/CMakeLists.txt
+++ b/libc/src/spawn/CMakeLists.txt
@@ -30,6 +30,7 @@ add_entrypoint_object(
.file_actions
libc.include.errno
libc.include.spawn
+ libc.src.__support.CPP.new
)
add_entrypoint_object(
@@ -42,6 +43,7 @@ add_entrypoint_object(
.file_actions
libc.include.errno
libc.include.spawn
+ libc.src.__support.CPP.new
)
add_entrypoint_object(
@@ -54,6 +56,7 @@ add_entrypoint_object(
.file_actions
libc.include.errno
libc.include.spawn
+ libc.src.__support.CPP.new
)
add_entrypoint_object(
@@ -66,6 +69,7 @@ add_entrypoint_object(
.file_actions
libc.include.errno
libc.include.spawn
+ libc.src.__support.CPP.new
)
add_entrypoint_object(
diff --git a/libc/src/spawn/file_actions.h b/libc/src/spawn/file_actions.h
index 11d9fb70d193d..ee509a1e0ad9c 100644
--- a/libc/src/spawn/file_actions.h
+++ b/libc/src/spawn/file_actions.h
@@ -23,6 +23,21 @@ struct BaseSpawnFileAction {
ActionType type;
BaseSpawnFileAction *next;
+
+ static void add_action(posix_spawn_file_actions_t *actions,
+ BaseSpawnFileAction *act) {
+ if (actions->__back != nullptr) {
+ auto *back = reinterpret_cast<BaseSpawnFileAction *>(actions->__back);
+ back->next = act;
+ actions->__back = act;
+ } else {
+ // First action is being added.
+ actions->__front = actions->__back = act;
+ }
+ }
+
+protected:
+ explicit BaseSpawnFileAction(ActionType t) : type(t), next(nullptr) {}
};
struct SpawnFileOpenAction : public BaseSpawnFileAction {
@@ -30,28 +45,27 @@ struct SpawnFileOpenAction : public BaseSpawnFileAction {
int fd;
int oflag;
mode_t mode;
+
+ SpawnFileOpenAction(const char *p, int fdesc, int flags, mode_t m)
+ : BaseSpawnFileAction(BaseSpawnFileAction::OPEN), path(p), fd(fdesc),
+ oflag(flags), mode(m) {}
};
struct SpawnFileCloseAction : public BaseSpawnFileAction {
int fd;
+
+ SpawnFileCloseAction(int fdesc)
+ : BaseSpawnFileAction(BaseSpawnFileAction::CLOSE), fd(fdesc) {}
};
struct SpawnFileDup2Action : public BaseSpawnFileAction {
int fd;
int newfd;
-};
-inline void enque_spawn_action(posix_spawn_file_actions_t *actions,
- BaseSpawnFileAction *act) {
- if (actions->__back != nullptr) {
- auto *back = reinterpret_cast<BaseSpawnFileAction *>(actions->__back);
- back->next = act;
- actions->__back = act;
- } else {
- // First action is being added.
- actions->__front = actions->__back = act;
- }
-}
+ SpawnFileDup2Action(int fdesc, int new_fdesc)
+ : BaseSpawnFileAction(BaseSpawnFileAction::DUP2), fd(fdesc),
+ newfd(new_fdesc) {}
+};
} // namespace __llvm_libc
diff --git a/libc/src/spawn/posix_spawn_file_actions_addclose.cpp b/libc/src/spawn/posix_spawn_file_actions_addclose.cpp
index 81d3878fe81d1..492c3e0b8bc11 100644
--- a/libc/src/spawn/posix_spawn_file_actions_addclose.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_addclose.cpp
@@ -9,11 +9,11 @@
#include "posix_spawn_file_actions_addclose.h"
#include "file_actions.h"
+#include "src/__support/CPP/new.h"
#include "src/__support/common.h"
#include <errno.h>
#include <spawn.h>
-#include <stdlib.h> // For malloc
namespace __llvm_libc {
@@ -24,14 +24,12 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_addclose,
if (fd < 0)
return EBADF;
- auto *act = reinterpret_cast<SpawnFileCloseAction *>(
- malloc(sizeof(SpawnFileCloseAction)));
- if (act == nullptr)
+ AllocChecker ac;
+ auto *act = new (ac) SpawnFileCloseAction(fd);
+ if (!ac)
return ENOMEM;
+ BaseSpawnFileAction::add_action(actions, act);
- act->type = BaseSpawnFileAction::CLOSE;
- act->fd = fd;
- enque_spawn_action(actions, act);
return 0;
}
diff --git a/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp b/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp
index a6a7f07110e92..ca18629ba6832 100644
--- a/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp
@@ -9,11 +9,11 @@
#include "posix_spawn_file_actions_adddup2.h"
#include "file_actions.h"
+#include "src/__support/CPP/new.h"
#include "src/__support/common.h"
#include <errno.h>
#include <spawn.h>
-#include <stdlib.h> // For malloc
namespace __llvm_libc {
@@ -24,16 +24,12 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_adddup2,
if (fd < 0 || newfd < 0)
return EBADF;
- auto *act = reinterpret_cast<SpawnFileDup2Action *>(
- malloc(sizeof(SpawnFileDup2Action)));
- if (act == nullptr)
+ AllocChecker ac;
+ auto *act = new (ac) SpawnFileDup2Action(fd, newfd);
+ if (!ac)
return ENOMEM;
+ BaseSpawnFileAction::add_action(actions, act);
- act->type = BaseSpawnFileAction::DUP2;
- act->fd = fd;
- act->newfd = newfd;
- act->next = nullptr;
- enque_spawn_action(actions, act);
return 0;
}
diff --git a/libc/src/spawn/posix_spawn_file_actions_addopen.cpp b/libc/src/spawn/posix_spawn_file_actions_addopen.cpp
index ac286631c0565..3b12c8bbc17ce 100644
--- a/libc/src/spawn/posix_spawn_file_actions_addopen.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_addopen.cpp
@@ -9,11 +9,11 @@
#include "posix_spawn_file_actions_addopen.h"
#include "file_actions.h"
+#include "src/__support/CPP/new.h"
#include "src/__support/common.h"
#include <errno.h>
#include <spawn.h>
-#include <stdlib.h> // For malloc
namespace __llvm_libc {
@@ -25,18 +25,12 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_addopen,
if (fd < 0)
return EBADF;
- auto *act = reinterpret_cast<SpawnFileOpenAction *>(
- malloc(sizeof(SpawnFileOpenAction)));
+ AllocChecker ac;
+ auto *act = new (ac) SpawnFileOpenAction(path, fd, oflag, mode);
if (act == nullptr)
return ENOMEM;
+ BaseSpawnFileAction::add_action(actions, act);
- act->type = BaseSpawnFileAction::OPEN;
- act->fd = fd;
- act->path = path;
- act->oflag = oflag;
- act->mode = mode;
- act->next = nullptr;
- enque_spawn_action(actions, act);
return 0;
}
diff --git a/libc/src/spawn/posix_spawn_file_actions_destroy.cpp b/libc/src/spawn/posix_spawn_file_actions_destroy.cpp
index 83346462c95bd..b1ea50724b9a3 100644
--- a/libc/src/spawn/posix_spawn_file_actions_destroy.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_destroy.cpp
@@ -9,11 +9,11 @@
#include "posix_spawn_file_actions_destroy.h"
#include "file_actions.h"
+#include "src/__support/CPP/new.h"
#include "src/__support/common.h"
#include <errno.h>
#include <spawn.h>
-#include <stdlib.h> // For free
namespace __llvm_libc {
@@ -27,11 +27,24 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_destroy,
auto *act = reinterpret_cast<BaseSpawnFileAction *>(actions->__front);
actions->__front = nullptr;
actions->__back = nullptr;
+ if (act == nullptr)
+ return 0;
+ act = act->next;
while (act != nullptr) {
- auto *next = act->next;
- free(act);
- act = next;
+ auto *temp = act;
+ act = act->next;
+ switch (temp->type) {
+ case BaseSpawnFileAction::OPEN:
+ delete reinterpret_cast<SpawnFileOpenAction *>(temp);
+ break;
+ case BaseSpawnFileAction::CLOSE:
+ delete reinterpret_cast<SpawnFileCloseAction *>(temp);
+ break;
+ case BaseSpawnFileAction::DUP2:
+ delete reinterpret_cast<SpawnFileDup2Action *>(temp);
+ break;
+ }
}
return 0;
More information about the libc-commits
mailing list