| Change the Dir Item pointer array for an Item array - sacc - sacc(omys), simple console gopher client |
| git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/ |
| Log |
| Files |
| Refs |
| Tags |
| LICENSE |
| --- |
| commit 5cef355a572cbf96818f75ebb5ad31be19bffb02 |
| parent ad655e3c1be072eb6f0d1d61279028707c836a82 |
| Author: Quentin Rameau |
| Date: Tue, 12 Sep 2017 02:01:50 +0200
Change the Dir Item pointer array for an Item array
We then allocate all items at once in molddiritem, saving us a lot of
malloc calls.
Diffstat:
M common.h | 2 +-
M sacc.c | 28 +++++++++++-----------------
M ui_ti.c | 18 +++++++++---------
M ui_txt.c | 8 ++++----
4 files changed, 25 insertions(+), 31 deletions(-)
--- |
| diff --git a/common.h b/common.h |
| @@ -16,7 +16,7 @@ struct item {
};
struct dir {
- Item **items;
+ Item *items;
size_t nitems;
size_t printoff;
size_t curline; |
| diff --git a/sacc.c b/sacc.c |
| @@ -101,7 +101,7 @@ static void
clearitem(Item *item)
{
Dir *dir;
- Item **items;
+ Item *items;
char *tag;
size_t i;
@@ -110,10 +110,8 @@ clearitem(Item *item)
if (dir = item->dat) {
items = dir->items;
- for (i = 0; i < dir->nitems; ++i) {
- clearitem(items[i]);
- free(items[i]);
- }
+ for (i = 0; i < dir->nitems; ++i)
+ clearitem(&items[i]);
free(items);
clear(&item->dat);
}
@@ -222,21 +220,18 @@ invaliditem(char *raw)
return (tabs == 3) ? NULL : raw;
}
-static Item *
-molditem(char **raw)
+static void
+molditem(Item *item, char **raw)
{
- Item *item;
char *next;
if (!*raw)
- return NULL;
-
- item = xcalloc(sizeof(Item));
+ return;
if ((next = invaliditem(*raw))) {
item->username = *raw;
*raw = next;
- return item;
+ return;
}
item->type = *raw[0]++;
@@ -246,14 +241,12 @@ molditem(char **raw)
item->port = pickfield(raw, '\r');
if (!*raw[0])
++*raw;
-
- return item;
}
static Dir *
molddiritem(char *raw)
{
- Item **items = NULL;
+ Item *items = NULL;
char *s, *nl, *p;
Dir *dir;
size_t i, nitems;
@@ -270,10 +263,11 @@ molddiritem(char *raw)
}
dir = xmalloc(sizeof(Dir));
- items = xreallocarray(items, nitems, sizeof(Item*));
+ items = xreallocarray(items, nitems, sizeof(Item));
+ memset(items, 0, nitems * sizeof(Item));
for (i = 0; i < nitems; ++i)
- items[i] = molditem(&raw);
+ molditem(&items[i], &raw);
dir->items = items;
dir->nitems = nitems; |
| diff --git a/ui_ti.c b/ui_ti.c |
| @@ -203,7 +203,7 @@ displayuri(Item *item)
void
uidisplay(Item *entry)
{
- Item **items;
+ Item *items;
Dir *dir;
size_t i, curln, lastln, nitems, printoff;
@@ -232,7 +232,7 @@ uidisplay(Item *entry)
putp(tparm(save_cursor));
putp(tparm(enter_standout_mode));
}
- printitem(items[i]);
+ printitem(&items[i]);
putp(tparm(column_address, 0));
if (i == curln)
putp(tparm(exit_standout_mode));
@@ -258,7 +258,7 @@ movecurline(Item *item, int l)
if (curline < 0 || curline >= nitems)
return;
- printitem(dir->items[dir->curline]);
+ printitem(&dir->items[dir->curline]);
dir->curline = curline;
if (l > 0) {
@@ -268,7 +268,7 @@ movecurline(Item *item, int l)
putp(tparm(cursor_address, plines, 0));
putp(tparm(scroll_forward));
- printitem(dir->items[offline]);
+ printitem(&dir->items[offline]);
putp(tparm(restore_cursor));
dir->printoff += l;
@@ -280,7 +280,7 @@ movecurline(Item *item, int l)
putp(tparm(cursor_address, 0, 0));
putp(tparm(scroll_reverse));
- printitem(dir->items[offline]);
+ printitem(&dir->items[offline]);
putchar('\n');
putp(tparm(restore_cursor));
@@ -290,7 +290,7 @@ movecurline(Item *item, int l)
putp(tparm(cursor_address, curline - dir->printoff, 0));
putp(tparm(enter_standout_mode));
- printitem(dir->items[curline]);
+ printitem(&dir->items[curline]);
putp(tparm(exit_standout_mode));
displaystatus(item);
fflush(stdout);
@@ -349,7 +349,7 @@ nearentry(Item *entry, int direction)
item = dir->curline + direction;
for (; item >= 0 && item < lastitem; item += direction) {
- if (dir->items[item]->type != 'i')
+ if (dir->items[item].type != 'i')
return item;
}
@@ -410,7 +410,7 @@ uiselectitem(Item *entry)
case '\n':
pgnext:
if (dir)
- return dir->items[dir->curline];
+ return &dir->items[dir->curline];
continue;
case _key_lndown:
lndown:
@@ -452,7 +452,7 @@ uiselectitem(Item *entry)
return entry;
case _key_uri:
if (dir)
- displayuri(dir->items[dir->curline]);
+ displayuri(&dir->items[dir->curline]);
continue;
case _key_help: /* FALLTHROUGH */
return help(entry); |
| diff --git a/ui_txt.c b/ui_txt.c |
| @@ -113,7 +113,7 @@ uiprompt(char *fmt, ...)
void
uidisplay(Item *entry)
{
- Item **items;
+ Item *items;
Dir *dir;
size_t i, lines, nitems;
int nd;
@@ -130,7 +130,7 @@ uidisplay(Item *entry)
for (i = dir->printoff; i < nitems && i < lines; ++i) {
printf("%*zu %s %s\n",
- nd, i+1, typedisplay(items[i]->type), items[i]->username);
+ nd, i+1, typedisplay(items[i].type), items[i].username);
}
fflush(stdout);
@@ -234,7 +234,7 @@ uiselectitem(Item *entry)
return entry;
case 'u':
if (item > 0 && item <= nitems)
- printuri(dir->items[item-1], item);
+ printuri(&dir->items[item-1], item);
continue;
case 'h':
case '?':
@@ -250,7 +250,7 @@ uiselectitem(Item *entry)
}
if (item > 0)
- return dir->items[item-1];
+ return &dir->items[item-1];
return entry->entry;
} |