Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 955f2a5 in mainline


Ignore:
Timestamp:
2011-05-21T11:26:23Z (11 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master
Children:
1b3e854
Parents:
0c33b1d5
Message:

implement assert_static() as a safer replacement to malloc_assert()

Location:
uspace/lib/c
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/assert.c

    r0c33b1d5 r955f2a5  
    3333#include <assert.h>
    3434#include <stdio.h>
     35#include <io/klog.h>
    3536#include <stdlib.h>
    3637#include <stacktrace.h>
     
    4445}
    4546
     47void assert_static_abort(const char *msg)
     48{
     49        klog_write(msg, str_size(msg));
     50        abort();
     51}
     52
    4653/** @}
    4754 */
  • uspace/lib/c/generic/malloc.c

    r0c33b1d5 r955f2a5  
    119119            (((uintptr_t) (head)) + (head)->size - sizeof(heap_block_foot_t)))
    120120
    121 #define malloc_assert(expr) \
    122         do { \
    123                 if (!(expr)) {\
    124                         futex_up(&malloc_futex); \
    125                         assert((expr)); \
    126                 } \
    127         } while (0)
    128 
    129 
    130121/** Heap area.
    131122 *
     
    237228        heap_block_head_t *head = (heap_block_head_t *) addr;
    238229       
    239         malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC);
     230        assert_static(head->magic == HEAP_BLOCK_HEAD_MAGIC);
    240231       
    241232        heap_block_foot_t *foot = BLOCK_FOOT(head);
    242233       
    243         malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC);
    244         malloc_assert(head->size == foot->size);
     234        assert_static(foot->magic == HEAP_BLOCK_FOOT_MAGIC);
     235        assert_static(head->size == foot->size);
    245236}
    246237
     
    256247        heap_area_t *area = (heap_area_t *) addr;
    257248       
    258         malloc_assert(area->magic == HEAP_AREA_MAGIC);
    259         malloc_assert(addr == area->start);
    260         malloc_assert(area->start < area->end);
    261         malloc_assert(((uintptr_t) area->start % PAGE_SIZE) == 0);
    262         malloc_assert(((uintptr_t) area->end % PAGE_SIZE) == 0);
     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);
    263254}
    264255
     
    391382       
    392383        block_check((void *) last_head);
    393         malloc_assert(last_head->area == area);
     384        assert_static(last_head->area == area);
    394385       
    395386        if (last_head->free) {
     
    404395               
    405396                block_check((void *) first_head);
    406                 malloc_assert(first_head->area == area);
     397                assert_static(first_head->area == area);
    407398               
    408399                size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE);
     
    506497static void split_mark(heap_block_head_t *cur, const size_t size)
    507498{
    508         malloc_assert(cur->size >= size);
     499        assert_static(cur->size >= size);
    509500       
    510501        /* See if we should split the block. */
     
    542533{
    543534        area_check((void *) area);
    544         malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    545         malloc_assert((void *) first_block < area->end);
     535        assert_static((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
     536        assert_static((void *) first_block < area->end);
    546537       
    547538        for (heap_block_head_t *cur = first_block; (void *) cur < area->end;
     
    670661static void *malloc_internal(const size_t size, const size_t align)
    671662{
    672         malloc_assert(first_heap_area != NULL);
     663        assert_static(first_heap_area != NULL);
    673664       
    674665        if (align == 0)
     
    795786       
    796787        block_check(head);
    797         malloc_assert(!head->free);
     788        assert_static(!head->free);
    798789       
    799790        heap_area_t *area = head->area;
    800791       
    801792        area_check(area);
    802         malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    803         malloc_assert((void *) head < area->end);
     793        assert_static((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
     794        assert_static((void *) head < area->end);
    804795       
    805796        void *ptr = NULL;
     
    872863       
    873864        block_check(head);
    874         malloc_assert(!head->free);
     865        assert_static(!head->free);
    875866       
    876867        heap_area_t *area = head->area;
    877868       
    878869        area_check(area);
    879         malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    880         malloc_assert((void *) head < area->end);
     870        assert_static((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
     871        assert_static((void *) head < area->end);
    881872       
    882873        /* Mark the block itself as free. */
  • uspace/lib/c/include/assert.h

    r0c33b1d5 r955f2a5  
    5555        } while (0)
    5656
     57#define assert_static(expr) \
     58        do { \
     59                if (!(expr)) \
     60                        assert_static_abort("Assertion failed (" #expr \
     61                            ") in file \"" __FILE__ "\".\n"); \
     62        } while (0)
     63
    5764#else /* NDEBUG */
    5865
    5966#define assert(expr)
     67#define assert_static(expr)
    6068
    6169#endif /* NDEBUG */
     
    6371extern void assert_abort(const char *, const char *, unsigned int)
    6472    __attribute__((noreturn));
     73extern void assert_static_abort(const char *);
     74
    6575
    6676#endif
Note: See TracChangeset for help on using the changeset viewer.