prometheus-client-mmap 0.7.0.beta45.7 → 0.7.0.beta45.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f9ed8e64e915588572b1eec01daf8cc1fe0b12f
4
- data.tar.gz: 3ff520858018c2b87c5530b169b8c06d4ac3644e
3
+ metadata.gz: d3a4f6437893fb0f0ed20e69a0fccc1ad297de91
4
+ data.tar.gz: 7c5afaa261b5b1810b8b7e350a05977ca1ee3180
5
5
  SHA512:
6
- metadata.gz: 51c868831e62e61ecd98f78f60f84e4e8e5e1c1662c0275fe54d49d632615431e39f76e9073baca0c78d4597e17d73a3c6f0efd9554bfcd93e3f0aada43b83da
7
- data.tar.gz: dd9c23e607e733ba2eb60043e8b5a411cb8b5e0342b2349983fb51c9f65ff3cb4205b086bdbc8121aa618d77c7f52feb058ec5c965c224bac8d769aaf053b3a9
6
+ metadata.gz: 5c450ca0c85ecea836356cf21bc56f003e34a482e86351d59bb57b2cdb62d6b800d03ba4495398093bad39d4b59951f297ae5fa5d07352234befa730429435e8
7
+ data.tar.gz: 5a0cab7e6177d8b0aab8b836ed6160872aec54ba5978b83ef59f80f5d868c169f3dd843ae76e73060162b6dd638422f3806e5cc26efe5db07cc142104fdca979
@@ -4,6 +4,7 @@
4
4
  #include <ruby/util.h>
5
5
  #include <sys/mman.h>
6
6
 
7
+ #include "file_format.h"
7
8
  #include "mmap.h"
8
9
  #include "utils.h"
9
10
 
