예제 #1
0
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
파일: main.cpp 프로젝트: krassius/hackathon
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);
}
예제 #3
0
void __wrap__free_r(struct _reent *r, void * ptr) {
    (void)r;
    mbed_ufree(ptr);
}
예제 #4
0
void $Sub$$free(void * ptr) {
    mbed_ufree(ptr);
}