Hold allocated memory till program exit

This commit is contained in:
Arun Prakash Jana 2017-04-21 01:46:00 +05:30
parent f10ef49534
commit 34a8ec2881
No known key found for this signature in database
GPG Key ID: A75979F35C080412
3 changed files with 27 additions and 24 deletions

View File

@ -42,7 +42,7 @@ Noice is Not Noice, a noicer fork...
nnn is a fork of [noice](http://git.2f30.org/noice/), a blazing-fast lightweight terminal file browser with easy keyboard shortcuts for navigation, opening files and running tasks. noice is developed considering terminal based systems. There is no config file and mime associations are hard-coded. However, the incredible user-friendliness and speed make it a perfect utility on modern distros.
nnn can use the default desktop opener at runtime. It adds new navigation options, enhanced DE integration, a disk usage analyzer mode, comprehensive file details and much more. For a complete list, see [nnn toppings](#nnn-toppings).
nnn can use the default desktop opener at runtime. It adds new navigation options, enhanced DE integration, a disk usage analyzer mode, comprehensive file details and much more. For a complete list, see [nnn toppings](#nnn-toppings). Add to it a huge [performance](#performance) boost.
Follow the instructions in the [quickstart](#quickstart) section and see how nnn simplifies those long desktop sessions.
@ -128,7 +128,7 @@ nnn vs. ncdu memory usage while listing an external disk with 13,790 files in di
```
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31163 vaio 20 0 65508 53980 2320 S 0.0 1.1 0:01.96 ncdu /
28863 vaio 20 0 24276 11088 2776 S 0.3 0.2 0:02.61 nnn -d
28863 vaio 20 0 21348 7812 2476 S 0.0 0.2 0:01.75 nnn -d
```
nnn vs. mc vs. ranger memory usage while viewing a directory with 10,178 files, sorted by size:
@ -137,7 +137,7 @@ nnn vs. mc vs. ranger memory usage while viewing a directory with 10,178 files,
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22465 vaio 20 0 233956 192136 7896 S 0.0 3.9 0:05.31 /usr/bin/python -O /usr/bin/ranger
20369 vaio 20 0 64664 10980 6888 S 0.0 0.2 0:00.70 mc
28863 vaio 20 0 20600 7220 2648 S 0.0 0.1 0:00.36 nnn -d
28863 vaio 20 0 19876 6436 2620 S 0.0 0.1 0:00.19 nnn -d
```
### Installation

2
nnn.1
View File

@ -13,7 +13,7 @@
.Op Ar PATH
.Sh DESCRIPTION
.Nm
(Noice is Not Noice) is a fork of the noice terminal file browser with improved desktop integration, navigation, disk usage analyzer mode, comprehensive file details and much more. It remains a simple and efficient file browser that stays out of your way.
(Noice is Not Noice) is a performance-optimized fork of the noice terminal file browser with improved desktop integration, navigation, disk usage analyzer mode, comprehensive file details and much more. It remains a simple and efficient file browser that stays out of your way.
.Pp
.Nm
defaults to the current directory if

43
nnn.c
View File

@ -129,7 +129,7 @@ extern void add_history(const char *string);
/* Global context */
static struct entry *dents;
static int ndents, cur;
static int ndents, cur, total_dents;
static int idle;
static char *opener;
static char *fallback_opener;
@ -239,7 +239,7 @@ static char *
xdirname(const char *path)
{
static char buf[PATH_MAX];
char *last_slash;
static char *last_slash;
xstrlcpy(buf, path, PATH_MAX);
@ -366,6 +366,7 @@ xstricmp(const char *s1, const char *s2)
return (int) (TOUPPER(*s1) - TOUPPER(*s2));
}
/* Trim all white space from both ends */
static char *
strstrip(char *s)
{
@ -411,9 +412,9 @@ openwith(char *file)
static int
setfilter(regex_t *regex, char *filter)
{
char errbuf[LINE_MAX];
size_t len;
int r;
static char errbuf[LINE_MAX];
static size_t len;
static int r;
r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
if (r != 0) {
@ -1034,8 +1035,9 @@ dentfill(char *path, struct entry **dents,
if (filter(re, dp->d_name) == 0)
continue;
if (((n >> 5) << 5) == n) {
*dents = realloc(*dents, (n + 32) * sizeof(**dents));
if (n == total_dents) {
total_dents += 64;
*dents = realloc(*dents, total_dents * sizeof(**dents));
if (*dents == NULL)
printerr(1, "realloc");
}
@ -1044,8 +1046,11 @@ dentfill(char *path, struct entry **dents,
/* Get mode flags */
mkpath(path, dp->d_name, newpath, sizeof(newpath));
r = lstat(newpath, &sb);
if (r == -1)
if (r == -1) {
if (*dents)
free(*dents);
printerr(1, "lstat");
}
(*dents)[n].mode = sb.st_mode;
(*dents)[n].t = sb.st_mtime;
(*dents)[n].size = sb.st_size;
@ -1075,8 +1080,11 @@ dentfill(char *path, struct entry **dents,
/* Should never be null */
r = closedir(dirp);
if (r == -1)
if (r == -1) {
if (*dents)
free(*dents);
printerr(1, "closedir");
}
return n;
}
@ -1128,11 +1136,6 @@ populate(char *path, char *oldpath, char *fltr)
if (r != 0)
return -1;
dentfree(dents);
ndents = 0;
dents = NULL;
ndents = dentfill(path, &dents, visible, &re);
qsort(dents, ndents, sizeof(*dents), entrycmp);
@ -1334,14 +1337,13 @@ nochange:
{
static char cmd[MAX_CMD_LEN];
static char *runvi = "vi";
static int status;
/* If default mime opener is set, use it */
if (opener) {
snprintf(cmd, MAX_CMD_LEN,
"%s \"%s\" > /dev/null 2>&1",
opener, newpath);
status = system(cmd);
r = system(cmd);
continue;
}
@ -1370,10 +1372,9 @@ nochange:
/dev/null 2>&1",
fallback_opener,
newpath);
status = system(cmd);
r = system(cmd);
continue;
} else {
status++; /* Dummy operation */
printmsg("No association");
goto nochange;
}
@ -1572,9 +1573,11 @@ nochange:
mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
r = lstat(oldpath, &sb);
if (r == -1)
if (r == -1) {
if (dents)
dentfree(dents);
printerr(1, "lstat");
else
} else
show_stats(oldpath, dents[cur].name, &sb);
goto begin;