Skip to content

Commit e14005b

Browse files
committed
BUILD: Adjusted BRK/SBRK conditionals to review feedback.
1 parent f116ab6 commit e14005b

File tree

6 files changed

+54
-73
lines changed

6 files changed

+54
-73
lines changed

config/m4/ucm.m4

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@
88
#
99
# Memory allocator selection
1010
#
11+
AC_CHECK_FUNCS([brk sbrk])
12+
1113
AC_ARG_WITH([allocator],
1214
[AS_HELP_STRING([--with-allocator=NAME],
1315
[Build UCX with predefined memory allocator. The supported values are:
1416
ptmalloc286. Default: ptmalloc286])],
1517
[],
1618
[with_allocator=ptmalloc286])
1719

18-
HAVE_UCM_PTMALLOC286=no
19-
2020
case ${with_allocator} in
2121
ptmalloc286)
2222
AC_MSG_NOTICE(Memory allocator is ptmalloc-2.8.6 version)
23-
AS_IF([test "x$have_brk_sbrk" = xyes],
24-
[AC_DEFINE([HAVE_UCM_PTMALLOC286], 1, [Use ptmalloc-2.8.6 version])
25-
HAVE_UCM_PTMALLOC286=yes],
26-
[AC_MSG_WARN([brk()/sbrk() not available; disabling ptmalloc286 allocator])
27-
HAVE_UCM_PTMALLOC286=no])
23+
AC_DEFINE([HAVE_UCM_PTMALLOC286], [1], [Use ptmalloc-2.8.6 version])
24+
HAVE_UCM_PTMALLOC286=yes
25+
AS_IF([test "x$ac_cv_func_sbrk" != xyes], [
26+
AC_DEFINE([HAVE_MORECORE], [0],
27+
[Disable MORECORE in ptmalloc (no sbrk available)])
28+
])
2829
;;
2930
*)
3031
AC_MSG_ERROR(Cannot continue. Unsupported memory allocator name
3132
in --with-allocator=[$with_allocator])
32-
;;
33+
3334
esac
3435

