Changes in uspace/lib/c/generic/malloc.c [21799398:6aeca0d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/malloc.c
r21799398 r6aeca0d 200 200 do { \ 201 201 if (!(expr)) {\ 202 futex_up(&malloc_futex); \202 heap_unlock(); \ 203 203 assert_abort(#expr, __FILE__, __LINE__); \ 204 204 } \ … … 210 210 211 211 #endif /* NDEBUG */ 212 213 214 #ifdef FUTEX_UPGRADABLE 215 /** 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 the 233 * locked region. Therefore, if there are no other threads 234 * except this one, the whole operation will complete without 235 * 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 the 249 * locked region. Therefore, if there are no other threads 250 * except this one, the whole operation will complete without 251 * any interruptions. 252 */ 253 } 254 } 255 256 #else 257 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 #endif 276 212 277 213 278 /** Initialize a heap block … … 289 354 size_t asize = ALIGN_UP(size, PAGE_SIZE); 290 355 void *astart = as_area_create(AS_AREA_ANY, asize, 291 AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE );356 AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE, AS_AREA_UNPAGED); 292 357 if (astart == AS_MAP_FAILED) 293 358 return false; … … 785 850 void *malloc(const size_t size) 786 851 { 787 futex_down(&malloc_futex);852 heap_lock(); 788 853 void *block = malloc_internal(size, BASE_ALIGN); 789 futex_up(&malloc_futex);790 854 heap_unlock(); 855 791 856 return block; 792 857 } … … 807 872 size_t palign = 808 873 1 << (fnzb(max(sizeof(void *), align) - 1) + 1); 809 810 futex_down(&malloc_futex);874 875 heap_lock(); 811 876 void *block = malloc_internal(size, palign); 812 futex_up(&malloc_futex);813 877 heap_unlock(); 878 814 879 return block; 815 880 } … … 828 893 return malloc(size); 829 894 830 futex_down(&malloc_futex);895 heap_lock(); 831 896 832 897 /* Calculate the position of the header. */ … … 885 950 } 886 951 887 futex_up(&malloc_futex);952 heap_unlock(); 888 953 889 954 if (reloc) { … … 908 973 return; 909 974 910 futex_down(&malloc_futex);975 heap_lock(); 911 976 912 977 /* Calculate the position of the header. */ … … 953 1018 heap_shrink(area); 954 1019 955 futex_up(&malloc_futex);1020 heap_unlock(); 956 1021 } 957 1022 958 1023 void *heap_check(void) 959 1024 { 960 futex_down(&malloc_futex);1025 heap_lock(); 961 1026 962 1027 if (first_heap_area == NULL) { 963 futex_up(&malloc_futex);1028 heap_unlock(); 964 1029 return (void *) -1; 965 1030 } … … 975 1040 (((uintptr_t) area->start % PAGE_SIZE) != 0) || 976 1041 (((uintptr_t) area->end % PAGE_SIZE) != 0)) { 977 futex_up(&malloc_futex);1042 heap_unlock(); 978 1043 return (void *) area; 979 1044 } … … 986 1051 /* Check heap block consistency */ 987 1052 if (head->magic != HEAP_BLOCK_HEAD_MAGIC) { 988 futex_up(&malloc_futex);1053 heap_unlock(); 989 1054 return (void *) head; 990 1055 } … … 994 1059 if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) || 995 1060 (head->size != foot->size)) { 996 futex_up(&malloc_futex);1061 heap_unlock(); 997 1062 return (void *) foot; 998 1063 } … … 1000 1065 } 1001 1066 1002 futex_up(&malloc_futex);1067 heap_unlock(); 1003 1068 1004 1069 return NULL;
Note:
See TracChangeset
for help on using the changeset viewer.