-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.h
85 lines (68 loc) · 2.96 KB
/
utility.h
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
// utility.h
// Miscellaneous useful definitions, including debugging routines.
//
// The debugging routines allow the user to turn on selected
// debugging messages, controllable from the command line arguments
// passed to Nachos (-d). You are encouraged to add your own
// debugging flags. The pre-defined debugging flags are:
//
// '+' -- turn on all debug messages
// 't' -- thread system
// 's' -- semaphores, locks, and conditions
// 'i' -- interrupt emulation
// 'm' -- machine emulation (USER_PROGRAM)
// 'd' -- disk emulation (FILESYS)
// 'f' -- file system (FILESYS)
// 'a' -- address spaces (USER_PROGRAM)
// 'n' -- network emulation (NETWORK)
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#ifndef UTILITY_H
#define UTILITY_H
#include "copyright.h"
// Miscellaneous useful routines
#include <bool.h>
// Boolean values.
// This is the same definition
// as in the g++ library.
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
// Divide and either round up or down
#define divRoundDown(n,s) ((n) / (s))
#define divRoundUp(n,s) (((n) / (s)) + ((((n) % (s)) > 0) ? 1 : 0))
// This declares the type "VoidFunctionPtr" to be a "pointer to a
// function taking an integer argument and returning nothing". With
// such a function pointer (say it is "func"), we can call it like this:
//
// (*func) (17);
//
// This is used by Thread::Fork and for interrupt handlers, as well
// as a couple of other places.
typedef void (*VoidFunctionPtr)(int arg);
typedef void (*VoidNoArgFunctionPtr)();
// Include interface that isolates us from the host machine system library.
// Requires definition of bool, and VoidFunctionPtr
#include "sysdep.h"
// Interface to debugging routines.
extern void DebugInit(char* flags); // enable printing debug messages
extern bool DebugIsEnabled(char flag); // Is this debug flag enabled?
extern void DEBUG (char flag, char* format, ...); // Print debug message
// if flag is enabled
//----------------------------------------------------------------------
// ASSERT
// If condition is false, print a message and dump core.
// Useful for documenting assumptions in the code.
//
// NOTE: needs to be a #define, to be able to print the location
// where the error occurred.
//----------------------------------------------------------------------
#define ASSERT(condition) \
if (!(condition)) { \
fprintf(stderr, "Assertion failed: line %d, file \"%s\"\n", \
__LINE__, __FILE__); \
fflush(stderr); \
Abort(); \
}
#endif // UTILITY_H