Changeset 13f2461 in mainline
- Timestamp:
- 2011-05-21T15:45:48Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 9d47440, e06e2716
- Parents:
- faeb7cc
- Location:
- uspace/lib/c
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/assert.c
rfaeb7cc r13f2461 35 35 #include <io/klog.h> 36 36 #include <stdlib.h> 37 #include <atomic.h> 37 38 #include <stacktrace.h> 38 39 39 void assert_abort(const char *cond, const char *file, unsigned int line) 40 #define MSG_START "Assertion failed (" 41 #define MSG_FILE ") in file \"" 42 #define MSG_LINE "\", line " 43 #define MSG_END ".\n" 44 45 static atomic_t failed_asserts; 46 47 void assert_abort(const char *cond, const char *file, const char *line) 40 48 { 41 printf("Assertion failed (%s) in file \"%s\", line %u.\n", 49 /* 50 * Send the message safely to klog. Nested asserts should not occur. 51 */ 52 klog_write(MSG_START, str_size(MSG_START)); 53 klog_write(cond, str_size(cond)); 54 klog_write(MSG_FILE, str_size(MSG_FILE)); 55 klog_write(file, str_size(file)); 56 klog_write(MSG_LINE, str_size(MSG_LINE)); 57 klog_write(line, str_size(line)); 58 klog_write(MSG_END, str_size(MSG_END)); 59 60 /* 61 * Check if this is a nested or parallel assert. 62 */ 63 if (atomic_postinc(&failed_asserts)) 64 abort(); 65 66 /* 67 * Attempt to print the message to standard output and display 68 * the stack trace. These operations can theoretically trigger nested 69 * assertions. 70 */ 71 printf(MSG_START "%s" MSG_FILE "%s" MSG_LINE "%s" MSG_END, 42 72 cond, file, line); 43 73 stacktrace_print(); 44 abort();45 }46 74 47 void assert_static_abort(const char *msg)48 {49 klog_write(msg, str_size(msg));50 75 abort(); 51 76 } -
uspace/lib/c/generic/malloc.c
rfaeb7cc r13f2461 188 188 static futex_t malloc_futex = FUTEX_INITIALIZER; 189 189 190 #ifndef NDEBUG 191 192 #define malloc_assert(expr) \ 193 do { \ 194 if (!(expr)) {\ 195 futex_up(&malloc_futex); \ 196 assert_abort(#expr, __FILE__, STR2(__LINE__)); \ 197 } \ 198 } while (0) 199 200 #else /* NDEBUG */ 201 202 #define malloc_assert(expr) 203 204 #endif /* NDEBUG */ 205 190 206 /** Initialize a heap block 191 207 * … … 228 244 heap_block_head_t *head = (heap_block_head_t *) addr; 229 245 230 assert_static(head->magic == HEAP_BLOCK_HEAD_MAGIC);246 malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC); 231 247 232 248 heap_block_foot_t *foot = BLOCK_FOOT(head); 233 249 234 assert_static(foot->magic == HEAP_BLOCK_FOOT_MAGIC);235 assert_static(head->size == foot->size);250 malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC); 251 malloc_assert(head->size == foot->size); 236 252 } 237 253 … … 247 263 heap_area_t *area = (heap_area_t *) addr; 248 264 249 assert_static(area->magic == HEAP_AREA_MAGIC);250 assert_static(addr == area->start);251 assert_static(area->start < area->end);252 assert_static(((uintptr_t) area->start % PAGE_SIZE) == 0);253 assert_static(((uintptr_t) area->end % PAGE_SIZE) == 0);265 malloc_assert(area->magic == HEAP_AREA_MAGIC); 266 malloc_assert(addr == area->start); 267 malloc_assert(area->start < area->end); 268 malloc_assert(((uintptr_t) area->start % PAGE_SIZE) == 0); 269 malloc_assert(((uintptr_t) area->end % PAGE_SIZE) == 0); 254 270 } 255 271 … … 382 398 383 399 block_check((void *) last_head); 384 assert_static(last_head->area == area);400 malloc_assert(last_head->area == area); 385 401 386 402 if (last_head->free) { … … 395 411 396 412 block_check((void *) first_head); 397 assert_static(first_head->area == area);413 malloc_assert(first_head->area == area); 398 414 399 415 size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE); … … 497 513 static void split_mark(heap_block_head_t *cur, const size_t size) 498 514 { 499 assert_static(cur->size >= size);515 malloc_assert(cur->size >= size); 500 516 501 517 /* See if we should split the block. */ … … 533 549 { 534 550 area_check((void *) area); 535 assert_static((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));536 assert_static((void *) first_block < area->end);551 malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 552 malloc_assert((void *) first_block < area->end); 537 553 538 554 for (heap_block_head_t *cur = first_block; (void *) cur < area->end; … … 661 677 static void *malloc_internal(const size_t size, const size_t align) 662 678 { 663 assert_static(first_heap_area != NULL);679 malloc_assert(first_heap_area != NULL); 664 680 665 681 if (align == 0) … … 786 802 787 803 block_check(head); 788 assert_static(!head->free);804 malloc_assert(!head->free); 789 805 790 806 heap_area_t *area = head->area; 791 807 792 808 area_check(area); 793 assert_static((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));794 assert_static((void *) head < area->end);809 malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 810 malloc_assert((void *) head < area->end); 795 811 796 812 void *ptr = NULL; … … 863 879 864 880 block_check(head); 865 assert_static(!head->free);881 malloc_assert(!head->free); 866 882 867 883 heap_area_t *area = head->area; 868 884 869 885 area_check(area); 870 assert_static((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));871 assert_static((void *) head < area->end);886 malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 887 malloc_assert((void *) head < area->end); 872 888 873 889 /* Mark the block itself as free. */ -
uspace/lib/c/include/assert.h
rfaeb7cc r13f2461 47 47 */ 48 48 49 #define STR(l) #l 50 #define STR2(l) STR(l) 51 49 52 #ifndef NDEBUG 50 53 … … 52 55 do { \ 53 56 if (!(expr)) \ 54 assert_abort(#expr, __FILE__, __LINE__); \ 55 } while (0) 56 57 #define assert_static(expr) \ 58 do { \ 59 if (!(expr)) \ 60 assert_static_abort("Assertion failed (" #expr \ 61 ") in file \"" __FILE__ "\".\n"); \ 57 assert_abort(#expr, __FILE__, STR2(__LINE__)); \ 62 58 } while (0) 63 59 … … 65 61 66 62 #define assert(expr) 67 #define assert_static(expr)68 63 69 64 #endif /* NDEBUG */ 70 65 71 extern void assert_abort(const char *, const char *, unsigned int)66 extern void assert_abort(const char *, const char *, const char *) 72 67 __attribute__((noreturn)); 73 extern void assert_static_abort(const char *);74 68 75 69
Note:
See TracChangeset
for help on using the changeset viewer.