]> git.pld-linux.org Git - packages/dietlibc.git/commitdiff
- memalign() and valloc() (ripped from uClibc)
authorJan Rękorajski <baggins@pld-linux.org>
Thu, 19 Mar 2009 15:14:39 +0000 (15:14 +0000)
committercvs2git <feedback@pld-linux.org>
Sun, 24 Jun 2012 12:13:13 +0000 (12:13 +0000)
Changed files:
    dietlibc-memalign.patch -> 1.1

dietlibc-memalign.patch [new file with mode: 0644]

diff --git a/dietlibc-memalign.patch b/dietlibc-memalign.patch
new file mode 100644 (file)
index 0000000..9ce6c79
--- /dev/null
@@ -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);
++}
This page took 0.087474 seconds and 4 git commands to generate.