Skip to content

Commit d02cfe3

Browse files
pcercueiQuzarDC
authored andcommitted
nmmgr: Use a single-linked list
There's no real reason to justify a double-linked list here, so switch to a single-linked list. Note the slightly more complex nmmgr_handler_remove() as we first check whether or not the element to remove is in the list before removing it (just because we return a different value if it does). This is fine as this function is not meant to be called again and again. Signed-off-by: Paul Cercueil <paul@crapouillou.net>
1 parent ed46bf0 commit d02cfe3

5 files changed

Lines changed: 19 additions & 17 deletions

File tree

include/kos/nmmgr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct nmmgr_handler;
3939
4040
Contrary to what doxygen may think, this is not a function.
4141
*/
42-
typedef LIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t;
42+
typedef SLIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t;
4343

4444
/** \brief List entry initializer for static structs.
4545
\ingroup system_namemgr
@@ -64,7 +64,7 @@ typedef struct nmmgr_handler {
6464
uint32_t version; /* Version code */
6565
uint32_t flags; /* Bitmask of flags */
6666
uint32_t type; /* Type of handler */
67-
LIST_ENTRY(nmmgr_handler) list_ent; /* Linked list entry */
67+
SLIST_ENTRY(nmmgr_handler) list_ent; /* Linked list entry */
6868
} nmmgr_handler_t;
6969

7070
/** \brief Alias handler interface.

kernel/exports/exports.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export_sym_t *export_lookup(const char *name) {
7373
nmmgrs = nmmgr_get_list();
7474

7575
/* Go through and look at each symtab entry */
76-
LIST_FOREACH(nmmgr, nmmgrs, list_ent) {
76+
SLIST_FOREACH(nmmgr, nmmgrs, list_ent) {
7777
/* Not a symtab -> ignore */
7878
if(nmmgr->type != NMMGR_TYPE_SYMTAB)
7979
continue;
@@ -127,7 +127,7 @@ export_sym_t *export_lookup_addr(uintptr_t addr) {
127127
nmmgrs = nmmgr_get_list();
128128

129129
/* Go through and look at each symtab entry */
130-
LIST_FOREACH(nmmgr, nmmgrs, list_ent) {
130+
SLIST_FOREACH(nmmgr, nmmgrs, list_ent) {
131131
/* Not a symtab -> ignore */
132132
if(nmmgr->type != NMMGR_TYPE_SYMTAB)
133133
continue;

kernel/exports/nmmgr.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ nmmgr_handler_t * nmmgr_lookup(const char *fn) {
3939
size_t cur_len = 0, tmp_len;
4040

4141
/* Scan the handler table and look for the best path match */
42-
LIST_FOREACH(tmp, &nmmgr_handlers, list_ent) {
42+
SLIST_FOREACH(tmp, &nmmgr_handlers, list_ent) {
4343
tmp_len = strlen(tmp->pathname);
4444
if(!strncasecmp(tmp->pathname, fn, tmp_len)) {
4545
if(cur_len < tmp_len) {
@@ -70,7 +70,7 @@ nmmgr_list_t * nmmgr_get_list(void) {
7070
int nmmgr_handler_add(nmmgr_handler_t *hnd) {
7171
mutex_lock(&mutex);
7272

73-
LIST_INSERT_HEAD(&nmmgr_handlers, hnd, list_ent);
73+
SLIST_INSERT_HEAD(&nmmgr_handlers, hnd, list_ent);
7474

7575
mutex_unlock(&mutex);
7676

@@ -79,19 +79,21 @@ int nmmgr_handler_add(nmmgr_handler_t *hnd) {
7979

8080
/* Remove a name handler */
8181
int nmmgr_handler_remove(nmmgr_handler_t *hnd) {
82-
nmmgr_handler_t *c, *tmp;
82+
nmmgr_handler_t *tmp;
8383
int rv = -1;
8484

8585
if(mutex_lock_irqsafe(&mutex) < 0)
8686
return -1;
8787

8888
/* Verify that it's actually in there */
89-
LIST_FOREACH_SAFE(c, &nmmgr_handlers, list_ent, tmp) {
90-
if(c == hnd) {
91-
LIST_REMOVE(hnd, list_ent);
92-
rv = 0;
89+
SLIST_FOREACH(tmp, &nmmgr_handlers, list_ent) {
90+
if(tmp == hnd)
9391
break;
94-
}
92+
}
93+
94+
if(tmp) {
95+
SLIST_REMOVE(&nmmgr_handlers, hnd, nmmgr_handler, list_ent);
96+
rv = 0;
9597
}
9698

9799
mutex_unlock(&mutex);
@@ -104,7 +106,7 @@ KOS_INIT_FLAG_WEAK(export_init, false);
104106
/* Initialize structures */
105107
void nmmgr_init(void) {
106108
/* Start with no handlers */
107-
LIST_INIT(&nmmgr_handlers);
109+
SLIST_INIT(&nmmgr_handlers);
108110

109111
/* Initialize our internal exports */
110112
KOS_INIT_FLAG_CALL(export_init);
@@ -113,10 +115,10 @@ void nmmgr_init(void) {
113115
void nmmgr_shutdown(void) {
114116
nmmgr_handler_t *c, *n;
115117

116-
c = LIST_FIRST(&nmmgr_handlers);
118+
c = SLIST_FIRST(&nmmgr_handlers);
117119

118120
while(c != NULL) {
119-
n = LIST_NEXT(c, list_ent);
121+
n = SLIST_NEXT(c, list_ent);
120122

121123
if(c->flags & NMMGR_FLAGS_NEEDSFREE)
122124
free(c);

kernel/fs/fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static dirent_t *fs_root_readdir(fs_hnd_t *handle) {
6868

6969
nmhead = nmmgr_get_list();
7070

71-
LIST_FOREACH(nmhnd, nmhead, list_ent) {
71+
SLIST_FOREACH(nmhnd, nmhead, list_ent) {
7272
if((nmhnd->flags & NMMGR_FLAGS_INDEV))
7373
continue;
7474

kernel/fs/fs_dev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static dirent_t *dev_root_readdir(dev_hnd_t * handle) {
3434

3535
nmhead = nmmgr_get_list();
3636

37-
LIST_FOREACH(nmhnd, nmhead, list_ent) {
37+
SLIST_FOREACH(nmhnd, nmhead, list_ent) {
3838
if(!(nmhnd->flags & NMMGR_FLAGS_INDEV))
3939
continue;
4040

0 commit comments

Comments
 (0)