SOURCES: dietlibc-memalign.patch (NEW) - memalign() and valloc() (ripped fr...

baggins baggins at pld-linux.org
Thu Mar 19 16:14:44 CET 2009


Author: baggins                      Date: Thu Mar 19 15:14:44 2009 GMT
Module: SOURCES                       Tag: HEAD
---- Log message:
- memalign() and valloc() (ripped from uClibc)

---- Files affected:
SOURCES:
   dietlibc-memalign.patch (NONE -> 1.1)  (NEW)

---- Diffs:

================================================================
Index: SOURCES/dietlibc-memalign.patch
diff -u /dev/null SOURCES/dietlibc-memalign.patch:1.1
--- /dev/null	Thu Mar 19 16:14:45 2009
+++ SOURCES/dietlibc-memalign.patch	Thu Mar 19 16:14:39 2009
@@ -0,0 +1,116 @@
+diff -ur dietlibc-0.31/include/stdlib.h dietlibc-0.31-memalign/include/stdlib.h
+--- dietlibc-0.31/include/stdlib.h	2009-03-19 15:39:48.000000000 +0100
++++ dietlibc-0.31-memalign/include/stdlib.h	2009-03-19 15:39:37.000000000 +0100
+@@ -13,6 +13,8 @@
+ void *malloc(size_t size)  __THROW __attribute_malloc__;
+ void free(void *ptr) __THROW;
+ void *realloc(void *ptr, size_t size) __THROW __attribute_malloc__;
++void *memalign(size_t alignment, size_t size) __THROW __attribute_malloc__;
++void *valloc(size_t size) __THROW __attribute_malloc__;
+ 
+ char *getenv(const char *name) __THROW __pure;
+ int putenv(const char *string) __THROW;
+diff -ur dietlibc-0.31/lib/alloc.c dietlibc-0.31-memalign/lib/alloc.c
+--- dietlibc-0.31/lib/alloc.c	2009-03-19 15:39:48.000000000 +0100
++++ dietlibc-0.31-memalign/lib/alloc.c	2009-03-19 15:38:33.000000000 +0100
+@@ -128,10 +128,14 @@
+ }
+ 
+ /* -- PUBLIC FUNCTIONS ---------------------------------------------------- */
+-
++int __libc_free_aligned(void *ptr);
+ static void _alloc_libc_free(void *ptr) {
+   register size_t size;
+-  if (ptr) {
++  if (ptr == NULL)
++	  return;
++  if (__libc_free_aligned(ptr))
++	  return;
++
+     size=((__alloc_t*)BLOCK_START(ptr))->size;
+     if (size) {
+       if (size<=__MAX_SMALL_SIZE)
+@@ -139,7 +143,6 @@
+       else
+ 	munmap(BLOCK_START(ptr),size);
+     }
+-  }
+ }
+ void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free")));
+ void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free")));
+@@ -268,3 +249,75 @@
+ }
+ void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc")));
+ 
++/* List of blocks allocated with memalign or valloc */
++struct alignlist {
++	struct alignlist *next;
++	void *aligned;	/* The address that memaligned returned.  */
++	void *exact;	/* The address that malloc returned.  */
++};
++struct alignlist *_aligned_blocks;
++
++/* Return memory to the heap. */
++int __libc_free_aligned(void *ptr);
++int __libc_free_aligned(void *ptr) {
++	struct alignlist *l;
++	register size_t size;
++
++	if (ptr == NULL)
++		return 0;
++
++	for (l = _aligned_blocks; l != NULL; l = l->next) {
++		if (l->aligned == ptr) {
++			size=((__alloc_t*)BLOCK_START(l->exact))->size;
++			if (size) {
++			  if (size<=__MAX_SMALL_SIZE)
++			    __small_free(l->exact,size);
++			  else
++			    munmap(BLOCK_START(l->exact),size);
++			}
++			/* Mark the block as free */
++			l->aligned = NULL;
++			return 1;
++		}
++	}
++	return 0;
++}
++
++void * memalign (size_t alignment, size_t size);
++void * memalign (size_t alignment, size_t size) {
++	void * result;
++	unsigned long int adj;
++
++	result = malloc (size + alignment - 1);
++	if (result == NULL)
++		return NULL;
++
++	adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
++	if (adj != 0) {
++		struct alignlist *l;
++		for (l = _aligned_blocks; l != NULL; l = l->next)
++			if (l->aligned == NULL)
++				/* This slot is free.  Use it.  */
++				break;
++		if (l == NULL) {
++			l = (struct alignlist *) malloc (sizeof (struct alignlist));
++			if (l == NULL) {
++				_alloc_libc_free(result);
++				result = NULL;
++				goto DONE;
++			}
++			l->next = _aligned_blocks;
++			_aligned_blocks = l;
++		}
++		l->exact = result;
++		result = l->aligned = (char *) result + alignment - adj;
++	}
++DONE:
++
++	return result;
++}
++
++void * valloc (size_t size);
++void * valloc (size_t size) {
++	return memalign(PAGE_SIZE, size);
++}
================================================================


More information about the pld-cvs-commit mailing list