Skip to content

Commit

Permalink
Remove configure code checking for various malloc alignments. Just us…
Browse files Browse the repository at this point in the history
…e aligned_alloc internally instead. Fixes #278.
  • Loading branch information
insertinterestingnamehere committed Sep 16, 2024
1 parent b889c39 commit 78c7aec
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 138 deletions.
36 changes: 0 additions & 36 deletions config/ax_check_page_aligned_malloc.m4

This file was deleted.

41 changes: 0 additions & 41 deletions config/qthread_check_working_valloc.m4

This file was deleted.

2 changes: 0 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,6 @@ AC_CHECK_FUNCS([strtol memalign posix_memalign memset memmove munmap memcpy fsta
QTHREAD_CHECK_QSORT
AC_CHECK_DECLS([MADV_ACCESS_LWP],[],[],[[#include <sys/types.h>
#include <sys/mman.h>]])
AX_CHECK_PAGE_ALIGNED_MALLOC
QTHREAD_CHECK_WORKING_VALLOC
QTHREAD_CHECK_ASSERT([],[AC_MSG_ERROR([assert() does not seem to work])])

AC_CACHE_SAVE
Expand Down
61 changes: 2 additions & 59 deletions src/alloc/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,68 +34,11 @@ void *qt_realloc(void *ptr, size_t size) { return realloc(ptr, size); }
void qt_internal_alignment_init(void) { _pagesize = getpagesize(); }

void *qt_internal_aligned_alloc(size_t alloc_size, uint_fast16_t alignment) {
void *ret;

assert(alloc_size > 0);
switch (alignment) {
case 0: ret = MALLOC(alloc_size); break;
case 16:
case 8:
case 4:
case 2: ret = MALLOC(alloc_size); break;
default:
#if defined(HAVE_WORKING_VALLOC)
if (alignment == pagesize) {
ret = valloc(alloc_size);
break;
}
#elif defined(HAVE_PAGE_ALIGNED_MALLOC)
if (alignment == pagesize) {
ret = MALLOC(alloc_size);
break;
}
#endif
#if defined(HAVE_MEMALIGN)
ret = memalign(alignment, alloc_size);
#elif defined(HAVE_POSIX_MEMALIGN)
posix_memalign(&(ret), alignment, alloc_size);
#else
{
uint8_t *tmp = MALLOC((alloc_size + alignment - 1) + sizeof(void *));
if (!tmp) { return NULL; }
ret = (void *)(((uintptr_t)(tmp + sizeof(void *) + alignment - 1)) &
~(alignment - 1));
*((void **)ret - 1) = tmp;
} break;
#endif /* if defined(HAVE_MEMALIGN) */
}
assert(ret);
assert(((uintptr_t)ret & (alignment - 1)) == 0);
return ret;
return aligned_alloc((size_t) alignment, alloc_size);
}

void qt_internal_aligned_free(void *ptr, uint_fast16_t alignment) {
assert(ptr);
switch (alignment) {
case 0: qt_free(ptr); break;
case 16:
case 8:
case 4:
case 2: qt_free(ptr); break;
default:
#if defined(HAVE_WORKING_VALLOC) || defined(HAVE_PAGE_ALIGNED_MALLOC)
if (alignment == pagesize) {
qt_free(ptr);
break;
}
#endif
#if defined(HAVE_MEMALIGN) || defined(HAVE_POSIX_MEMALIGN)
qt_free(ptr);
#else
assert((uintptr_t) * ((void **)ptr - 1) > 4096);
qt_free(*((void **)ptr - 1));
#endif
}
qt_free(ptr);
}

/* vim:set expandtab: */

0 comments on commit 78c7aec

Please sign in to comment.