]>
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 | |
0aadcf56 | 4 | @@ -13,6 +13,9 @@ |
7ab3114f JR |
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__; | |
0aadcf56 | 9 | +int posix_memalign(void **memptr, size_t alignment, size_t size) __THROW __attribute_malloc__; |
7ab3114f JR |
10 | +void *valloc(size_t size) __THROW __attribute_malloc__; |
11 | ||
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 | |
17 | @@ -128,10 +128,14 @@ | |
18 | } | |
19 | ||
20 | /* -- PUBLIC FUNCTIONS ---------------------------------------------------- */ | |
21 | - | |
22 | +int __libc_free_aligned(void *ptr); | |
23 | static void _alloc_libc_free(void *ptr) { | |
24 | register size_t size; | |
25 | - if (ptr) { | |
26 | + if (ptr == NULL) | |
27 | + return; | |
28 | + if (__libc_free_aligned(ptr)) | |
29 | + return; | |
30 | + | |
31 | size=((__alloc_t*)BLOCK_START(ptr))->size; | |
32 | if (size) { | |
33 | if (size<=__MAX_SMALL_SIZE) | |
34 | @@ -139,7 +143,6 @@ | |
35 | else | |
36 | munmap(BLOCK_START(ptr),size); | |
37 | } | |
38 | - } | |
39 | } | |
40 | void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free"))); | |
41 | void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free"))); | |
b2599875 | 42 | @@ -268,3 +249,79 @@ |
7ab3114f JR |
43 | } |
44 | void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc"))); | |
45 | ||
46 | +/* List of blocks allocated with memalign or valloc */ | |
47 | +struct alignlist { | |
48 | + struct alignlist *next; | |
49 | + void *aligned; /* The address that memaligned returned. */ | |
50 | + void *exact; /* The address that malloc returned. */ | |
51 | +}; | |
52 | +struct alignlist *_aligned_blocks; | |
53 | + | |
54 | +/* Return memory to the heap. */ | |
7ab3114f JR |
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 | + | |
7ab3114f JR |
79 | +void * memalign (size_t alignment, size_t size) { |
80 | + void * result; | |
81 | + unsigned long int adj; | |
82 | + | |
83 | + result = malloc (size + alignment - 1); | |
84 | + if (result == NULL) | |
85 | + return NULL; | |
86 | + | |
87 | + adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment; | |
88 | + if (adj != 0) { | |
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. */ | |
93 | + break; | |
94 | + if (l == NULL) { | |
95 | + l = (struct alignlist *) malloc (sizeof (struct alignlist)); | |
96 | + if (l == NULL) { | |
97 | + _alloc_libc_free(result); | |
98 | + result = NULL; | |
99 | + goto DONE; | |
100 | + } | |
101 | + l->next = _aligned_blocks; | |
102 | + _aligned_blocks = l; | |
103 | + } | |
104 | + l->exact = result; | |
105 | + result = l->aligned = (char *) result + alignment - adj; | |
106 | + } | |
107 | +DONE: | |
108 | + | |
109 | + return result; | |
110 | +} | |
111 | + | |
0aadcf56 JB |
112 | +int posix_memalign(void **memptr, size_t alignment, size_t size) |
113 | +{ | |
114 | + if(alignment % sizeof(void*) != 0) return EINVAL; | |
115 | + *memptr = memalign(alignment, size); | |
116 | + return (*memptr != NULL) ? 0 : ENOMEM; | |
117 | +} | |
118 | + | |
7ab3114f | 119 | +void * valloc (size_t size) { |
d18630a9 | 120 | + return memalign(PAGE_SIZE, size); |
7ab3114f | 121 | +} |