-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachedarray.c
More file actions
414 lines (321 loc) · 8.56 KB
/
cachedarray.c
File metadata and controls
414 lines (321 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "lich.h"
#include <sys/types.h>
#include <unistd.h>
#ifndef LICH_CACHE_MAX_SIZE
# define LICH_CACHE_MAX_SIZE 64
#endif
/*
*
* Implements arrays that store only a fixed amount in RAM,
* caching anything beyond that in a tempfile on the HDD.
*
*/
static VALUE lich_cache_dump(VALUE);
static VALUE lich_cache_setname(VALUE);
VALUE lich_cCachedAry;
static int lich_cache_max_size;
extern VALUE rb_cArray;
static VALUE rb_mObSpace;
static ID id_dir;
static ID id_file;
static ID id_filename;
static ID id_define_finalizer;
static char *
lich_cache_dirname()
{
char pwd[512] = { '\0' };
char *tmpdir;
VALUE cv_dir;
cv_dir = rb_cvar_get(lich_cCachedAry, id_dir);
if (RTEST(cv_dir))
return strdup(RSTRING(cv_dir)->ptr);
(tmpdir = getenv("TEMP")) ||
(tmpdir = getenv("TMP")) ||
(tmpdir = getenv("TMPDIR"));
if (tmpdir)
return strdup(tmpdir);
getcwd(pwd, sizeof(pwd));
return strdup(pwd);
}
static VALUE
lich_cache_s_dir(VALUE klass)
{
char *dptr;
VALUE dir;
dptr = lich_cache_dirname();
dir = rb_str_new2(dptr);
free(dptr);
return dir;
}
static VALUE
lich_cache_openfile(VALUE self)
{
char *filename;
VALUE rfile = Qfalse;
if ( CLASS_OF(self) != lich_cCachedAry )
rb_raise(rb_eArgError, "object of type `%s' expected (`%s' received)", rb_class2name(lich_cCachedAry), rb_class2name(CLASS_OF(self)));
rfile = rb_ivar_get(self, id_file);
if (RTEST(rfile))
return rfile;
filename = RSTRING(lich_cache_setname(self))->ptr;
rfile = rb_file_open(filename, "wb+");
rb_ivar_set(self, id_file, rfile);
return rfile;
}
static VALUE
lich_cache_setname(VALUE self)
{
char buffer[512] = { '\0' };
char separator, *dptr;
VALUE name;
if ( CLASS_OF(self) != lich_cCachedAry )
rb_raise(rb_eArgError, "object of type `%s' expected (`%s' given)",
rb_class2name(lich_cCachedAry),
rb_class2name(CLASS_OF(self)));
name = rb_ivar_get(self, id_filename);
if (RTEST(name))
return name;
if (getenv("windir"))
separator = '\\';
else
separator = '/';
dptr = lich_cache_dirname();
snprintf(buffer, sizeof(buffer), "%s%c.cache.%d.%d", dptr, separator, getpid(), abs(NUM2LONG(rb_obj_id(self))) );
free(dptr);
return rb_ivar_set(self, id_filename, rb_str_new2(buffer));
}
/* Finalizer called by the GC anytime a CachedArray object is sweeped away; ensures deletion of the tempfile. */
static VALUE
lich_cache_finalize(VALUE self, VALUE arg)
{
VALUE fname, fobj;
fname = rb_ivar_get(self, id_filename);
fobj = rb_ivar_get(self, id_file);
if (RTEST(fobj))
rb_io_close(fobj);
if (!RTEST(fname))
{
fname = rb_ivar_get(arg, id_filename);
fobj = rb_ivar_get(arg, id_file);
if (RTEST(fobj))
rb_io_close(fobj);
}
unlink(RSTRING(fname)->ptr);
return Qnil;
}
static VALUE
lich_cache_purge(VALUE self)
{
VALUE fname;
fname = rb_ivar_get(self, id_filename);
if (!RTEST(fname))
fname = lich_cache_setname(self);
unlink(RSTRING(fname)->ptr);
return Qnil;
}
static VALUE
lich_cache_init(VALUE self)
{
VALUE proc;
lich_cache_setname(self);
proc = rb_proc_new(lich_cache_finalize, self);
// return rb_funcall(rb_mObSpace, id_define_finalizer, 2, self, proc);
rb_funcall(rb_mObSpace, id_define_finalizer, 2, self, proc);
return Qnil;
}
static VALUE
lich_cache_size_check(int argc, VALUE *argv, VALUE self)
{
int len = RARRAY(self)->len;
if (len >= lich_cache_max_size)
lich_cache_dump(self);
return rb_call_super(argc, argv);
}
static VALUE
lich_cache_dump(VALUE self)
{
VALUE *aptr, filename = rb_ivar_get(self, id_filename);
int len, i;
char *sptr, *end;
OpenFile *of;
FILE *f;
if (!RTEST(filename))
filename = lich_cache_setname(self);
len = RARRAY(self)->len;
aptr = RARRAY(self)->ptr;
GetOpenFile(lich_cache_openfile(self), of);
f = GetWriteFile(of);
if (!f)
rb_raise(rb_eSystemCallError, strerror(errno));
for (i = 0; i < len; i++)
{
sptr = RSTRING(aptr[i])->ptr;
end = &sptr[RSTRING(aptr[i])->len - 1];
fputs(sptr, f);
if (*end != '\n')
#ifdef __MINGW32__
fputs("\r\n", f);
#else
fputs("\n", f);
#endif
}
return rb_ary_clear(self);
}
static VALUE
lich_cache_history(VALUE self)
{
FILE *f;
OpenFile *of;
int size;
char buffer[4096];
VALUE ary, fname;
size = sizeof(buffer);
fname = rb_ivar_get(self, id_filename);
if (!RTEST(fname))
fname = lich_cache_setname(self);
GetOpenFile(lich_cache_openfile(self), of);
if (!of)
return rb_ary_new();
f = GetReadFile(of);
rewind(f);
ary = rb_ary_new();
while (fgets(buffer, size, f))
rb_ary_push(ary, rb_str_new2(buffer));
return ary;
}
static VALUE
lich_cache_to_a(VALUE self)
{
VALUE *aptr, ary = lich_cache_history(self);
int i, len;
len = RARRAY(self)->len;
aptr = RARRAY(self)->ptr;
for (i = 0; i < len; i++)
{
rb_ary_push(ary, aptr[i]);
}
return ary;
}
static VALUE
lich_cache_find(VALUE self)
{
VALUE *aptr, bval, ary = lich_cache_to_a(self);
int i, len;
aptr = RARRAY(ary)->ptr;
len = RARRAY(ary)->len;
for (i = 0; i < len; i++)
{
bval = rb_yield(aptr[i]);
if (RTEST(bval))
return aptr[i];
}
return Qnil;
}
static VALUE
lich_cache_find_all(VALUE self)
{
VALUE *aptr, bval, rary, ary = lich_cache_to_a(self);
int i, len;
rary = rb_ary_new();
aptr = RARRAY(ary)->ptr;
len = RARRAY(ary)->len;
for (i = 0; i < len; i++)
{
bval = rb_yield(aptr[i]);
if (RTEST(bval))
rb_ary_push(rary, aptr[i]);
}
return rary;
}
static VALUE
lich_cache_collect(VALUE self)
{
VALUE *aptr, bval, rary, ary = lich_cache_to_a(self);
int i, len;
rary = rb_ary_new();
aptr = RARRAY(ary)->ptr;
len = RARRAY(ary)->len;
for (i = 0; i < len; i++)
{
bval = rb_yield(aptr[i]);
rb_ary_push(rary, bval);
}
return rary;
}
static VALUE
lich_cache_f_get_max(VALUE klass)
{
return INT2FIX(lich_cache_max_size);
}
static VALUE
lich_cache_f_set_max(VALUE klass, VALUE newsize)
{
rb_secure(1);
lich_cache_max_size = FIX2INT(newsize);
return newsize;
}
static VALUE
lich_cache_s_m_missing(int argc, VALUE *argv, VALUE klass)
{
return Qnil;
}
static VALUE
lich_cache_m_missing(int argc, VALUE *argv, VALUE self)
{
int i;
if ( !RTEST(rb_gv_get("$LICH_DEBUG")) )
return Qnil;
for (i = 0; i < argc; i++)
{
rb_warn("no method `%s' for class CachedArray.",
rb_funcall(argv[i], rb_intern("to_s"), 0));
}
return Qnil;
}
/* Serves no purpose other than avoiding the hassle of sifting the older code line-by-line looking for references to the now-obsolete Buffer lib */
void
Init_buffer()
{
}
void
Init_cachedarray()
{
rb_mObSpace = rb_eval_string("ObjectSpace");
lich_cCachedAry = rb_define_class("CachedArray", rb_cArray);
rb_define_global_const("Buffer", lich_cCachedAry);
rb_cv_set(lich_cCachedAry, "@@dir", Qnil);
lich_cache_max_size = LICH_CACHE_MAX_SIZE;
id_filename = rb_intern("@filename");
id_dir = rb_intern("@@dir");
id_file = rb_intern("@file");
id_define_finalizer = rb_intern("define_finalizer");
rb_global_variable(&id_file);
rb_global_variable(&id_filename);
rb_global_variable(&id_dir);
rb_global_variable(&id_define_finalizer);
rb_define_singleton_method(lich_cCachedAry, "max_size", lich_cache_f_get_max, 0);
rb_define_singleton_method(lich_cCachedAry, "max_size=", lich_cache_f_set_max, 1);
rb_define_singleton_method(lich_cCachedAry, "dir", lich_cache_s_dir, 0);
/* rb_define_singleton_method(lich_cCachedAry, "method_missing", lich_cache_s_m_missing, -1); */
rb_define_method(lich_cCachedAry, "initialize", lich_cache_init, 0);
rb_define_method(lich_cCachedAry, "push", lich_cache_size_check, -1);
rb_define_method(lich_cCachedAry, "unshift", lich_cache_size_check, -1);
rb_define_method(lich_cCachedAry, "dump", lich_cache_dump, 0);
rb_define_method(lich_cCachedAry, "history", lich_cache_history, 0);
rb_define_method(lich_cCachedAry, "purge", lich_cache_purge, 0);
rb_define_method(lich_cCachedAry, "getfd", lich_cache_setname, 0);
rb_define_method(lich_cCachedAry, "to_s", lich_cache_setname, 0);
/* FIXME: the following all seem to cause objects of differing classes (perhaps only String?) within a CachedArray structure to be assigned a filename as though they were CachedArray objects themselves; these empty files are not deleted when objects are GC'd
*/
rb_define_method(lich_cCachedAry, "to_a", lich_cache_to_a, 0);
rb_define_method(lich_cCachedAry, "find", lich_cache_find, 0);
rb_define_method(lich_cCachedAry, "find_all", lich_cache_find_all, 0);
rb_define_method(lich_cCachedAry, "collect", lich_cache_collect, 0);
rb_define_method(lich_cCachedAry, "method_missing", lich_cache_m_missing, -1);
rb_define_attr(lich_cCachedAry, "filename", 1, 0);
#ifdef __MINGW32__
rb_ary_push(rb_gv_get("$\""), rb_str_new2("cachedarray.dll"));
#else
rb_ary_push(rb_gv_get("$\""), rb_str_new2("cachedarray.so"));
#endif
}