@@ -119,7 +120,6 @@ static void mm_free(mm_ipc *i_mm) {
119
120
  if (i_mm->t->path) {
120
121
  if (munmap(i_mm->t->addr, i_mm->t->len) != 0) {
121
122
  if (i_mm->t->path != (char *)-1 && i_mm->t->path != NULL) {
122
- close(i_mm->t->fd);
123
123
  free(i_mm->t->path);
124
124
  }
125
125
  free(i_mm);
@@ -134,7 +134,6 @@ static void mm_free(mm_ipc *i_mm) {
134
134
  free(i_mm);
135
135
  rb_raise(rb_eTypeError, "truncate");
136
136
  }
137
- close(i_mm->t->fd);
138
137
  free(i_mm->t->path);
139
138
  }
140
139
  }
@@ -167,7 +166,6 @@ VALUE mm_s_alloc(VALUE obj) {
167
166
  res = Data_Make_Struct(obj, mm_ipc, 0, mm_free, i_mm);
168
167
  i_mm->t = ALLOC_N(mm_mmap, 1);
169
168
  MEMZERO(i_mm->t, mm_mmap, 1);
170
- i_mm->t->incr = EXP_INCR_SIZE;
171
169
  return res;
172
170
  }
173
171
 
@@ -217,24 +215,23 @@ VALUE mm_init(VALUE obj, VALUE fname) {
217
215
  init = 0;
218
216
 
219
217
  if (size == 0 && (smode & O_RDWR)) {
220
- if (lseek(fd, i_mm->t->incr - 1, SEEK_END) == -1) {
221
- rb_raise(rb_eIOError, "Can't lseek %zu", i_mm->t->incr - 1);
218
+ if (lseek(fd, INITIAL_SIZE - 1, SEEK_END) == -1) {
219
+ rb_raise(rb_eIOError, "Can't lseek %zu", INITIAL_SIZE - 1);
222
220
  }
223
221
  if (write(fd, "\000", 1) != 1) {
224
222
  rb_raise(rb_eIOError, "Can't extend %s", path);
225
223
  }
226
224
  init = 1;
227
- size = i_mm->t->incr;
225
+ size = INITIAL_SIZE;
228
226
  }
229
227
 
230
- addr = mmap(0, size, pmode, vscope, fd, offset);
228
+ addr = mmap(0, size, pmode, vscope, fd, offset);
229
+ close(fd);
231
230
 
232
231
  if (addr == MAP_FAILED || !addr) {
233
- close(fd);
234
232
  rb_raise(rb_eArgError, "mmap failed (%d)", errno);
235
233
  }
236
234
 
237
- i_mm->t->fd = fd;
238
235
  i_mm->t->addr = addr;
239
236
  i_mm->t->len = size;
240
237
  if (!init) {
@@ -317,9 +314,7 @@ VALUE mm_unmap(VALUE obj) {
317
314
  if (i_mm->t->path) {
318
315
  if (munmap(i_mm->t->addr, i_mm->t->len) != 0) {
319
316
  if (i_mm->t->path != (char *)-1 && i_mm->t->path != NULL) {
320
- close(i_mm->t->fd);
321
317
  free(i_mm->t->path);
322
- i_mm->t->fd = -1;
323
318
  i_mm->t->path = NULL;
324
319
  }
325
320
 
@@ -331,10 +326,8 @@ VALUE mm_unmap(VALUE obj) {
331
326
  truncate(i_mm->t->path, i_mm->t->real) == -1) {
332
327
  rb_raise(rb_eTypeError, "truncate");
333
328
  }
334
- close(i_mm->t->fd);
335
329
  free(i_mm->t->path);
336
330
  }
337
- i_mm->t->fd = -1;
338
331
  i_mm->t->path = NULL;
339
332
  }
340
333
  return Qnil;
@@ -1,8 +1,8 @@
1
1
  #ifndef MMAP_H
2
2
  #define MMAP_H
3
3
 
4
- #include <unistd.h>
5
4
  #include <ruby.h>
5
+ #include <unistd.h>
6
6
 
7
7
  #define MM_MODIFY 1
8
8
  #define MM_ORIGIN 2
@@ -17,21 +17,18 @@
17
17
  #define MM_TMP (1 << 5)
18
18
 
19
19
  #ifndef MMAP_RETTYPE
20
- #define MMAP_RETTYPE void *
20
+ #define MMAP_RETTYPE void *
21
21
  #endif
22
22
 
23
- #define EXP_INCR_SIZE 4096
24
-
25
23
  typedef struct {
26
24
  MMAP_RETTYPE addr;
27
25
  int smode, pmode, vscope;
28
26
  int advice, flag;
29
27
  VALUE key;
30
28
  int semid, shmid;
31
- size_t len, real, incr;
29
+ size_t len, real;
32
30
  off_t offset;
33
31
  char *path, *template;
34
- int fd;
35
32
  } mm_mmap;
36
33
 
37
34
  typedef struct {
@@ -39,14 +36,13 @@ typedef struct {
39
36
  mm_mmap *t;
40
37
  } mm_ipc;
41
38
 
42
-
43
- #define GET_MMAP(obj, i_mm, t_modify) \
44
- Data_Get_Struct(obj, mm_ipc, i_mm); \
45
- if (!i_mm->t->path) { \
46
- rb_raise(rb_eIOError, "unmapped file"); \
47
- } \
48
- if ((t_modify & MM_MODIFY) && (i_mm->t->flag & MM_FROZEN)) { \
49
- rb_error_frozen("mmap"); \
39
+ #define GET_MMAP(obj, i_mm, t_modify) \
40
+ Data_Get_Struct(obj, mm_ipc, i_mm); \
41
+ if (!i_mm->t->path || i_mm->t->addr == 0 || i_mm->t->addr == MAP_FAILED) { \
42
+ rb_raise(rb_eIOError, "unmapped file"); \
43
+ } \
44
+ if ((t_modify & MM_MODIFY) && (i_mm->t->flag & MM_FROZEN)) { \
45
+ rb_error_frozen("mmap"); \
50
46
  }
51
47
 
52
48
  VALUE mm_s_alloc(VALUE obj);
@@ -2,16 +2,30 @@
2
2
 
3
3
  #include "utils.h"
4
4
 
5
+ static void va_save_exception(VALUE exception, const char *fmt, va_list args) {
6
+ VALUE message = rb_vsprintf(fmt, args);
7
+
8
+ VALUE current_thread = rb_thread_current();
9
+ rb_thread_local_aset(current_thread, rb_intern("prometheus_last_exception"), exception);
10
+ rb_thread_local_aset(current_thread, rb_intern("prometheus_last_exception_message"), message);
11
+ }
12
+
5
13
  void save_exception(VALUE exception, const char *fmt, ...) {
6
14
  va_list args;
7
15
 
8
16
  va_start(args, fmt);
9
- VALUE message = rb_vsprintf(fmt, args);
17
+ va_save_exception(exception, fmt, args);
10
18
  va_end(args);
19
+ }
11
20
 
12
- VALUE current_thread = rb_thread_current();
13
- rb_thread_local_aset(current_thread, rb_intern("prometheus_last_exception"), exception);
14
- rb_thread_local_aset(current_thread, rb_intern("prometheus_last_exception_message"), message);
21
+ int with_exception(VALUE exception, const char *fmt, ...) {
22
+ va_list args;
23
+
24
+ va_start(args, fmt);
25
+ va_save_exception(exception, fmt, args);
26
+ va_end(args);
27
+
28
+ return FAILURE;
15
29
  }
16
30
 
17
31
  NORETURN(void raise_last_exception()) {
@@ -3,14 +3,18 @@
3
3
 
4
4
  #ifdef UNUSED
5
5
  #elif defined(__GNUC__)
6
- #define UNUSED(x) UNUSED_ ## x __attribute__((unused))
6
+ #define UNUSED(x) UNUSED_##x __attribute__((unused))
7
7
  #elif defined(__LCLINT__)
8
8
  #define UNUSED(x) /*@unused@*/ x
9
9
  #else
10
10
  #define UNUSED(x) x
11
11
  #endif
12
12
 
13
+ #define SUCCESS 1
14
+ #define FAILURE 0
15
+
13
16
  NORETURN(void raise_last_exception());
14
17
  void save_exception(VALUE exception, const char *fmt, ...);
18
+ int with_exception(VALUE exception, const char *fmt, ...);
15
19
 
16
20
  #endif
@@ -10,56 +10,81 @@
10
10
  #include "mmap.h"
11
11
  #include "value_access.h"
12
12
 
13
- static int open_and_extend_file(mm_ipc *i_mm, size_t len) {
13
+ #include "utils.h"
14
+
15
+ static int open_and_extend_file(mm_ipc *i_mm, size_t len, int *fd_p) {
14
16
  int fd;
15
17
 
16
18
  if ((fd = open(i_mm->t->path, i_mm->t->smode)) == -1) {
17
- rb_raise(rb_eArgError, "Can't open %s", i_mm->t->path);
19
+ return with_exception(rb_eArgError, "Can't open %s", i_mm->t->path);
18
20
  }
19
21
 
20
22
  if (lseek(fd, len - i_mm->t->len - 1, SEEK_END) == -1) {
21
23
  close(fd);
22
- rb_raise(rb_eIOError, "Can't lseek %zu", len - i_mm->t->len - 1);
24
+ return with_exception(rb_eIOError, "Can't lseek %zu", len - i_mm->t->len - 1);
23
25
  }
24
26
 
25
27
  if (write(fd, "\000", 1) != 1) {
26
28
  close(fd);
27
- rb_raise(rb_eIOError, "Can't extend %s", i_mm->t->path);
29
+ return with_exception(rb_eIOError, "Can't extend %s", i_mm->t->path);
28
30
  }
29
31
 
30
- return fd;
32
+ *fd_p = fd;
33
+ return SUCCESS;
31
34
  }
32
35
 
33
- static void expand(mm_ipc *i_mm, size_t len) {
34
- if (len < i_mm->t->len) {
35
- rb_raise(rb_eArgError, "Can't reduce the size of mmap");
36
+ static int perform_munmap(mm_ipc *i_mm) {
37
+ if (i_mm->t->addr > 0 && munmap(i_mm->t->addr, i_mm->t->len)) {
38
+ i_mm->t->addr = 0;
39
+ return with_exception(rb_eArgError, "munmap failed");
36
40
  }
37
41
 
38
- if (munmap(i_mm->t->addr, i_mm->t->len)) {
39
- rb_raise(rb_eArgError, "munmap failed");
42
+ i_mm->t->addr = 0;
43
+
44
+ return SUCCESS;
45
+ }
46
+
47
+ static int perform_mmap(mm_ipc *i_mm, int fd, size_t len) {
48
+ MMAP_RETTYPE addr = mmap(0, len, i_mm->t->pmode, i_mm->t->vscope, fd, i_mm->t->offset);
49
+
50
+ if (addr == MAP_FAILED) {
51
+ return with_exception(rb_eArgError, "mmap failed");
40
52
  }
53
+ i_mm->t->addr = addr;
54
+ i_mm->t->len = len;
55
+ i_mm->t->real = len;
56
+
57
+ return SUCCESS;
58
+ }
41
59
 
42
- if (i_mm->t->fd > 0 && close(i_mm->t->fd)){
43
- rb_raise(rb_eArgError, "Closing mmaped fd failed with %d", errno);
60
+ static int expand(mm_ipc *i_mm, size_t len) {
61
+ if (len < i_mm->t->len) {
62
+ return with_exception(rb_eArgError, "Can't reduce the size of mmap");
44
63
  }
45
64
 
46
- int fd = open_and_extend_file(i_mm, len);
65
+ if (!perform_munmap(i_mm)) {
66
+ return FAILURE;
67
+ }
47
68
 
48
- i_mm->t->addr = mmap(0, len, i_mm->t->pmode, i_mm->t->vscope, fd, i_mm->t->offset);
69
+ int fd;
70
+ if (!open_and_extend_file(i_mm, len, &fd)) {
71
+ return FAILURE;
72
+ }
49
73
 
50
- if (i_mm->t->addr == MAP_FAILED) {
74
+ if (!perform_mmap(i_mm, fd, len)) {
51
75
  close(fd);
52
- rb_raise(rb_eArgError, "mmap failed");
76
+ return FAILURE;
77
+ }
78
+
79
+ if (close(fd) == -1) {
80
+ return with_exception(rb_eArgError, "Can't close %s", i_mm->t->path);
53
81
  }
54
82
 
55
83
  if ((i_mm->t->flag & MM_LOCK) && mlock(i_mm->t->addr, len) == -1) {
56
- close(fd);
57
- rb_raise(rb_eArgError, "mlock(%d)", errno);
84
+ return with_exception(rb_eArgError, "mlock(%d)", errno);
58
85
  }
59
86
 
60
- i_mm->t->fd = fd;
61
- i_mm->t->len = len;
62
- i_mm->t->real = len;
87
+ return SUCCESS;
63
88
  }
64
89
 
65
90
  static void save_entry(mm_ipc *i_mm, size_t offset, VALUE key, VALUE value) {
@@ -120,9 +145,7 @@ uint32_t load_used(mm_ipc *i_mm) {
120
145
  return used;
121
146
  }
122
147
 
123
- void save_used(mm_ipc *i_mm, uint32_t used) {
124
- *((uint32_t *)i_mm->t->addr) = used;
125
- }
148
+ void save_used(mm_ipc *i_mm, uint32_t used) { *((uint32_t *)i_mm->t->addr) = used; }
126
149
 
127
150
  static VALUE initialize_entry(mm_ipc *i_mm, VALUE positions, VALUE key, VALUE value) {
128
151
  if (i_mm->t->flag & MM_FROZEN) {
@@ -139,7 +162,9 @@ static VALUE initialize_entry(mm_ipc *i_mm, VALUE positions, VALUE key, VALUE va
139
162
 
140
163
  uint32_t used = load_used(i_mm);
141
164
  while (i_mm->t->len < (used + entry_length)) {
142
- expand(i_mm, i_mm->t->len * 2);
165
+ if (!expand(i_mm, i_mm->t->len * 2)) {
166
+ raise_last_exception();
167
+ }
143
168
  }
144
169
  save_entry(i_mm, used, key, value);
145
170
  save_used(i_mm, used + entry_length);
@@ -200,7 +225,9 @@ VALUE method_save_used(VALUE self, VALUE value) {
200
225
  }
201
226
 
202
227
  if (i_mm->t->len < INITIAL_SIZE) {
203
- expand(i_mm, INITIAL_SIZE);
228
+ if (!expand(i_mm, INITIAL_SIZE)) {
229
+ raise_last_exception();
230
+ }
204
231
  }
205
232
 
206
233
  save_used(i_mm, NUM2UINT(value));
Binary file
@@ -1,5 +1,5 @@
1
1
  module Prometheus
2
2
  module Client
3
- VERSION = '0.7.0.beta45.7'.freeze
3
+ VERSION = '0.7.0.beta45.8'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prometheus-client-mmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.beta45.7
4
+ version: 0.7.0.beta45.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Schmidt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-02-05 00:00:00.000000000 Z
12
+ date: 2018-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fuzzbert