1 | #ifndef RAMLOCK_H |
---|
2 | #define RAMLOCK_H |
---|
3 | |
---|
4 | #include <stdint.h> |
---|
5 | #include <iostream> |
---|
6 | |
---|
7 | using namespace std; |
---|
8 | |
---|
9 | namespace hierarchy_memory { |
---|
10 | namespace ramlock { |
---|
11 | |
---|
12 | const static bool UNLOCK = false; |
---|
13 | const static bool LOCK = true; |
---|
14 | |
---|
15 | class param_t |
---|
16 | { |
---|
17 | public : char * name; |
---|
18 | public : uint32_t size; |
---|
19 | |
---|
20 | public : param_t () {}; |
---|
21 | |
---|
22 | public : param_t (char * name, |
---|
23 | uint32_t size) |
---|
24 | { |
---|
25 | this->name = name; |
---|
26 | this->size = size; |
---|
27 | } |
---|
28 | }; |
---|
29 | |
---|
30 | class Ramlock |
---|
31 | { |
---|
32 | private : char * name; |
---|
33 | protected : bool * lock; |
---|
34 | protected : const uint32_t size; |
---|
35 | |
---|
36 | public : Ramlock (param_t param): |
---|
37 | size (param.size) |
---|
38 | { |
---|
39 | uint32_t size_name = strlen(param.name)+1; |
---|
40 | name = new char [size_name]; |
---|
41 | strncpy(name,param.name,size_name); |
---|
42 | lock = new bool [size]; |
---|
43 | } |
---|
44 | |
---|
45 | // *****[ reset ]***** |
---|
46 | public : void reset () |
---|
47 | { |
---|
48 | for (uint32_t it = 0; it < size; it ++) |
---|
49 | lock [it] = UNLOCK; |
---|
50 | } |
---|
51 | |
---|
52 | // *****[ test ]***** |
---|
53 | public : bool test (uint32_t num_lock) |
---|
54 | { |
---|
55 | return num_lock < size; |
---|
56 | } |
---|
57 | |
---|
58 | // *****[ read ]***** |
---|
59 | // return the value of the lock and take this |
---|
60 | // NOTE : the caller must test if the num_lock is < size |
---|
61 | public : bool read (uint32_t num_lock) |
---|
62 | { |
---|
63 | bool val = lock [num_lock]; |
---|
64 | lock [num_lock] = LOCK; |
---|
65 | |
---|
66 | return val; |
---|
67 | } |
---|
68 | |
---|
69 | // *****[ write ]***** |
---|
70 | // return the value of the lock and untake this |
---|
71 | // NOTE : the caller must test if the num_lock is < size |
---|
72 | public : bool write (uint32_t num_lock) |
---|
73 | { |
---|
74 | bool val = lock [num_lock]; |
---|
75 | lock [num_lock] = UNLOCK; |
---|
76 | |
---|
77 | return val; |
---|
78 | } |
---|
79 | |
---|
80 | public : friend ostream& operator<< (ostream& output_stream, const Ramlock & x) |
---|
81 | { |
---|
82 | output_stream << "<" << x.name << ">" << endl; |
---|
83 | output_stream << " * size : " << x.size << endl; |
---|
84 | |
---|
85 | for (uint32_t it = 0; it < x.size; it ++) |
---|
86 | { |
---|
87 | if (it % 32 == 0) |
---|
88 | output_stream << endl; |
---|
89 | |
---|
90 | output_stream << x.lock [it] << " "; |
---|
91 | } |
---|
92 | output_stream << endl; |
---|
93 | |
---|
94 | return output_stream; |
---|
95 | }; |
---|
96 | }; |
---|
97 | |
---|
98 | };}; //end namespace |
---|
99 | #endif //!RAMLOCK_H |
---|