Esempio n. 1
0
static void
bighash_grow(bighash_table_t *table)
{
    AIM_ASSERT(aim_is_pow2_u32(table->bucket_count), "Bucket count must be a power of 2");

    int new_bucket_count = table->bucket_count * 2;
    bighash_entry_t **new_buckets = aim_malloc(sizeof(new_buckets[0]) * new_bucket_count);

    /* Bit that decides whether we go in the hi or lo bucket */
    uint32_t bit = table->bucket_count;

    int i;
    for (i = 0; i < table->bucket_count; i++) {
        bighash_entry_t *cur = table->buckets[i];
        bighash_entry_t **new_tail_lo = &new_buckets[i];
        bighash_entry_t **new_tail_hi = &new_buckets[bit + i];

        /* Initialize new buckets to an empty list */
        *new_tail_lo = NULL;
        *new_tail_hi = NULL;

        while (cur != NULL) {
            /* Get the new tail */
            bighash_entry_t ***new_tail_ptr = cur->hash & bit ? &new_tail_hi
                                                              : &new_tail_lo;
            bighash_entry_t **new_tail = *new_tail_ptr;
            bighash_entry_t *next = cur->next;

            /* Add cur to the end of the list */
            *new_tail = cur;
            cur->next = NULL;

            /* Advance local list pointers */
            *new_tail_ptr = &cur->next;
            cur = next;
        }
    }

    aim_free(table->buckets);
    table->bucket_count = new_bucket_count;
    table->buckets = new_buckets;
}
Esempio n. 2
0
int aim_main(int argc, char* argv[])
{
    int i;

    {
        const char* tstStrings[] = { "This", "is", "a", "complete", "sentence." };
        char* join = aim_strjoin(" ", tstStrings, AIM_ARRAYSIZE(tstStrings));
        if(strcmp(join, "This is a complete sentence.")) {
            printf("fail: join='%s'\n", join);
        }
        aim_free(join);
    }

    for(i = 0; i < argc; i++) {
        aim_printf(&aim_pvs_stdout, "arg%d: '%s'\n", i, argv[i]);
    }

    {
        /* Test data */
        char data[2500];
        memset(data, 0xFF, sizeof(data));
        aim_printf(&aim_pvs_stdout, "data is %{data}", data, sizeof(data));
    }

    {
        char* sdata = "DEADBEEFCAFE";
        char* data;
        int size;
        aim_sparse(&sdata, &aim_pvs_stdout, "{data}", &data, &size);
        aim_printf(&aim_pvs_stdout, "data is %{data}\n", data, size);
        aim_free(data);
    }

    utest_list();

    AIM_LOG_MSG("Should print 1-27");
    AIM_LOG_MSG("%d %d %d %d %d %d %d %d %d "
                "%d %d %d %d %d %d %d %d %d "
                "%d %d %d %d %d %d %d %d %d",
                1, 2, 3, 4, 5, 6, 7, 8, 9,
                10, 11, 12, 13, 14, 15, 16, 17, 18,
                19, 20, 21, 22, 23, 24, 25, 26, 27);


    aim_printf(&aim_pvs_stdout, "aim_pvs_stdout from %s:%d\n",
               __FILE__, __LINE__);


    {
        char c;
        aim_pvs_t* pvs = aim_pvs_buffer_create();
        aim_printf(pvs, "\nConsider ");
        aim_printf(pvs, "%s ", "the");
        aim_printf(pvs, "alphabet: ");
        for(c = 'A'; c <= 'Z'; c++) {
            aim_printf(pvs, "%c", c);
        }
        aim_printf(pvs, "\n");
        {
            char* s = aim_pvs_buffer_get(pvs);
            aim_printf(&aim_pvs_stdout, "first: %s", s);
            free(s);
            aim_printf(pvs, "(second)");
            s = aim_pvs_buffer_get(pvs);
            aim_printf(&aim_pvs_stdout, "second: %s", s);
            free(s);
            aim_pvs_destroy(pvs);
        }
        {
            aim_ratelimiter_t rl;
            aim_ratelimiter_init(&rl, 10, 5, NULL);

            /* 5 (6?) tokens available at t=0 */
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) < 0);

            /* Another token at t=10 */
            assert(aim_ratelimiter_limit(&rl, 10) == 0);
            assert(aim_ratelimiter_limit(&rl, 10) < 0);

            /* Nothing at t=15 */
            assert(aim_ratelimiter_limit(&rl, 15) < 0);

            /* 4 more tokens granted by t=50 */
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) < 0);
        }
        {
            aim_printf(&aim_pvs_stdout, "valgrind_status=%d\n",
                       aim_valgrind_status());
        }

        AIM_LOG_MSG("%{aim_error}", AIM_ERROR_PARAM);
    }

    /* Test integer power of 2 utilities */
    {
       assert(!aim_is_pow2_u32(0));
       assert(aim_log2_u32(0) == 0);

       assert(aim_is_pow2_u32(1));
       assert(aim_log2_u32(1) == 0);

       assert(aim_is_pow2_u32(2));
       assert(aim_log2_u32(2) == 1);

       assert(!aim_is_pow2_u32(3));
       assert(aim_log2_u32(3) == 1);

       assert(aim_is_pow2_u32(4));
       assert(aim_log2_u32(4) == 2);

       assert(!aim_is_pow2_u32(5));
       assert(aim_log2_u32(5) == 2);

       assert(!aim_is_pow2_u32(6));
       assert(aim_log2_u32(6) == 2);

       assert(!aim_is_pow2_u32(7));
       assert(aim_log2_u32(7) == 2);

       assert(aim_is_pow2_u32(8));
       assert(aim_log2_u32(8) == 3);

       assert(!aim_is_pow2_u32(2147483647));
       assert(aim_log2_u32(2147483647) == 30);

       assert(aim_is_pow2_u32(2147483648));
       assert(aim_log2_u32(2147483648) == 31);

       assert(!aim_is_pow2_u32(4294967295));
       assert(aim_log2_u32(4294967295) == 31);
    }

    return 0;
}