aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/memory.h65
-rw-r--r--src/eval/eval_exe.cc16
-rw-r--r--src/util/string.cc5
3 files changed, 16 insertions, 70 deletions
diff --git a/include/memory.h b/include/memory.h
index 6cf4536..15cdd11 100644
--- a/include/memory.h
+++ b/include/memory.h
@@ -1,67 +1,8 @@
#pragma once
-#include <string>
+#include <cstdio>
-namespace ov4
+inline void cleanup_free_argv(char ***p)
{
-
-template <typename T>
-class T_raw_mem
-{
-
-public:
-
- void *_p;
- std::size_t _len;
-
- explicit T_raw_mem(std::size_t size)
- {
- _p = operator new(size);
- _len = size;
- }
-
- ~T_raw_mem()
- {
- operator delete(_p);
- }
-
- T *get() const noexcept
- {
- return static_cast<T*>(_p);
- }
-
- std::size_t get_size() const noexcept
- {
- return len;
- }
-
- // COPY is not allowed
- T_raw_mem(const T_raw_mem&) = delete;
- T_raw_mem& operator=(const T_raw_mem&) = delete;
-
- // MOVE constructor
- T_raw_mem(T_raw_mem &&x) noexcept
- : p(std::exchange(x._p, nullptr)), slen(std::exchange(x._len, 0))
- { /* empty */ }
-
- T_raw_mem& operator=(T_raw_mem &&x) noexcept
- {
- // if they are same
- // release itself, which is not expected
- if (this != &x)
- {
- // consider statement: y = x;
- // release memory of y (if exist)
- operator delete(_p);
-
- // and then move
- _p = std::exchange(x._p, nullptr);
- _len = std::exchange(x._len, 0);
- }
-
- return *this;
- }
-
-};
-
+ free(*p);
}
diff --git a/src/eval/eval_exe.cc b/src/eval/eval_exe.cc
index 65e5a34..009fc1c 100644
--- a/src/eval/eval_exe.cc
+++ b/src/eval/eval_exe.cc
@@ -12,6 +12,7 @@
#include "lang_analysis/symbol_property.h"
#include "lang_analysis/parseline.h"
#include "util/io.h"
+#include "memory.h"
using namespace std;
@@ -34,7 +35,7 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size
bool task_type_is_subshell = false;
const char *raw_input = nullptr;
- char **argv = nullptr;
+ __attribute__((cleanup(cleanup_free_argv))) char **argv = nullptr;
const char *unified_cmd_c = nullptr;
string unified_cmd, s_wrap_eliminated;
@@ -55,9 +56,13 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size
// NB: remember to delete
argv = parseline(s);
- if (argv == nullptr || argv[0] == nullptr) { operator delete(argv); return 0; }
- if (exe_bultin_command(argv)) { operator delete(argv); return 0; }
+ // the compiler should free() it here
+ // if returns
+
+ if (argv == nullptr || argv[0] == nullptr) { return 0; }
+
+ if (exe_bultin_command(argv)) { return 0; }
unified_cmd = find_cmd(argv[0]);
unified_cmd_c = unified_cmd.c_str();
@@ -102,8 +107,7 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size
}
// parent
-
- operator delete(argv);
+ free(argv);
argv = nullptr;
exit_required_pid = pid;
@@ -136,4 +140,4 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size
return 0;
}
-} \ No newline at end of file
+}
diff --git a/src/util/string.cc b/src/util/string.cc
index bec9638..794e5a1 100644
--- a/src/util/string.cc
+++ b/src/util/string.cc
@@ -1,6 +1,7 @@
#include "util/string.h"
#include <print>
+#include <cstdio>
using namespace std;
@@ -38,8 +39,8 @@ void string2argv(const vector<string> &v, char **argv)
*/
char **string2argv_alloc(const std::vector<std::string> &v)
{
- size_t size = argv_get_required_size(v);
- char **p = static_cast<char**>(operator new(size));
+ size_t size = argv_get_required_size(v) * sizeof(char);
+ char **p = (char**)malloc(size);
string2argv(v, p);
return p;
}