1 diff -ur dietlibc-0.31/include/stdlib.h dietlibc-0.31-memalign/include/stdlib.h
2 --- dietlibc-0.31/include/stdlib.h 2009-03-19 15:39:48.000000000 +0100
3 +++ dietlibc-0.31-memalign/include/stdlib.h 2009-03-19 15:39:37.000000000 +0100
5 void *malloc(size_t size) __THROW __attribute_malloc__;
6 void free(void *ptr) __THROW;
7 void *realloc(void *ptr, size_t size) __THROW __attribute_malloc__;
8 +void *memalign(size_t alignment, size_t size) __THROW __attribute_malloc__;
9 +void *valloc(size_t size) __THROW __attribute_malloc__;
11 char *getenv(const char *name) __THROW __pure;
12 int putenv(const char *string) __THROW;
13 diff -ur dietlibc-0.31/lib/alloc.c dietlibc-0.31-memalign/lib/alloc.c
14 --- dietlibc-0.31/lib/alloc.c 2009-03-19 15:39:48.000000000 +0100
15 +++ dietlibc-0.31-memalign/lib/alloc.c 2009-03-19 15:38:33.000000000 +0100
19 /* -- PUBLIC FUNCTIONS ---------------------------------------------------- */
21 +int __libc_free_aligned(void *ptr);
22 static void _alloc_libc_free(void *ptr) {
27 + if (__libc_free_aligned(ptr))
30 size=((__alloc_t*)BLOCK_START(ptr))->size;
32 if (size<=__MAX_SMALL_SIZE)
35 munmap(BLOCK_START(ptr),size);
39 void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free")));
40 void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free")));
43 void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc")));
45 +/* List of blocks allocated with memalign or valloc */
47 + struct alignlist *next;
48 + void *aligned; /* The address that memaligned returned. */
49 + void *exact; /* The address that malloc returned. */
51 +struct alignlist *_aligned_blocks;
53 +/* Return memory to the heap. */
54 +int __libc_free_aligned(void *ptr);
55 +int __libc_free_aligned(void *ptr) {
56 + struct alignlist *l;
57 + register size_t size;
62 + for (l = _aligned_blocks; l != NULL; l = l->next) {
63 + if (l->aligned == ptr) {
64 + size=((__alloc_t*)BLOCK_START(l->exact))->size;
66 + if (size<=__MAX_SMALL_SIZE)
67 + __small_free(l->exact,size);
69 + munmap(BLOCK_START(l->exact),size);
71 + /* Mark the block as free */
79 +void * memalign (size_t alignment, size_t size);
80 +void * memalign (size_t alignment, size_t size) {
82 + unsigned long int adj;
84 + result = malloc (size + alignment - 1);
88 + adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
90 + struct alignlist *l;
91 + for (l = _aligned_blocks; l != NULL; l = l->next)
92 + if (l->aligned == NULL)
93 + /* This slot is free. Use it. */
96 + l = (struct alignlist *) malloc (sizeof (struct alignlist));
98 + _alloc_libc_free(result);
102 + l->next = _aligned_blocks;
103 + _aligned_blocks = l;
106 + result = l->aligned = (char *) result + alignment - adj;
113 +void * valloc (size_t size);
114 +void * valloc (size_t size) {
115 + return memalign(PAGE_SIZE, size);