[pulseaudio-commits] [SCM] PulseAudio Sound Server branch, master, updated. 913bbd4489799b057a5b47f19843d2d83e1003c1

Lennart Poettering gitmailer-noreply at 0pointer.de
Fri Jun 27 12:39:22 PDT 2008


This is an automated email from the git hooks/post-receive script. It was
generated because of a push to the "PulseAudio Sound Server" repository.

The master branch has been updated
      from  12278f4c839b235913e8faf4ec01acb63ea31a56 (commit)

- Log -----------------------------------------------------------------
913bbd4... save a bit of memory
36021b1... modernize idxset a bit, reduce memory consumption, get rid of pa_idxset_foreach()
113c62b... halve memory consumption of mempool flist, since we know we cannot have more than n_blocks entries in it
c26be0d... modernize hashmap implementation a bit, reduce memory consumption a bit
6dca92b... rework the flist implementation to halve memory consumption by merging the state field and the pointer in the flist cells
a087014... some modernizations
232c955... rename pa_queu_is_empty() to pa_queue_isempty() to follow idxset/hashmap nomenclatura
3db7dcb... save some memory by increasing the dynamic array at a slower rate
c0f97aa... some modernizations
-----------------------------------------------------------------------

Summary of changes:
 src/modules/module-zeroconf-publish.c |    2 +-
 src/modules/rtp/module-rtp-recv.c     |    2 +-
 src/pulsecore/core.h                  |    8 +-
 src/pulsecore/dynarray.c              |   15 +-
 src/pulsecore/dynarray.h              |   10 +-
 src/pulsecore/flist.c                 |   95 ++++-----
 src/pulsecore/flist.h                 |    2 +-
 src/pulsecore/hashmap.c               |  155 ++++++++------
 src/pulsecore/hashmap.h               |   20 +-
 src/pulsecore/idxset.c                |  387 +++++++++++++++++----------------
 src/pulsecore/idxset.h                |   34 ++-
 src/pulsecore/memblock.c              |    6 +-
 src/pulsecore/module.c                |   72 +++----
 src/pulsecore/pstream.c               |    2 +-
 src/pulsecore/queue.c                 |   11 +-
 src/pulsecore/queue.h                 |   15 +-
 src/tests/queue-test.c                |    6 +-
 17 files changed, 439 insertions(+), 403 deletions(-)

-----------------------------------------------------------------------

commit c0f97aa580a48d790361258d44775828e2eff134
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 19:11:28 2008 +0200

    some modernizations

diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c
index a1fcd8a..bc7716d 100644
--- a/src/pulsecore/dynarray.c
+++ b/src/pulsecore/dynarray.c
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -41,21 +41,23 @@ struct pa_dynarray {
 
 pa_dynarray* pa_dynarray_new(void) {
     pa_dynarray *a;
+
     a = pa_xnew(pa_dynarray, 1);
     a->data = NULL;
     a->n_entries = 0;
     a->n_allocated = 0;
+
     return a;
 }
 
-void pa_dynarray_free(pa_dynarray* a, void (*func)(void *p, void *userdata), void *userdata) {
+void pa_dynarray_free(pa_dynarray* a, pa_free2_cb_t free_func, void *userdata) {
     unsigned i;
     pa_assert(a);
 
-    if (func)
+    if (free_func)
         for (i = 0; i < a->n_entries; i++)
             if (a->data[i])
-                func(a->data[i], userdata);
+                free_func(a->data[i], userdata);
 
     pa_xfree(a->data);
     pa_xfree(a);
@@ -89,6 +91,7 @@ unsigned pa_dynarray_append(pa_dynarray*a, void *p) {
 
     i = a->n_entries;
     pa_dynarray_put(a, i, p);
+
     return i;
 }
 
diff --git a/src/pulsecore/dynarray.h b/src/pulsecore/dynarray.h
index 82b4208..9a8e64e 100644
--- a/src/pulsecore/dynarray.h
+++ b/src/pulsecore/dynarray.h
@@ -1,10 +1,10 @@
-#ifndef foodynarrayhfoo
-#define foodynarrayhfoo
+#ifndef foopulsecoredynarrayhfoo
+#define foopulsecoredynarrayhfoo
 
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -22,6 +22,8 @@
   USA.
 ***/
 
+#include <pulsecore/idxset.h>
+
 typedef struct pa_dynarray pa_dynarray;
 
 /* Implementation of a simple dynamically sized array. The array
@@ -32,7 +34,7 @@ pa_dynarray* pa_dynarray_new(void);
 
 /* Free the array calling the specified function for every entry in
  * the array. The function may be NULL. */
-void pa_dynarray_free(pa_dynarray* a, void (*func)(void *p, void *userdata), void *userdata);
+void pa_dynarray_free(pa_dynarray* a, pa_free2_cb_t free_func, void *userdata);
 
 /* Store p at position i in the array */
 void pa_dynarray_put(pa_dynarray*a, unsigned i, void *p);

commit 3db7dcb9628bc4e281125826e09887763cc1bb2c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 19:12:03 2008 +0200

    save some memory by increasing the dynamic array at a slower rate

diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c
index bc7716d..69d835a 100644
--- a/src/pulsecore/dynarray.c
+++ b/src/pulsecore/dynarray.c
@@ -31,8 +31,8 @@
 
 #include "dynarray.h"
 
-/* If the array becomes to small, increase its size by 100 entries */
-#define INCREASE_BY 100
+/* If the array becomes to small, increase its size by 25 entries */
+#define INCREASE_BY 25
 
 struct pa_dynarray {
     void **data;

commit 232c9558ffafe699cfa0467b0e32642c0f13df83
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 19:14:59 2008 +0200

    rename pa_queu_is_empty() to pa_queue_isempty() to follow idxset/hashmap nomenclatura

diff --git a/src/pulsecore/pstream.c b/src/pulsecore/pstream.c
index e26ca47..6b1af67 100644
--- a/src/pulsecore/pstream.c
+++ b/src/pulsecore/pstream.c
@@ -942,7 +942,7 @@ pa_bool_t pa_pstream_is_pending(pa_pstream *p) {
     if (p->dead)
         b = FALSE;
     else
-        b = p->write.current || !pa_queue_is_empty(p->send_queue);
+        b = p->write.current || !pa_queue_isempty(p->send_queue);
 
     return b;
 }
diff --git a/src/pulsecore/queue.c b/src/pulsecore/queue.c
index 08fd142..72e5467 100644
--- a/src/pulsecore/queue.c
+++ b/src/pulsecore/queue.c
@@ -116,7 +116,7 @@ void* pa_queue_pop(pa_queue *q) {
     return p;
 }
 
-int pa_queue_is_empty(pa_queue *q) {
+int pa_queue_isempty(pa_queue *q) {
     pa_assert(q);
 
     return q->length == 0;
diff --git a/src/pulsecore/queue.h b/src/pulsecore/queue.h
index b09216c..c55ac4d 100644
--- a/src/pulsecore/queue.h
+++ b/src/pulsecore/queue.h
@@ -35,6 +35,6 @@ void pa_queue_free(pa_queue* q, void (*destroy)(void *p, void *userdata), void *
 void pa_queue_push(pa_queue *q, void *p);
 void* pa_queue_pop(pa_queue *q);
 
-int pa_queue_is_empty(pa_queue *q);
+int pa_queue_isempty(pa_queue *q);
 
 #endif
diff --git a/src/tests/queue-test.c b/src/tests/queue-test.c
index 105f094..ceae4e4 100644
--- a/src/tests/queue-test.c
+++ b/src/tests/queue-test.c
@@ -37,12 +37,12 @@ int main(int argc, char *argv[]) {
 
     pa_assert_se(q = pa_queue_new());
 
-    pa_assert(pa_queue_is_empty(q));
+    pa_assert(pa_queue_isempty(q));
 
     pa_queue_push(q, (void*) "eins");
     pa_log("%s\n", (char*) pa_queue_pop(q));
 
-    pa_assert(pa_queue_is_empty(q));
+    pa_assert(pa_queue_isempty(q));
 
     pa_queue_push(q, (void*) "zwei");
     pa_queue_push(q, (void*) "drei");
@@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
     pa_log("%s\n", (char*) pa_queue_pop(q));
     pa_log("%s\n", (char*) pa_queue_pop(q));
 
-    pa_assert(pa_queue_is_empty(q));
+    pa_assert(pa_queue_isempty(q));
 
     pa_queue_push(q, (void*) "sechs");
     pa_queue_push(q, (void*) "sieben");

commit a0870149699b69d42c0439ad56f4bce55597f9cd
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 19:15:32 2008 +0200

    some modernizations

diff --git a/src/pulsecore/queue.c b/src/pulsecore/queue.c
index 72e5467..2c73a3d 100644
--- a/src/pulsecore/queue.c
+++ b/src/pulsecore/queue.c
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -52,13 +52,13 @@ pa_queue* pa_queue_new(void) {
     return q;
 }
 
-void pa_queue_free(pa_queue* q, void (*destroy)(void *p, void *userdata), void *userdata) {
+void pa_queue_free(pa_queue* q, pa_free2_cb_t free_func, void *userdata) {
     void *data;
     pa_assert(q);
 
     while ((data = pa_queue_pop(q)))
-        if (destroy)
-            destroy(data, userdata);
+        if (free_func)
+            free_func(data, userdata);
 
     pa_assert(!q->front);
     pa_assert(!q->back);
@@ -94,6 +94,7 @@ void pa_queue_push(pa_queue *q, void *p) {
 void* pa_queue_pop(pa_queue *q) {
     void *p;
     struct queue_entry *e;
+
     pa_assert(q);
 
     if (!(e = q->front))
diff --git a/src/pulsecore/queue.h b/src/pulsecore/queue.h
index c55ac4d..f3cec9b 100644
--- a/src/pulsecore/queue.h
+++ b/src/pulsecore/queue.h
@@ -1,10 +1,10 @@
-#ifndef fooqueuehfoo
-#define fooqueuehfoo
+#ifndef foopulsecorequeuehfoo
+#define foopulsecorequeuehfoo
 
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -22,6 +22,8 @@
   USA.
 ***/
 
+#include <pulsecore/idxset.h>
+
 typedef struct pa_queue pa_queue;
 
 /* A simple implementation of the abstract data type queue. Stores
@@ -29,8 +31,9 @@ typedef struct pa_queue pa_queue;
 
 pa_queue* pa_queue_new(void);
 
-/* Free the queue and run the specified callback function for every remaining entry. The callback function may be NULL. */
-void pa_queue_free(pa_queue* q, void (*destroy)(void *p, void *userdata), void *userdata);
+/* Free the queue and run the specified callback function for every
+ * remaining entry. The callback function may be NULL. */
+void pa_queue_free(pa_queue* q, pa_free2_cb_t free_func, void *userdata);
 
 void pa_queue_push(pa_queue *q, void *p);
 void* pa_queue_pop(pa_queue *q);

commit 6dca92be9696d6e3b5d4dfe28c6156f73f730dd4
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 19:18:19 2008 +0200

    rework the flist implementation to halve memory consumption by merging the state field and the pointer in the flist cells

diff --git a/src/pulsecore/flist.c b/src/pulsecore/flist.c
index f166ee3..6fb944f 100644
--- a/src/pulsecore/flist.c
+++ b/src/pulsecore/flist.c
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2006 Lennart Poettering
+  Copyright 2006-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
@@ -38,14 +38,8 @@
  * from the flist although it isn't empty, and fail to push into the
  * flist, although it isn't full.
  *
- * We keep a fixed size array of entries, each item is either marked
- * UNUSED, USED or BUSY and contains a user data pointer. When pushing
- * into the queue we look for an UNUSED cell and mark it BUSY with a
- * CAS operation. If successful we use it and mark it USED, otherwise
- * we go on and look for the next UNUSED cell. The algorithm for
- * popping an item from the queue is practically inverse: look for a
- * USED cell and and mark it BUSY with a CAS operation, after reading
- * from it mark it UNUSED again.
+ * We keep a fixed size array of entries, each item is an atomic
+ * pointer.
  *
  * To accelerate finding of used/unused cells we maintain a read and a
  * write index which is used like a ring buffer. After each push we
@@ -72,7 +66,7 @@
  * Please note that this algorithm is home grown.*/
 
 #define FLIST_SIZE 128
-#define N_EXTRA_SCAN 2
+#define N_EXTRA_SCAN 3
 
 /* For debugging purposes we can define _Y to put and extra thread
  * yield between each operation. */
@@ -83,17 +77,6 @@
 #define _Y do { } while(0)
 #endif
 
-enum {
-    STATE_UNUSED,
-    STATE_USED,
-    STATE_BUSY
-};
-
-struct cell {
-    pa_atomic_t state;
-    void *data;
-};
-
 struct pa_flist {
     unsigned size;
     pa_atomic_t length;
@@ -101,7 +84,7 @@ struct pa_flist {
     pa_atomic_t write_idx;
 };
 
-#define PA_FLIST_CELLS(x) ((struct cell*) ((uint8_t*) (x) + PA_ALIGN(sizeof(struct pa_flist))))
+#define PA_FLIST_CELLS(x) ((pa_atomic_ptr_t*) ((uint8_t*) (x) + PA_ALIGN(sizeof(struct pa_flist))))
 
 pa_flist *pa_flist_new(unsigned size) {
     pa_flist *l;
@@ -111,7 +94,7 @@ pa_flist *pa_flist_new(unsigned size) {
 
     pa_assert(pa_is_power_of_two(size));
 
-    l = pa_xmalloc0(PA_ALIGN(sizeof(pa_flist)) + (sizeof(struct cell) * size));
+    l = pa_xmalloc0(PA_ALIGN(sizeof(pa_flist)) + (sizeof(pa_atomic_ptr_t) * size));
 
     l->size = size;
 
@@ -122,28 +105,24 @@ pa_flist *pa_flist_new(unsigned size) {
     return l;
 }
 
-static int reduce(pa_flist *l, int value) {
-    return value & (unsigned) (l->size - 1);
+static unsigned reduce(pa_flist *l, unsigned value) {
+    return value & (l->size - 1);
 }
 
 void pa_flist_free(pa_flist *l, pa_free_cb_t free_cb) {
     pa_assert(l);
 
     if (free_cb) {
-        struct cell *cells;
-        int len, idx;
+        pa_atomic_ptr_t*cells;
+        unsigned idx;
 
         cells = PA_FLIST_CELLS(l);
 
-        idx = reduce(l, pa_atomic_load(&l->read_idx));
-        len = pa_atomic_load(&l->length);
-
-        for (; len > 0; len--) {
-
-            if (pa_atomic_load(&cells[idx].state) == STATE_USED)
-                free_cb(cells[idx].data);
+        for (idx = 0; idx < l->size; idx ++) {
+            void *p;
 
-            idx = reduce(l, idx + 1);
+            if ((p = pa_atomic_ptr_load(&cells[idx])))
+                free_cb(p);
         }
     }
 
@@ -151,30 +130,31 @@ void pa_flist_free(pa_flist *l, pa_free_cb_t free_cb) {
 }
 
 int pa_flist_push(pa_flist*l, void *p) {
-    int idx, len, n;
-    struct cell *cells;
+    unsigned idx, n, len;
+    pa_atomic_ptr_t*cells;
 
     pa_assert(l);
     pa_assert(p);
 
     cells = PA_FLIST_CELLS(l);
 
-    n = len = (int) l->size - pa_atomic_load(&l->length) + N_EXTRA_SCAN;
+    n = len = l->size + N_EXTRA_SCAN - (unsigned) pa_atomic_load(&l->length);
+
     _Y;
-    idx = reduce(l, pa_atomic_load(&l->write_idx));
+    idx = reduce(l, (unsigned) pa_atomic_load(&l->write_idx));
 
     for (; n > 0 ; n--) {
+
         _Y;
 
-        if (pa_atomic_cmpxchg(&cells[idx].state, STATE_UNUSED, STATE_BUSY)) {
+        if (pa_atomic_ptr_cmpxchg(&cells[idx], NULL, p)) {
+
             _Y;
             pa_atomic_inc(&l->write_idx);
-            _Y;
-            cells[idx].data = p;
-            _Y;
-            pa_atomic_store(&cells[idx].state, STATE_USED);
+
             _Y;
             pa_atomic_inc(&l->length);
+
             return 0;
         }
 
@@ -191,31 +171,36 @@ int pa_flist_push(pa_flist*l, void *p) {
 }
 
 void* pa_flist_pop(pa_flist*l) {
-    int idx, len, n;
-    struct cell *cells;
+    unsigned idx, len, n;
+    pa_atomic_ptr_t *cells;
 
     pa_assert(l);
 
     cells = PA_FLIST_CELLS(l);
 
-    n = len = pa_atomic_load(&l->length) + N_EXTRA_SCAN;
+    n = len = (unsigned) pa_atomic_load(&l->length) + N_EXTRA_SCAN;
+
     _Y;
-    idx = reduce(l, pa_atomic_load(&l->read_idx));
+    idx = reduce(l, (unsigned) pa_atomic_load(&l->read_idx));
 
     for (; n > 0 ; n--) {
+        void *p;
+
         _Y;
+        p = pa_atomic_ptr_load(&cells[idx]);
+
+        if (p) {
 
-        if (pa_atomic_cmpxchg(&cells[idx].state, STATE_USED, STATE_BUSY)) {
-            void *p;
-            _Y;
-            pa_atomic_inc(&l->read_idx);
-            _Y;
-            p = cells[idx].data;
             _Y;
-            pa_atomic_store(&cells[idx].state, STATE_UNUSED);
+            if (!pa_atomic_ptr_cmpxchg(&cells[idx], p, NULL))
+                continue;
+
             _Y;
+            pa_atomic_inc(&l->read_idx);
 
+            _Y;
             pa_atomic_dec(&l->length);
+
             return p;
         }
 
diff --git a/src/pulsecore/flist.h b/src/pulsecore/flist.h
index c040667..2d8422f 100644
--- a/src/pulsecore/flist.h
+++ b/src/pulsecore/flist.h
@@ -4,7 +4,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2006 Lennart Poettering
+  Copyright 2006-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as

commit c26be0d7623434a4595071f8ea7c55aeb240fcca
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 20:12:24 2008 +0200

    modernize hashmap implementation a bit, reduce memory consumption a bit

diff --git a/src/modules/module-zeroconf-publish.c b/src/modules/module-zeroconf-publish.c
index cb9c128..7892917 100644
--- a/src/modules/module-zeroconf-publish.c
+++ b/src/modules/module-zeroconf-publish.c
@@ -615,7 +615,7 @@ void pa__done(pa_module*m) {
     if (u->services) {
         struct service *s;
 
-        while ((s = pa_hashmap_get_first(u->services)))
+        while ((s = pa_hashmap_first(u->services)))
             service_free(s);
 
         pa_hashmap_free(u->services, NULL, NULL);
diff --git a/src/modules/rtp/module-rtp-recv.c b/src/modules/rtp/module-rtp-recv.c
index cff5cf8..e04e461 100644
--- a/src/modules/rtp/module-rtp-recv.c
+++ b/src/modules/rtp/module-rtp-recv.c
@@ -690,7 +690,7 @@ void pa__done(pa_module*m) {
     pa_sap_context_destroy(&u->sap_context);
 
     if (u->by_origin) {
-        while ((s = pa_hashmap_get_first(u->by_origin)))
+        while ((s = pa_hashmap_first(u->by_origin)))
             session_free(s);
 
         pa_hashmap_free(u->by_origin, NULL, NULL);
diff --git a/src/pulsecore/hashmap.c b/src/pulsecore/hashmap.c
index b7f4109..3c6f41e 100644
--- a/src/pulsecore/hashmap.c
+++ b/src/pulsecore/hashmap.c
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
@@ -27,7 +27,6 @@
 #include <string.h>
 
 #include <pulse/xmalloc.h>
-
 #include <pulsecore/idxset.h>
 #include <pulsecore/log.h>
 #include <pulsecore/flist.h>
@@ -35,37 +34,39 @@
 
 #include "hashmap.h"
 
-#define BUCKETS 127
+#define NBUCKETS 127
 
 struct hashmap_entry {
-    struct hashmap_entry *next, *previous, *bucket_next, *bucket_previous;
-    unsigned hash;
     const void *key;
     void *value;
+
+    struct hashmap_entry *bucket_next, *bucket_previous;
+    struct hashmap_entry *iterate_next, *iterate_previous;
 };
 
 struct pa_hashmap {
-    unsigned size;
-    struct hashmap_entry **data;
-    struct hashmap_entry *first_entry;
-
-    unsigned n_entries;
     pa_hash_func_t hash_func;
     pa_compare_func_t compare_func;
+
+    struct hashmap_entry *iterate_list_head, *iterate_list_tail;
+    unsigned n_entries;
 };
 
+#define BY_HASH(h) ((struct hashmap_entry**) ((uint8_t*) (h) + PA_ALIGN(sizeof(pa_hashmap))))
+
 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
 
 pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
     pa_hashmap *h;
 
-    h = pa_xnew(pa_hashmap, 1);
-    h->data = pa_xnew0(struct hashmap_entry*, h->size = BUCKETS);
-    h->first_entry = NULL;
-    h->n_entries = 0;
+    h = pa_xmalloc0(PA_ALIGN(sizeof(pa_hashmap)) + NBUCKETS*sizeof(struct hashmap_entry*));
+
     h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
     h->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
 
+    h->n_entries = 0;
+    h->iterate_list_head = h->iterate_list_tail = NULL;
+
     return h;
 }
 
@@ -73,47 +74,56 @@ static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
     pa_assert(h);
     pa_assert(e);
 
-    if (e->next)
-        e->next->previous = e->previous;
-    if (e->previous)
-        e->previous->next = e->next;
+    /* Remove from iteration list */
+    if (e->iterate_next)
+        e->iterate_next->iterate_previous = e->iterate_previous;
     else
-        h->first_entry = e->next;
+        h->iterate_list_tail = e->iterate_previous;
 
+    if (e->iterate_previous)
+        e->iterate_previous->iterate_next = e->iterate_next;
+    else
+        h->iterate_list_head = e->iterate_next;
+
+    /* Remove from hash table bucket list */
     if (e->bucket_next)
         e->bucket_next->bucket_previous = e->bucket_previous;
+
     if (e->bucket_previous)
         e->bucket_previous->bucket_next = e->bucket_next;
     else {
-        pa_assert(e->hash < h->size);
-        h->data[e->hash] = e->bucket_next;
+        unsigned hash = h->hash_func(e->key) % NBUCKETS;
+        BY_HASH(h)[hash] = e->bucket_next;
     }
 
     if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
         pa_xfree(e);
 
+    pa_assert(h->n_entries >= 1);
     h->n_entries--;
 }
 
-void pa_hashmap_free(pa_hashmap*h, void (*free_func)(void *p, void *userdata), void *userdata) {
+void pa_hashmap_free(pa_hashmap*h, pa_free2_cb_t free_cb, void *userdata) {
     pa_assert(h);
 
-    while (h->first_entry) {
-        if (free_func)
-            free_func(h->first_entry->value, userdata);
-        remove_entry(h, h->first_entry);
+    while (h->iterate_list_head) {
+        void *data;
+        data = h->iterate_list_head->value;
+        remove_entry(h, h->iterate_list_head);
+
+        if (free_cb)
+            free_cb(data, userdata);
     }
 
-    pa_xfree(h->data);
     pa_xfree(h);
 }
 
-static struct hashmap_entry *get(pa_hashmap *h, unsigned hash, const void *key) {
+static struct hashmap_entry *hash_scan(pa_hashmap *h, unsigned hash, const void *key) {
     struct hashmap_entry *e;
     pa_assert(h);
-    pa_assert(hash < h->size);
+    pa_assert(hash < NBUCKETS);
 
-    for (e = h->data[hash]; e; e = e->bucket_next)
+    for (e = BY_HASH(h)[hash]; e; e = e->bucket_next)
         if (h->compare_func(e->key, key) == 0)
             return e;
 
@@ -123,33 +133,42 @@ static struct hashmap_entry *get(pa_hashmap *h, unsigned hash, const void *key)
 int pa_hashmap_put(pa_hashmap *h, const void *key, void *value) {
     struct hashmap_entry *e;
     unsigned hash;
+
     pa_assert(h);
 
-    hash = h->hash_func(key) % h->size;
+    hash = h->hash_func(key) % NBUCKETS;
 
-    if ((e = get(h, hash, key)))
+    if ((e = hash_scan(h, hash, key)))
         return -1;
 
     if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
         e = pa_xnew(struct hashmap_entry, 1);
 
-    e->hash = hash;
     e->key = key;
     e->value = value;
 
-    e->previous = NULL;
-    e->next = h->first_entry;
-    if (h->first_entry)
-        h->first_entry->previous = e;
-    h->first_entry = e;
-
+    /* Insert into hash table */
+    e->bucket_next = BY_HASH(h)[hash];
     e->bucket_previous = NULL;
-    e->bucket_next = h->data[hash];
-    if (h->data[hash])
-        h->data[hash]->bucket_previous = e;
-    h->data[hash] = e;
+    if (BY_HASH(h)[hash])
+        BY_HASH(h)[hash]->bucket_previous = e;
+    BY_HASH(h)[hash] = e;
+
+    /* Insert into iteration list */
+    e->iterate_previous = h->iterate_list_tail;
+    e->iterate_next = NULL;
+    if (h->iterate_list_tail) {
+        pa_assert(h->iterate_list_head);
+        h->iterate_list_tail->iterate_next = e;
+    } else {
+        pa_assert(!h->iterate_list_head);
+        h->iterate_list_head = e;
+    }
+    h->iterate_list_tail = e;
+
+    h->n_entries++;
+    pa_assert(h->n_entries >= 1);
 
-    h->n_entries ++;
     return 0;
 }
 
@@ -159,9 +178,9 @@ void* pa_hashmap_get(pa_hashmap *h, const void *key) {
 
     pa_assert(h);
 
-    hash = h->hash_func(key) % h->size;
+    hash = h->hash_func(key) % NBUCKETS;
 
-    if (!(e = get(h, hash, key)))
+    if (!(e = hash_scan(h, hash, key)))
         return NULL;
 
     return e->value;
@@ -174,18 +193,15 @@ void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
 
     pa_assert(h);
 
-    hash = h->hash_func(key) % h->size;
+    hash = h->hash_func(key) % NBUCKETS;
 
-    if (!(e = get(h, hash, key)))
+    if (!(e = hash_scan(h, hash, key)))
         return NULL;
 
     data = e->value;
     remove_entry(h, e);
-    return data;
-}
 
-unsigned pa_hashmap_size(pa_hashmap *h) {
-    return h->n_entries;
+    return data;
 }
 
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
@@ -197,13 +213,13 @@ void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
     if (*state == (void*) -1)
         goto at_end;
 
-    if ((!*state && !h->first_entry))
+    if (!*state && !h->iterate_list_head)
         goto at_end;
 
-    e = *state ? *state : h->first_entry;
+    e = *state ? *state : h->iterate_list_head;
 
-    if (e->next)
-        *state = e->next;
+    if (e->iterate_next)
+        *state = e->iterate_next;
     else
         *state = (void*) -1;
 
@@ -221,24 +237,37 @@ at_end:
     return NULL;
 }
 
+void* pa_hashmap_first(pa_hashmap *h) {
+    pa_assert(h);
+
+    if (!h->iterate_list_head)
+        return NULL;
+
+    return h->iterate_list_head->value;
+}
+
 void* pa_hashmap_steal_first(pa_hashmap *h) {
     void *data;
 
     pa_assert(h);
 
-    if (!h->first_entry)
+    if (!h->iterate_list_head)
         return NULL;
 
-    data = h->first_entry->value;
-    remove_entry(h, h->first_entry);
+    data = h->iterate_list_head->value;
+    remove_entry(h, h->iterate_list_head);
+
     return data;
 }
 
-void *pa_hashmap_get_first(pa_hashmap *h) {
+unsigned pa_hashmap_size(pa_hashmap *h) {
     pa_assert(h);
 
-    if (!h->first_entry)
-        return NULL;
+    return h->n_entries;
+}
+
+pa_bool_t pa_hashmap_isempty(pa_hashmap *h) {
+    pa_assert(h);
 
-    return h->first_entry->value;
+    return h->n_entries == 0;
 }
diff --git a/src/pulsecore/hashmap.h b/src/pulsecore/hashmap.h
index a4505c4..70d78b7 100644
--- a/src/pulsecore/hashmap.h
+++ b/src/pulsecore/hashmap.h
@@ -1,10 +1,10 @@
-#ifndef foohashmaphfoo
-#define foohashmaphfoo
+#ifndef foopulsecorehashmaphfoo
+#define foopulsecorehashmaphfoo
 
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
@@ -30,23 +30,27 @@
 
 typedef struct pa_hashmap pa_hashmap;
 
-typedef void (*pa_free2_cb_t)(void *p, void *userdata);
-
 /* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map */
 pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
 
 /* Free the hash table. Calls the specified function for every value in the table. The function may be NULL */
 void pa_hashmap_free(pa_hashmap*, pa_free2_cb_t free_cb, void *userdata);
 
-/* Returns non-zero when the entry already exists */
+/* Add an entry to the hashmap. Returns non-zero when the entry already exists */
 int pa_hashmap_put(pa_hashmap *h, const void *key, void *value);
+
+/* Return an entry from the hashmap */
 void* pa_hashmap_get(pa_hashmap *h, const void *key);
 
 /* Returns the data of the entry while removing */
 void* pa_hashmap_remove(pa_hashmap *h, const void *key);
 
+/* Return the current number of entries of the hashmap */
 unsigned pa_hashmap_size(pa_hashmap *h);
 
+/* Return TRUE if the hashmap is empty */
+pa_bool_t pa_hashmap_isempty(pa_hashmap *h);
+
 /* May be used to iterate through the hashmap. Initially the opaque
    pointer *state has to be set to NULL. The hashmap may not be
    modified during iteration -- except for deleting the current entry
@@ -55,8 +59,10 @@ unsigned pa_hashmap_size(pa_hashmap *h);
    returned. */
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void**key);
 
+/* Remove the oldest entry in the hashmap and return it */
 void *pa_hashmap_steal_first(pa_hashmap *h);
 
-void *pa_hashmap_get_first(pa_hashmap *h);
+/* Return the oldest entry in the hashmap */
+void* pa_hashmap_first(pa_hashmap *h);
 
 #endif
diff --git a/src/pulsecore/idxset.h b/src/pulsecore/idxset.h
index 0a4e528..f089ef3 100644
--- a/src/pulsecore/idxset.h
+++ b/src/pulsecore/idxset.h
@@ -32,6 +32,9 @@
 /* A special index value denoting the invalid index. */
 #define PA_IDXSET_INVALID ((uint32_t) -1)
 
+/* Similar to pa_free_cb_t, but takes a userdata argument */
+typedef void (*pa_free2_cb_t)(void *p, void *userdata);
+
 /* Generic implementations for hash and comparison functions. Just
  * compares the pointer or calculates the hash value directly from the
  * pointer value. */
diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index c2ee136..85b9207 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -845,7 +845,7 @@ void pa_memimport_free(pa_memimport *i) {
 
     pa_mutex_lock(i->mutex);
 
-    while ((b = pa_hashmap_get_first(i->blocks)))
+    while ((b = pa_hashmap_first(i->blocks)))
         memblock_replace_import(b);
 
     pa_assert(pa_hashmap_size(i->segments) == 0);

commit 113c62bf5dcdae52a73b5d41ecd9b53137f41462
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 20:13:26 2008 +0200

    halve memory consumption of mempool flist, since we know we cannot have more than n_blocks entries in it

diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index 85b9207..b43113d 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -693,7 +693,7 @@ pa_mempool* pa_mempool_new(pa_bool_t shared) {
     PA_LLIST_HEAD_INIT(pa_memimport, p->imports);
     PA_LLIST_HEAD_INIT(pa_memexport, p->exports);
 
-    p->free_slots = pa_flist_new(p->n_blocks*2);
+    p->free_slots = pa_flist_new(p->n_blocks);
 
     return p;
 }
@@ -747,7 +747,7 @@ void pa_mempool_vacuum(pa_mempool *p) {
 
     pa_assert(p);
 
-    list = pa_flist_new(p->n_blocks*2);
+    list = pa_flist_new(p->n_blocks);
 
     while ((slot = pa_flist_pop(p->free_slots)))
         while (pa_flist_push(list, slot) < 0)

commit 36021b117b10530bc94e960a3d3d319ff03fe27a
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 20:34:14 2008 +0200

    modernize idxset a bit, reduce memory consumption, get rid of pa_idxset_foreach()

diff --git a/src/pulsecore/idxset.c b/src/pulsecore/idxset.c
index 7c9520a..fb4497b 100644
--- a/src/pulsecore/idxset.c
+++ b/src/pulsecore/idxset.c
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
   Copyright 2006 Pierre Ossman <ossman at cendio.se> for Cendio AB
 
   PulseAudio is free software; you can redistribute it and/or modify
@@ -29,29 +29,36 @@
 #include <string.h>
 
 #include <pulse/xmalloc.h>
-#include <pulsecore/macro.h>
+#include <pulsecore/log.h>
 #include <pulsecore/flist.h>
+#include <pulsecore/macro.h>
 
 #include "idxset.h"
 
+#define NBUCKETS 127
+
 struct idxset_entry {
+    uint32_t idx;
     void *data;
-    uint32_t index;
-    unsigned hash_value;
 
-    struct idxset_entry *hash_prev, *hash_next;
-    struct idxset_entry* iterate_prev, *iterate_next;
+    struct idxset_entry *data_next, *data_previous;
+    struct idxset_entry *index_next, *index_previous;
+    struct idxset_entry *iterate_next, *iterate_previous;
 };
 
 struct pa_idxset {
     pa_hash_func_t hash_func;
     pa_compare_func_t compare_func;
 
-    unsigned hash_table_size, n_entries;
-    struct idxset_entry **hash_table, **array, *iterate_list_head, *iterate_list_tail;
-    uint32_t index, start_index, array_size;
+    uint32_t current_index;
+
+    struct idxset_entry *iterate_list_head, *iterate_list_tail;
+    unsigned n_entries;
 };
 
+#define BY_DATA(i) ((struct idxset_entry**) ((uint8_t*) (i) + PA_ALIGN(sizeof(pa_idxset))))
+#define BY_INDEX(i) (BY_DATA(i) + NBUCKETS)
+
 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
 
 unsigned pa_idxset_string_hash_func(const void *p) {
@@ -79,131 +86,140 @@ int pa_idxset_trivial_compare_func(const void *a, const void *b) {
 pa_idxset* pa_idxset_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
     pa_idxset *s;
 
-    s = pa_xnew(pa_idxset, 1);
+    s = pa_xmalloc0(PA_ALIGN(sizeof(pa_idxset)) + NBUCKETS*2*sizeof(struct idxset_entry*));
+
     s->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
     s->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
-    s->hash_table_size = 127;
-    s->hash_table = pa_xnew0(struct idxset_entry*, s->hash_table_size);
-    s->array = NULL;
-    s->array_size = 0;
-    s->index = 0;
-    s->start_index = 0;
-    s->n_entries = 0;
 
+    s->current_index = 0;
+    s->n_entries = 0;
     s->iterate_list_head = s->iterate_list_tail = NULL;
 
     return s;
 }
 
-void pa_idxset_free(pa_idxset *s, void (*free_func) (void *p, void *userdata), void *userdata) {
+static void remove_entry(pa_idxset *s, struct idxset_entry *e) {
     pa_assert(s);
+    pa_assert(e);
 
-    while (s->iterate_list_head) {
-        struct idxset_entry *e = s->iterate_list_head;
-        s->iterate_list_head = s->iterate_list_head->iterate_next;
+    /* Remove from iteration linked list */
+    if (e->iterate_next)
+        e->iterate_next->iterate_previous = e->iterate_previous;
+    else
+        s->iterate_list_tail = e->iterate_previous;
 
-        if (free_func)
-            free_func(e->data, userdata);
+    if (e->iterate_previous)
+        e->iterate_previous->iterate_next = e->iterate_next;
+    else
+        s->iterate_list_head = e->iterate_next;
+
+    /* Remove from data hash table */
+    if (e->data_next)
+        e->data_next->data_previous = e->data_previous;
 
-        if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
-            pa_xfree(e);
+    if (e->data_previous)
+        e->data_previous->data_next = e->data_next;
+    else {
+        unsigned hash = s->hash_func(e->data) % NBUCKETS;
+        BY_DATA(s)[hash] = e->data_next;
     }
 
-    pa_xfree(s->hash_table);
-    pa_xfree(s->array);
-    pa_xfree(s);
-}
+    /* Remove from index hash table */
+    if (e->index_next)
+        e->index_next->index_previous = e->index_previous;
 
-static struct idxset_entry* hash_scan(pa_idxset *s, struct idxset_entry* e, const void *p) {
-    pa_assert(p);
+    if (e->index_previous)
+        e->index_previous->index_next = e->index_next;
+    else
+        BY_INDEX(s)[e->idx % NBUCKETS] = e->index_next;
 
-    pa_assert(s->compare_func);
-    for (; e; e = e->hash_next)
-        if (s->compare_func(e->data, p) == 0)
-            return e;
+    if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
+        pa_xfree(e);
 
-    return NULL;
+    pa_assert(s->n_entries >= 1);
+    s->n_entries--;
 }
 
-static void extend_array(pa_idxset *s, uint32_t idx) {
-    uint32_t i, j, l;
-    struct idxset_entry** n;
-
+void pa_idxset_free(pa_idxset *s, pa_free2_cb_t free_cb, void *userdata) {
     pa_assert(s);
-    pa_assert(idx >= s->start_index);
 
-    if (idx < s->start_index + s->array_size)
-        return;
+    while (s->iterate_list_head) {
+        void *data = s->iterate_list_head->data;
 
-    for (i = 0; i < s->array_size; i++)
-        if (s->array[i])
-            break;
+        remove_entry(s, s->iterate_list_head);
 
-    l = idx - s->start_index - i + 100;
-    n = pa_xnew0(struct idxset_entry*, l);
+        if (free_cb)
+            free_cb(data, userdata);
+    }
 
-    for (j = 0; j < s->array_size-i; j++)
-        n[j] = s->array[i+j];
+    pa_xfree(s);
+}
 
-    pa_xfree(s->array);
+static struct idxset_entry* data_scan(pa_idxset *s, unsigned hash, const void *p) {
+    struct idxset_entry *e;
+    pa_assert(s);
+    pa_assert(hash < NBUCKETS);
+    pa_assert(p);
 
-    s->array = n;
-    s->array_size = l;
-    s->start_index += i;
+    for (e = BY_DATA(s)[hash]; e; e = e->data_next)
+        if (s->compare_func(e->data, p) == 0)
+            return e;
+
+    return NULL;
 }
 
-static struct idxset_entry** array_index(pa_idxset*s, uint32_t idx) {
+static struct idxset_entry* index_scan(pa_idxset *s, unsigned hash, uint32_t idx) {
+    struct idxset_entry *e;
     pa_assert(s);
+    pa_assert(hash < NBUCKETS);
 
-    if (idx >= s->start_index + s->array_size)
-        return NULL;
-
-    if (idx < s->start_index)
-        return NULL;
+    for (e = BY_INDEX(s)[hash]; e; e = e->index_next)
+        if (e->idx == idx)
+            return e;
 
-    return s->array + idx - s->start_index;
+    return NULL;
 }
 
 int pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx) {
-    unsigned h;
-    struct idxset_entry *e, **a;
+    unsigned hash;
+    struct idxset_entry *e;
 
     pa_assert(s);
-    pa_assert(p);
 
-    pa_assert(s->hash_func);
-    h = s->hash_func(p) % s->hash_table_size;
+    hash = s->hash_func(p) % NBUCKETS;
 
-    pa_assert(s->hash_table);
-    if ((e = hash_scan(s, s->hash_table[h], p))) {
+    if ((e = data_scan(s, hash, p))) {
         if (idx)
-            *idx = e->index;
+            *idx = e->idx;
 
         return -1;
     }
 
     if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
         e = pa_xnew(struct idxset_entry, 1);
+
     e->data = p;
-    e->index = s->index++;
-    e->hash_value = h;
-
-    /* Insert into hash table */
-    e->hash_next = s->hash_table[h];
-    e->hash_prev = NULL;
-    if (s->hash_table[h])
-        s->hash_table[h]->hash_prev = e;
-    s->hash_table[h] = e;
-
-    /* Insert into array */
-    extend_array(s, e->index);
-    a = array_index(s, e->index);
-    pa_assert(a && !*a);
-    *a = e;
-
-    /* Insert into linked list */
+    e->idx = s->current_index++;
+
+    /* Insert into data hash table */
+    e->data_next = BY_DATA(s)[hash];
+    e->data_previous = NULL;
+    if (BY_DATA(s)[hash])
+        BY_DATA(s)[hash]->data_previous = e;
+    BY_DATA(s)[hash] = e;
+
+    hash = e->idx % NBUCKETS;
+
+    /* Insert into index hash table */
+    e->index_next = BY_INDEX(s)[hash];
+    e->index_previous = NULL;
+    if (BY_INDEX(s)[hash])
+        BY_INDEX(s)[hash]->index_previous = e;
+    BY_INDEX(s)[hash] = e;
+
+    /* Insert into iteration list */
+    e->iterate_previous = s->iterate_list_tail;
     e->iterate_next = NULL;
-    e->iterate_prev = s->iterate_list_tail;
     if (s->iterate_list_tail) {
         pa_assert(s->iterate_list_head);
         s->iterate_list_tail->iterate_next = e;
@@ -217,117 +233,76 @@ int pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx) {
     pa_assert(s->n_entries >= 1);
 
     if (idx)
-        *idx = e->index;
+        *idx = e->idx;
 
     return 0;
 }
 
 void* pa_idxset_get_by_index(pa_idxset*s, uint32_t idx) {
-    struct idxset_entry **a;
+    unsigned hash;
+    struct idxset_entry *e;
+
     pa_assert(s);
 
-    if (!(a = array_index(s, idx)))
-        return NULL;
+    hash = idx % NBUCKETS;
 
-    if (!*a)
+    if (!(e = index_scan(s, hash, idx)))
         return NULL;
 
-    return (*a)->data;
+    return e->data;
 }
 
 void* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx) {
-    unsigned h;
+    unsigned hash;
     struct idxset_entry *e;
 
     pa_assert(s);
-    pa_assert(p);
 
-    pa_assert(s->hash_func);
-    h = s->hash_func(p) % s->hash_table_size;
+    hash = s->hash_func(p) % NBUCKETS;
 
-    pa_assert(s->hash_table);
-    if (!(e = hash_scan(s, s->hash_table[h], p)))
+    if (!(e = data_scan(s, hash, p)))
         return NULL;
 
     if (idx)
-        *idx = e->index;
+        *idx = e->idx;
 
     return e->data;
 }
 
-static void remove_entry(pa_idxset *s, struct idxset_entry *e) {
-    struct idxset_entry **a;
-
-    pa_assert(s);
-    pa_assert(e);
-
-    /* Remove from array */
-    a = array_index(s, e->index);
-    pa_assert(a && *a && *a == e);
-    *a = NULL;
-
-    /* Remove from linked list */
-    if (e->iterate_next)
-        e->iterate_next->iterate_prev = e->iterate_prev;
-    else
-        s->iterate_list_tail = e->iterate_prev;
-
-    if (e->iterate_prev)
-        e->iterate_prev->iterate_next = e->iterate_next;
-    else
-        s->iterate_list_head = e->iterate_next;
-
-    /* Remove from hash table */
-    if (e->hash_next)
-        e->hash_next->hash_prev = e->hash_prev;
-
-    if (e->hash_prev)
-        e->hash_prev->hash_next = e->hash_next;
-    else
-        s->hash_table[e->hash_value] = e->hash_next;
-
-    if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
-        pa_xfree(e);
-
-    pa_assert(s->n_entries >= 1);
-    s->n_entries--;
-}
-
 void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx) {
-    struct idxset_entry **a;
+    struct idxset_entry *e;
+    unsigned hash;
     void *data;
 
     pa_assert(s);
 
-    if (!(a = array_index(s, idx)))
-        return NULL;
+    hash = idx % NBUCKETS;
 
-    if (!*a)
+    if (!(e = index_scan(s, hash, idx)))
         return NULL;
 
-    data = (*a)->data;
-    remove_entry(s, *a);
+    data = e->data;
+    remove_entry(s, e);
 
     return data;
 }
 
 void* pa_idxset_remove_by_data(pa_idxset*s, const void *data, uint32_t *idx) {
     struct idxset_entry *e;
-    unsigned h;
+    unsigned hash;
     void *r;
 
     pa_assert(s);
 
-    pa_assert(s->hash_func);
-    h = s->hash_func(data) % s->hash_table_size;
+    hash = s->hash_func(data) % NBUCKETS;
 
-    pa_assert(s->hash_table);
-    if (!(e = hash_scan(s, s->hash_table[h], data)))
+    if (!(e = data_scan(s, hash, data)))
         return NULL;
 
     r = e->data;
+
     if (idx)
-        *idx = e->index;
+        *idx = e->idx;
 
     remove_entry(s, e);
 
@@ -335,76 +310,113 @@ void* pa_idxset_remove_by_data(pa_idxset*s, const void *data, uint32_t *idx) {
 }
 
 void* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx) {
-    struct idxset_entry **a, *e = NULL;
+    unsigned hash;
+    struct idxset_entry *e;
 
     pa_assert(s);
     pa_assert(idx);
 
-    if ((a = array_index(s, *idx)) && *a)
-        e = (*a)->iterate_next;
+    hash = *idx % NBUCKETS;
 
-    if (!e)
+    if (!(e = index_scan(s, hash, *idx)))
+        return NULL;
+
+    if (e && e->iterate_next)
+        e = e->iterate_next;
+    else
         e = s->iterate_list_head;
 
     if (!e)
         return NULL;
 
-    *idx = e->index;
+    *idx = e->idx;
     return e->data;
 }
 
-void* pa_idxset_first(pa_idxset *s, uint32_t *idx) {
+void *pa_idxset_iterate(pa_idxset *s, void **state, uint32_t *idx) {
+    struct idxset_entry *e;
+
     pa_assert(s);
+    pa_assert(state);
 
-    if (!s->iterate_list_head)
-        return NULL;
+    if (*state == (void*) -1)
+        goto at_end;
+
+    if ((!*state && !s->iterate_list_head))
+        goto at_end;
+
+    e = *state ? *state : s->iterate_list_head;
+
+    if (e->iterate_next)
+        *state = e->iterate_next;
+    else
+        *state = (void*) -1;
 
     if (idx)
-        *idx = s->iterate_list_head->index;
-    return s->iterate_list_head->data;
+        *idx = e->idx;
+
+    return e->data;
+
+at_end:
+    *state = (void *) -1;
+
+    if (idx)
+        *idx = PA_IDXSET_INVALID;
+
+    return NULL;
 }
 
-void *pa_idxset_next(pa_idxset *s, uint32_t *idx) {
-    struct idxset_entry **a, *e = NULL;
+void* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx) {
+    void *data;
 
     pa_assert(s);
-    pa_assert(idx);
 
-    if ((a = array_index(s, *idx)) && *a)
-        e = (*a)->iterate_next;
+    if (!s->iterate_list_head)
+        return NULL;
 
-    if (e) {
-        *idx = e->index;
-        return e->data;
-    } else {
-        *idx = PA_IDXSET_INVALID;
+    data = s->iterate_list_head->data;
+
+    if (idx)
+        *idx = s->iterate_list_head->idx;
+
+    remove_entry(s, s->iterate_list_head);
+
+    return data;
+}
+
+void* pa_idxset_first(pa_idxset *s, uint32_t *idx) {
+    pa_assert(s);
+
+    if (!s->iterate_list_head)
         return NULL;
-    }
+
+    if (idx)
+        *idx = s->iterate_list_head->idx;
+
+    return s->iterate_list_head->data;
 }
 
-int pa_idxset_foreach(pa_idxset*s, int (*func)(void *p, uint32_t idx, int *del, void*userdata), void *userdata) {
+void *pa_idxset_next(pa_idxset *s, uint32_t *idx) {
     struct idxset_entry *e;
+    unsigned hash;
 
     pa_assert(s);
-    pa_assert(func);
-
-    e = s->iterate_list_head;
-    while (e) {
-        int del = 0, r;
-        struct idxset_entry *n = e->iterate_next;
-
-        r = func(e->data, e->index, &del, userdata);
+    pa_assert(idx);
 
-        if (del)
-            remove_entry(s, e);
+    hash = *idx % NBUCKETS;
 
-        if (r < 0)
-            return r;
+    if (!(e = index_scan(s, hash, *idx)))
+        return NULL;
 
-        e = n;
+    if (!e->iterate_next) {
+        *idx = PA_IDXSET_INVALID;
+        return NULL;
     }
 
-    return 0;
+    e = e->iterate_next;
+
+    *idx = e->idx;
+    return e->data;
 }
 
 unsigned pa_idxset_size(pa_idxset*s) {
@@ -413,9 +425,8 @@ unsigned pa_idxset_size(pa_idxset*s) {
     return s->n_entries;
 }
 
-int pa_idxset_isempty(pa_idxset *s) {
+pa_bool_t pa_idxset_isempty(pa_idxset *s) {
     pa_assert(s);
 
     return s->n_entries == 0;
 }
-
diff --git a/src/pulsecore/idxset.h b/src/pulsecore/idxset.h
index f089ef3..7531ea3 100644
--- a/src/pulsecore/idxset.h
+++ b/src/pulsecore/idxset.h
@@ -1,10 +1,10 @@
-#ifndef fooidxsethfoo
-#define fooidxsethfoo
+#ifndef foopulsecoreidxsethfoo
+#define foopulsecoreidxsethfoo
 
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
   Copyright 2006 Pierre Ossman <ossman at cendio.se> for Cendio AB
 
   PulseAudio is free software; you can redistribute it and/or modify
@@ -25,9 +25,12 @@
 
 #include <inttypes.h>
 
+#include <pulsecore/macro.h>
+
 /* A combination of a set and a dynamic array. Entries are indexable
- * both through a numeric automatically generated index and the entry's
- * data pointer. As usual, memory management is the user's job. */
+ * both through an automatically generated numeric index and the
+ * entry's data pointer. As usual, memory management is the user's
+ * job. */
 
 /* A special index value denoting the invalid index. */
 #define PA_IDXSET_INVALID ((uint32_t) -1)
@@ -54,7 +57,7 @@ typedef struct pa_idxset pa_idxset;
 pa_idxset* pa_idxset_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
 
 /* Free the idxset. When the idxset is not empty the specified function is called for every entry contained */
-void pa_idxset_free(pa_idxset *s, void (*free_func) (void *p, void *userdata), void *userdata);
+void pa_idxset_free(pa_idxset *s, pa_free2_cb_t free_cb, void *userdata);
 
 /* Store a new item in the idxset. The index of the item is returned in *idx */
 int pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx);
@@ -79,6 +82,12 @@ void* pa_idxset_remove_by_data(pa_idxset*s, const void *p, uint32_t *idx);
    returned before the an entry is returned the second time.*/
 void* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx);
 
+/* Iterate through the idxset. At first iteration state should be NULL */
+void *pa_idxset_iterate(pa_idxset *s, void **state, uint32_t *idx);
+
+/* Return the oldest entry in the idxset and remove it. If idx is not NULL fill in its index in *idx */
+void* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx);
+
 /* Return the oldest entry in the idxset. Fill in its index in *idx. */
 void* pa_idxset_first(pa_idxset *s, uint32_t *idx);
 
@@ -88,14 +97,10 @@ void* pa_idxset_first(pa_idxset *s, uint32_t *idx);
  * iterate through the set.*/
 void *pa_idxset_next(pa_idxset *s, uint32_t *idx);
 
-/* Call a function for every item in the set. If the callback function
-   returns -1, the loop is terminated. If *del is set to non-zero that
-   specific item is removed. It is not safe to call any other
-   functions on the idxset while pa_idxset_foreach is executed. */
-int pa_idxset_foreach(pa_idxset*s, int (*func)(void *p, uint32_t idx, int *del, void*userdata), void *userdata);
-
+/* Return the current number of entries in the idxset */
 unsigned pa_idxset_size(pa_idxset*s);
 
-int pa_idxset_isempty(pa_idxset *s);
+/* Return TRUE of the idxset is empty */
+pa_bool_t pa_idxset_isempty(pa_idxset *s);
 
 #endif
diff --git a/src/pulsecore/module.c b/src/pulsecore/module.c
index f1eeb76..e003dd7 100644
--- a/src/pulsecore/module.c
+++ b/src/pulsecore/module.c
@@ -194,25 +194,19 @@ void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
     pa_module_free(m);
 }
 
-static void free_callback(void *p, PA_GCC_UNUSED void *userdata) {
-    pa_module *m = p;
-    pa_assert(m);
-    pa_module_free(m);
-}
-
 void pa_module_unload_all(pa_core *c) {
-    pa_module *m;
 
     pa_assert(c);
 
-    if (!c->modules)
-        return;
+    if (c->modules) {
+        pa_module *m;
 
-    while ((m = pa_idxset_first(c->modules, NULL)))
-        pa_module_unload(c, m);
+        while ((m = pa_idxset_steal_first(c->modules, NULL)))
+            pa_module_free(m);
 
-    pa_idxset_free(c->modules, free_callback, NULL);
-    c->modules = NULL;
+        pa_idxset_free(c->modules, NULL, NULL);
+        c->modules = NULL;
+    }
 
     if (c->module_auto_unload_event) {
         c->mainloop->time_free(c->module_auto_unload_event);
@@ -225,55 +219,47 @@ void pa_module_unload_all(pa_core *c) {
     }
 }
 
-static int unused_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void *userdata) {
-    pa_module *m = p;
-    time_t *now = userdata;
-
-    pa_assert(m);
-    pa_assert(del);
-    pa_assert(now);
-
-    if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->module_idle_time <= *now) {
-        pa_module_free(m);
-        *del = 1;
-    }
-
-    return 0;
-}
-
 void pa_module_unload_unused(pa_core *c) {
+    void *state = NULL;
     time_t now;
+    pa_module *m;
+
     pa_assert(c);
 
     if (!c->modules)
         return;
 
     time(&now);
-    pa_idxset_foreach(c->modules, unused_callback, &now);
-}
 
-static int unload_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, PA_GCC_UNUSED void *userdata) {
-    pa_module *m = p;
-    pa_assert(m);
+    while ((m = pa_idxset_iterate(c->modules, &state, NULL))) {
 
-    if (m->unload_requested) {
-        pa_module_free(m);
-        *del = 1;
-    }
+        if (m->n_used > 0)
+            continue;
+
+        if (!m->auto_unload)
+            continue;
 
-    return 0;
+        if (m->last_used_time + m->core->module_idle_time > now)
+            continue;
+
+        pa_module_unload(c, m);
+    }
 }
 
 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
-    pa_core *core = PA_CORE(userdata);
+    void *state = NULL;
+    pa_core *c = PA_CORE(userdata);
+    pa_module *m;
 
-    pa_core_assert_ref(core);
+    pa_core_assert_ref(c);
     api->defer_enable(e, 0);
 
-    if (!core->modules)
+    if (!c->modules)
         return;
 
-    pa_idxset_foreach(core->modules, unload_callback, NULL);
+    while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
+        if (m->unload_requested)
+            pa_module_unload(c, m);
 }
 
 void pa_module_unload_request(pa_module *m) {

commit 913bbd4489799b057a5b47f19843d2d83e1003c1
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 27 21:37:33 2008 +0200

    save a bit of memory

diff --git a/src/pulsecore/core.h b/src/pulsecore/core.h
index d9ed46f..14a2c78 100644
--- a/src/pulsecore/core.h
+++ b/src/pulsecore/core.h
@@ -119,11 +119,13 @@ struct pa_core {
 
     pa_time_event *scache_auto_unload_event;
 
-    pa_bool_t disallow_module_loading, running_as_daemon;
+    pa_bool_t disallow_module_loading:1;
+    pa_bool_t running_as_daemon:1;
+    pa_bool_t realtime_scheduling:1;
+    pa_bool_t disable_remixing:1;
+
     pa_resample_method_t resample_method;
-    pa_bool_t realtime_scheduling;
     int realtime_priority;
-    pa_bool_t disable_remixing;
 
     /* hooks */
     pa_hook hooks[PA_CORE_HOOK_MAX];

-- 
hooks/post-receive
PulseAudio Sound Server



More information about the pulseaudio-commits mailing list