📄 table.h
字号:
#include "synch-sem.h"/*
Table implements a simple fixed-size table, a common kernel data
structure. The table consists of "size" entries, each of which
holds a pointer to an object. Each object in the table can be
named by an index in the range [0..size-1], corresponding to its
position in the table. Table::Alloc allocates a free entry,
stores an object pointer in it, and returns its index. The
object pointer can be retrieved by passing its index to Table::Get.
An entry is released by passing its index to Table::Release.
Table knows nothing about the objects it indexes. In particular,
it is the responsibility of the caller to delete each object when
it is no longer needed (some time after the table entry is released).
It is also the caller's responsibility to correctly handle the types
of the objects stored in the Table. The object pointer in each Table
entry is untyped (void*). It is necessary to cast an object pointer
to a (void *) before storing it in the table, and to cast it back to
its correct type (e.g., (Process *)) after retrieving it with Get.
A more sophisticated solution would use parameterized types.aux
In later assignments, the Table class may be used to implement internal
operating system tables of processes, threads, memory page frames, open
files, etc.
*/
class TElement { public: TElement(){flag=0;object=0;}; // initialize a table element int flag; void *object; };
class Table {
public:
// create a table to hold at most 'size' entries.
Table(int fixsize);
~Table();
// allocate a table slot for 'object'.
// return the table index for the slot or -1 on error.
int Alloc(void *object); bool IsFull();
// return the object from table index 'index' or NULL on error.
// (assert index is in range). Leave the table entry allocated
// and the pointer in place.
void *Get(int index);
// free a table slot
void Release(int index);
private: int size; int now; TElement *p; LockSem *lock; // enforce mutual exclusive access to the table ConditionSem *tableFull; // wait in Remove if the table is full
// Your code here.
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -