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 /* Define the counters used in the demo application... */
45
46 unsigned long tm_memory_allocation_counter;
47
48 /* Define the test thread prototypes. */
49
50 void tm_memory_allocation_thread_0_entry(void *p1, void *p2, void *p3);
51
52 /* Define the reporting function prototype. */
53
54 void tm_memory_allocation_thread_report(void);
55
56 /* Define the initialization prototype. */
57
58 void tm_memory_allocation_initialize(void);
59
60 /* Define main entry point. */
61
main(void)62 int main(void)
63 {
64
65 /* Initialize the test. */
66 tm_initialize(tm_memory_allocation_initialize);
67
68 return 0;
69 }
70
71 /* Define the memory allocation processing test initialization. */
72
tm_memory_allocation_initialize(void)73 void tm_memory_allocation_initialize(void)
74 {
75 /* Create a memory pool. */
76 tm_memory_pool_create(0);
77
78 /* Create thread 0 at priority 10. */
79 tm_thread_create(0, 10, tm_memory_allocation_thread_0_entry);
80
81 /* Resume thread 0. */
82 tm_thread_resume(0);
83
84 tm_memory_allocation_thread_report();
85 }
86
87 /* Define the memory allocation processing thread. */
tm_memory_allocation_thread_0_entry(void * p1,void * p2,void * p3)88 void tm_memory_allocation_thread_0_entry(void *p1, void *p2, void *p3)
89 {
90
91 int status;
92 unsigned char *memory_ptr;
93
94 while (1) {
95
96 /* Allocate memory from pool. */
97 tm_memory_pool_allocate(0, &memory_ptr);
98
99 /* Release the memory back to the pool. */
100 status = tm_memory_pool_deallocate(0, memory_ptr);
101
102 /* Check for invalid memory allocation/deallocation. */
103 if (status != TM_SUCCESS) {
104 break;
105 }
106
107 /* Increment the number of memory allocations sent and received. */
108 tm_memory_allocation_counter++;
109 }
110 }
111
112 /* Define the memory allocation test reporting function. */
tm_memory_allocation_thread_report(void)113 void tm_memory_allocation_thread_report(void)
114 {
115
116 unsigned long last_counter;
117 unsigned long relative_time;
118
119 /* Initialize the last counter. */
120 last_counter = 0;
121
122 /* Initialize the relative time. */
123 relative_time = 0;
124
125 while (1) {
126
127 /* Sleep to allow the test to run. */
128 tm_thread_sleep(TM_TEST_DURATION);
129
130 /* Increment the relative time. */
131 relative_time = relative_time + TM_TEST_DURATION;
132
133 /* Print results to the stdio window. */
134 printf("**** Thread-Metric Memory Allocation Test **** Relative Time: %lu\n",
135 relative_time);
136
137 /* See if there are any errors. */
138 if (tm_memory_allocation_counter == last_counter) {
139
140 printf("ERROR: Invalid counter value(s). Error allocating/deallocating "
141 "memory!\n");
142 }
143
144 /* Show the time period total. */
145 printf("Time Period Total: %lu\n\n", tm_memory_allocation_counter - last_counter);
146
147 /* Save the last counter. */
148 last_counter = tm_memory_allocation_counter;
149 }
150 }
151