Пример #1
0
static void test_extendable_pool_allocator() {
    // Create the allocator
    const size_t initial_elements = 20, new_pool_elements = 12, element_size = 6;
    UAllocTraits_t traits = {0};
    ExtendablePoolAllocator allocator;
    TEST_ASSERT_TRUE(allocator.init(initial_elements, new_pool_elements, element_size, traits));

    // Fill the first pool
    for (unsigned i = 0; i < initial_elements; i ++) {
        TEST_ASSERT_TRUE(check_value_and_alignment(allocator.alloc()));
    }
    TEST_ASSERT_EQUAL(1, allocator.get_num_pools());

    // Allocating just another element should add another pool
    void *p = allocator.alloc();
    TEST_ASSERT_TRUE(check_value_and_alignment(p));
    TEST_ASSERT_EQUAL(2, allocator.get_num_pools());
    // Fill the new pool
    for (unsigned i = 0; i < new_pool_elements - 1; i ++) {
        TEST_ASSERT_TRUE(check_value_and_alignment(allocator.alloc()));
    }
    TEST_ASSERT_EQUAL(2, allocator.get_num_pools());

    // Free one previously allocated area
    allocator.free(p);
    // And allocate again. Since there's a single free place, it should be used now
    void *newp = allocator.alloc();
    TEST_ASSERT_TRUE(check_value_and_alignment(newp));
    TEST_ASSERT_EQUAL(p, newp);
    TEST_ASSERT_EQUAL(2, allocator.get_num_pools());
}
Пример #2
0
void app_start(int, char**) {
    MBED_HOSTTEST_TIMEOUT(5);
    MBED_HOSTTEST_SELECT(default);
    MBED_HOSTTEST_DESCRIPTION(mbed-util extendable pool allocator test);
    MBED_HOSTTEST_START("MBED_UTIL_EXTENDABLE_POOL_ALLOCATOR_TEST");

    // Create the allocator
    const size_t initial_elements = 20, new_pool_elements = 12, element_size = 6;
    UAllocTraits_t traits = {0};
    ExtendablePoolAllocator allocator;
    MBED_HOSTTEST_ASSERT(allocator.init(initial_elements, new_pool_elements, element_size, traits));

    // Fill the first pool
    for (unsigned i = 0; i < initial_elements; i ++) {
        MBED_HOSTTEST_ASSERT(check_value_and_alignment(allocator.alloc()));
    }
    MBED_HOSTTEST_ASSERT(allocator.get_num_pools() == 1);

    // Allocating just another element should add another pool
    void *p = allocator.alloc();
    MBED_HOSTTEST_ASSERT(check_value_and_alignment(p));
    MBED_HOSTTEST_ASSERT(allocator.get_num_pools() == 2);
    // Fill the new pool 
    for (unsigned i = 0; i < new_pool_elements - 1; i ++) {
        MBED_HOSTTEST_ASSERT(check_value_and_alignment(allocator.alloc()));
    }
    MBED_HOSTTEST_ASSERT(allocator.get_num_pools() == 2);

    // Free one previously allocated area
    allocator.free(p);
    // And allocate again. Since there's a single free place, it should be used now
    void *newp = allocator.alloc();
    MBED_HOSTTEST_ASSERT(check_value_and_alignment(newp));
    MBED_HOSTTEST_ASSERT(newp == p);
    MBED_HOSTTEST_ASSERT(allocator.get_num_pools() == 2);

    MBED_HOSTTEST_RESULT(true);
}