Dash:
[t2.git] / package / base / dietlibc / memalign.patch
blobaece86720d8469ad4ddcad29f4a4d517714e2e8a
1 # --- T2-COPYRIGHT-NOTE-BEGIN ---
2 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
3 #
4 # T2 SDE: package/.../dietlibc/memalign.patch
5 # Copyright (C) 2011 The T2 SDE Project
6 #
7 # More information can be found in the files COPYING and README.
8 #
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
14 # version.
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
20 @@ -13,6 +13,9 @@
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
33 @@ -128,10 +128,14 @@
36 /* -- PUBLIC FUNCTIONS ---------------------------------------------------- */
38 +int __libc_free_aligned(void *ptr);
39 static void _alloc_libc_free(void *ptr) {
40 register size_t size;
41 - if (ptr) {
42 + if (ptr == NULL)
43 + return;
44 + if (__libc_free_aligned(ptr))
45 + return;
47 size=((__alloc_t*)BLOCK_START(ptr))->size;
48 if (size) {
49 if (size<=__MAX_SMALL_SIZE)
50 @@ -139,7 +143,6 @@
51 else
52 munmap(BLOCK_START(ptr),size);
54 - }
56 void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free")));
57 void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free")));
58 @@ -268,3 +249,83 @@
60 void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc")));
62 +/* List of blocks allocated with memalign or valloc */
63 +struct alignlist {
64 + struct alignlist *next;
65 + void *aligned; /* The address that memaligned returned. */
66 + void *exact; /* The address that malloc returned. */
67 +};
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;
76 + if (ptr == NULL)
77 + return 0;
79 + for (l = _aligned_blocks; l != NULL; l = l->next) {
80 + if (l->aligned == ptr) {
81 + size=((__alloc_t*)BLOCK_START(l->exact))->size;
82 + if (size) {
83 + if (size<=__MAX_SMALL_SIZE)
84 + __small_free(l->exact,size);
85 + else
86 + munmap(BLOCK_START(l->exact),size);
87 + }
88 + /* Mark the block as free */
89 + l->aligned = NULL;
90 + return 1;
91 + }
92 + }
93 + return 0;
96 +void * memalign (size_t alignment, size_t size);
97 +void * memalign (size_t alignment, size_t size) {
98 + void * result;
99 + unsigned long int adj;
101 + result = malloc (size + alignment - 1);
102 + if (result == NULL)
103 + return NULL;
105 + adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
106 + if (adj != 0) {
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. */
111 + break;
112 + if (l == NULL) {
113 + l = (struct alignlist *) malloc (sizeof (struct alignlist));
114 + if (l == NULL) {
115 + _alloc_libc_free(result);
116 + result = NULL;
117 + goto DONE;
119 + l->next = _aligned_blocks;
120 + _aligned_blocks = l;
122 + l->exact = result;
123 + result = l->aligned = (char *) result + alignment - adj;
125 +DONE:
127 + return result;
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);