-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatorFunctions.cpp
More file actions
103 lines (88 loc) · 2.34 KB
/
SimulatorFunctions.cpp
File metadata and controls
103 lines (88 loc) · 2.34 KB
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
// Program Information /////////////////////////////////////////////////////////
/**
* @file SimulatorFunctions.c
*
* @brief Memory Address Locator and Timer Implementations for CS 446 at the
* Univserity of Nevada, Reno
*
* @details This file includes the necessary function implementations for
* assignment 2 of the CS 446 course simulator.
*
* @author Cayler Miley
*
* @version 2.0 (17 October 16)
* updated by Austin Bachman
*/
// PRECOMPILER DIRECTIVES //////////////////////////////////////////////////////
#ifndef MEM_FUNC_C
#define MEM_FUNC_C
// HEADER FILES ////////////////////////////////////////////////////////////////
#include "SimulatorFunctions.h"
// GLOBAL CONSTANTS ////////////////////////////////////////////////////////////
// None
/**
* @brief Memory Adress Return Function
*
* @details Reads in an integer representing kilobytes of total memory. Using
* the total memory, the function returns the address as a hexadecimal
* string.
*
* @param in: total memory in kb (int)
*
* @pre None
*
* @post Hexadecimal string address returned
*
* @exception Requires value > 0
*
* @exception Address pointer should be null
*/
unsigned int AllocateMemory( int totMem, int blockSize, int lastLoc )
{
//memory uninitialized
if( lastLoc < 0 )
{
return 0;
}
//check if previous starting address
// + space for both previous and new blocks
// is less than the total memory, else start at 0
else if( lastLoc + blockSize * 2 < totMem )
{
return lastLoc + blockSize;
}
else
{
return 0;
}
}
/**
* @brief Timer Function
*
* @details Calculates time passed through the timeval struct
*
* @param in: a reference time (struct timeval)
*
* @pre None
*
* @post Returns the microseconds passed as an integer
*/
int timePassed( struct timeval refTime )
{
struct timeval currentTime;
int microsec, seconds;
gettimeofday( ¤tTime, NULL );
seconds = currentTime.tv_sec - refTime.tv_sec;
microsec = currentTime.tv_usec - refTime.tv_usec;
if( microsec < 0 )
{
microsec += 1000000;
seconds -= 1;
}
if( seconds > 0 )
{
microsec = microsec + ( seconds * 1000000 );
}
return microsec;
}
#endif // MEM_FUNC_C