/[dtapublic]/projs/trunk/projs/20120418_blackjacksim/source/auxprogs/malloc_test/malloc_test.c
ViewVC logotype

Contents of /projs/trunk/projs/20120418_blackjacksim/source/auxprogs/malloc_test/malloc_test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations) (download)
Fri Oct 7 03:35:12 2016 UTC (8 years, 1 month ago) by dashley
Original Path: swprojs/trunk/projs/20120418_blackjacksim/source/auxprogs/malloc_test/malloc_test.c
File MIME type: text/plain
File size: 6648 byte(s)
Commit of Blackjack simulation project.
1 //====================================================================================================
2 //$Header: /home/dashley/cvsrep/e3ft_gpl01/e3ft_gpl01/dtaipubs/cron/2010/blackjack_201010/source/auxprogs/malloc_test/malloc_test.c,v 1.7 2012/03/11 16:31:29 dashley Exp $
3 //====================================================================================================
4 //The purpose of this program is to ensure that the target system can allocate at least 1G of memory
5 //in fairly small chunks.
6 //
7 //This program was used to evaluate the Cygwin C development environment on my PC before beginning
8 //work on the "bjcceval" program. It was determined the Cygwin will successfully allocate about 2G
9 //of memory. This is more than adequate, and 1G is probably enough. Systems that won't allow at
10 //least 1G to be allocated via malloc() may be problematic. In any case, the "bjcceval" program
11 //will provide an error message if it runs out of memory (rather than a logical error affecting the
12 //results).
13 //====================================================================================================
14 #include <malloc.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19
20 #define ALLOC_CHUNK (1024U) //1K
21 #define ALLOC_NUM (1048576U) //1M
22 #define ITERATIONS (1000U)
23
24 struct base_struct
25 {
26 struct base_struct *next;
27 void *chunk;
28 };
29
30
31 typedef struct base_struct NODE;
32
33
34 int main(void)
35 {
36 unsigned i;
37 unsigned iteration;
38 NODE *head = NULL;
39 NODE *cur = NULL;
40
41 //=================================================================================================
42 //=================================================================================================
43 //=================================================================================================
44 printf("Execution begins.\n");
45
46 //=================================================================================================
47 //=================================================================================================
48 //=================================================================================================
49 printf("==============================================================================\n");
50 printf("This program will attempt to allocate and deallocate about 1 gigabyte of\n");
51 printf("dynamic memory using malloc() and free(), %u times. This test should\n", ITERATIONS);
52 printf("detect any gross anomalies in the computing environment.\n");
53 printf("==============================================================================\n");
54 printf("CVS version control information for the .c source file:\n $RCSfile: malloc_test.c,v $\n $Revision: 1.7 $\n $Date: 2012/03/11 16:31:29 $\n");
55 printf("==============================================================================\n");
56
57 for (iteration = 0; iteration < ITERATIONS; iteration++)
58 {
59 printf("Iteration number %u/%u.\n", iteration+1, ITERATIONS);
60 printf("Attempting to allocate %u elements of a linked list, each occupying %u bytes.\n",
61 ALLOC_NUM,
62 (unsigned)(sizeof(NODE) + ALLOC_CHUNK));
63
64 for (i=1; i<=ALLOC_NUM; i++)
65 {
66 //Explain to the user what is going on.
67 if ((i % 100000) == 0)
68 {
69 printf(" Allocating NODE number %u.\n", i);
70 }
71
72 //Allocate the NODE base.
73 cur = malloc(sizeof(NODE));
74 if (!cur)
75 {
76 printf("FAILURE: Out of memory.\n");
77 return(1);
78 }
79
80 //No child right now.
81 cur->next = NULL;
82
83 //Allocate the chunk.
84 cur->chunk = malloc(ALLOC_CHUNK);
85 if (!(cur->chunk))
86 {
87 printf("FAILURE: Out of memory.\n");
88 return(1);
89 }
90
91 //Write the chunk. This is just to trigger an exception
92 //hopefully if the allocation system of the target is somehow
93 //defective.
94 memset(cur->chunk, i, ALLOC_CHUNK);
95
96 //Link the NODE into the list.
97 if (!head)
98 {
99 //First one--easy.
100 head = cur;
101 }
102 else
103 {
104 //Not the first one.
105 cur->next = head;
106 head = cur;
107 }
108 }
109
110 //==============================================================================================
111 //==============================================================================================
112 //==============================================================================================
113 //Count the nodes and walk them, just to be sure no surprises.
114 printf("Counting nodes to be sure all accounted for ... ");
115
116 i = 0;
117 cur = head;
118
119 while (cur)
120 {
121 i++;
122 cur = cur->next;
123 }
124
125 if (i != ALLOC_NUM)
126 {
127 printf("\nFAILURE: Node miscount.\n");
128 return(1);
129 }
130 else
131 {
132 printf("OK.\n");
133 }
134
135 //==============================================================================================
136 //==============================================================================================
137 //==============================================================================================
138 //Deallocate the nodes.
139 printf("Deallocating nodes ... ");
140
141 while (head)
142 {
143 cur = head;
144 head = head->next;
145 free(cur->chunk);
146 free(cur);
147 }
148
149 printf("OK.\n");
150 printf("Test successful.\n");
151 }
152
153 return(0);
154 }
155
156
157 //====================================================================================================
158 //$Log: malloc_test.c,v $
159 //Revision 1.7 2012/03/11 16:31:29 dashley
160 //Iterations increased to 1000.
161 //
162 //Revision 1.6 2012/03/11 16:30:35 dashley
163 //Minor changes.
164 //
165 //Revision 1.5 2012/03/11 16:29:24 dashley
166 //Version control information enhanced.
167 //
168 //Revision 1.4 2012/03/11 16:27:26 dashley
169 //Version control information added.
170 //
171 //Revision 1.3 2012/03/11 16:24:13 dashley
172 //Iterations added.
173 //
174 //Revision 1.2 2012/03/11 16:09:34 dashley
175 //Cosmetic edits.
176 //
177 //Revision 1.1 2012/03/11 15:26:09 dashley
178 //Initial checkin.
179 //====================================================================================================
180 //End of $RCSfile: malloc_test.c,v $.
181 //====================================================================================================

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25