store.c

filter: Statementstmt 66.0% 72 / 109 Branchbran 40.9% 18 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 58.8% 90 / 153 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary Mlock.xs »
1 #include "store.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/mman.h> /* for mlock/munlock */
5 #include <unistd.h>
6 #include <string.h>
7  
8 5 AddressRegion *new(int nSize)
9 {
10 5 AddressRegion *pAddressRegion = (AddressRegion *) malloc(sizeof (AddressRegion));
11 5 pAddressRegion->pBytes = NULL;
12 5 pAddressRegion->sBytes = 0;
13 5 pAddressRegion->processLocked = 0;
14 5 pAddressRegion->memLocked = 0;
15 5 pAddressRegion->nPageSize = pagesize(pAddressRegion);
16  
17 5 ▶ if (nSize)
Branch: 1 branch
BranchTrueFalseTotal
if (nSize) 4 1 5
18 {
19 4 pAddressRegion->nSize = nSize;
20 4 pAddressRegion->nBytes = nSize * pAddressRegion->nPageSize * sizeof(char);
21 4 initialize(pAddressRegion);
22 }
23 5 return pAddressRegion;
24 }
25  
26  
27 6 int initialize(AddressRegion *pAddressRegion)
28 {
29 int r;
30 6 ▶ if (!pAddressRegion->pBytes)
Branch: 1 branch
BranchTrueFalseTotal
if (!pAddressRegion->pBytes) 5 1 6
31 {
32 5 pAddressRegion->pBytes = (char *) malloc(pAddressRegion->nBytes); /* Allocate it */
33 5 r = (!mlock(pAddressRegion->pBytes, pAddressRegion->nBytes)); /* lock it to memory */
34 5 memset(pAddressRegion->pBytes, 0, pAddressRegion->nBytes); /* clear it, this will stop copy on write as well */
35 5 pAddressRegion->memLocked = r;
36 }
37 6 return pAddressRegion->nBytes;
38 }
39  
40 0 int process_locked(AddressRegion *pAddressRegion)
41 {
42 0 return pAddressRegion->processLocked;
43 }
44  
45 0 int is_locked(AddressRegion *pAddressRegion)
46 {
47 0 return pAddressRegion->memLocked;
48 }
49  
50 2 int set_pages(AddressRegion *pAddressRegion, int pages)
51 {
52 int ps;
53 2 ps = pagesize(pAddressRegion);
54 2 return set_size(pAddressRegion, (pages * ps * (int)sizeof(char)));
55 }
56  
57 6 int set_size (AddressRegion *pAddressRegion, int bytes)
58 {
59 char *t;
60 int r, l, s;
61 6 s = pAddressRegion->sBytes; /* store the original size */
62 6 ▶ if (pAddressRegion->pBytes) /* realloc (change size) */
Branch: 1 branch
BranchTrueFalseTotal
if (pAddressRegion->pBytes) /* realloc (change size) */ 5 1 6
63 {
64 5 ▶ if (bytes <= pAddressRegion->nBytes)
Branch: 1 branch
BranchTrueFalseTotal
if (bytes <= pAddressRegion->nBytes) 3 2 5
65 3 l = bytes; /* new length is shorter */
66 else
67 2 l = pAddressRegion->nBytes; /* new length is greater */
68  
69 5 t = (char *) malloc(bytes); /* new area as requested */
70 5 memset(t, 0, bytes); /* clear it */
71 5 r = (!mlock(t, bytes)); /* lock it */
72 5 ▶ if (pAddressRegion->sBytes >= bytes) /* currently stored is greater than the new space */
Branch: 1 branch
BranchTrueFalseTotal
if (pAddressRegion->sBytes >= bytes) /* currently stored is greater than the new space */ 2 3 5
73 2 s = bytes - 1; /* set the new size to one less than the size */
74 5 ▶ if (pAddressRegion->sBytes) /* don't bother copying if there is nothing stored */
Branch: 1 branch
BranchTrueFalseTotal
if (pAddressRegion->sBytes) /* don't bother copying if there is nothing stored */ 5 0 5
75 5 memcpy(t, pAddressRegion->pBytes, (size_t) s);
76 5 memset(pAddressRegion->pBytes, 0, pAddressRegion->nBytes); /* clear the old data */
77 5 ▶ if (pAddressRegion->memLocked)
Branch: 1 branch
BranchTrueFalseTotal
if (pAddressRegion->memLocked) 5 0 5
78 5 munlock(pAddressRegion->pBytes, pAddressRegion->nBytes); /* unlock it */
79 5 free(pAddressRegion->pBytes); /* free it */
80 /* update the stored info with the new memory info */
81 5 pAddressRegion->pBytes = t;
82 5 pAddressRegion->nBytes = bytes;
83 5 pAddressRegion->sBytes = s;
84 5 pAddressRegion->memLocked = r;
85 } else {
86 /* nothing stored, so just initialise */
87 1 pAddressRegion->nBytes = bytes;
88 1 initialize(pAddressRegion);
89 }
90 6 return pAddressRegion->nBytes;
91 }
92  
93 9 int pagesize(AddressRegion *pAddressRegion)
94 {
95 9 pAddressRegion->nPageSize = getpagesize();
96 9 return pAddressRegion->nPageSize;
97 }
98  
99 5 void DESTROY(AddressRegion *pAddressRegion)
100 {
101 5 memset(pAddressRegion->pBytes, 0, pAddressRegion->nBytes); /* clear it before releasing it */
102 5 pAddressRegion->sBytes = 0;
103 5 ▶ if (pAddressRegion->memLocked)
Branch: 1 branch
BranchTrueFalseTotal
if (pAddressRegion->memLocked) 5 0 5
104 5 munlock(pAddressRegion->pBytes, pAddressRegion->nBytes); /* unlock it */
105 5 ▶ if (pAddressRegion->processLocked)
Branch: 1 branch
BranchTrueFalseTotal
if (pAddressRegion->processLocked) 0 5 5
106 0 munlockall();
107 5 free(pAddressRegion->pBytes); /* free it */
108 5 free(pAddressRegion);
109 5 }
110  
111 0 void dump(AddressRegion *pAddressRegion)
112 {
113 int i;
114 char *p;
115 0 char *b = (char *)malloc(65);
116  
117 0 p=b;
118  
119 0 ▶ for (i=0; i<pAddressRegion->nBytes; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<pAddressRegion->nBytes; i++) 0 0 0
120 {
121 0 ▶ if (!(i % 16))
Branch: 1 branch
BranchTrueFalseTotal
if (!(i % 16)) 0 0 0
122 {
123 0 ▶ if (i)
Branch: 1 branch
BranchTrueFalseTotal
if (i) 0 0 0
124 {
125 0 fprintf(stderr, " %s\n%2d\t", b, i);
126 0 memset(b, 0, 65);
127 0 p=b;
128 } else {
129 0 fprintf(stderr, "%2d\t", i);
130 0 memset(b, 0, 65);
131 }
132 0 ▶ } else if (!(i % 8)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!(i % 8)) { 0 0 0
133 0 fprintf(stderr, " ");
134 0 *p++ = ' ';
135 }
136 0 fprintf(stderr, "%02x ", pAddressRegion->pBytes[i]);
137 0 ▶ if ((pAddressRegion->pBytes[i]) > 31 && (pAddressRegion->pBytes[i]) < 127)
Branch: 2 branches
BranchTrueFalseTotal
if ((pAddressRegion->pBytes[i]) > 31 && (pAddressRegion->pBytes[i]) < 127) 0 0 0
if ((pAddressRegion->pBytes[i]) > 31 && (pAddressRegion->pBytes[i]) < 127) 0 0 0
138 0 *p++ = (pAddressRegion->pBytes[i]);
139 else
140 0 *p++ = '.';
141 }
142 0 fprintf(stderr, " %s\n", b);
143 0 free(b);
144 0 }
145  
146 5 char *get(AddressRegion *pAddressRegion)
147 {
148 5 ▶ if (!pAddressRegion->nBytes)
Branch: 1 branch
BranchTrueFalseTotal
if (!pAddressRegion->nBytes) 0 5 5
149 0 return NULL;
150 5 return pAddressRegion->pBytes;
151 }
152  
153 2 int store(AddressRegion *pAddressRegion, char *data, int len)
154 {
155 2 ▶ if (len > pAddressRegion->nBytes)
Branch: 1 branch
BranchTrueFalseTotal
if (len > pAddressRegion->nBytes) 0 2 2
156 0 return 0;
157 2 memcpy(pAddressRegion->pBytes, data, (size_t) len);
158 2 pAddressRegion->sBytes = len;
159 2 return 1;
160 }
161  
162 0 int unlockall(AddressRegion *pAddressRegion)
163 {
164 0 int r = -1;
165 0 ▶ if (pAddressRegion->processLocked)
Branch: 1 branch
BranchTrueFalseTotal
if (pAddressRegion->processLocked) 0 0 0
166 {
167 0 r = munlockall(); /* unlock the process */
168 0 ▶ if (!r && pAddressRegion->memLocked)
Branch: 2 branches
BranchTrueFalseTotal
if (!r && pAddressRegion->memLocked) 0 0 0
if (!r && pAddressRegion->memLocked) 0 0 0
169 0 mlock(pAddressRegion->pBytes, pAddressRegion->nBytes); /* relock the region in memory */
170 0 pAddressRegion->processLocked = 0;
171 }
172 0 return r;
173 }
174  
175 1 int lockall(AddressRegion *pAddressRegion)
176 {
177 1 int r = -1;
178 1 ▶ if (!pAddressRegion->processLocked)
Branch: 1 branch
BranchTrueFalseTotal
if (!pAddressRegion->processLocked) 1 0 1
179 {
180 1 r = mlockall(MCL_CURRENT | MCL_FUTURE); /* Lock everything now and future */
181 1 ▶ if (!r)
Branch: 1 branch
BranchTrueFalseTotal
if (!r) 0 1 1
182 0 pAddressRegion->processLocked = 1; /* Record that it's locked if it succeeded */
183 }
184 1 return r;
185 }
↑ Summary Mlock.xs »