Skip to content

Commit 1276c23

Browse files
committed
thread.c: use ruby_sized_xfree for fdset and interupt tasks
1 parent 4d5cd81 commit 1276c23

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

thread.c

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4251,13 +4251,20 @@ rb_fd_init(rb_fdset_t *fds)
42514251
FD_ZERO(fds->fdset);
42524252
}
42534253

4254+
static inline size_t
4255+
fdset_memsize(int maxfd)
4256+
{
4257+
size_t o = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
4258+
if (o < sizeof(fd_set)) {
4259+
return sizeof(fd_set);
4260+
}
4261+
return o;
4262+
}
4263+
42544264
void
42554265
rb_fd_init_copy(rb_fdset_t *dst, rb_fdset_t *src)
42564266
{
4257-
size_t size = howmany(rb_fd_max(src), NFDBITS) * sizeof(fd_mask);
4258-
4259-
if (size < sizeof(fd_set))
4260-
size = sizeof(fd_set);
4267+
size_t size = fdset_memsize(rb_fd_max(src));
42614268
dst->maxfd = src->maxfd;
42624269
dst->fdset = xmalloc(size);
42634270
memcpy(dst->fdset, src->fdset, size);
@@ -4266,7 +4273,7 @@ rb_fd_init_copy(rb_fdset_t *dst, rb_fdset_t *src)
42664273
void
42674274
rb_fd_term(rb_fdset_t *fds)
42684275
{
4269-
xfree(fds->fdset);
4276+
ruby_sized_xfree(fds->fdset, fdset_memsize(fds->maxfd));
42704277
fds->maxfd = 0;
42714278
fds->fdset = 0;
42724279
}
@@ -4281,14 +4288,11 @@ rb_fd_zero(rb_fdset_t *fds)
42814288
static void
42824289
rb_fd_resize(int n, rb_fdset_t *fds)
42834290
{
4284-
size_t m = howmany(n + 1, NFDBITS) * sizeof(fd_mask);
4285-
size_t o = howmany(fds->maxfd, NFDBITS) * sizeof(fd_mask);
4286-
4287-
if (m < sizeof(fd_set)) m = sizeof(fd_set);
4288-
if (o < sizeof(fd_set)) o = sizeof(fd_set);
4291+
size_t m = fdset_memsize(n + 1);
4292+
size_t o = fdset_memsize(fds->maxfd);
42894293

42904294
if (m > o) {
4291-
fds->fdset = xrealloc(fds->fdset, m);
4295+
fds->fdset = ruby_sized_xrealloc(fds->fdset, m, o);
42924296
memset((char *)fds->fdset + o, 0, m - o);
42934297
}
42944298
if (n >= fds->maxfd) fds->maxfd = n + 1;
@@ -4318,23 +4322,18 @@ rb_fd_isset(int n, const rb_fdset_t *fds)
43184322
void
43194323
rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int max)
43204324
{
4321-
size_t size = howmany(max, NFDBITS) * sizeof(fd_mask);
4322-
4323-
if (size < sizeof(fd_set)) size = sizeof(fd_set);
4325+
size_t size = fdset_memsize(max);
4326+
dst->fdset = ruby_sized_xrealloc(dst->fdset, size, fdset_memsize(dst->maxfd));
43244327
dst->maxfd = max;
4325-
dst->fdset = xrealloc(dst->fdset, size);
43264328
memcpy(dst->fdset, src, size);
43274329
}
43284330

43294331
void
43304332
rb_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src)
43314333
{
4332-
size_t size = howmany(rb_fd_max(src), NFDBITS) * sizeof(fd_mask);
4333-
4334-
if (size < sizeof(fd_set))
4335-
size = sizeof(fd_set);
4334+
size_t size = fdset_memsize(rb_fd_max(src));
4335+
dst->fdset = ruby_sized_xrealloc(dst->fdset, size, fdset_memsize(dst->maxfd));
43364336
dst->maxfd = src->maxfd;
4337-
dst->fdset = xrealloc(dst->fdset, size);
43384337
memcpy(dst->fdset, src->fdset, size);
43394338
}
43404339

@@ -4386,10 +4385,19 @@ rb_fd_init_copy(rb_fdset_t *dst, rb_fdset_t *src)
43864385
rb_fd_dup(dst, src);
43874386
}
43884387

4388+
static inline size_t
4389+
fdset_memsize(int capa)
4390+
{
4391+
if (capa == FD_SETSIZE) {
4392+
return sizeof(fd_set);
4393+
}
4394+
return sizeof(unsigned int) + (capa * sizeof(SOCKET));
4395+
}
4396+
43894397
void
43904398
rb_fd_term(rb_fdset_t *set)
43914399
{
4392-
xfree(set->fdset);
4400+
ruby_sized_xfree(set->fdset, fdset_memsize(set->capa));
43934401
set->fdset = NULL;
43944402
set->capa = 0;
43954403
}
@@ -6265,7 +6273,7 @@ threadptr_interrupt_exec_exec(rb_thread_t *th)
62656273
else {
62666274
(*task->func)(task->data);
62676275
}
6268-
ruby_xfree(task);
6276+
SIZED_FREE(task);
62696277
}
62706278
else {
62716279
break;
@@ -6281,7 +6289,7 @@ threadptr_interrupt_exec_cleanup(rb_thread_t *th)
62816289
struct rb_interrupt_exec_task *task;
62826290

62836291
while ((task = ccan_list_pop(&th->interrupt_exec_tasks, struct rb_interrupt_exec_task, node)) != NULL) {
6284-
ruby_xfree(task);
6292+
SIZED_FREE(task);
62856293
}
62866294
}
62876295
rb_native_mutex_unlock(&th->interrupt_lock);

0 commit comments

Comments
 (0)