Skip to content

Commit

Permalink
Convert InfoStore to TAILQ.
Browse files Browse the repository at this point in the history
Use TAILQ instead of a linked list for InfoStore. This also reduces
looping over the list multiple times in some situations.

This fixes #993.
  • Loading branch information
somiaj authored and ThomasAdam committed Nov 18, 2024
1 parent 9948377 commit 603b262
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 76 deletions.
103 changes: 37 additions & 66 deletions fvwm/infostore.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@

/* ---------------------------- local types -------------------------------- */

static MetaInfo *mi_store;

/* ---------------------------- forward declarations ----------------------- */

static void delete_metainfo(const char *);
Expand All @@ -47,23 +45,19 @@ static void delete_metainfo(const char *);

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

MetaInfo *new_metainfo(void)
{
MetaInfo *mi;

mi = fxcalloc(1, sizeof *mi);
struct meta_infos meta_info_q;

return mi;
}
/* ---------------------------- local functions ---------------------------- */

void insert_metainfo(char *key, char *value)
{
MetaInfo *mi;
MetaInfo *mi_new;

for (mi = mi_store; mi; mi = mi->next)
if (TAILQ_EMPTY(&meta_info_q))
TAILQ_INIT(&meta_info_q);

TAILQ_FOREACH(mi, &meta_info_q, entry)
{
if (StrEquals(mi->key, key))
{
Expand All @@ -78,93 +72,65 @@ void insert_metainfo(char *key, char *value)
}

/* It's a new item, add it to the list. */
mi_new = new_metainfo();
mi_new = fxcalloc(1, sizeof *mi_new);
mi_new->key = fxstrdup(key);
CopyString(&mi_new->value, value);

mi_new->next = mi_store;
mi_store = mi_new;
TAILQ_INSERT_TAIL(&meta_info_q, mi_new, entry);

return;
}

static void delete_metainfo(const char *key)
{
MetaInfo *mi_current, *mi_prev;
mi_prev = NULL;
MetaInfo *mi = NULL;

for(mi_current = mi_store; mi_current != NULL;
mi_prev = mi_current, mi_current = mi_current->next)
TAILQ_FOREACH(mi, &meta_info_q, entry)
{
if (StrEquals(mi_current->key, key)) {
if (mi_prev == NULL)
mi_store = mi_current->next;
else
mi_prev->next = mi_current->next;
if (StrEquals(mi->key, key))
break;
}

free(mi_current->key);
free(mi_current->value);
free(mi_current);
if (mi != NULL)
{
TAILQ_REMOVE(&meta_info_q, mi, entry);

break;
}
free(mi->key);
free(mi->value);
free(mi);
}

return;
}

inline char *get_metainfo_value(const char *key)
{
MetaInfo *mi_current;
MetaInfo *mi;

for(mi_current = mi_store; mi_current; mi_current = mi_current->next)
TAILQ_FOREACH(mi, &meta_info_q, entry)
{
if (StrEquals(mi_current->key, key))
return mi_current->value;
if (StrEquals(mi->key, key))
return mi->value;
}

return NULL;
}

int get_metainfo_length(void)
{
MetaInfo *mi;
int count;

count = 0;

for(mi = mi_store; mi; mi = mi->next)
count++;

return count;
}

MetaInfo *
get_metainfo(void)
{
return mi_store;
}

void print_infostore(void)
{
MetaInfo *mi;

fvwm_debug(__func__, "Current items in infostore (key, value):\n\n");

if (get_metainfo_length() == 0)
{
if (TAILQ_EMPTY(&meta_info_q)) {
fvwm_debug(__func__,
"No items are currently stored in the infostore.\n");
return;
}

for(mi = mi_store; mi; mi = mi->next)
{
fvwm_debug(__func__, "Current items in infostore (key, value):\n\n");
TAILQ_FOREACH(mi, &meta_info_q, entry)
fvwm_debug(__func__, "%s\t%s\n", mi->key, mi->value);
}

return;

}

/* ---------------------------- interface functions ------------------------ */
Expand All @@ -189,6 +155,9 @@ void CMD_InfoStoreAdd(F_CMD_ARGS)
if (!key || !value)
{
fvwm_debug(__func__, "Bad arguments given.");
if (key)
free(key);

return;
}

Expand Down Expand Up @@ -218,11 +187,13 @@ void CMD_InfoStoreRemove(F_CMD_ARGS)

void CMD_InfoStoreClear(F_CMD_ARGS)
{
MetaInfo *mi;
MetaInfo *mi, *mi2;

if (get_metainfo_length() == 0)
return;

for (mi = mi_store; mi; mi = mi->next)
delete_metainfo(mi->key);
TAILQ_FOREACH_SAFE(mi, &meta_info_q, entry, mi2)
{
TAILQ_REMOVE(&meta_info_q, mi, entry);
free(mi->key);
free(mi->value);
free(mi);
}
}
9 changes: 4 additions & 5 deletions fvwm/infostore.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@

/* ---------------------------- type definitions --------------------------- */

typedef struct MetaInfo
typedef struct meta_info
{
char *key;
char *value;

struct MetaInfo *next;
TAILQ_ENTRY(meta_info) entry;
} MetaInfo;
TAILQ_HEAD(meta_infos, meta_info);
extern struct meta_infos meta_info_q;

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- interface functions ------------------------ */

MetaInfo *new_metainfo(void);
void insert_metainfo(char *, char *);
char *get_metainfo_value(const char *);
int get_metainfo_length(void);
MetaInfo *get_metainfo(void);
void print_infostore(void);

#endif /* FVWM_INFOSTORE_H */
10 changes: 5 additions & 5 deletions fvwm/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ SaveGlobalState(FILE *f)
f, " [STYLE] %i %i\n", Scr.gs.do_emulate_mwm,
Scr.gs.do_emulate_win);

if (get_metainfo_length() > 0) {
MetaInfo *mi = get_metainfo(), *mi_i;
if (!TAILQ_EMPTY(&meta_info_q)) {
MetaInfo *mi;

fprintf(f, " [INFOSTORE]\n");
for (mi_i = mi; mi_i; mi_i = mi_i->next) {
fprintf(f, " [KEY] %s\n", mi_i->key);
fprintf(f, " [VALUE] %s\n", mi_i->value);
TAILQ_FOREACH(mi, &meta_info_q, entry) {
fprintf(f, " [KEY] %s\n", mi->key);
fprintf(f, " [VALUE] %s\n", mi->value);
}
}

Expand Down

0 comments on commit 603b262

Please sign in to comment.