lib/Global/IPC/StaticVariable.xs

filter: Statementstmt 93.0% 80 / 86 Branchbran 66.6% 40 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.1% 120 / 146 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« builder/GMBuilder.pm ↑ Summary blib/lib/Global/IPC/StaticVariable.pm »
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4  
5 #define PERL_NO_GET_CONTEXT /* we want efficiency */
6 #include <EXTERN.h>
7 #include <perl.h>
8 #include <XSUB.h>
9 #include <sys/shm.h>
10 #include <string.h>
11 #include <pthread.h>
12  
13 #define MAX_VARSIZE 536870912 // 512MB
14  
15 typedef struct {
16 pthread_mutex_t m;
17 char str[MAX_VARSIZE];
18 int len;
19 } SharedObject;
20  
21 4 void clear_sharedobject(SharedObject *o) {
22 4 memset(o->str, '\0', MAX_VARSIZE);
23 4 o->len = 0;
24 4 }
25  
26 1 void init_sharedobject(SharedObject *o) {
27 pthread_mutexattr_t mat;
28 1 pthread_mutexattr_init(&mat);
29 1 pthread_mutexattr_setpshared(&mat, PTHREAD_PROCESS_SHARED);
30 1 pthread_mutex_init(&(o->m), &mat);
31 1 clear_sharedobject(o);
32 1 }
33  
34 #ifdef __cplusplus
35 } /* extern "C" */
36 #endif
37  
38 #include "ppport.h"
39  
40 MODULE = Global::IPC::StaticVariable PACKAGE = Global::IPC::StaticVariable
41  
42 PROTOTYPES: DISABLE
43  
44  
45 SV*
46 var_create()
47 PREINIT:
48 SharedObject *o;
49 1 int shmid = -1;
50 CODE:
51 1 ▶ if ((shmid = shmget(IPC_PRIVATE, sizeof(SharedObject), 0666))!=-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((shmid = shmget(IPC_PRIVATE, sizeof(SharedObject), 0666))!=-1) { 1 0 1
52 1 o = (SharedObject*)shmat(shmid, NULL, 0);
53 1 init_sharedobject(o);
54 1 RETVAL = newSViv(shmid);
55 } else {
56 0 RETVAL = &PL_sv_undef;
57 }
58 OUTPUT:
59 RETVAL
60  
61 SV*
62 var_destory(sv_shmid)
63 SV* sv_shmid;
64 PREINIT:
65 3 int shmid = -1;
66 CODE:
67 3 ▶ if (SvOK(sv_shmid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_shmid)) { 2 1 3
68 2 shmid = SvIV(sv_shmid);
69 }
70 3 ▶ if (shmid == -1 || shmctl(shmid, IPC_RMID, NULL) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (shmid == -1 || shmctl(shmid, IPC_RMID, NULL) != 0) { 1 2 3
if (shmid == -1 || shmctl(shmid, IPC_RMID, NULL) != 0) { 0 1 1
71 2 RETVAL = &PL_sv_undef;
72 } else {
73 1 RETVAL = newSViv(1);
74 }
75 OUTPUT:
76 RETVAL
77  
78 SV*
79 var_update(sv_shmid, sv_str)
80 SV* sv_shmid;
81 SV* sv_str;
82 PREINIT:
83 6 SharedObject *o = NULL;
84 6 int shmid = -1;
85 6 STRLEN sv_str_len = 0;
86 6 char *ptr_sv_str = NULL;
87 CODE:
88 6 ▶ if (SvOK(sv_shmid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_shmid)) { 5 1 6
89 5 shmid = SvIV(sv_shmid);
90 5 ▶ if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0); 5 0 5
91 }
92 6 ▶ if (o != NULL && (long)o != -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (o != NULL && (long)o != -1) { 5 1 6
if (o != NULL && (long)o != -1) { 5 0 5
93 9 ▶ if (SvOK(sv_str)) ptr_sv_str = SvPV(sv_str, sv_str_len);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_str)) ptr_sv_str = SvPV(sv_str, sv_str_len); 4 1 5
94 5 pthread_mutex_lock(&(o->m));
95 5 ▶ if (sv_str_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_str_len == 0) { 2 3 5
96 2 clear_sharedobject(o);
97 } else {
98 3 ▶ if (o->len) memset(o->str, '\0', o->len);
Branch: 1 branch
BranchTrueFalseTotal
if (o->len) memset(o->str, '\0', o->len); 1 2 3
99 3 strncpy(o->str, ptr_sv_str, sv_str_len);
100 3 o->len = sv_str_len;
101 }
102 5 pthread_mutex_unlock(&(o->m));
103 5 RETVAL = newSViv(1);
104 } else {
105 1 RETVAL = &PL_sv_undef;
106 }
107 OUTPUT:
108 RETVAL
109  
110 SV*
111 var_read(sv_shmid)
112 SV* sv_shmid;
113 PREINIT:
114 10 SharedObject *o = NULL;
115 10 int shmid = -1;
116 CODE:
117 10 ▶ if (SvOK(sv_shmid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_shmid)) { 9 1 10
118 9 shmid = SvIV(sv_shmid);
119 9 ▶ if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0); 8 1 9
120 }
121 10 ▶ if (o != NULL && (long)o != -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (o != NULL && (long)o != -1) { 8 2 10
if (o != NULL && (long)o != -1) { 8 0 8
122 8 RETVAL = newSVpvn(o->str, o->len);
123 } else {
124 2 RETVAL = &PL_sv_undef;
125 }
126 OUTPUT:
127 RETVAL
128  
129 SV*
130 var_append(sv_shmid, sv_str)
131 SV* sv_shmid;
132 SV* sv_str;
133 PREINIT:
134 1 SharedObject *o = NULL;
135 1 int shmid = -1;
136 1 STRLEN sv_str_len = 0;
137 1 char *ptr_sv_str = NULL, *tmp_ptr = NULL;
138 CODE:
139 1 ▶ if (SvOK(sv_shmid) && SvOK(sv_str)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(sv_shmid) && SvOK(sv_str)) { 1 0 1
if (SvOK(sv_shmid) && SvOK(sv_str)) { 1 0 1
140 1 ptr_sv_str = SvPV(sv_str, sv_str_len);
141 1 ▶ if (ptr_sv_str && sv_str_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr_sv_str && sv_str_len > 0) { 1 0 1
if (ptr_sv_str && sv_str_len > 0) { 1 0 1
142 1 shmid = SvIV(sv_shmid);
143 1 ▶ if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0); 1 0 1
144 1 ▶ if (o != NULL && (long)o != -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (o != NULL && (long)o != -1) { 1 0 1
if (o != NULL && (long)o != -1) { 1 0 1
145 1 pthread_mutex_lock(&(o->m));
146 1 tmp_ptr = o->str + o->len;
147 1 strncpy(tmp_ptr, ptr_sv_str, sv_str_len);
148 1 o->len += sv_str_len;
149 1 pthread_mutex_unlock(&(o->m));
150 1 RETVAL = newSViv(1);
151 } else {
152 0 RETVAL = &PL_sv_undef;
153 }
154 } else {
155 0 RETVAL = &PL_sv_undef;
156 }
157 } else {
158 0 RETVAL = &PL_sv_undef;
159 }
160 OUTPUT:
161 RETVAL
162  
163 SV*
164 var_getreset(sv_shmid)
165 SV* sv_shmid;
166 PREINIT:
167 1 SharedObject *o = NULL;
168 1 int shmid = -1;
169 CODE:
170 1 ▶ if (SvOK(sv_shmid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_shmid)) { 1 0 1
171 1 shmid = SvIV(sv_shmid);
172 1 ▶ if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0); 1 0 1
173 }
174 1 ▶ if (o != NULL && (long)o != -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (o != NULL && (long)o != -1) { 1 0 1
if (o != NULL && (long)o != -1) { 1 0 1
175 1 pthread_mutex_lock(&(o->m));
176 1 RETVAL = newSVpvn(o->str, o->len);
177 1 clear_sharedobject(o);
178 1 pthread_mutex_unlock(&(o->m));
179 } else {
180 0 RETVAL = &PL_sv_undef;
181 }
182 OUTPUT:
183 RETVAL
184  
185 SV*
186 var_length(sv_shmid)
187 SV* sv_shmid;
188 PREINIT:
189 1 SharedObject *o = NULL;
190 1 int shmid = -1;
191 CODE:
192 1 ▶ if (SvOK(sv_shmid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_shmid)) { 1 0 1
193 1 shmid = SvIV(sv_shmid);
194 1 ▶ if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (shmid != -1) o = (SharedObject*)shmat(shmid, NULL, 0); 1 0 1
195 }
196 1 ▶ if (o != NULL && (long)o != -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (o != NULL && (long)o != -1) { 1 0 1
if (o != NULL && (long)o != -1) { 1 0 1
197 1 RETVAL = newSViv(o->len);
198 } else {
199 0 RETVAL = &PL_sv_undef;
200 }
201 OUTPUT:
202 RETVAL
« builder/GMBuilder.pm ↑ Summary blib/lib/Global/IPC/StaticVariable.pm »