Esempio n. 1
0
/** Unit test to check that the Membag re-allocates previously allocated and
 *  subsequently freed blocks of memory correctly.
 *
 *  \param test  Pointer to the unit test case instance
 */
static void run_membag_realloc_test(const struct test_case *test)
{
    void *data;

    /* Initialize membag system. */
    membag_init();

    /* Allocate as many small chunks as there are sufficiently sized blocks. */
    while (membag_get_largest_free_block_size() >=
            CONF_TEST_ALLOC_SIZE_SMALL) {
        data = membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);

        test_assert_false(test, data == NULL,
                          "Unable to allocate a small chunk!");
    }

    /* Free last allocated chunk of memory */
    membag_free(data);

    /* Re-allocate a small chunk, should succeed by re-using the last freed
     * block of memory. */
    data = membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);

    test_assert_false(test, data == NULL,
                      "Unable to re-allocate a small chunk!");
}
Esempio n. 2
0
/** Unit test to check that the Membag functions to determine memory status work
 *  correctly.
 *
 *  \param test  Pointer to the unit test case instance
 */
static void run_membag_get_test(const struct test_case *test)
{
    void *data;
    size_t prev_total_free, chunk_size;

    /* Initialize membag system. */
    membag_init();

    prev_total_free = membag_get_total_free();

    /* Keep allocating chunks until all memory allocated */
    while (membag_get_total_free() > 0) {
        /* Keep track of how much memory we have left and the largest
         * block size */
        prev_total_free = membag_get_total_free();
        chunk_size = membag_get_largest_free_block_size();

        /* Allocate the next largest block sized chunk of memory */
        data = membag_alloc(chunk_size);
        test_assert_false(test, data == NULL,
                          "Unable to allocate a block sized chunk!");

        /* Check that the new memory usage was calculated correctly */
        test_assert_true(test, membag_get_total_free() ==
                         (prev_total_free - chunk_size),
                         "Failed to calculate correct memory usage!");
    }
}
Esempio n. 3
0
int 
mon_memory(int argc, char **argv){
  size_t total, free;
  total = membag_get_total();
  free = membag_get_total_free();
  printf("Allocated %d of %d bytes (%d%%)\n",total-free,total,((total-free)*100)/total);
  printf("Largest free block: %d bytes\n",membag_get_largest_free_block_size());
  printf("Smallest free block: %d bytes\n",membag_get_smallest_free_block_size());
  return 0;
}
Esempio n. 4
0
/** Unit test to check that the Membag allocations fail once all suitable bags
 * are already
 *  allocated.
 *
 *  \param test  Pointer to the unit test case instance
 */
static void run_membag_alloc_when_full_test(const struct test_case *test)
{
    void *data;

    /* Initialize membag system. */
    membag_init();

    /* Allocate as many small chunks as there are sufficiently sized blocks. */
    while (membag_get_largest_free_block_size() >=
            CONF_TEST_ALLOC_SIZE_SMALL) {
        data = membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);

        test_assert_false(test, data == NULL,
                          "Unable to allocate a small chunk!");
    }

    /* Try to allocate one more small chunk, should fail */
    data = membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);

    test_assert_true(test, data == NULL,
                     "Should not be able to allocate small chunk while full!");

    /* Re-initialize the membag system. */
    membag_init();

    /* Allocate as many large chunks as there are sufficiently sized blocks. */
    while (membag_get_largest_free_block_size() >=
            CONF_TEST_ALLOC_SIZE_LARGE) {
        data = membag_alloc(CONF_TEST_ALLOC_SIZE_LARGE);

        test_assert_false(test, data == NULL,
                          "Unable to allocate a large chunk!");
    }

    /* Try to allocate one more large chunk, should fail */
    data = membag_alloc(CONF_TEST_ALLOC_SIZE_LARGE);

    test_assert_true(test, data == NULL,
                     "Should not be able to allocate large chunk while full!");
}