-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_obj.c
1447 lines (1342 loc) · 46.5 KB
/
memory_obj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
MODULE: memory_obj.c
CONTAINING SYSTEM: SLEUTH-3r (based on SLEUTH Model 3.0 Beta)
(Slope, Land-cover, Exclusion, Urbanization,
Transportation, and Hillshade)
also known as UGM 3.0 Beta (for Urban Growth Model)
VERSION: SLEUTH-3r [Includes Version D features]
REVISION DATE: August 31, 2006a
[Annotations added August 19, 2009]
PURPOSE:
This module is a pseudo-object which sets, stores, and provides
pointers to dynamically allocated storage and other values
stored in main memory.
NOTES:
MODIFICATIONS:
TO DO:
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "globals.h"
#include "igrid_obj.h"
#include "pgrid_obj.h"
#include "wgrid_obj.h"
#include "scenario_obj.h"
#include "ugm_typedefs.h"
#include "ugm_macros.h"
#include "memory_obj.h"
/*****************************************************************************\
*******************************************************************************
** **
** MACROS **
** **
*******************************************************************************
\*****************************************************************************/
#define MEM_ARRAY_SIZE 50
#define INVALID_VAL 0xAAAAAAAA
/*****************************************************************************\
*******************************************************************************
** **
** SCCS ID **
** **
*******************************************************************************
\*****************************************************************************/
char memory_obj_c_sccs_id[] = "@(#)memory_obj.c 1.84 12/4/00";
/*****************************************************************************\
*******************************************************************************
** **
** STATIC MEMORY FOR THIS OBJECT **
** **
*******************************************************************************
\*****************************************************************************/
typedef struct
{
char previous_owner[MAX_FILENAME_LEN];
char current_owner[MAX_FILENAME_LEN];
char released_by[MAX_FILENAME_LEN];
BOOLEAN free;
GRID_P ptr;
}
mem_track_info;
static int nrows;
static int ncols;
static int total_pixels;
static PIXEL invalid_val;
static int igrid_free[MEM_ARRAY_SIZE];
static int igrid_free_tos;
static mem_track_info igrid_array[MEM_ARRAY_SIZE];
static int pgrid_free[MEM_ARRAY_SIZE];
static int pgrid_free_tos;
static mem_track_info pgrid_array[MEM_ARRAY_SIZE];
static int wgrid_free[MEM_ARRAY_SIZE];
static int wgrid_free_tos;
static int min_wgrid_free_tos;
static mem_track_info wgrid_array[MEM_ARRAY_SIZE];
static PIXEL *mem_check_array[MEM_ARRAY_SIZE];
static int mem_check_count;
static int mem_check_size;
static int igrid_size;
static int pgrid_size;
static int wgrid_size;
static int bytes_p_grid;
static int bytes_p_grid_rounded2wordboundary;
static int bytes_p_packed_grid;
static int bytes_p_packed_grid_rounded2wordboundary;
static int bytes2allocate;
static void *mem_ptr;
static int igrid_count;
static int pgrid_count;
static int wgrid_count;
static FILE *memlog_fp;
static char mem_log_filename[MAX_FILENAME_LEN];
/* D.D. Added for growth Row and Column (GRC)arrays and for road-pixel-only */
/* (RPO) arrays -- July 28, 2006 */
static void *g_row_ptr;
static short *g_col_ptr;
static int *road_expansion_row_ptr;
static int *road_expansion_col_ptr;
static short *z_row_ptr; /* D.D. 8/17/2006 Added for accumulating urban */
static short *z_col_ptr; /* pixels over year simulations. */
static int zgrwthcount;/* */
static GRID_P zgrwthpointer;
static int bytes2allocateGRC;
static int bytes2allocateRERC;
static int *roadLineRows_ptr;
static int *roadLineCols_ptr;
static int bytes2allocateRPOcol;
static short *rporowNum_ptr;
static short *rporowMin_ptr;
static short *rporowMax_ptr;
static int *rporowIdx_ptr;
static short *rpocol_ptr;
/** D.D. July 28, 2006 *******************/
/*****************************************************************************\
*******************************************************************************
** **
** STATIC FUNCTION PROTOTYPES **
** **
*******************************************************************************
\*****************************************************************************/
static void mem_CheckCheckArray ();
static void mem_partition ();
static void mem_allocate ();
static void mem_igrid_push (int i);
static int mem_igrid_pop ();
static void mem_pgrid_push (int i);
static int mem_pgrid_pop ();
static void mem_wgrid_push (int i);
static int mem_wgrid_pop ();
static void mem_InvalidateGrid (GRID_P ptr);
static void mem_CheckInvalidateGrid (GRID_P ptr);
static void mem_InvalidateCheckArray ();
void mem_Init ();
GRID_P mem_GetIGridPtr (char *owner);
GRID_P mem_GetPGridPtr (char *owner);
GRID_P mem_GetWGridPtr (char *module, char *who, int line);
GRID_P mem_GetWGridFree (char *module, char *who, int line, GRID_P ptr);
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_MemoryLog
** PURPOSE: log memory map to FILE* fp
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
void
mem_MemoryLog (FILE * fp)
{
LOG_MEM (fp, &nrows, sizeof (int), 1);
LOG_MEM (fp, &ncols, sizeof (int), 1);
LOG_MEM (fp, &total_pixels, sizeof (int), 1);
LOG_MEM (fp, &invalid_val, sizeof (PIXEL), 1);
LOG_MEM (fp, &igrid_free[0], sizeof (int), MEM_ARRAY_SIZE);
LOG_MEM (fp, &igrid_free_tos, sizeof (int), 1);
LOG_MEM (fp, &igrid_array[0], sizeof (mem_track_info), MEM_ARRAY_SIZE);
LOG_MEM (fp, &pgrid_free[0], sizeof (int), MEM_ARRAY_SIZE);
LOG_MEM (fp, &pgrid_free_tos, sizeof (int), 1);
LOG_MEM (fp, &pgrid_array[0], sizeof (mem_track_info), MEM_ARRAY_SIZE);
LOG_MEM (fp, &wgrid_free[0], sizeof (int), MEM_ARRAY_SIZE);
LOG_MEM (fp, &wgrid_free_tos, sizeof (int), 1);
LOG_MEM (fp, &min_wgrid_free_tos, sizeof (int), 1);
LOG_MEM (fp, &wgrid_array[0], sizeof (mem_track_info), MEM_ARRAY_SIZE);
LOG_MEM (fp, &mem_check_count, sizeof (int), 1);
LOG_MEM (fp, &mem_check_size, sizeof (int), 1);
LOG_MEM (fp, &igrid_size, sizeof (int), 1);
LOG_MEM (fp, &pgrid_size, sizeof (int), 1);
LOG_MEM (fp, &wgrid_size, sizeof (int), 1);
LOG_MEM (fp, &bytes_p_grid, sizeof (int), 1);
LOG_MEM (fp, &bytes_p_grid_rounded2wordboundary, sizeof (int), 1);
LOG_MEM (fp, &bytes_p_packed_grid, sizeof (int), 1);
LOG_MEM (fp, &bytes_p_packed_grid_rounded2wordboundary, sizeof (int), 1);
LOG_MEM (fp, &bytes2allocate, sizeof (int), 1);
LOG_MEM (fp, &mem_ptr, sizeof (void *), 1);
LOG_MEM (fp, &igrid_count, sizeof (int), 1);
LOG_MEM (fp, &pgrid_count, sizeof (int), 1);
LOG_MEM (fp, &wgrid_count, sizeof (int), 1);
LOG_MEM (fp, &memlog_fp, sizeof (FILE *), 1);
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_GetPackedBytesPerGrid
** PURPOSE: return # bytes per grid rounded to a word boundary
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
int
mem_GetPackedBytesPerGrid ()
{
return bytes_p_packed_grid_rounded2wordboundary;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_GetTotalPixels
** PURPOSE: return total pixel count in a grid
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
int
mem_GetTotalPixels ()
{
return total_pixels;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_GetIGridPtr
** PURPOSE: return the pointer to the next igrid
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
GRID_P
mem_GetIGridPtr (char *owner)
{
int index;
index = mem_igrid_pop ();
strcpy (igrid_array[index].current_owner, owner);
return igrid_array[index].ptr;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_GetPGridPtr
** PURPOSE: return ptr to next pgrid
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
GRID_P
mem_GetPGridPtr (char *owner)
{
int index;
index = mem_pgrid_pop ();
strcpy (pgrid_array[index].current_owner, owner);
return pgrid_array[index].ptr;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_GetWGridPtr
** PURPOSE: return ptr to next wgrid
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
GRID_P
mem_GetWGridPtr (char *module, char *who, int line)
{
int index;
index = mem_wgrid_pop ();
strcpy (wgrid_array[index].previous_owner, wgrid_array[index].current_owner);
sprintf (wgrid_array[index].current_owner,
"Module: %s Function: %s Line %u", module, who, line);
return wgrid_array[index].ptr;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_GetWGridFree
** PURPOSE:
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
GRID_P
mem_GetWGridFree (char *module, char *who, int line, GRID_P ptr)
{
char func[] = "mem_GetWGridFree";
int i;
int index;
BOOLEAN match = FALSE;
for (i = 0; i < wgrid_count; i++)
{
if (wgrid_array[i].ptr == ptr)
{
match = TRUE;
index = i;
break;
}
}
if (match == FALSE)
{
sprintf (msg_buf, "%s %u look in module %s %s %u\n",
__FILE__, __LINE__, module, who, line);
LOG_ERROR (msg_buf);
EXIT (1);
}
if (wgrid_array[index].free == TRUE)
{
sprintf (msg_buf, "wgrid_array[%u].free == TRUE", index);
LOG_ERROR (msg_buf);
EXIT (1);
}
strcpy (wgrid_array[index].current_owner, "");
sprintf (wgrid_array[index].released_by,
"Module: %s Function: %s Line %u", module, who, line);
mem_wgrid_push (index);
return NULL;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_Init
** PURPOSE: initialization routine
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
void
mem_Init ()
{
char func[] = "mem_Init";
int check_pixel_count, i;
FILE *DD01DBG;
sprintf (mem_log_filename, "%smemory.log", scen_GetOutputDir ());
memlog_fp = NULL;
if (scen_GetLogMemoryMapFlag ())
{
FILE_OPEN (memlog_fp, mem_log_filename, "w");
}
invalid_val = INVALID_VAL;
igrid_free_tos = 0;
pgrid_free_tos = 0;
wgrid_free_tos = 0;
nrows = igrid_GetNumRows ();
ncols = igrid_GetNumCols ();
total_pixels = nrows * ncols;
igrid_count = igrid_GetIGridCount ();
pgrid_count = pgrid_GetPGridCount ();
wgrid_count = wgrid_GetWGridCount ();
check_pixel_count = igrid_count + pgrid_count + wgrid_count + 1;
bytes_p_grid = BYTES_PER_PIXEL * total_pixels;
bytes_p_grid_rounded2wordboundary =
ROUND_BYTES_TO_WORD_BNDRY (bytes_p_grid);
#ifdef PACKING
bytes_p_packed_grid = BYTES_PER_PIXEL_PACKED * total_pixels;
bytes_p_packed_grid_rounded2wordboundary =
ROUND_BYTES_TO_WORD_BNDRY (bytes_p_packed_grid);
bytes2allocate = igrid_count * bytes_p_packed_grid_rounded2wordboundary +
pgrid_count * bytes_p_grid_rounded2wordboundary +
wgrid_count * bytes_p_grid_rounded2wordboundary +
check_pixel_count * BYTES_PER_PIXEL;
igrid_size = bytes_p_packed_grid_rounded2wordboundary / BYTES_PER_WORD;
#else
bytes2allocate = igrid_count * bytes_p_grid_rounded2wordboundary +
pgrid_count * bytes_p_grid_rounded2wordboundary +
wgrid_count * bytes_p_grid_rounded2wordboundary +
check_pixel_count * BYTES_PER_PIXEL;
igrid_size = bytes_p_grid_rounded2wordboundary / BYTES_PER_WORD;
/** D.D. Code added July 28, 2006 ***/
/* Allows for up to half the pixels in a grid to be used for new ***/
/* growth pixels or 15,000,000 - whichever is less. (8/17/2006) ***/
/* D.D. Type of storage to be allocated changed from int to short 8/10/2006 */
if (bytes_p_grid_rounded2wordboundary/2 > 15000000) {
bytes2allocateGRC=15000000*sizeof(short);
bytes2allocateRERC = 15000000 * sizeof(int);
}
else
{
bytes2allocateGRC = (bytes_p_grid_rounded2wordboundary/2)*sizeof(short);
bytes2allocateRERC = (bytes_p_grid_rounded2wordboundary / 2) * sizeof(int);
}
#endif
mem_check_size = 1;
pgrid_size = bytes_p_grid_rounded2wordboundary / BYTES_PER_WORD;
wgrid_size = bytes_p_grid_rounded2wordboundary / BYTES_PER_WORD;
if (memlog_fp)
{
fprintf (memlog_fp, "nrows = %u\n", nrows);
fprintf (memlog_fp, "ncols = %u\n", ncols);
fprintf (memlog_fp, "total_pixels = %u\n", total_pixels);
fprintf (memlog_fp, "igrid_count = %u\n", igrid_count);
fprintf (memlog_fp, "pgrid_count = %u\n", pgrid_count);
fprintf (memlog_fp, "wgrid_count = %u\n", wgrid_count);
fprintf (memlog_fp, "check_pixel_count = %u\n", check_pixel_count);
fprintf (memlog_fp, "BYTES_PER_WORD = %u\n", BYTES_PER_WORD);
fprintf (memlog_fp, "BYTES_PER_PIXEL = %u\n", BYTES_PER_PIXEL);
fprintf (memlog_fp, "bytes_p_grid = %u\n", bytes_p_grid);
fprintf (memlog_fp, "bytes_p_grid_rounded2wordboundary = %u\n",
bytes_p_grid_rounded2wordboundary);
fprintf (memlog_fp, "words in a grid = %u\n",
bytes_p_grid_rounded2wordboundary / BYTES_PER_WORD);
#ifdef PACKING
fprintf (memlog_fp, "BYTES_PER_PIXEL_PACKED = %u\n",
BYTES_PER_PIXEL_PACKED);
fprintf (memlog_fp, "bytes_p_packed_grid = %u\n", bytes_p_packed_grid);
fprintf (memlog_fp, "bytes_p_packed_grid_rounded2wordboundary = %u\n",
bytes_p_packed_grid_rounded2wordboundary);
#endif
fprintf (memlog_fp, "igrid_size = %u words\n", igrid_size);
fprintf (memlog_fp, "pgrid_size = %u words\n", pgrid_size);
fprintf (memlog_fp, "wgrid_size = %u words\n", wgrid_size);
fprintf (memlog_fp, "mem_check_size = %u words\n", mem_check_size);
fprintf (memlog_fp, "bytes2allocate = %u\n", bytes2allocate);
}
mem_allocate ();
mem_partition (memlog_fp);
mem_InvalidateCheckArray ();
mem_CloseLog ();
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_GetLogFP
** PURPOSE: return memory log fp
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
FILE *
mem_GetLogFP ()
{
char func[] = "mem_GetLogFP";
if (memlog_fp == NULL)
{
FILE_OPEN (memlog_fp, mem_log_filename, "a");
}
return memlog_fp;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_CloseLog
** PURPOSE: close log file
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
void
mem_CloseLog ()
{
fclose (memlog_fp);
memlog_fp = NULL;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_CheckMemory
** PURPOSE: check memory
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
void
mem_CheckMemory (FILE * fp, char *module, char *function, int line)
{
char func[] = "mem_CheckMemory";
int i;
int j;
fprintf (fp, "%s %u MEMORY CHECK at %s %s %u\n",
__FILE__, __LINE__, module, function, line);
mem_CheckCheckArray ();
for (i = 0; i < wgrid_count; i++)
{
if (wgrid_array[i].free)
{
#ifdef MEMORY_CHECK_LEVEL3
for (j = 0; j < total_pixels; j++)
{
if (wgrid_array[i].ptr[j] != invalid_val)
{
sprintf (msg_buf, "grid %d is not invalid", wgrid_array[i].ptr);
LOG_ERROR (msg_buf);
sprintf (msg_buf, "current_owner: %s", wgrid_array[i].current_owner);
LOG_ERROR (msg_buf);
sprintf (msg_buf, "previous_owner: %s", wgrid_array[i].previous_owner);
LOG_ERROR (msg_buf);
EXIT (1);
}
}
#endif
}
else
{
for (j = 0; j < total_pixels; j++)
{
if (!((0 <= wgrid_array[i].ptr[j]) && (wgrid_array[i].ptr[j] < 256)))
{
sprintf (msg_buf, "grid %d is out of range", wgrid_array[i].ptr);
LOG_ERROR (msg_buf);
sprintf (msg_buf, "current_owner: %s", wgrid_array[i].current_owner);
LOG_ERROR (msg_buf);
sprintf (msg_buf, "previous_owner: %s", wgrid_array[i].previous_owner);
LOG_ERROR (msg_buf);
EXIT (1);
}
}
}
}
fprintf (fp, "MEMORY CHECK OK\n");
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_InvalidateCheckArray
** PURPOSE: invalidate the check memory array elements
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_InvalidateCheckArray ()
{
int i;
for (i = 0; i < mem_check_count; i++)
{
*(mem_check_array[i]) = invalid_val;
}
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_CheckCheckArray
** PURPOSE: memory check
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_CheckCheckArray ()
{
char func[] = "mem_CheckCheckArray";
int i;
for (i = 0; i < mem_check_count; i++)
{
if (*(mem_check_array[i]) != invalid_val)
{
sprintf (msg_buf, "ptr = %d failed memory check", mem_check_array[i]);
LOG_ERROR (msg_buf);
sprintf (msg_buf, "i=%u *ptr=%X invalid_val=%X",
i, *(mem_check_array[i]), invalid_val);
LOG_ERROR (msg_buf);
EXIT (1);
}
}
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_partition
** PURPOSE: partition the memory
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_partition (FILE * fp)
{
int i;
PIXEL *temp_ptr = (PIXEL *) mem_ptr;
PIXEL *end_ptr;
if (fp)
{
end_ptr = (temp_ptr + bytes2allocate / BYTES_PER_WORD);
fprintf (fp, "\n\nMemory starts at %d and ends at %d\n",
temp_ptr, end_ptr);
}
mem_check_count = 0;
for (i = 0; i < igrid_GetIGridCount (); i++)
{
mem_check_array[mem_check_count++] = temp_ptr;
temp_ptr += mem_check_size;
igrid_array[i].ptr = (GRID_P) temp_ptr;
temp_ptr += igrid_size;
strcpy (igrid_array[i].current_owner, "");
mem_igrid_push (i);
if (fp)
{
fprintf (fp, "%d mem_check_array[%2u]\n",
mem_check_array[mem_check_count - 1], mem_check_count - 1);
fprintf (fp, "%d igrid_array[%2u]\n", igrid_array[i].ptr, i);
}
}
for (i = 0; i < pgrid_GetPGridCount (); i++)
{
mem_check_array[mem_check_count++] = temp_ptr;
temp_ptr += mem_check_size;
pgrid_array[i].ptr = (GRID_P) temp_ptr;
temp_ptr += pgrid_size;
strcpy (pgrid_array[i].current_owner, "");
mem_pgrid_push (i);
if (fp)
{
fprintf (fp, "%d mem_check_array[%2u]\n",
mem_check_array[mem_check_count - 1], mem_check_count - 1);
fprintf (fp, "%d pgrid_array[%2u]\n", pgrid_array[i].ptr, i);
}
}
for (i = 0; i < wgrid_GetWGridCount (); i++)
{
mem_check_array[mem_check_count++] = temp_ptr;
temp_ptr += mem_check_size;
wgrid_array[i].ptr = (GRID_P) temp_ptr;
mem_InvalidateGrid (wgrid_array[i].ptr);
temp_ptr += wgrid_size;
strcpy (wgrid_array[i].current_owner, "");
mem_wgrid_push (i);
if (fp)
{
fprintf (fp, "%d mem_check_array[%2u]\n",
mem_check_array[mem_check_count - 1], mem_check_count - 1);
fprintf (fp, "%d wgrid_array[%2u]\n", wgrid_array[i].ptr, i);
}
}
mem_check_array[mem_check_count++] = temp_ptr;
if (fp)
{
fprintf (fp, "%d mem_check_array[%2u]\n",
mem_check_array[mem_check_count - 1], mem_check_count - 1);
fprintf (fp, "%d End of memory \n", end_ptr);
}
min_wgrid_free_tos = wgrid_free_tos;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_InvalidateGrid
** PURPOSE: invalidate a grid
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_InvalidateGrid (GRID_P ptr)
{
int i;
for (i = 0; i < total_pixels; i++)
{
ptr[i] = invalid_val;
}
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_CheckInvalidateGrid
** PURPOSE: memory check a grid
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_CheckInvalidateGrid (GRID_P ptr)
{
char func[] = "mem_CheckInvalidateGrid";
int i;
for (i = 0; i < total_pixels; i++)
{
if (ptr[i] != invalid_val)
{
sprintf (msg_buf, "grid %d is not invalid", ptr);
LOG_ERROR (msg_buf);
EXIT (1);
}
}
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_allocate
** PURPOSE: allocate memory
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_allocate ()
{
char func[] = "mem_allocate";
int i;
/** Allocate memory for igrids, pgrids, and wgrids **/
mem_ptr = malloc (bytes2allocate);
if (mem_ptr == NULL)
{
sprintf (msg_buf, "Unable to allocate %u bytes of memory", bytes2allocate);
LOG_ERROR (msg_buf);
EXIT (1);
}
if (scen_GetLogFlag ())
{
scen_Append2Log ();
fprintf (scen_GetLogFP (), "%s %u Allocated %u bytes of memory\n",
__FILE__, __LINE__, bytes2allocate);
scen_CloseLog ();
}
memset (mem_ptr, 0, bytes2allocate);
/** Allocate memory for the growth row arrays. **/
g_row_ptr = malloc (bytes2allocateGRC);
// Allcoate memory for road expansion row array
road_expansion_row_ptr = malloc(bytes2allocateRERC);
/* D.D. 8/24/2006 Allow Z to use twice the pixels of delta ***
** when bytes2allocateGRC is less than 7,500,000. **/
if (bytes2allocateGRC < 7500000) { z_row_ptr = malloc (2*bytes2allocateGRC); }
else { z_row_ptr = malloc ( bytes2allocateGRC); }
if ( (g_row_ptr == NULL) || (z_row_ptr == NULL) || (road_expansion_row_ptr) == NULL)
{
sprintf (msg_buf, "Unable to allocate %u bytes of memory (GRC row)", bytes2allocateGRC*3 + bytes2allocateRERC);
LOG_ERROR (msg_buf);
EXIT (1);
}
if (scen_GetLogFlag ())
{
scen_Append2Log ();
fprintf (scen_GetLogFP (), "%s %u Allocated %u bytes of memory (GRC row)\n",
__FILE__, __LINE__, bytes2allocateGRC);
scen_CloseLog ();
}
/** Allocate memory for the growth col arrays. **/
g_col_ptr = malloc (bytes2allocateGRC);
// Allcoate memory for road expansion col array
road_expansion_col_ptr = malloc(bytes2allocateRERC);
/* D.D. 8/24/2006 Allow Z to use twice the pixels of delta ***
** when bytes2allocateGRC is less than 7,500,000. **/
if (bytes2allocateGRC < 7500000) { z_col_ptr = malloc (2*bytes2allocateGRC); }
else { z_col_ptr = malloc ( bytes2allocateGRC); }
if ( (g_col_ptr == NULL) || (z_col_ptr == NULL) || (road_expansion_col_ptr) == NULL)
{
sprintf (msg_buf, "Unable to allocate %u bytes of memory (GRC col)", bytes2allocateGRC*3 + bytes2allocateRERC);
LOG_ERROR (msg_buf);
EXIT (1);
}
if (scen_GetLogFlag ())
{
scen_Append2Log ();
fprintf (scen_GetLogFP (), "%s %u Allocated %u bytes of memory (GRC col)\n",
__FILE__, __LINE__, bytes2allocateGRC);
scen_CloseLog ();
}
/** Allocate memory for the Road-Pixel-Only row array. **/
/** "sizeof(int)" changed to "sizeof(short)" 8/10/2006 **/
rporowNum_ptr = malloc(nrows*sizeof(short));
rporowMin_ptr = malloc(nrows*sizeof(short));
rporowMax_ptr = malloc(nrows*sizeof(short));
rporowIdx_ptr = malloc(nrows*sizeof(int ));
if (
rporowNum_ptr == NULL ||
rporowMin_ptr == NULL ||
rporowMax_ptr == NULL ||
rporowIdx_ptr == NULL
)
{
sprintf (msg_buf, "Unable to allocate %u bytes of memory (RPO row)", 3*nrows*sizeof(short)+nrows*sizeof(int));
LOG_ERROR (msg_buf);
EXIT (1);
}
if (scen_GetLogFlag ())
{
scen_Append2Log ();
fprintf (scen_GetLogFP (), "%s %u Allocated %u bytes of memory (RPO row)\n",
__FILE__, __LINE__, 4*nrows*sizeof(short));
scen_CloseLog ();
}
/* Allocate memory for new road line pointer arrays*/
roadLineRows_ptr = malloc(nrows * sizeof(int));
roadLineCols_ptr = malloc(ncols * sizeof(int));
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_igrid_push
** PURPOSE: push igrid onto a stack
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_igrid_push (int i)
{
char func[] = "mem_igrid_push";
if (igrid_free_tos >= 50)
{
sprintf (msg_buf, "igrid_free_tos >= 50");
LOG_ERROR (msg_buf);
EXIT (1);
}
igrid_free[igrid_free_tos] = i;
igrid_array[i].free = TRUE;
igrid_free_tos++;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_igrid_pop
** PURPOSE: pop an igrid from the stack
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static int
mem_igrid_pop ()
{
char func[] = "mem_igrid_pop";
igrid_free_tos--;
if (igrid_free_tos < 0)
{
sprintf (msg_buf, "igrid_free_tos < 0");
LOG_ERROR (msg_buf);
EXIT (1);
}
igrid_array[igrid_free_tos].free = FALSE;
return igrid_free[igrid_free_tos];
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_pgrid_push
** PURPOSE: push a pgrid onto a stack
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_pgrid_push (int i)
{
char func[] = "mem_pgrid_push";
if (pgrid_free_tos >= 50)
{
sprintf (msg_buf, "pgrid_free_tos >= 50");
LOG_ERROR (msg_buf);
EXIT (1);
}
pgrid_free[pgrid_free_tos] = i;
pgrid_array[i].free = TRUE;
pgrid_free_tos++;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_pgrid_pop
** PURPOSE: pop a pgrid from the stack
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static int
mem_pgrid_pop ()
{
char func[] = "mem_pgrid_pop";
pgrid_free_tos--;
if (pgrid_free_tos < 0)
{
sprintf (msg_buf, "pgrid_free_tos < 0");
LOG_ERROR (msg_buf);
EXIT (1);
}
pgrid_array[pgrid_free_tos].free = FALSE;
return pgrid_free[pgrid_free_tos];
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: mem_wgrid_push
** PURPOSE: push a wgrid onto the stack
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
mem_wgrid_push (int i)
{
char func[] = "mem_wgrid_push";
if (wgrid_free_tos >= 50)
{
sprintf (msg_buf, "wgrid_free_tos >= 50");
LOG_ERROR (msg_buf);
EXIT (1);
}
wgrid_free[wgrid_free_tos] = i;
wgrid_array[i].free = TRUE;
#ifdef MEMORY_CHECK_LEVEL3
mem_InvalidateGrid (wgrid_array[i].ptr);
#endif
wgrid_free_tos++;
}
/******************************************************************************
*******************************************************************************