From: Eric Wong <e@80x24.org>
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 [thread overview]
Message-ID: <20221219111922.1079128-7-e@80x24.org> (raw)
In-Reply-To: <20221219111922.1079128-1-e@80x24.org>
From: Eric Wong <mwrap-perl@80x24.org>
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 {
next prev parent reply other threads:[~2022-12-19 11:19 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-19 11:19 [PATCH 0/7] shrinkage Eric Wong
2022-12-19 11:19 ` [PATCH 1/7] core: shrink src_loc 8 bytes on x86-64 Eric Wong
2022-12-19 11:19 ` [PATCH 2/7] core: shrink src_loc by another " Eric Wong
2022-12-19 11:19 ` [PATCH 3/7] core: drop FreeBSD-specific mutex initialization Eric Wong
2022-12-19 11:19 ` [PATCH 4/7] calloc: consolidate ENOMEM handling Eric Wong
2022-12-19 11:19 ` [PATCH 5/7] *alloc: limit scope of RCU lock Eric Wong
2022-12-19 11:19 ` Eric Wong [this message]
2022-12-19 11:19 ` [PATCH 7/7] README: add some caveats (signalfd usage w/ current URCU) Eric Wong
2022-12-19 22:57 ` [WIP] attempt to workaround older URCU for signalfd Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221219111922.1079128-7-e@80x24.org \
--to=e@80x24.org \
--cc=mwrap-perl@80x24.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mwrap-perl.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).