From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12317 Path: news.gmane.org!.POSTED!not-for-mail From: Jens Gustedt Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH 5/7] use the new lock algorithm for malloc Date: Wed, 3 Jan 2018 14:17:12 +0100 Message-ID: <163ca89d544cffbaba5fcfbacf56fb589fd2b03a.1514985618.git.Jens.Gustedt@inria.fr> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1514987216 27248 195.159.176.226 (3 Jan 2018 13:46:56 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 3 Jan 2018 13:46:56 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-12333-gllmg-musl=m.gmane.org@lists.openwall.com Wed Jan 03 14:46:52 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1eWjNZ-0006YB-Hb for gllmg-musl@m.gmane.org; Wed, 03 Jan 2018 14:46:49 +0100 Original-Received: (qmail 7375 invoked by uid 550); 3 Jan 2018 13:48:18 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 6081 invoked from network); 3 Jan 2018 13:48:14 -0000 X-IronPort-AV: E=Sophos;i="5.45,501,1508796000"; d="scan'208";a="307522380" In-Reply-To: Resent-Date: Wed, 3 Jan 2018 14:45:11 +0100 Resent-From: Jens Gustedt Resent-Message-ID: <20180103144511.69eceb88@inria.fr> Resent-To: musl Xref: news.gmane.org gmane.linux.lib.musl.general:12317 Archived-At: Malloc used a specialized lock implementation in many places. Now that we have a generic lock that has the desired properties, we should just use this, instead of this multitude of very similar lock mechanisms. --- src/malloc/malloc.c | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c index 9e05e1d6..6c667a5a 100644 --- a/src/malloc/malloc.c +++ b/src/malloc/malloc.c @@ -13,6 +13,8 @@ #define inline inline __attribute__((always_inline)) #endif +#include "__lock.h" + void *__mmap(void *, size_t, int, int, int, off_t); int __munmap(void *, size_t); void *__mremap(void *, size_t, size_t, int, ...); @@ -24,7 +26,7 @@ struct chunk { }; struct bin { - volatile int lock[2]; + volatile int lock[1]; struct chunk *head; struct chunk *tail; }; @@ -32,7 +34,7 @@ struct bin { static struct { volatile uint64_t binmap; struct bin bins[64]; - volatile int free_lock[2]; + volatile int free_lock[1]; } mal; @@ -58,30 +60,16 @@ static struct { /* Synchronization tools */ -static inline void lock(volatile int *lk) -{ - if (libc.threads_minus_1) - while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1); -} - -static inline void unlock(volatile int *lk) -{ - if (lk[0]) { - a_store(lk, 0); - if (lk[1]) __wake(lk, 1, 1); - } -} - static inline void lock_bin(int i) { - lock(mal.bins[i].lock); + __lock_fast(mal.bins[i].lock); if (!mal.bins[i].head) mal.bins[i].head = mal.bins[i].tail = BIN_TO_CHUNK(i); } static inline void unlock_bin(int i) { - unlock(mal.bins[i].lock); + __unlock_fast(mal.bins[i].lock); } static int first_set(uint64_t x) @@ -161,7 +149,7 @@ void *__expand_heap(size_t *); static struct chunk *expand_heap(size_t n) { - static int heap_lock[2]; + static volatile int heap_lock[1]; static void *end; void *p; struct chunk *w; @@ -171,11 +159,11 @@ static struct chunk *expand_heap(size_t n) * we need room for an extra zero-sized sentinel chunk. */ n += SIZE_ALIGN; - lock(heap_lock); + __lock_fast(heap_lock); p = __expand_heap(&n); if (!p) { - unlock(heap_lock); + __unlock_fast(heap_lock); return 0; } @@ -200,7 +188,7 @@ static struct chunk *expand_heap(size_t n) w = MEM_TO_CHUNK(p); w->csize = n | C_INUSE; - unlock(heap_lock); + __unlock_fast(heap_lock); return w; } @@ -481,10 +469,10 @@ void free(void *p) next->psize = final_size | C_INUSE; i = bin_index(final_size); lock_bin(i); - lock(mal.free_lock); + __lock_fast(mal.free_lock); if (self->psize & next->csize & C_INUSE) break; - unlock(mal.free_lock); + __unlock_fast(mal.free_lock); unlock_bin(i); } @@ -510,7 +498,7 @@ void free(void *p) self->csize = final_size; next->psize = final_size; - unlock(mal.free_lock); + __unlock_fast(mal.free_lock); self->next = BIN_TO_CHUNK(i); self->prev = mal.bins[i].tail; -- 2.15.1