HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
pbank.c
Go to the documentation of this file.
1 #include <string.h>
2 
3 #include <hipc/atomic.h>
4 #include <hipc/pbank.h>
5 
14 void hipcPbankInit(struct HIPC_pbank *const bank_p,
15  void *const buf_p, const size_t page_size)
16 {
17  bank_p->stat = HIPC_PBANK_STAT_GROUND;
18  bank_p->page_size = page_size;
19  bank_p->page_ro_p = buf_p;
20  bank_p->page_rw_p = (unsigned char *) buf_p + page_size;
21  memcpy(bank_p->page_rw_p, buf_p, page_size);
23 
24  return;
25 }
26 
33 size_t hipcPbankGetPageSize(struct HIPC_pbank * const bank_p)
34 {
35  return bank_p->page_size;
36 }
37 
44 const void *hipcPbankGetReadOnlyPage(struct HIPC_pbank *const bank_p)
45 {
46  return bank_p->page_ro_p;
47 }
48 
55 void *hipcPbankGetWritablePage(struct HIPC_pbank *const bank_p)
56 {
57  return bank_p->page_rw_p;
58 }
59 
66 enum HIPC_pbank_stat hipcPbankGetStat(struct HIPC_pbank *const bank_p)
67 {
68  return bank_p->stat;
69 }
70 
77 enum HIPC_errno hipcPbankSwitchRequest(struct HIPC_pbank *const bank_p)
78 {
79  if (HIPC_PBANK_STAT_GROUND != bank_p->stat) {
81  }
82 
85 
86  return HIPC_SUCCESS;
87 }
88 
96 int hipcPbankSwitchAccept(struct HIPC_pbank *const bank_p)
97 {
98  void *tmp_p;
99 
100  if (HIPC_PBANK_STAT_REQUESTED != bank_p->stat) {
101  return 0;
102  } else {
103  /* swap page_ro_p for page_rw_p */
104  tmp_p = bank_p->page_ro_p;
105  bank_p->page_ro_p = bank_p->page_rw_p;
106  bank_p->page_rw_p = tmp_p;
107 
109  bank_p->stat = HIPC_PBANK_STAT_ACCEPTED;
110 
111  return 1;
112  }
113 }
114 
122 {
123  if (HIPC_PBANK_STAT_ACCEPTED != bank_p->stat) {
125  }
126 
127  memcpy(bank_p->page_rw_p, bank_p->page_ro_p, bank_p->page_size);
129  bank_p->stat = HIPC_PBANK_STAT_GROUND;
130 
131  return HIPC_SUCCESS;
132 }
133 
142 {
143  enum HIPC_errno retval;
144 
145  retval = hipcPbankSwitchRequest(bank_p);
146  if (HIPC_SUCCESS != retval) {
147  return retval;
148  }
149 
150  while (HIPC_PBANK_STAT_REQUESTED == hipcPbankGetStat(bank_p)) {
151  ; /* nothing to do */
152  }
153 
154  return hipcPbankSwitchComplete(bank_p);
155 }
void * hipcPbankGetWritablePage(struct HIPC_pbank *const bank_p)
Gets a current writable page.
Definition: pbank.c:55
size_t page_size
Definition: pbank.h:14
void * page_rw_p
Definition: pbank.h:16
HIPC_pbank_stat
Definition: pbank.h:6
enum HIPC_errno hipcPbankSwitchRequest(struct HIPC_pbank *const bank_p)
Requests to switch pages.
Definition: pbank.c:77
size_t hipcPbankGetPageSize(struct HIPC_pbank *const bank_p)
Gets page size.
Definition: pbank.c:33
const void * hipcPbankGetReadOnlyPage(struct HIPC_pbank *const bank_p)
Gets a current read only page.
Definition: pbank.c:44
HIPC_errno
Definition: hipc.h:4
#define HIPC_MEM_BARRIER
Definition: atomic.h:7
enum HIPC_pbank_stat hipcPbankGetStat(struct HIPC_pbank *const bank_p)
Gets current status of a bank.
Definition: pbank.c:66
enum HIPC_pbank_stat stat
Definition: pbank.h:13
int hipcPbankSwitchAccept(struct HIPC_pbank *const bank_p)
Accepts a switching request and switches pages if switching is requested.
Definition: pbank.c:96
void hipcPbankInit(struct HIPC_pbank *const bank_p, void *const buf_p, const size_t page_size)
Initializes a parameter bank.
Definition: pbank.c:14
enum HIPC_errno hipcPbankSwitchComplete(struct HIPC_pbank *const bank_p)
Makes both pages have the same latest contents and completes the switching.
Definition: pbank.c:121
enum HIPC_errno hipcPbankSwitchReqWaitCmpl(struct HIPC_pbank *const bank_p)
Calls hipcPbankSwitchRequest(), waits for acceptance of the request, and calls hipcPbankSwitchComplet...
Definition: pbank.c:141
void * page_ro_p
Definition: pbank.h:15