Skip to content

Commit

Permalink
Avoid excessive dynamic memory allocs in TinBash
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherJohnH committed May 2, 2019
1 parent 3808aa0 commit d67a638
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions Source/launcher/TinBash.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ class TinBash
bool quit{false};
std::string prev_cmd;
std::string cmd;
std::string filepath;
std::vector<std::string> argv;
size_t argc{};
std::mutex curses_mutex;
std::atomic<bool> an_output_spooler_quit{false};

bool openScript(const std::string& filename)
{
std::string filepath = "Scripts/";
filepath = "Scripts/";
filepath += filename;
filepath += ".tin";

Expand Down Expand Up @@ -251,7 +253,7 @@ class TinBash
//! Split command into argument list
void split()
{
argv.clear();
argc = 0;

bool in_space = true;
bool in_quotes = false;
Expand All @@ -261,13 +263,17 @@ class TinBash
{
if (!in_quotes)
{
argv.push_back("");
if (argv.size() == argc)
{
argv.push_back("");
}
argv[argc++] = "";
}
in_quotes = !in_quotes;
}
else if (in_quotes)
{
argv.back().push_back(ch);
argv[argc - 1] += ch;
}
else if ((ch == ' ') || (ch == '\t'))
{
Expand All @@ -278,17 +284,21 @@ class TinBash
if (in_space)
{
in_space = false;
argv.push_back("");
if (argv.size() == argc)
{
argv.push_back("");
}
argv[argc++] = "";
}
argv.back().push_back(ch);
argv[argc - 1] += ch;
}
}
}

//! Run a command
void run()
{
if (argv.size() > 0)
if (argc > 0)
{
if (argv[0] == "exit") { cmd_exit(); }
else if (argv[0] == "restart") { cmd_restart(); }
Expand All @@ -307,7 +317,7 @@ class TinBash
//! Change directory
void cmd_cd()
{
if (argv.size() >= 2)
if (argc >= 2)
{
if (chdir(argv[1].c_str()) < 0)
{
Expand Down Expand Up @@ -340,6 +350,7 @@ class TinBash
if (pid == -1)
{
error("fork() failed");
return;
}
else if (pid == 0)
{
Expand All @@ -349,17 +360,18 @@ class TinBash
stderr_pipe.assignWriteFD(STDERR_FILENO);

// XXX no need to delete this the whole process is about to be swapped or die
char** args = new char*[argv.size() + 1];
for(size_t i=0; i<argv.size(); i++)
char** args = new char*[argc + 1];
for(size_t i=0; i<argc; i++)
{
args[i] = (char*)argv[i].c_str();
}
args[argv.size()] = nullptr;
args[argc] = nullptr;

if (execvp(args[0], args) < 0)
{
error("execvp() failed");
fprintf(stderr, "ERROR - execvp(\"%s\", ...) failed\n", argv[0].c_str());
}
exit(1);
}

an_output_spooler_quit = false;
Expand Down

0 comments on commit d67a638

Please sign in to comment.