/** 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!"); }
/** 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!"); }
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; }