From aaa1bda695472c20ce61bec824eac59d0eae3633 Mon Sep 17 00:00:00 2001
From: Marcus Comstedt <marcus@mc.pp.se>
Date: Sat, 30 Jan 2021 15:28:19 +0100
Subject: [PATCH] block_allocator: Remove unused alignment functionality

---
 src/block_allocator.c | 51 ++++++++++---------------------------------
 src/block_allocator.h | 16 ++++----------
 src/configure.in      |  1 -
 src/pike_memory.c     | 18 ---------------
 src/pike_memory.h     |  1 -
 5 files changed, 16 insertions(+), 71 deletions(-)

diff --git a/src/block_allocator.c b/src/block_allocator.c
index 2644e0b47c..25a6c8e067 100644
--- a/src/block_allocator.c
+++ b/src/block_allocator.c
@@ -7,9 +7,9 @@
 
 #include <stdlib.h>
 
-#define BA_BLOCKN(l, p, n) ((struct ba_block_header *)((char*)(p) + (l).doffset + (n)*((l).block_size)))
-#define BA_LASTBLOCK(l, p) ((struct ba_block_header*)((char*)(p) + (l).doffset + (l).offset))
-#define BA_CHECK_PTR(l, p, ptr)	((size_t)((char*)(ptr) - (char*)(p)) <= (l).offset + (l).doffset)
+#define BA_BLOCKN(l, p, n) ((struct ba_block_header *)((char*)(p) + sizeof(struct ba_page) + (n)*((l).block_size)))
+#define BA_LASTBLOCK(l, p) ((struct ba_block_header*)((char*)(p) + sizeof(struct ba_page) + (l).offset))
+#define BA_CHECK_PTR(l, p, ptr)	((size_t)((char*)(ptr) - (char*)(p)) <= (l).offset + sizeof(struct ba_page))
 
 #define BA_ONE	((struct ba_block_header *)1)
 #define BA_FLAG_SORTED 1u
