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 +int posix_memalign(void **memptr, size_t alignment, size_t size) __THROW __attribute_malloc__;
10 +void *valloc(size_t size) __THROW __attribute_malloc__;
12 char *getenv(const char *name) __THROW __pure;
13 int putenv(const char *string) __THROW;
14 diff -ur dietlibc-0.31/lib/alloc.c dietlibc-0.31-memalign/lib/alloc.c
15 --- dietlibc-0.31/lib/alloc.c 2009-03-19 15:39:48.000000000 +0100
16 +++ dietlibc-0.31-memalign/lib/alloc.c 2009-03-19 15:38:33.000000000 +0100
20 /* -- PUBLIC FUNCTIONS ---------------------------------------------------- */
22 +int __libc_free_aligned(void *ptr);
23 static void _alloc_libc_free(void *ptr) {
28 + if (__libc_free_aligned(ptr))
31 size=((__alloc_t*)BLOCK_START(ptr))->size;
33 if (size<=__MAX_SMALL_SIZE)
36 munmap(BLOCK_START(ptr),size);
40 void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free")));
41 void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free")));
44 void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc")));
46 +/* List of blocks allocated with memalign or valloc */
48 + struct alignlist *next;
49 + void *aligned; /* The address that memaligned returned. */
50 + void *exact; /* The address that malloc returned. */
52 +struct alignlist *_aligned_blocks;
54 +/* Return memory to the heap. */
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) {
81 + unsigned long int adj;
83 + result = malloc (size + alignment - 1);
87 + adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
89 + struct alignlist *l;
90 + for (l = _aligned_blocks; l != NULL; l = l->next)
91 + if (l->aligned == NULL)
92 + /* This slot is free. Use it. */
95 + l = (struct alignlist *) malloc (sizeof (struct alignlist));
97 + _alloc_libc_free(result);
101 + l->next = _aligned_blocks;
102 + _aligned_blocks = l;
105 + result = l->aligned = (char *) result + alignment - adj;
112 +int posix_memalign(void **memptr, size_t alignment, size_t size)
114 + if(alignment % sizeof(void*) != 0) return EINVAL;
115 + *memptr = memalign(alignment, size);
116 + return (*memptr != NULL) ? 0 : ENOMEM;
119 +void * valloc (size_t size) {
120 + return memalign(__DIET_PAGE_SIZE, size);