Changes in uspace/lib/c/generic/malloc.c [6aeca0d:21799398] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/malloc.c
r6aeca0d r21799398 200 200 do { \ 201 201 if (!(expr)) {\ 202 heap_unlock(); \202 futex_up(&malloc_futex); \ 203 203 assert_abort(#expr, __FILE__, __LINE__); \ 204 204 } \ … … 210 210 211 211 #endif /* NDEBUG */ 212 213 214 #ifdef FUTEX_UPGRADABLE215 /** True if the heap may be accessed from multiple threads. */216 static bool multithreaded = false;217 218 /** Makes accesses to the heap thread safe. */219 void malloc_enable_multithreaded(void)220 {221 multithreaded = true;222 }223 224 /** Serializes access to the heap from multiple threads. */225 static inline void heap_lock(void)226 {227 if (multithreaded) {228 futex_down(&malloc_futex);229 } else {230 /*231 * Malloc never switches fibrils while the heap is locked.232 * Similarly, it never creates new threads from within the233 * locked region. Therefore, if there are no other threads234 * except this one, the whole operation will complete without235 * any interruptions.236 */237 }238 }239 240 /** Serializes access to the heap from multiple threads. */241 static inline void heap_unlock(void)242 {243 if (multithreaded) {244 futex_up(&malloc_futex);245 } else {246 /*247 * Malloc never switches fibrils while the heap is locked.248 * Similarly, it never creates new threads from within the249 * locked region. Therefore, if there are no other threads250 * except this one, the whole operation will complete without251 * any interruptions.252 */253 }254 }255 256 #else257 258 /** Makes accesses to the heap thread safe. */259 void malloc_enable_multithreaded(void)260 {261 /* No-op. Already using thread-safe heap locking operations. */262 }263 264 /** Serializes access to the heap from multiple threads. */265 static inline void heap_lock(void)266 {267 futex_down(&malloc_futex);268 }269 270 /** Serializes access to the heap from multiple threads. */271 static inline void heap_unlock(void)272 {273 futex_up(&malloc_futex);274 }275 #endif276 277 212 278 213 /** Initialize a heap block … … 354 289 size_t asize = ALIGN_UP(size, PAGE_SIZE); 355 290 void *astart = as_area_create(AS_AREA_ANY, asize, 356 AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE , AS_AREA_UNPAGED);291 AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE); 357 292 if (astart == AS_MAP_FAILED) 358 293 return false; … … 850 785 void *malloc(const size_t size) 851 786 { 852 heap_lock();787 futex_down(&malloc_futex); 853 788 void *block = malloc_internal(size, BASE_ALIGN); 854 heap_unlock();855 789 futex_up(&malloc_futex); 790 856 791 return block; 857 792 } … … 872 807 size_t palign = 873 808 1 << (fnzb(max(sizeof(void *), align) - 1) + 1); 874 875 heap_lock();809 810 futex_down(&malloc_futex); 876 811 void *block = malloc_internal(size, palign); 877 heap_unlock();878 812 futex_up(&malloc_futex); 813 879 814 return block; 880 815 } … … 893 828 return malloc(size); 894 829 895 heap_lock();830 futex_down(&malloc_futex); 896 831 897 832 /* Calculate the position of the header. */ … … 950 885 } 951 886 952 heap_unlock();887 futex_up(&malloc_futex); 953 888 954 889 if (reloc) { … … 973 908 return; 974 909 975 heap_lock();910 futex_down(&malloc_futex); 976 911 977 912 /* Calculate the position of the header. */ … … 1018 953 heap_shrink(area); 1019 954 1020 heap_unlock();955 futex_up(&malloc_futex); 1021 956 } 1022 957 1023 958 void *heap_check(void) 1024 959 { 1025 heap_lock();960 futex_down(&malloc_futex); 1026 961 1027 962 if (first_heap_area == NULL) { 1028 heap_unlock();963 futex_up(&malloc_futex); 1029 964 return (void *) -1; 1030 965 } … … 1040 975 (((uintptr_t) area->start % PAGE_SIZE) != 0) || 1041 976 (((uintptr_t) area->end % PAGE_SIZE) != 0)) { 1042 heap_unlock();977 futex_up(&malloc_futex); 1043 978 return (void *) area; 1044 979 } … … 1051 986 /* Check heap block consistency */ 1052 987 if (head->magic != HEAP_BLOCK_HEAD_MAGIC) { 1053 heap_unlock();988 futex_up(&malloc_futex); 1054 989 return (void *) head; 1055 990 } … … 1059 994 if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) || 1060 995 (head->size != foot->size)) { 1061 heap_unlock();996 futex_up(&malloc_futex); 1062 997 return (void *) foot; 1063 998 } … … 1065 1000 } 1066 1001 1067 heap_unlock();1002 futex_up(&malloc_futex); 1068 1003 1069 1004 return NULL;
Note:
See TracChangeset
for help on using the changeset viewer.