@@ -71,7 +71,7 @@ static INLINE void ba_clear_page(struct block_allocator * VALGRINDUSED(a), struc
 
 static struct ba_page * ba_alloc_page(struct block_allocator * a, int i) {
     struct ba_layout l = ba_get_layout(a, i);
-    size_t n = l.offset + l.block_size + l.doffset;
+    size_t n = l.offset + l.block_size + sizeof(struct ba_page);
     struct ba_page * p;
 
     /*
@@ -82,9 +82,7 @@ static struct ba_page * ba_alloc_page(struct block_allocator * a, int i) {
         Pike_error("Overflow.\n");
     }
 
-    if (l.alignment) {
-	p = xalloc_aligned(n, l.alignment);
-    } else {
+    {
 #ifdef DEBUG_MALLOC
 	/* In debug malloc mode, calling xalloc from the block alloc may result
 	 * in a deadlock, since xalloc will call ba_alloc, which in turn may call xalloc.
@@ -99,7 +97,7 @@ static struct ba_page * ba_alloc_page(struct block_allocator * a, int i) {
 #endif
     }
     ba_clear_page(a, p, &a->l);
-    PIKE_MEM_NA_RANGE((char*)p + l.doffset, n - l.doffset);
+    PIKE_MEM_NA_RANGE((char*)p + sizeof(struct ba_page), n - sizeof(struct ba_page));
     return p;
 }
 
@@ -123,21 +121,11 @@ static void ba_free_empty_pages(struct block_allocator * a) {
     }
 }
 
-PMOD_EXPORT void ba_low_init_aligned(struct block_allocator * a) {
+static void ba_low_init(struct block_allocator * a) {
     unsigned INT32 block_size = MAXIMUM(a->l.block_size, sizeof(struct ba_block_header));
 
     PIKE_MEMPOOL_CREATE(a);
 
-    if (a->l.alignment) {
-	if (a->l.alignment & (a->l.alignment - 1))
-	    Pike_fatal("Block allocator a->l.alignment is not a power of 2.\n");
-	if (block_size & (a->l.alignment-1))
-	    Pike_fatal("Block allocator block size is not aligned.\n");
-	a->l.doffset = PIKE_ALIGNTO(sizeof(struct ba_page), a->l.alignment);
-    } else {
-	a->l.doffset = sizeof(struct ba_page);
-    }
-
     if (a->l.blocks & (a->l.blocks - 1)) {
         unsigned INT32 tmp = round_up32(a->l.blocks);
         if (tmp) a->l.blocks = tmp;
@@ -146,12 +134,11 @@ PMOD_EXPORT void ba_low_init_aligned(struct block_allocator * a) {
     a->l.offset = block_size * (a->l.blocks-1);
 }
 
-PMOD_EXPORT void ba_init_aligned(struct block_allocator * a, unsigned INT32 block_size,
-				 unsigned INT32 blocks, unsigned INT32 alignment) {
+PMOD_EXPORT void ba_init(struct block_allocator * a, unsigned INT32 block_size,
+			 unsigned INT32 blocks) {
     a->l.blocks = blocks;
     a->l.block_size = block_size;
-    a->l.alignment = alignment;
-    ba_low_init_aligned(a);
+    ba_low_init(a);
     a->alloc = a->last_free = a->size = 0;
     memset(a->pages, 0, sizeof(a->pages));
 }
@@ -206,7 +193,7 @@ PMOD_EXPORT void ba_count_all(const struct block_allocator * a, size_t * num, si
     for( i=0; i<a->size; i++ )
     {
         struct ba_layout l = ba_get_layout( a, i );
-        b += l.offset + l.block_size + l.doffset;
+        b += l.offset + l.block_size + sizeof(struct ba_page);
         n += a->pages[i]->h.used;
     }
     *num = n;
@@ -217,7 +204,7 @@ static void ba_low_alloc(struct block_allocator * a) {
     int i;
 
     if (!a->l.offset) {
-        ba_low_init_aligned(a);
+        ba_low_init(a);
     }
 
     /*
@@ -275,13 +262,6 @@ PMOD_EXPORT void * ba_alloc(struct block_allocator * a) {
     }
     PIKE_MEM_WO_RANGE(ptr, sizeof(struct ba_block_header));
 
-#if PIKE_DEBUG
-    if (a->l.alignment && (size_t)ptr & (a->l.alignment - 1)) {
-	print_allocator(a);
-	Pike_fatal("Returning unaligned pointer.\n");
-    }
-#endif
-
     return ptr;
 }
 
@@ -290,13 +270,6 @@ PMOD_EXPORT void ba_free(struct block_allocator * a, void * ptr) {
     struct ba_page * p = a->pages[i];
     struct ba_layout l = ba_get_layout(a, i);
 
-#if PIKE_DEBUG
-    if (a->l.alignment && (size_t)ptr & (a->l.alignment - 1)) {
-	print_allocator(a);
-	Pike_fatal("Returning unaligned pointer.\n");
-    }
-#endif
-
     if (BA_CHECK_PTR(l, p, ptr)) goto found;
 
 #ifdef PIKE_DEBUG
diff --git a/src/block_allocator.h b/src/block_allocator.h
index e314915b86..f0d4faf83e 100644
--- a/src/block_allocator.h
+++ b/src/block_allocator.h
@@ -10,11 +10,9 @@ struct ba_layout {
     unsigned INT32 offset;
     unsigned INT32 block_size;
     unsigned INT32 blocks;
-    unsigned INT32 alignment;
-    unsigned INT32 doffset;
 };
 
-#define BA_LAYOUT_INIT(block_size, blocks, alignment)  { 0, (block_size), (blocks), (alignment), 0 }
+#define BA_LAYOUT_INIT(block_size, blocks)  { 0, (block_size), (blocks) }
 
 struct ba_page_header {
     struct ba_block_header * first;
@@ -62,20 +60,17 @@ static INLINE void ATTRIBUTE((unused)) * ba_it_val(struct ba_iterator * it) {
     return it->cur;
 }
 
-#define BA_INIT_ALIGNED(block_size, blocks, alignment) {    \
-    BA_LAYOUT_INIT(block_size, blocks, alignment),	    \
+#define BA_INIT(block_size, blocks) {			    \
+    BA_LAYOUT_INIT(block_size, blocks),			    \
     0, 0, 0,						    \
     { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,		    \
       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,		    \
       NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL }		    \
 }
 
-#define BA_INIT(block_size, blocks) BA_INIT_ALIGNED(block_size, blocks, 0)
-
 #define BA_INIT_PAGES(block_size, pages)	BA_INIT(block_size, ((pages) * PIKE_MALLOC_PAGE_SIZE)/(block_size))
 
-PMOD_EXPORT void ba_init_aligned(struct block_allocator * a, unsigned INT32 block_size, unsigned INT32 blocks,
-				 unsigned INT32 alignment);
+PMOD_EXPORT void ba_init(struct block_allocator * a, unsigned INT32 block_size, unsigned INT32 blocks);
 ATTRIBUTE((malloc)) PMOD_EXPORT void * ba_alloc(struct block_allocator * a);
 PMOD_EXPORT void ba_free(struct block_allocator * a, void * ptr);
 PMOD_EXPORT void ba_destroy(struct block_allocator * a);
@@ -83,7 +78,4 @@ PMOD_EXPORT void ba_free_all(struct block_allocator * a);
 PMOD_EXPORT size_t ba_count(const struct block_allocator * a);
 PMOD_EXPORT void ba_count_all(const struct block_allocator * a, size_t * num, size_t * size);
 
-static INLINE void ATTRIBUTE((unused)) ba_init(struct block_allocator * a, unsigned INT32 block_size, unsigned INT32 blocks) {
-    ba_init_aligned(a, block_size, blocks, 0);
-}
 #endif
diff --git a/src/configure.in b/src/configure.in
index 995c87b678..2503b897d4 100644
--- a/src/configure.in
+++ b/src/configure.in
@@ -4655,7 +4655,6 @@ AC_CHECK_FUNCS( \
  SetErrorMode \
  thread_info \
  host_get_clock_service \
- posix_memalign \
 )
 
 # SunOS 4 realloc() returns NULL when reallocing NULL.
diff --git a/src/pike_memory.c b/src/pike_memory.c
index fc192ca44d..12a81a3639 100644
--- a/src/pike_memory.c
+++ b/src/pike_memory.c
@@ -429,24 +429,6 @@ PMOD_EXPORT void *debug_xcalloc(size_t n, size_t s)
   return 0;
 }
 
-PMOD_EXPORT void *xalloc_aligned(size_t size, size_t alignment) {
-    void * ret;
-
-    if (!size) return 0;
-
-#ifdef HAVE_POSIX_MEMALIGN
-    if (posix_memalign(&ret, alignment, size)) {
-	Pike_error(msg_out_of_mem_2, size);
-    }
-#else
-    if ((ret = memalign(alignment, size)) == NULL) {
-	Pike_error(msg_out_of_mem_2, size);
-    }
-#endif
-
-    return ret;
-}
-
 PMOD_EXPORT char *debug_xstrdup(const char *src)
 {
   char *dst = NULL;
diff --git a/src/pike_memory.h b/src/pike_memory.h
index 961c3ac72e..f737807438 100644
--- a/src/pike_memory.h
+++ b/src/pike_memory.h
@@ -233,7 +233,6 @@ PMOD_EXPORT void *debug_xmalloc(size_t s) MALLOC_FUNCTION;
 PMOD_EXPORT void debug_xfree(void *mem);
 PMOD_EXPORT void *debug_xrealloc(void *m, size_t s) MALLOC_FUNCTION;
 PMOD_EXPORT void *debug_xcalloc(size_t n, size_t s) MALLOC_FUNCTION;
-PMOD_EXPORT void *xalloc_aligned(size_t size, size_t alignment) MALLOC_FUNCTION;
 
 #define PIKE_ALIGNTO(x, a)	(((x) + (a)-1) & ~((a)-1))
 
-- 
GitLab