From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 55EE01FAF4 for ; Mon, 19 Dec 2022 11:19:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1671448764; bh=lUmUyROnVfaDgwXREmUmSISry31pGr2fQaUQEDwaoVw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=hhJIan0hbcK1JQ/cXw7tWkNzxcCs7veHIRyKvxQIpVI86fC6f0nKZ/koPzlbnMv2H eGoilIPHlDASYVhLlRTkc9gRUOAlYXp+qN2MVbcOa0Nl/WFWOIURWOH7u3btRG7yWz 9QEmWM9O4bNJfzvYXvaGewK5668RATloWcJZTX7s= From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH 6/7] core: simplify callers of alloc_insert_rcu Date: Mon, 19 Dec 2022 11:19:21 +0000 Message-Id: <20221219111922.1079128-7-e@80x24.org> In-Reply-To: <20221219111922.1079128-1-e@80x24.org> References: <20221219111922.1079128-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: From: Eric Wong We can actually handle all RCU locking/unlocking inside alloc_insert_rcu to simplify callers. --- mwrap_core.h | 60 +++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/mwrap_core.h b/mwrap_core.h index 3161243..b014f5d 100644 --- a/mwrap_core.h +++ b/mwrap_core.h @@ -319,11 +319,6 @@ again: return l; } -static void update_stats_rcu_unlock(const struct src_loc *l) -{ - if (caa_likely(l)) rcu_read_unlock(); -} - static const COP *mwp_curcop(void) { #if MWRAP_PERL @@ -506,17 +501,20 @@ void free(void *p) } static void -alloc_insert_rcu(struct src_loc *l, struct alloc_hdr *h, size_t size, - void *real, size_t generation) +alloc_insert_rcu(struct src_loc *sl, struct alloc_hdr *h, size_t size, + void *real) { h->size = size; h->real = real; + size_t gen = 0; + struct src_loc *l = update_stats_rcu_lock(&gen, size, sl); h->as.live.loc = l; - h->as.live.gen = generation; + h->as.live.gen = gen; if (l) { pthread_mutex_t *mtx = src_loc_mutex_lock(l); cds_list_add_rcu(&h->anode, &l->allocs); CHECK(int, 0, pthread_mutex_unlock(mtx)); + rcu_read_unlock(); } } @@ -564,10 +562,7 @@ mwrap_memalign(void **pp, size_t alignment, size_t size, struct src_loc *sl) if (!ptr_is_aligned(p, alignment)) p = ptr_align(p, alignment); struct alloc_hdr *h = ptr2hdr(p); - size_t gen = 0; - struct src_loc *l = update_stats_rcu_lock(&gen, size, sl); - alloc_insert_rcu(l, h, size, real, gen); - update_stats_rcu_unlock(l); + alloc_insert_rcu(sl, h, size, real); *pp = p; } @@ -649,15 +644,11 @@ void *malloc(size_t size) void *p = real_malloc(asize); if (p) { - size_t gen = 0; SRC_LOC_BT(bt); struct alloc_hdr *h = p; - struct src_loc *l = update_stats_rcu_lock(&gen, size, &bt.sl); - alloc_insert_rcu(l, h, size, h, gen); - update_stats_rcu_unlock(l); - p = hdr2ptr(h); + alloc_insert_rcu(&bt.sl, h, size, h); + return hdr2ptr(h); } - if (p) return p; enomem: errno = ENOMEM; return 0; @@ -673,12 +664,9 @@ void *calloc(size_t nmemb, size_t size) goto enomem; void *p = real_malloc(asize); if (p) { - size_t gen = 0; struct alloc_hdr *h = p; SRC_LOC_BT(bt); - struct src_loc *l = update_stats_rcu_lock(&gen, size, &bt.sl); - alloc_insert_rcu(l, h, size, h, gen); - update_stats_rcu_unlock(l); + alloc_insert_rcu(&bt.sl, h, size, h); return memset(hdr2ptr(h), 0, size); } enomem: @@ -694,28 +682,24 @@ void *realloc(void *ptr, size_t size) free(ptr); return 0; } - if (__builtin_add_overflow(size, sizeof(struct alloc_hdr), &asize)) { - errno = ENOMEM; - return 0; - } + if (__builtin_add_overflow(size, sizeof(struct alloc_hdr), &asize)) + goto enomem; void *p = real_malloc(asize); if (p) { - size_t gen = 0; struct alloc_hdr *h = p; SRC_LOC_BT(bt); - struct src_loc *l = update_stats_rcu_lock(&gen, size, &bt.sl); - alloc_insert_rcu(l, h, size, h, gen); - update_stats_rcu_unlock(l); + alloc_insert_rcu(&bt.sl, h, size, h); p = hdr2ptr(h); + if (ptr) { + struct alloc_hdr *old = ptr2hdr(ptr); + memcpy(p, ptr, old->size < size ? old->size : size); + free(ptr); + } + return p; } - - if (ptr && p) { - struct alloc_hdr *old = ptr2hdr(ptr); - memcpy(p, ptr, old->size < size ? old->size : size); - free(ptr); - } - if (caa_unlikely(!p)) errno = ENOMEM; - return p; +enomem: + errno = ENOMEM; + return 0; } struct dump_arg {