1 # --- T2-COPYRIGHT-NOTE-BEGIN ---
2 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 # T2 SDE: package/.../dietlibc/memalign.patch
5 # Copyright (C) 2011 The T2 SDE Project
7 # More information can be found in the files COPYING and README.
9 # This patch file is dual-licensed. It is available under the license the
10 # patched project is licensed under, as long as it is an OpenSource license
11 # as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
12 # of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
15 # --- T2-COPYRIGHT-NOTE-END ---
17 diff -ur dietlibc-0.31/include/stdlib.h dietlibc-0.31-memalign/include/stdlib.h
18 --- dietlibc-0.31/include/stdlib.h 2009-03-19 15:39:48.000000000 +0100
19 +++ dietlibc-0.31-memalign/include/stdlib.h 2009-03-19 15:39:37.000000000 +0100
21 void *malloc(size_t size) __THROW __attribute_malloc__;
22 void free(void *ptr) __THROW;
23 void *realloc(void *ptr, size_t size) __THROW __attribute_malloc__;
24 +void *memalign(size_t alignment, size_t size) __THROW __attribute_malloc__;
25 +int posix_memalign(void **memptr, size_t alignment, size_t size) __THROW __attribute_malloc__;
26 +void *valloc(size_t size) __THROW __attribute_malloc__;
28 char *getenv(const char *name) __THROW __pure;
29 int putenv(const char *string) __THROW;
30 diff -ur dietlibc-0.31/lib/alloc.c dietlibc-0.31-memalign/lib/alloc.c
31 --- dietlibc-0.31/lib/alloc.c 2009-03-19 15:39:48.000000000 +0100
32 +++ dietlibc-0.31-memalign/lib/alloc.c 2009-03-19 15:38:33.000000000 +0100
36 /* -- PUBLIC FUNCTIONS ---------------------------------------------------- */
38 +int __libc_free_aligned(void *ptr);
39 static void _alloc_libc_free(void *ptr) {
44 + if (__libc_free_aligned(ptr))
47 size=((__alloc_t*)BLOCK_START(ptr))->size;
49 if (size<=__MAX_SMALL_SIZE)
52 munmap(BLOCK_START(ptr),size);
56 void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free")));
57 void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free")));
60 void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc")));
62 +/* List of blocks allocated with memalign or valloc */
64 + struct alignlist *next;
65 + void *aligned; /* The address that memaligned returned. */
66 + void *exact; /* The address that malloc returned. */
68 +struct alignlist *_aligned_blocks;
70 +/* Return memory to the heap. */
71 +int __libc_free_aligned(void *ptr);
72 +int __libc_free_aligned(void *ptr) {
73 + struct alignlist *l;
74 + register size_t size;
79 + for (l = _aligned_blocks; l != NULL; l = l->next) {
80 + if (l->aligned == ptr) {
81 + size=((__alloc_t*)BLOCK_START(l->exact))->size;
83 + if (size<=__MAX_SMALL_SIZE)
84 + __small_free(l->exact,size);
86 + munmap(BLOCK_START(l->exact),size);
88 + /* Mark the block as free */
96 +void * memalign (size_t alignment, size_t size);
97 +void * memalign (size_t alignment, size_t size) {
99 + unsigned long int adj;
101 + result = malloc (size + alignment - 1);
102 + if (result == NULL)
105 + adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
107 + struct alignlist *l;
108 + for (l = _aligned_blocks; l != NULL; l = l->next)
109 + if (l->aligned == NULL)
110 + /* This slot is free. Use it. */
113 + l = (struct alignlist *) malloc (sizeof (struct alignlist));
115 + _alloc_libc_free(result);
119 + l->next = _aligned_blocks;
120 + _aligned_blocks = l;
123 + result = l->aligned = (char *) result + alignment - adj;
130 +int posix_memalign(void **memptr, size_t alignment, size_t size);
131 +int posix_memalign(void **memptr, size_t alignment, size_t size)
133 + if(alignment % sizeof(void*) != 0) return EINVAL;
134 + *memptr = memalign(alignment, size);
135 + return (*memptr != NULL) ? 0 : ENOMEM;
138 +void * valloc (size_t size);
139 +void * valloc (size_t size) {
140 + return memalign(PAGE_SIZE, size);