3536
AM_CONDITIONAL([HAVE_UCM_PTMALLOC286],[test "x$HAVE_UCM_PTMALLOC286" = "xyes"])
@@ -63,14 +64,8 @@ AC_CHECK_DECLS([getauxval], [], [],
6364
# SYS_xxx macro
6465
#
6566
mmap_hooks_happy=yes
66-
AC_CHECK_DECLS([SYS_mmap,
67-
SYS_munmap,
68-
SYS_mremap,
69-
SYS_brk,
70-
SYS_madvise],
71-
[],
72-
[mmap_hooks_happy=no], dnl mmap syscalls are not defined
73-
[#include <sys/syscall.h>])
67+
AC_CHECK_DECLS([SYS_mmap, SYS_munmap, SYS_mremap, SYS_madvise, SYS_brk],
68+
[], [mmap_hooks_happy=no], [#include <sys/syscall.h>])
7469

7570
shm_hooks_happy=yes
7671
AC_CHECK_DECLS([SYS_shmat,
@@ -89,9 +84,6 @@ AS_IF([test "x$mmap_hooks_happy" = "xyes"],
8984
AS_IF([test "x$ipc_hooks_happy" = "xyes" -o "x$shm_hooks_happy" = "xyes"],
9085
[bistro_hooks_happy=yes]))
9186

92-
AS_IF([test "x$have_brk_sbrk" != xyes],
93-
[bistro_hooks_happy=no])
94-
9587
AS_IF([test "x$bistro_hooks_happy" = "xyes"],
9688
[AC_DEFINE([UCM_BISTRO_HOOKS], [1], [Enable BISTRO hooks])],
9789
[AC_DEFINE([UCM_BISTRO_HOOKS], [0], [Enable BISTRO hooks])

configure.ac

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ AC_FUNC_STRERROR_R
8787

8888
AC_PATH_TOOL([PKG_CONFIG], [pkg-config], [pkg-config])
8989

90-
AC_CHECK_FUNCS([brk sbrk])
91-
92-
AS_IF([test "x$ac_cv_func_brk" = "xyes" && test "x$ac_cv_func_sbrk" = "xyes"],
93-
[have_brk_sbrk=yes],
94-
[have_brk_sbrk=no])
95-
96-
AS_IF([test "x$have_brk_sbrk" = "xyes"],
97-
[AC_DEFINE([HAVE_BRK_SBRK], [1],
98-
[Define if both brk() and sbrk() are available])])
99-
10090

10191
#
10292
# Define SHARED_LIB preprocessor macro when building a shared library

src/ucm/malloc/malloc_hook.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -556,17 +556,7 @@ static int ucm_setenv(const char *name, const char *value, int overwrite)
556556
return ret;
557557
}
558558

559-
static inline int ucm_malloc_sanitize_events(int events)
560-
{
561-
#ifdef HAVE_BRK_SBRK
562-
return events;
563-
#else
564-
/* If brk/sbrk are not available, don't wait for or install sbrk/brk hooks */
565-
return events & ~(UCM_EVENT_SBRK | UCM_EVENT_BRK);
566-
#endif
567-
}
568-
569-
#ifdef HAVE_BRK_SBRK
559+
#ifdef HAVE_SBRK
570560
static void ucm_malloc_sbrk(ucm_event_type_t event_type,
571561
ucm_event_t *event, void *arg)
572562
{
@@ -616,9 +606,6 @@ static void ucm_malloc_event_test_callback(ucm_event_type_t event_type,
616606
/* Has to be called with install_mutex held */
617607
static void ucm_malloc_test(int events)
618608
{
619-
#if !defined(HAVE_BRK_SBRK)
620-
events = ucm_malloc_sanitize_events(events);
621-
#endif
622609
static const size_t small_alloc_count = 128;
623610
static const size_t small_alloc_size = 4096;
624611
static const size_t large_alloc_size = 4 * UCS_MBYTE;
@@ -824,11 +811,7 @@ static void ucm_malloc_init_orig_funcs()
824811

825812
ucs_status_t ucm_malloc_install(int events)
826813
{
827-
#if !defined(HAVE_BRK_SBRK)
828-
events = ucm_malloc_sanitize_events(events);
829-
#endif
830-
831-
#ifdef HAVE_BRK_SBRK
814+
#ifdef HAVE_SBRK
832815
static ucm_event_handler_t sbrk_handler = {
833816
.events = UCM_EVENT_SBRK,
834817
.priority = 1000,
@@ -857,7 +840,21 @@ ucs_status_t ucm_malloc_install(int events)
857840
#endif
858841
}
859842

860-
#ifdef HAVE_BRK_SBRK
843+
#ifndef HAVE_BRK
844+
if (events & UCM_EVENT_BRK) {
845+
ucm_debug("brk event requested, but brk() is not available");
846+
events &= ~UCM_EVENT_BRK;
847+
}
848+
#endif
849+
850+
#ifndef HAVE_SBRK
851+
if (events & UCM_EVENT_SBRK) {
852+
ucm_debug("sbrk event requested, but sbrk() is not available");
853+
events &= ~UCM_EVENT_SBRK;
854+
}
855+
#endif
856+
857+
#ifdef HAVE_SBRK
861858
if (!(ucm_malloc_hook_state.install_state & UCM_MALLOC_INSTALLED_SBRK_EVH)) {
862859
ucm_debug("installing malloc-sbrk event handler");
863860
ucm_event_handler_add(&sbrk_handler);

src/ucm/mmap/install.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ static ucm_mmap_func_t ucm_mmap_funcs[] = {
8686
#endif
8787
{ UCM_MMAP_RELOC_ENTRY(shmat), UCM_EVENT_SHMAT, UCM_EVENT_NONE},
8888
{ UCM_MMAP_RELOC_ENTRY(shmdt), UCM_EVENT_SHMDT, UCM_EVENT_SHMAT},
89-
#ifdef HAVE_BRK_SBRK
89+
#ifdef HAVE_SBRK
9090
{ UCM_MMAP_RELOC_ENTRY(sbrk), UCM_EVENT_SBRK, UCM_EVENT_NONE},
91+
#endif
92+
#ifdef HAVE_BRK
9193
{ UCM_MMAP_RELOC_ENTRY(brk), UCM_EVENT_BRK, UCM_EVENT_NONE},
9294
#endif
9395
{ UCM_MMAP_RELOC_ENTRY(madvise), UCM_EVENT_MADVISE, UCM_EVENT_NONE},
@@ -137,16 +139,16 @@ static void ucm_mmap_event_test_callback(ucm_event_type_t event_type,
137139
}
138140
}
139141

140-
#ifdef HAVE_BRK_SBRK
141142
/* Call brk() and check return value, to avoid compile error of unused result */
142143
static void ucm_brk_checked(void *addr)
143144
{
145+
#ifdef HAVE_BRK
144146
int ret = brk(addr);
145147
if ((ret != 0) && (addr != NULL)) {
146148
ucm_diag("brk(addr=%p) failed: %m", addr);
147149
}
148-
}
149150
#endif
151+
}
150152

151153
/* Fire events with pre/post action. The problem is in call sequence: we
152154
* can't just fire single event - most of the system calls require set of
@@ -203,7 +205,7 @@ ucm_fire_mmap_events_internal(int events, ucm_mmap_test_events_data_t *data,
203205
}
204206

205207
if (exclusive && !RUNNING_ON_VALGRIND) {
206-
#ifdef HAVE_BRK_SBRK
208+
#if defined(HAVE_BRK) && defined(HAVE_SBRK)
207209
sbrk_size = ucm_get_page_size();
208210
if (events & (UCM_EVENT_BRK|UCM_EVENT_VM_MAPPED|UCM_EVENT_VM_UNMAPPED)) {
209211
p = ucm_get_current_brk();
@@ -224,7 +226,7 @@ ucm_fire_mmap_events_internal(int events, ucm_mmap_test_events_data_t *data,
224226
* pass invalid parameters. We assume that if the natives events are
225227
* delivered, it means VM_MAPPED/UNMAPPED would be delivered as well.
226228
*/
227-
#ifdef HAVE_BRK_SBRK
229+
#ifdef HAVE_BRK
228230
if (events & UCM_EVENT_BRK) {
229231
UCM_FIRE_EVENT(events, UCM_EVENT_BRK, data, ucm_brk_checked(NULL));
230232
}

src/ucm/util/replace.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ UCM_DEFINE_REPLACE_FUNC(mremap, void*, MAP_FAILED, void*, size_t, size_t, int,
5555
#endif
5656
UCM_DEFINE_REPLACE_FUNC(shmat, void*, MAP_FAILED, int, const void*, int)
5757
UCM_DEFINE_REPLACE_FUNC(shmdt, int, -1, const void*)
58-
#ifdef HAVE_BRK_SBRK
58+
#ifdef HAVE_SBRK
5959
UCM_DEFINE_REPLACE_FUNC(sbrk, void*, MAP_FAILED, intptr_t)
60+
#endif
61+
#ifdef HAVE_BRK
6062
UCM_DEFINE_REPLACE_FUNC(brk, int, -1, void*)
6163
#endif
6264
UCM_DEFINE_REPLACE_FUNC(madvise, int, -1, void*, size_t, int)
@@ -124,7 +126,7 @@ int ucm_orig_shmdt(const void *shmaddr)
124126

125127
#endif
126128

127-
#ifdef HAVE_BRK_SBRK
129+
#ifdef HAVE_BRK
128130
_UCM_DEFINE_DLSYM_FUNC(brk, ucm_orig_dlsym_brk, ucm_override_brk, int, void*)
129131

130132
int ucm_orig_brk(void *addr)
@@ -143,7 +145,21 @@ int ucm_orig_brk(void *addr)
143145
return 0;
144146
}
145147
}
148+
#else
149+
int ucm_orig_brk(void *addr)
150+
{
151+
(void)addr;
152+
errno = ENOSYS;
153+
return -1;
154+
}
155+
156+
int ucm_override_brk(void *addr)
157+
{
158+
return ucm_orig_brk(addr);
159+
}
160+
#endif
146161

162+
#if defined(HAVE_SBRK) && defined(HAVE_BRK)
147163
_UCM_DEFINE_DLSYM_FUNC(sbrk, ucm_orig_dlsym_sbrk, ucm_override_sbrk, void*,
148164
intptr_t)
149165

@@ -160,25 +176,13 @@ void *ucm_orig_sbrk(intptr_t increment)
160176
}
161177
}
162178
#else
163-
int ucm_orig_brk(void *addr)
164-
{
165-
(void)addr;
166-
errno = ENOSYS;
167-
return -1;
168-
}
169-
170179
void *ucm_orig_sbrk(intptr_t increment)
171180
{
172181
(void)increment;
173182
errno = ENOSYS;
174183
return MAP_FAILED;
175184
}
176185

177-
int ucm_override_brk(void *addr)
178-
{
179-
return ucm_orig_brk(addr);
180-
}
181-
182186
void *ucm_override_sbrk(intptr_t increment)
183187
{
184188
return ucm_orig_sbrk(increment);
@@ -196,13 +200,9 @@ UCM_DEFINE_DLSYM_FUNC(shmdt, int, const void*)
196200

197201
void *ucm_get_current_brk()
198202
{
199-
#ifdef HAVE_BRK_SBRK
200203
#if HAVE___CURBRK
201204
return __curbrk;
202205
#else
203206
return ucm_brk_syscall(0);
204207
#endif
205-
#else
206-
return NULL;
207-
#endif
208208
}

src/ucm/util/sys.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ char *ucm_concat_path(char *buffer, size_t max, const char *dir, const char *fil
377377

378378
void *ucm_brk_syscall(void *addr)
379379
{
380-
#ifdef HAVE_BRK_SBRK
380+
#ifdef HAVE_DECL_SYS_BRK
381381
/* Return type is equivalent to full pointer size */
382382
UCS_STATIC_ASSERT(sizeof(syscall(0)) == sizeof(void*));
383383

0 commit comments

Comments
 (0)