forked from github/server
reduce number of allocations in report_region (edges).
remove the swap database file for orders (when not using memory).
This commit is contained in:
parent
281ed3d3e9
commit
eddcefed77
3 changed files with 44 additions and 32 deletions
|
@ -11,6 +11,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static sqlite3 *g_game_db;
|
static sqlite3 *g_game_db;
|
||||||
|
@ -179,11 +180,14 @@ static int db_open_swap(const char *dbname) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *g_swapname;
|
||||||
|
|
||||||
int db_driver_open(database_t db, const char *dbname)
|
int db_driver_open(database_t db, const char *dbname)
|
||||||
{
|
{
|
||||||
ERRNO_CHECK();
|
ERRNO_CHECK();
|
||||||
|
|
||||||
if (db == DB_SWAP) {
|
if (db == DB_SWAP) {
|
||||||
|
g_swapname = dbname;
|
||||||
return db_open_swap(dbname);
|
return db_open_swap(dbname);
|
||||||
}
|
}
|
||||||
else if (db == DB_GAME) {
|
else if (db == DB_GAME) {
|
||||||
|
@ -205,6 +209,13 @@ void db_driver_close(database_t db)
|
||||||
assert(err == SQLITE_OK);
|
assert(err == SQLITE_OK);
|
||||||
err = sqlite3_close(g_temp_db);
|
err = sqlite3_close(g_temp_db);
|
||||||
assert(err == SQLITE_OK);
|
assert(err == SQLITE_OK);
|
||||||
|
if (g_swapname) {
|
||||||
|
FILE * F = fopen(g_swapname, "r");
|
||||||
|
if (F) {
|
||||||
|
fclose(F);
|
||||||
|
remove(g_swapname);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (db == DB_GAME) {
|
else if (db == DB_GAME) {
|
||||||
assert(g_game_db);
|
assert(g_game_db);
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
void orderdb_open(void)
|
void orderdb_open(void)
|
||||||
{
|
{
|
||||||
const char *dbname;
|
const char *dbname;
|
||||||
|
|
||||||
dbname = config_get("game.dbswap");
|
dbname = config_get("game.dbswap");
|
||||||
db_driver_open(DB_SWAP, dbname);
|
db_driver_open(DB_SWAP, dbname);
|
||||||
}
|
}
|
||||||
|
|
64
src/report.c
64
src/report.c
|
@ -873,6 +873,8 @@ bool see_border(const connection * b, const faction * f, const region * r)
|
||||||
return cs;
|
return cs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define MAX_EDGES 16
|
||||||
|
|
||||||
void report_region(struct stream *out, const region * r, faction * f)
|
void report_region(struct stream *out, const region * r, faction * f)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
@ -883,13 +885,13 @@ void report_region(struct stream *out, const region * r, faction * f)
|
||||||
attrib *a;
|
attrib *a;
|
||||||
const char *tname;
|
const char *tname;
|
||||||
struct edge {
|
struct edge {
|
||||||
struct edge *next;
|
|
||||||
char *name;
|
char *name;
|
||||||
bool transparent;
|
bool transparent;
|
||||||
bool block;
|
bool block;
|
||||||
bool exist[MAXDIRECTIONS];
|
bool exist[MAXDIRECTIONS];
|
||||||
direction_t lastd;
|
direction_t lastd;
|
||||||
} *edges = NULL, *e;
|
} edges[MAX_EDGES];
|
||||||
|
int ne = 0;
|
||||||
bool see[MAXDIRECTIONS];
|
bool see[MAXDIRECTIONS];
|
||||||
char buf[8192];
|
char buf[8192];
|
||||||
char *bufp = buf;
|
char *bufp = buf;
|
||||||
|
@ -908,7 +910,8 @@ void report_region(struct stream *out, const region * r, faction * f)
|
||||||
if (!r2)
|
if (!r2)
|
||||||
continue;
|
continue;
|
||||||
for (b = get_borders(r, r2); b;) {
|
for (b = get_borders(r, r2); b;) {
|
||||||
struct edge *edg = edges;
|
int e;
|
||||||
|
struct edge *match = NULL;
|
||||||
bool transparent = b->type->transparent(b, f);
|
bool transparent = b->type->transparent(b, f);
|
||||||
const char *name = border_name(b, r, f, GF_DETAILED | GF_ARTICLE);
|
const char *name = border_name(b, r, f, GF_DETAILED | GF_ARTICLE);
|
||||||
|
|
||||||
|
@ -919,18 +922,22 @@ void report_region(struct stream *out, const region * r, faction * f)
|
||||||
b = b->next;
|
b = b->next;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
while (edg && (edg->transparent != transparent || strcmp(name, edg->name)!=0)) {
|
for (e = 0; e!=ne; ++e) {
|
||||||
edg = edg->next;
|
struct edge *edg = edges + e;
|
||||||
|
if (edg->transparent == transparent && 0 == strcmp(name, edg->name)) {
|
||||||
|
match = edg;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!edg) {
|
if (match == NULL) {
|
||||||
edg = calloc(sizeof(struct edge), 1);
|
match = edges + ne;
|
||||||
edg->name = str_strdup(name);
|
match->name = str_strdup(name);
|
||||||
edg->transparent = transparent;
|
match->transparent = transparent;
|
||||||
edg->next = edges;
|
++ne;
|
||||||
edges = edg;
|
assert(ne < MAX_EDGES);
|
||||||
}
|
}
|
||||||
edg->lastd = d;
|
match->lastd = d;
|
||||||
edg->exist[d] = true;
|
match->exist[d] = true;
|
||||||
b = b->next;
|
b = b->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1227,27 +1234,22 @@ void report_region(struct stream *out, const region * r, faction * f)
|
||||||
nr_curses(out, 0, f, TYP_REGION, r);
|
nr_curses(out, 0, f, TYP_REGION, r);
|
||||||
n = 0;
|
n = 0;
|
||||||
|
|
||||||
if (edges)
|
if (ne > 0) {
|
||||||
|
int e;
|
||||||
newline(out);
|
newline(out);
|
||||||
for (e = edges; e; e = e->next) {
|
for (e = 0; e != ne; ++e) {
|
||||||
message *msg;
|
message *msg;
|
||||||
|
|
||||||
for (d = 0; d != MAXDIRECTIONS; ++d) {
|
for (d = 0; d != MAXDIRECTIONS; ++d) {
|
||||||
if (e->exist[d]) {
|
if (edges[e].exist[d]) {
|
||||||
msg = msg_message(e->transparent ? "nr_border_transparent" : "nr_border_opaque",
|
msg = msg_message(edges[e].transparent ? "nr_border_transparent" : "nr_border_opaque",
|
||||||
"object dir", e->name, d);
|
"object dir", edges[e].name, d);
|
||||||
nr_render(msg, f->locale, buf, sizeof(buf), f);
|
nr_render(msg, f->locale, buf, sizeof(buf), f);
|
||||||
msg_release(msg);
|
msg_release(msg);
|
||||||
paragraph(out, buf, 0, 0, 0);
|
paragraph(out, buf, 0, 0, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
free(edges[e].name);
|
||||||
}
|
|
||||||
if (edges) {
|
|
||||||
while (edges) {
|
|
||||||
e = edges->next;
|
|
||||||
free(edges->name);
|
|
||||||
free(edges);
|
|
||||||
edges = e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue