1 /*************************************************************************** 2 * Copyright (c) 2024 Microsoft Corporation 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the MIT License which is available at 6 * https://opensource.org/licenses/MIT. 7 * 8 * SPDX-License-Identifier: MIT 9 **************************************************************************/ 10 11 /**************************************************************************/ 12 /**************************************************************************/ 13 /** */ 14 /** Thread-Metric Component */ 15 /** */ 16 /** Memory Allocation Test */ 17 /** */ 18 /**************************************************************************/ 19 /**************************************************************************/ 20 21 /**************************************************************************/ 22 /* */ 23 /* FUNCTION RELEASE */ 24 /* */ 25 /* tm_memory_allocation_test PORTABLE C */ 26 /* 6.1.7 */ 27 /* AUTHOR */ 28 /* */ 29 /* William E. Lamie, Microsoft Corporation */ 30 /* */ 31 /* DESCRIPTION */ 32 /* */ 33 /* This file defines the Message exchange processing test. */ 34 /* */ 35 /* RELEASE HISTORY */ 36 /* */ 37 /* DATE NAME DESCRIPTION */ 38 /* */ 39 /* 10-15-2021 William E. Lamie Initial Version 6.1.7 */ 40 /* */ 41 /**************************************************************************/ 42 #include "tm_api.h" 43 44 45 /* Define the counters used in the demo application... */ 46 47 unsigned long tm_memory_allocation_counter; 48 49 50 /* Define the test thread prototypes. */ 51 52 void tm_memory_allocation_thread_0_entry(void); 53 54 55 /* Define the reporting thread prototype. */ 56 57 void tm_memory_allocation_thread_report(void); 58 59 60 /* Define the initialization prototype. */ 61 62 void tm_memory_allocation_initialize(void); 63 64 65 /* Define main entry point. */ 66 tm_main()67void tm_main() 68 { 69 70 /* Initialize the test. */ 71 tm_initialize(tm_memory_allocation_initialize); 72 } 73 74 75 /* Define the memory allocation processing test initialization. */ 76 tm_memory_allocation_initialize(void)77void tm_memory_allocation_initialize(void) 78 { 79 80 /* Create thread 0 at priority 10. */ 81 tm_thread_create(0, 10, tm_memory_allocation_thread_0_entry); 82 83 /* Resume thread 0. */ 84 tm_thread_resume(0); 85 86 /* Create a memory pool. */ 87 tm_memory_pool_create(0); 88 89 /* Create the reporting thread. It will preempt the other 90 threads and print out the test results. */ 91 tm_thread_create(5, 2, tm_memory_allocation_thread_report); 92 tm_thread_resume(5); 93 } 94 95 96 /* Define the memory allocation processing thread. */ tm_memory_allocation_thread_0_entry(void)97void tm_memory_allocation_thread_0_entry(void) 98 { 99 100 int status; 101 unsigned char *memory_ptr; 102 103 104 while(1) 105 { 106 107 /* Allocate memory from pool. */ 108 tm_memory_pool_allocate(0, &memory_ptr); 109 110 /* Release the memory back to the pool. */ 111 status = tm_memory_pool_deallocate(0, memory_ptr); 112 113 /* Check for invalid memory allocation/deallocation. */ 114 if (status != TM_SUCCESS) 115 break; 116 117 /* Increment the number of memory allocations sent and received. */ 118 tm_memory_allocation_counter++; 119 } 120 } 121 122 123 /* Define the memory allocation test reporting thread. */ tm_memory_allocation_thread_report(void)124void tm_memory_allocation_thread_report(void) 125 { 126 127 unsigned long last_counter; 128 unsigned long relative_time; 129 130 131 /* Initialize the last counter. */ 132 last_counter = 0; 133 134 /* Initialize the relative time. */ 135 relative_time = 0; 136 137 while(1) 138 { 139 140 /* Sleep to allow the test to run. */ 141 tm_thread_sleep(TM_TEST_DURATION); 142 143 /* Increment the relative time. */ 144 relative_time = relative_time + TM_TEST_DURATION; 145 146 /* Print results to the stdio window. */ 147 printf("**** Thread-Metric Memory Allocation Test **** Relative Time: %lu\n", relative_time); 148 149 /* See if there are any errors. */ 150 if (tm_memory_allocation_counter == last_counter) 151 { 152 153 printf("ERROR: Invalid counter value(s). Error allocating/deallocating memory!\n"); 154 } 155 156 /* Show the time period total. */ 157 printf("Time Period Total: %lu\n\n", tm_memory_allocation_counter - last_counter); 158 159 /* Save the last counter. */ 160 last_counter = tm_memory_allocation_counter; 161 } 162 } 163 164