xbad 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/xbad/xbad.c +22 -15
- data/ext/xbad/xbad.h +24 -13
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fafb42bcbc0fa8b8439fe6f2c08acf8c0957c64
|
4
|
+
data.tar.gz: dda1a795814fd9a98b88061bd8ffa7f0fa36bd82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c533eaab413ff1462f5880e4b8fd61a37b884dfb222e6ed1ba0d6e632659ea7c9f18de1f59f5eb33798ac8b3d7e67bf631e0964e04980f64eb6e50407c3eb47
|
7
|
+
data.tar.gz: 51ced76814b9bb55743d78f51f2e07d3c365758d5eb93e96aafde2da37e8de0a95cc5def07f1224362efe1144e96da0ef9171e8766682ea4fb2c6b25eec3c087
|
data/ext/xbad/xbad.c
CHANGED
@@ -4,21 +4,26 @@
|
|
4
4
|
#include "xbad.h"
|
5
5
|
|
6
6
|
|
7
|
-
|
8
|
-
int shmid = NUM2INT(_shmid);
|
9
|
-
int item_size = NUM2INT(_item_size);
|
10
|
-
int hash_bits = NUM2INT(_hash_bits);
|
11
|
-
struct shared_pool *p = shared_pool_init(shmid,hash_bits,item_size);
|
12
|
-
VALUE sp = Data_Wrap_Struct(class, 0, shared_pool_destroy, p);
|
13
|
-
return sp;
|
14
|
-
}
|
15
|
-
|
16
|
-
inline struct shared_pool *shared_pool(VALUE obj) {
|
7
|
+
inline struct shared_pool *get_shared_pool(VALUE obj) {
|
17
8
|
struct shared_pool *sp;
|
18
9
|
Data_Get_Struct(obj,struct shared_pool, sp);
|
19
10
|
return sp;
|
20
11
|
}
|
21
12
|
|
13
|
+
static VALUE XBAD_alloc(VALUE class) {
|
14
|
+
struct shared_pool *p = shared_pool_alloc();
|
15
|
+
return Data_Wrap_Struct(class, 0, shared_pool_destroy, p);
|
16
|
+
}
|
17
|
+
|
18
|
+
static VALUE XBAD_init(VALUE self, VALUE _shmid, VALUE _hash_bits, VALUE _item_size ) {
|
19
|
+
int shmid = NUM2INT(_shmid);
|
20
|
+
int item_size = NUM2INT(_item_size);
|
21
|
+
int hash_bits = NUM2INT(_hash_bits);
|
22
|
+
struct shared_pool *sp = get_shared_pool(self);
|
23
|
+
shared_pool_init(sp,shmid,hash_bits,item_size);
|
24
|
+
return self;
|
25
|
+
}
|
26
|
+
|
22
27
|
static inline void die_unless_string(VALUE x) {
|
23
28
|
if (TYPE(x) != T_STRING)
|
24
29
|
rb_raise(rb_eTypeError, "invalid type for input %s",rb_obj_classname(x));
|
@@ -27,7 +32,7 @@ static inline void die_unless_string(VALUE x) {
|
|
27
32
|
static VALUE XBAD_store(VALUE self, VALUE key, VALUE value, VALUE _expire_after) {
|
28
33
|
die_unless_string(key);
|
29
34
|
die_unless_string(value);
|
30
|
-
struct shared_pool *sp =
|
35
|
+
struct shared_pool *sp = get_shared_pool(self);
|
31
36
|
int expire_after = NUM2INT(_expire_after);
|
32
37
|
if (t_add(sp,RSTRING_PTR(key),RSTRING_LEN(key),RSTRING_PTR(value),RSTRING_LEN(value),expire_after) != 0)
|
33
38
|
rb_raise(rb_eArgError,"failed to store");
|
@@ -37,7 +42,7 @@ static VALUE XBAD_store(VALUE self, VALUE key, VALUE value, VALUE _expire_after)
|
|
37
42
|
static VALUE XBAD_store_and_broadcast(VALUE self, VALUE key, VALUE value, VALUE _expire_after,VALUE _port) {
|
38
43
|
die_unless_string(key);
|
39
44
|
die_unless_string(value);
|
40
|
-
struct shared_pool *sp =
|
45
|
+
struct shared_pool *sp = get_shared_pool(self);
|
41
46
|
int expire_after = NUM2INT(_expire_after);
|
42
47
|
unsigned short port = NUM2INT(_port);
|
43
48
|
struct in_addr ip = { .s_addr = htonl(0xffffffff) };
|
@@ -49,7 +54,7 @@ static VALUE XBAD_store_and_broadcast(VALUE self, VALUE key, VALUE value, VALUE
|
|
49
54
|
|
50
55
|
static VALUE XBAD_find(VALUE self, VALUE key) {
|
51
56
|
VALUE ret = Qnil;
|
52
|
-
struct shared_pool *sp =
|
57
|
+
struct shared_pool *sp = get_shared_pool(self);
|
53
58
|
struct item *item = t_find_and_lock(sp,RSTRING_PTR(key),RSTRING_LEN(key));
|
54
59
|
if (item) {
|
55
60
|
ret = rb_str_new(ITEM_BLOB(item),item->blob_len);
|
@@ -59,14 +64,16 @@ static VALUE XBAD_find(VALUE self, VALUE key) {
|
|
59
64
|
}
|
60
65
|
|
61
66
|
static VALUE XBAD_reset(VALUE self) {
|
62
|
-
struct shared_pool *sp =
|
67
|
+
struct shared_pool *sp = get_shared_pool(self);
|
63
68
|
shared_pool_reset(sp);
|
64
69
|
return Qtrue;
|
65
70
|
}
|
66
71
|
|
67
72
|
void Init_xbad() {
|
68
73
|
VALUE c = rb_define_class("XBAD", rb_cObject);
|
69
|
-
|
74
|
+
rb_define_alloc_func(c, XBAD_alloc);
|
75
|
+
rb_define_method(c, "initialize", XBAD_init, 3);
|
76
|
+
//rb_define_singleton_method(c, "initialize", RUBY_METHOD_FUNC(XBAD_init), 3);
|
70
77
|
rb_define_method(c, "find", XBAD_find, 1);
|
71
78
|
rb_define_method(c, "store", XBAD_store, 3);
|
72
79
|
rb_define_method(c, "store_and_broadcast", XBAD_store_and_broadcast, 4);
|
data/ext/xbad/xbad.h
CHANGED
@@ -191,15 +191,20 @@ static void t_bind_and_wait_for_updates(struct shared_pool *sp, struct in_addr i
|
|
191
191
|
}
|
192
192
|
}
|
193
193
|
|
194
|
-
static struct shared_pool *
|
194
|
+
static struct shared_pool *shared_pool_alloc(void) {
|
195
|
+
struct shared_pool *sp;
|
196
|
+
BAD_ALLOC(sp,struct shared_pool);
|
197
|
+
memset(sp,0,sizeof(*sp));
|
198
|
+
return sp;
|
199
|
+
}
|
200
|
+
|
201
|
+
static struct shared_pool *shared_pool_init(struct shared_pool *sp, int key, int hash_bits, int item_size) {
|
195
202
|
if (hash_bits < 16 || hash_bits > 32 )
|
196
203
|
SAYX("hash bits must be between 16 and 32");
|
197
204
|
|
198
205
|
if (item_size <= sizeof(struct item))
|
199
206
|
SAYX("item size must be at least %zu",sizeof(struct item));
|
200
207
|
|
201
|
-
struct shared_pool *sp;
|
202
|
-
BAD_ALLOC(sp,struct shared_pool);
|
203
208
|
sp->c_sock = 0;
|
204
209
|
sp->s_sock = 0;
|
205
210
|
sp->pool = NULL;
|
@@ -220,6 +225,10 @@ static struct shared_pool *shared_pool_init(int key, int hash_bits, int item_siz
|
|
220
225
|
return sp;
|
221
226
|
}
|
222
227
|
|
228
|
+
static struct shared_pool *shared_pool_alloc_and_init(int key, int hash_bits, int item_size) {
|
229
|
+
struct shared_pool *sp = shared_pool_alloc();
|
230
|
+
return shared_pool_init(sp,key,hash_bits,item_size);
|
231
|
+
}
|
223
232
|
static void shared_pool_reset(struct shared_pool *sp) {
|
224
233
|
int i;
|
225
234
|
for (i = 0; i < sp->hash_mask + 1; i++) {
|
@@ -257,16 +266,18 @@ static void shared_pool_blind_guardian(struct shared_pool *sp, int interval_us,i
|
|
257
266
|
}
|
258
267
|
|
259
268
|
static void shared_pool_destroy(struct shared_pool *sp) {
|
260
|
-
if (
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
if (
|
269
|
-
|
269
|
+
if (sp->pool) {
|
270
|
+
if (shmdt(sp->pool) != 0)
|
271
|
+
SAYPX("detach failed");
|
272
|
+
|
273
|
+
struct shmid_ds ds;
|
274
|
+
|
275
|
+
if (shmctl(sp->shm_id, IPC_STAT, &ds) != 0)
|
276
|
+
SAYPX("IPC_STAT failed");
|
277
|
+
if (ds.shm_nattch == 0) {
|
278
|
+
if (shmctl(sp->shm_id, IPC_RMID, NULL) != 0)
|
279
|
+
SAYPX("IPC_RMID failed on shm_id %d",sp->shm_id);
|
280
|
+
}
|
270
281
|
}
|
271
282
|
BAD_FREE(sp);
|
272
283
|
}
|