]>
Commit | Line | Data |
---|---|---|
7ab3114f JR |
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 | |
4 | @@ -13,6 +13,8 @@ | |
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__; | |
10 | ||
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 | |
16 | @@ -128,10 +128,14 @@ | |
17 | } | |
18 | ||
19 | /* -- PUBLIC FUNCTIONS ---------------------------------------------------- */ | |
20 | - | |
21 | +int __libc_free_aligned(void *ptr); | |
22 | static void _alloc_libc_free(void *ptr) { | |
23 | register size_t size; | |
24 | - if (ptr) { | |
25 | + if (ptr == NULL) | |
26 | + return; | |
27 | + if (__libc_free_aligned(ptr)) | |
28 | + return; | |
29 | + | |
30 | size=((__alloc_t*)BLOCK_START(ptr))->size; | |
31 | if (size) { | |
32 | if (size<=__MAX_SMALL_SIZE) | |
33 | @@ -139,7 +143,6 @@ | |
34 | else | |
35 | munmap(BLOCK_START(ptr),size); | |
36 | } | |
37 | - } | |
38 | } | |
39 | void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free"))); | |
40 | void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free"))); | |
41 | @@ -268,3 +249,75 @@ | |
42 | } | |
43 | void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc"))); | |
44 | ||
45 | +/* List of blocks allocated with memalign or valloc */ | |
46 | +struct alignlist { | |
47 | + struct alignlist *next; | |
48 | + void *aligned; /* The address that memaligned returned. */ | |
49 | + void *exact; /* The address that malloc returned. */ | |
50 | +}; | |
51 | +struct alignlist *_aligned_blocks; | |
52 | + | |
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; | |
58 | + | |
59 | + if (ptr == NULL) | |
60 | + return 0; | |
61 | + | |
62 | + for (l = _aligned_blocks; l != NULL; l = l->next) { | |
63 | + if (l->aligned == ptr) { | |
64 | + size=((__alloc_t*)BLOCK_START(l->exact))->size; | |
65 | + if (size) { | |
66 | + if (size<=__MAX_SMALL_SIZE) | |
67 | + __small_free(l->exact,size); | |
68 | + else | |
69 | + munmap(BLOCK_START(l->exact),size); | |
70 | + } | |
71 | + /* Mark the block as free */ | |
72 | + l->aligned = NULL; | |
73 | + return 1; | |
74 | + } | |
75 | + } | |
76 | + return 0; | |
77 | +} | |
78 | + | |
79 | +void * memalign (size_t alignment, size_t size); | |
80 | +void * memalign (size_t alignment, size_t size) { | |
81 | + void * result; | |
82 | + unsigned long int adj; | |
83 | + | |
84 | + result = malloc (size + alignment - 1); | |
85 | + if (result == NULL) | |
86 | + return NULL; | |
87 | + | |
88 | + adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment; | |
89 | + if (adj != 0) { | |
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. */ | |
94 | + break; | |
95 | + if (l == NULL) { | |
96 | + l = (struct alignlist *) malloc (sizeof (struct alignlist)); | |
97 | + if (l == NULL) { | |
98 | + _alloc_libc_free(result); | |
99 | + result = NULL; | |
100 | + goto DONE; | |
101 | + } | |
102 | + l->next = _aligned_blocks; | |
103 | + _aligned_blocks = l; | |
104 | + } | |
105 | + l->exact = result; | |
106 | + result = l->aligned = (char *) result + alignment - adj; | |
107 | + } | |
108 | +DONE: | |
109 | + | |
110 | + return result; | |
111 | +} | |
112 | + | |
113 | +void * valloc (size_t size); | |
114 | +void * valloc (size_t size) { | |
115 | + return memalign(PAGE_SIZE, size); | |
116 | +} |