static err_t low_level_output(struct netif* netif, struct pbuf* p)
{
    err_t retVal = ERR_CONN;

    if (netif_is_link_up(netif)) {
        pbuf_ref(p);
        panIf.statusCallback(cbIP_NETWORK_ACTIVITY, NULL, NULL, panIf.callbackArg);

        cb_uint32 totSize = cbIP_getDataFrameSize((cbIP_frame*)p);
        UAllocTraits_t t;
        t.flags = 0;
        t.extended = 0;
        cb_uint8* buf = mbed_ualloc(totSize,t);
        MBED_ASSERT(buf != NULL); // Throw away packets if we can not allocate?
        cb_boolean status = cbIP_copyFromDataFrame(buf, (cbIP_frame*)p, totSize, 0);
        MBED_ASSERT(status);
        cb_int32 result = cbBTPAN_reqData(panIf.connHandle,buf,totSize);
        if(result == cbBTPAN_RESULT_OK) {
            retVal = ERR_OK;
            LINK_STATS_INC(link.xmit);
        } else {
            printf("low_level_output - packet dropped\n");
        }
        mbed_ufree(buf);

        return retVal;
    }

    LINK_STATS_INC(link.drop);

    return retVal;
}
示例#2
0
void * mbed_urealloc(void * ptr, size_t bytes, UAllocTraits_t traits)
{
    void * caller = (void*) caller_addr();
    void *newptr = NULL;
    if (ptr == NULL) {
        return mbed_ualloc(bytes, traits);
    }
    if(traits.flags & ~UALLOC_TRAITS_BITMASK) {
        // Traits not supported in urealloc yet
        ualloc_debug(UALLOC_DEBUG_WARNING, "ua c:%p fail\n", caller);
        return NULL;
    }
    uintptr_t ptr_tmp = (uintptr_t) ptr;
    if ((ptr_tmp < (uintptr_t) mbed_sbrk_ptr) &&
            (ptr_tmp >= (uintptr_t)&__mbed_sbrk_start)) {
        newptr = dlrealloc(ptr, bytes);
    } else {
        ualloc_debug(UALLOC_DEBUG_LOG, "uf c:%p m:%p non-heap realloc\n", caller, ptr);
    }

    if(newptr == NULL) {
        ualloc_debug(UALLOC_DEBUG_WARNING, "ur c:%p m0:%p fail\n", caller, ptr);
    } else {
        ualloc_debug(UALLOC_DEBUG_LOG, "ur c:%p m0:%p m1:%p\n", caller, ptr, newptr);
    }
    return newptr;
}
示例#3
0
void app_start(int, char**) {
    MBED_HOSTTEST_TIMEOUT(10);
    MBED_HOSTTEST_SELECT(default);
    MBED_HOSTTEST_DESCRIPTION(ualloc zone test);
    MBED_HOSTTEST_START("UALLOC_ZONE");
    const char * current_test = "none";
    UAllocTraits_t traits;

    uintptr_t heap_start = reinterpret_cast<uintptr_t>(&__mbed_sbrk_start);
    uintptr_t heap_end   = reinterpret_cast<uintptr_t>(&__mbed_krbs_start);

    bool tests_pass = false;

    do {
        uintptr_t sbrk_ptv, krbs_ptv;
        void * ptr;
        // Make sure that stack variables are not on the heap
        current_test = "stack/heap";
        uint32_t testval;
        uintptr_t ptv = reinterpret_cast<uintptr_t>(&testval);
        if ((ptv > heap_start) && (ptv < heap_end)) {
            break;
        }

        // malloc
        current_test = "malloc";
        ptr = malloc(TEST_SIZE_0);
        ptv = reinterpret_cast<uintptr_t>(ptr);
        sbrk_ptv = reinterpret_cast<uintptr_t>(mbed_sbrk_ptr);
        if (!((ptv > heap_start) && (ptv < sbrk_ptv))) {
            break;
        }
        // free
        free(ptr);

        // calloc
        current_test = "calloc";
        ptr = calloc(TEST_SIZE_0,4);
        ptv = reinterpret_cast<uintptr_t>(ptr);
        sbrk_ptv = reinterpret_cast<uintptr_t>(mbed_sbrk_ptr);
        if (!((ptv > heap_start) && (ptv < sbrk_ptv))) {
            break;
        }
        // free
        free(ptr);

        // realloc
        current_test = "realloc";
        // malloc two blocks
        void * ptr0 = malloc(TEST_SIZE_0);
        if (ptr0 == NULL) {break;}
        void * ptr1 = malloc(TEST_SIZE_0);
        if (ptr1 == NULL) {break;}
        // realloc the first one and make sure the pointer moves
        void * ptr2 = realloc(ptr0, 2*TEST_SIZE_0);
        if (ptr2 == NULL) {break;}
        if (ptr2 == ptr0) {break;}
        // free
        free(ptr1);
        free(ptr2);

        // ualloc
        current_test = "ualloc no_flags";
        traits.flags = 0;
        ptr = mbed_ualloc(TEST_SIZE_0, traits);
        ptv = reinterpret_cast<uintptr_t>(ptr);
        sbrk_ptv = reinterpret_cast<uintptr_t>(mbed_sbrk_ptr);
        if (!((ptv > heap_start) && (ptv < sbrk_ptv))) {
            break;
        }
        mbed_ufree(ptr);

        traits.flags = UALLOC_TRAITS_BITMASK;
        ptr = mbed_ualloc(TEST_SIZE_0, traits);
        ptv = reinterpret_cast<uintptr_t>(ptr);
        sbrk_ptv = reinterpret_cast<uintptr_t>(mbed_sbrk_ptr);
        if (!((ptv > heap_start) && (ptv < sbrk_ptv))) {
            break;
        }
        mbed_ufree(ptr);

        current_test = "ualloc never_free";
        traits.flags = UALLOC_TRAITS_NEVER_FREE;
        ptr = mbed_ualloc(TEST_SIZE_0, traits);
        ptv = reinterpret_cast<uintptr_t>(ptr);
        krbs_ptv = reinterpret_cast<uintptr_t>(mbed_krbs_ptr);
        if (!((ptv >= krbs_ptv) && (ptv < heap_end))) {
            break;
        }
        // this call is ignored, but should generate a debug message
        mbed_ufree(ptr);

        current_test = "ualloc zero-fill";
        traits.flags = UALLOC_TRAITS_BITMASK;
        // allocate a block
        ptr0 = mbed_ualloc(TEST_SIZE_0, traits);
        // Validate the allocation
        ptv = reinterpret_cast<uintptr_t>(ptr0);
        sbrk_ptv = reinterpret_cast<uintptr_t>(mbed_sbrk_ptr);
        if (!((ptv > heap_start) && (ptv < sbrk_ptv))) {
            break;
        }

        // Fill the memory
        memset(ptr, 0xA5, TEST_SIZE_0);
        // free the memory
        mbed_ufree(ptr0);
        // allocate the same size block with ualloc zero-fill
        traits.flags = UALLOC_TRAITS_ZERO_FILL;
        ptr1 = mbed_ualloc(TEST_SIZE_0, traits);

        // Validate that the pointers are the same
        if (ptr1 != ptr0) {
            break;
        }

        // Check for zero-fill
        bool ok = true;
        for (unsigned i = 0; ok && (i < TEST_SIZE_0/sizeof(uint32_t)); i++) {
            ok = ((uint32_t *)ptr1)[i] == 0;
        }
        if (!ok) {break;}

        mbed_ufree(ptr1);

        // never_free + zero-fill
        current_test = "never free/zero-fill";
        // prefill the memory
        memset((char*) mbed_krbs_ptr - TEST_SIZE_0, 0xA5, TEST_SIZE_0);
        // Allocate the memory
        traits.flags = UALLOC_TRAITS_ZERO_FILL | UALLOC_TRAITS_NEVER_FREE;
        ptr = mbed_ualloc(TEST_SIZE_0, traits);

        ok = true;
        for (unsigned i = 0; ok && (i < TEST_SIZE_0/sizeof(uint32_t)); i++) {
            ok = ((uint32_t *)ptr)[i] == 0;
        }
        if (!ok) {break;}

        current_test = "urealloc";
        traits.flags = 0;
        // malloc two blocks
        ptr0 = mbed_ualloc(TEST_SIZE_0, traits);
        if (ptr0 == NULL) {break;}
        ptr1 = mbed_ualloc(TEST_SIZE_0, traits);
        if (ptr1 == NULL) {break;}
        // realloc the first one and make sure the pointer moves
        ptr2 = mbed_urealloc(ptr0, 2 * TEST_SIZE_0, traits);
        if (ptr2 == NULL) {break;}
        if (ptr2 == ptr0) {break;}
        // free
        mbed_ufree(ptr1);
        mbed_ufree(ptr2);

        // Test failing allocations
        // try to realloc a never-free pointer
        current_test = "urealloc/never-free";
        // Allocate the memory
        traits.flags = UALLOC_TRAITS_ZERO_FILL | UALLOC_TRAITS_NEVER_FREE;
        void * nfptr = mbed_ualloc(TEST_SIZE_0, traits);
        if (nfptr == NULL) {break;}
        traits.flags = 0;
        ptr = mbed_urealloc(nfptr, 2 * TEST_SIZE_0, traits);
        if (ptr != NULL) {break;}

        // try to allocate with reserved flags
        current_test = "ualloc/reserved flags";
        traits.flags = UALLOC_RESERVED_MASK | UALLOC_TRAITS_BITMASK;
        ptr = mbed_ualloc(TEST_SIZE_0, traits);
        if (ptr != NULL) {break;}

        // try urealloc with any flags
        current_test = "urealloc/flags";
        traits.flags = 0;
        // malloc two blocks
        ptr0 = mbed_ualloc(TEST_SIZE_0, traits);

        traits.flags = UALLOC_TRAITS_NEVER_FREE;
        ptr2 = mbed_urealloc(ptr0, 2 * TEST_SIZE_0, traits);
        if (ptr2 != NULL) {break;}
        // free
        mbed_ufree(ptr0);


        tests_pass = true;
    } while (0);

    if (!tests_pass) {
        printf("First failing test: %s\r\n", current_test);
    }

    MBED_HOSTTEST_RESULT(tests_pass);
}
示例#4
0
void * __wrap__calloc_r(struct _reent *r, size_t elements, size_t size) {
    (void) r;
    UAllocTraits_t traits = {UALLOC_TRAITS_ZERO_FILL};
    return mbed_ualloc(elements*size, traits);
}
示例#5
0
void * __wrap__malloc_r(struct _reent *r, size_t size) {
    (void) r;
    UAllocTraits_t traits = {0};
    return mbed_ualloc(size, traits);
}
示例#6
0
void * $Sub$$calloc(size_t elements, size_t size) {
    UAllocTraits_t traits = {UALLOC_TRAITS_ZERO_FILL};
    return mbed_ualloc(elements*size, traits);
}
示例#7
0
void * $Sub$$malloc(size_t size) {
    UAllocTraits_t traits = {0};
    return mbed_ualloc(size, traits);
}