Exemplo n.º 1
0
/** Unit test to check that the Membag is initialized and re-initialized
 *  correctly.
 *
 *  \param test  Pointer to the unit test case instance
 */
static void run_membag_init_test(const struct test_case *test)
{
    /* Initialize membag system. */
    membag_init();

    /* Check that no memory is currently allocated */
    test_assert_true(test, membag_get_total() == membag_get_total_free(),
                     "Initialized membag should contain no allocated memory!");

    /* Allocate a small chunk of memory */
    membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);

    /* Check that sufficient memory was allocated */
    test_assert_false(test,
                      (membag_get_total() - membag_get_total_free()) < CONF_TEST_ALLOC_SIZE_SMALL,
                      "Not enough memory was allocated!");

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

    /* Check that no memory is now allocated */
    test_assert_true(test,
                     membag_get_total() == membag_get_total_free(),
                     "Re-Initialized membag should contain no allocated memory!");
}
Exemplo n.º 2
0
/** Unit test to check that the Membag frees previously allocated memory
 *  correctly.
 *
 *  \param test  Pointer to the unit test case instance
 */
static void run_membag_free_test(const struct test_case *test)
{
    void *data1, *data2, *data3;

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

    /* Allocate three small chunks of data. */
    data1 = membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);
    data2 = membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);
    data3 = membag_alloc(CONF_TEST_ALLOC_SIZE_SMALL);

    /* Check that all three membag allocations completed successfully. */
    test_assert_false(test, (data1 == NULL) || (data2 == NULL) ||
                      (data3 == NULL),
                      "Less than three small chunks were allocated!");

    /* Check that all three membag allocations actually reserved sufficient
     *memory. */
    test_assert_false(test,
                      (membag_get_total() - membag_get_total_free()) <
                      (CONF_TEST_ALLOC_SIZE_SMALL * 3),
                      "Not enough memory was allocated!");

    membag_free(data1);
    membag_free(data2);
    membag_free(data3);

    /* Check that all memory has been returned to the membag. */
    test_assert_true(test, membag_get_total() == membag_get_total_free(),
                     "Not all memory is free!");
}
Exemplo n.º 3
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!");
    }
}
Exemplo n.º 4
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;
}