summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2011-01-17 13:14:05 -0800
committerDavid S. Miller <davem@davemloft.net>2011-01-17 13:14:05 -0800
commitdfe79d6036c298a65e0d25cfeaebe6a98f98446d (patch)
tree027ac6af9ab5666dc44339890dbaf6447917aec4
parent09541f1c2edfd6aaac74df63e40ed35f2ef6cc38 (diff)
downloadsilo-dfe79d6036c298a65e0d25cfeaebe6a98f98446d.tar.gz
malloc: Provide posix_memalign() implementation.
ext2progs library really wants a working version of this, and thankfully it's not that hard to do. With help from Richard Mortimer. Reported-by: Alex Buell <alex.buell@munted.org.uk> Tested-by: Alex Buell <alex.buell@munted.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--common/malloc.c32
-rw-r--r--second/fs/ext2.c6
2 files changed, 31 insertions, 7 deletions
diff --git a/common/malloc.c b/common/malloc.c
index cc3946d..94fc29c 100644
--- a/common/malloc.c
+++ b/common/malloc.c
@@ -27,6 +27,12 @@ static char *malloc_ptr = (char *) MALLOC_BASE;
static char *last_alloc = 0;
+static char *align_ptr_to(char *ptr, unsigned long align)
+{
+ return (char *) ((((unsigned long) ptr) + (align - 1UL)) &
+ ~(align - 1UL));
+}
+
void *malloc (int size)
{
char *caddr;
@@ -34,10 +40,34 @@ void *malloc (int size)
caddr = malloc_ptr;
malloc_ptr += size;
last_alloc = caddr;
- malloc_ptr = (char *) ((((unsigned long) malloc_ptr) + 7) & (~7));
+ malloc_ptr = align_ptr_to(malloc_ptr, 8UL);
return caddr;
}
+int posix_memalign(void **memptr, unsigned long alignment, unsigned long size)
+{
+ char *caddr;
+
+ if (alignment & (alignment - 1UL))
+ return -1;
+ if (alignment & (sizeof(void *) - 1UL))
+ return -1;
+
+ if (size == 0) {
+ *memptr = (void *) 0;
+ return 0;
+ }
+
+ caddr = align_ptr_to(malloc_ptr, alignment);
+ malloc_ptr = (caddr + size);
+ last_alloc = caddr;
+ malloc_ptr = align_ptr_to(malloc_ptr, 8UL);
+
+ *memptr = caddr;
+
+ return 0;
+}
+
void free (void *m)
{
if (m == last_alloc)
diff --git a/second/fs/ext2.c b/second/fs/ext2.c
index 12d00dc..57f5e9a 100644
--- a/second/fs/ext2.c
+++ b/second/fs/ext2.c
@@ -163,9 +163,3 @@ void *realloc(void *p, int size)
{
return NULL;
}
-
-int posix_memalign(void **memptr, size_t alignment, size_t size)
-{
- *memptr = NULL;
- return -1;
-}