From 88fc433e5dfc3ddc3948d27b78be2e531113fa1e Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 3 Apr 2016 09:27:34 +0000 Subject: MISRA-related fixes. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@9229 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/common/oslib/src/chheap.c | 18 +++++++++++++----- os/common/oslib/src/chmemcore.c | 4 +--- 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'os/common/oslib/src') diff --git a/os/common/oslib/src/chheap.c b/os/common/oslib/src/chheap.c index d49c68386..91d41864a 100644 --- a/os/common/oslib/src/chheap.c +++ b/os/common/oslib/src/chheap.c @@ -66,6 +66,14 @@ #define H_SIZE(hp) ((hp)->used.size) +/* + * Number of pages between two pointers in a MISRA-compatible way. + */ +#define NPAGES(p1, p2) \ + /*lint -save -e9033 [10.8] The cast is safe.*/ \ + ((size_t)((p1) - (p2))) \ + /*lint -restore*/ + /*===========================================================================*/ /* Module exported variables. */ /*===========================================================================*/ @@ -188,7 +196,7 @@ void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align) { /* Pointer aligned to the requested alignment.*/ ahp = (heap_header_t *)MEM_ALIGN_NEXT(H_BLOCK(hp), align) - 1U; - if ((ahp < H_LIMIT(hp)) && (pages <= (size_t)(H_LIMIT(hp) - 1U - ahp))) { + if ((ahp < H_LIMIT(hp)) && (pages <= NPAGES(H_LIMIT(hp), ahp - 1U))) { /* The block is large enough to contain a correctly aligned area of sufficient size.*/ @@ -196,15 +204,15 @@ void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align) { /* The block is not properly aligned, must split it.*/ size_t bpages; - bpages = H_LIMIT(hp) - H_BLOCK(ahp); - H_PAGES(hp) = ahp - H_BLOCK(hp); + bpages = NPAGES(H_LIMIT(hp), H_BLOCK(ahp)); + H_PAGES(hp) = NPAGES(ahp, H_BLOCK(hp)); if (bpages > pages) { /* The block is bigger than required, must split the excess.*/ heap_header_t *fp; /* Creating the excess block.*/ fp = H_BLOCK(ahp) + pages; - H_PAGES(fp) = bpages - pages - 1U; + H_PAGES(fp) = NPAGES(bpages, pages - 1U); /* Linking the excess block.*/ H_NEXT(fp) = H_NEXT(hp); @@ -226,7 +234,7 @@ void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align) { fp = H_BLOCK(hp) + pages; H_NEXT(fp) = H_NEXT(hp); - H_PAGES(fp) = H_LIMIT(hp) - H_BLOCK(fp); + H_PAGES(fp) = NPAGES(H_LIMIT(hp), H_BLOCK(fp)); H_NEXT(qp) = fp; } } diff --git a/os/common/oslib/src/chmemcore.c b/os/common/oslib/src/chmemcore.c index fd90ac971..c094ac2aa 100644 --- a/os/common/oslib/src/chmemcore.c +++ b/os/common/oslib/src/chmemcore.c @@ -114,9 +114,7 @@ void *chCoreAllocAlignedI(size_t size, unsigned align) { size = MEM_ALIGN_NEXT(size, align); p = (uint8_t *)MEM_ALIGN_NEXT(nextmem, align); - /* ---????? lint -save -e9033 [10.8] The cast is safe.*/ - if ((size_t)(endmem - p) < size) { - /* ---????? lint -restore*/ + if (((size_t)endmem - (size_t)p) < size) { return NULL; } nextmem = p + size; -- cgit v1.2.3