convert git-grep to use grep_source interface
The grep_source interface (as opposed to grep_buffer) will eventually gives us a richer interface for telling the low-level grep code about our buffers. Eventually this will lead to things like better binary-file handling. For now, it lets us drop a lot of now-redundant code. The conversion is mostly straight-forward. One thing to note is that the memory ownership rules for "struct grep_source" are different than the "struct work_item" found here (the former will copy things like the filename, rather than taking ownership). Therefore you will also see some slight tweaking of when filename buffers are released. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Feb 2, 2012 at 03:19 UTC
8f24a6323ece9be1bf1a04b4b5856112438337f2
1 file changed
+23
-119
builtin/grep.c
+23
-119
@@ -29,25 +29,12 @@ static int use_threads = 1;
29
#define THREADS 8
30
static pthread_t threads[THREADS];
31
32
-static void *load_sha1(const unsigned char *sha1, unsigned long *size,
33
- const char *name);
34
-static void *load_file(const char *filename, size_t *sz);
35
-
36
-enum work_type {WORK_SHA1, WORK_FILE};
37
-
32
/* We use one producer thread and THREADS consumer
33
* threads. The producer adds struct work_items to 'todo' and the
34
* consumers pick work items from the same array.
35
*/
36
struct work_item {
43
- enum work_type type;
44
- char *name;
45
-
46
- /* if type == WORK_SHA1, then 'identifier' is a SHA1,
47
- * otherwise type == WORK_FILE, and 'identifier' is a NUL
48
- * terminated filename.
49
- */
50
- void *identifier;
37
+ struct grep_source source;
38
char done;
39
struct strbuf out;
40
};
@@ -98,7 +85,8 @@ static pthread_cond_t cond_result;
85
86
static int skip_first_line;
87
101
-static void add_work(enum work_type type, char *name, void *id)
88
+static void add_work(enum grep_source_type type, const char *name,
89
+ const void *id)
90
{
91
grep_lock();
92
@@ -106,9 +94,7 @@ static void add_work(enum work_type type, char *name, void *id)
94
pthread_cond_wait(&cond_write, &grep_mutex);
95
}
96
109
- todo[todo_end].type = type;
110
- todo[todo_end].name = name;
111
- todo[todo_end].identifier = id;
97
+ grep_source_init(&todo[todo_end].source, type, name, id);
98
todo[todo_end].done = 0;
99
strbuf_reset(&todo[todo_end].out);
100
todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
@@ -136,21 +122,6 @@ static struct work_item *get_work(void)
122
return ret;
123
}
124
139
-static void grep_sha1_async(struct grep_opt *opt, char *name,
140
- const unsigned char *sha1)
141
-{
142
- unsigned char *s;
143
- s = xmalloc(20);
144
- memcpy(s, sha1, 20);
145
- add_work(WORK_SHA1, name, s);
146
-}
147
-
148
-static void grep_file_async(struct grep_opt *opt, char *name,
149
- const char *filename)
150
-{
151
- add_work(WORK_FILE, name, xstrdup(filename));
152
-}
153
-
125
static void work_done(struct work_item *w)
126
{
127
int old_done;
@@ -177,8 +148,7 @@ static void work_done(struct work_item *w)
148
149
write_or_die(1, p, len);
150
}
180
- free(w->name);
181
- free(w->identifier);
151
+ grep_source_clear(&w->source);
152
}
153
154
if (old_done != todo_done)
@@ -201,25 +171,8 @@ static void *run(void *arg)
171
break;
172
173
opt->output_priv = w;
204
- if (w->type == WORK_SHA1) {
205
- unsigned long sz;
206
- void* data = load_sha1(w->identifier, &sz, w->name);
207
-
208
- if (data) {
209
- hit |= grep_buffer(opt, w->name, data, sz);
210
- free(data);
211
- }
212
- } else if (w->type == WORK_FILE) {
213
- size_t sz;
214
- void* data = load_file(w->identifier, &sz);
215
- if (data) {
216
- hit |= grep_buffer(opt, w->name, data, sz);
217
- free(data);
218
- }
219
- } else {
220
- assert(0);
221
- }
222
-
174
+ hit |= grep_source(opt, &w->source);
175
+ grep_source_clear_data(&w->source);
176
work_done(w);
177
}
178
free_grep_patterns(arg);
@@ -365,23 +318,10 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
318
return data;
319
}
320
368
-static void *load_sha1(const unsigned char *sha1, unsigned long *size,
369
- const char *name)
370
-{
371
- enum object_type type;
372
- void *data = lock_and_read_sha1_file(sha1, &type, size);
373
-
374
- if (!data)
375
- error(_("'%s': unable to read %s"), name, sha1_to_hex(sha1));
376
-
377
- return data;
378
-}
379
-
321
static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
322
const char *filename, int tree_name_len)
323
{
324
struct strbuf pathbuf = STRBUF_INIT;
384
- char *name;
325
326
if (opt->relative && opt->prefix_length) {
327
quote_path_relative(filename + tree_name_len, -1, &pathbuf,
@@ -391,87 +331,51 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
331
strbuf_addstr(&pathbuf, filename);
332
}
333
394
- name = strbuf_detach(&pathbuf, NULL);
395
-
334
#ifndef NO_PTHREADS
335
if (use_threads) {
398
- grep_sha1_async(opt, name, sha1);
336
+ add_work(GREP_SOURCE_SHA1, pathbuf.buf, sha1);
337
+ strbuf_release(&pathbuf);
338
return 0;
339
} else
340
#endif
341
{
342
+ struct grep_source gs;
343
int hit;
404
- unsigned long sz;
405
- void *data = load_sha1(sha1, &sz, name);
406
- if (!data)
407
- hit = 0;
408
- else
409
- hit = grep_buffer(opt, name, data, sz);
344
411
- free(data);
412
- free(name);
413
- return hit;
414
- }
415
-}
345
+ grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
346
+ strbuf_release(&pathbuf);
347
+ hit = grep_source(opt, &gs);
348
417
-static void *load_file(const char *filename, size_t *sz)
418
-{
419
- struct stat st;
420
- char *data;
421
- int i;
422
-
423
- if (lstat(filename, &st) < 0) {
424
- err_ret:
425
- if (errno != ENOENT)
426
- error(_("'%s': %s"), filename, strerror(errno));
427
- return NULL;
428
- }
429
- if (!S_ISREG(st.st_mode))
430
- return NULL;
431
- *sz = xsize_t(st.st_size);
432
- i = open(filename, O_RDONLY);
433
- if (i < 0)
434
- goto err_ret;
435
- data = xmalloc(*sz + 1);
436
- if (st.st_size != read_in_full(i, data, *sz)) {
437
- error(_("'%s': short read %s"), filename, strerror(errno));
438
- close(i);
439
- free(data);
440
- return NULL;
349
+ grep_source_clear(&gs);
350
+ return hit;
351
}
442
- close(i);
443
- data[*sz] = 0;
444
- return data;
352
}
353
354
static int grep_file(struct grep_opt *opt, const char *filename)
355
{
356
struct strbuf buf = STRBUF_INIT;
450
- char *name;
357
358
if (opt->relative && opt->prefix_length)
359
quote_path_relative(filename, -1, &buf, opt->prefix);
360
else
361
strbuf_addstr(&buf, filename);
456
- name = strbuf_detach(&buf, NULL);
362
363
#ifndef NO_PTHREADS
364
if (use_threads) {
460
- grep_file_async(opt, name, filename);
365
+ add_work(GREP_SOURCE_FILE, buf.buf, filename);
366
+ strbuf_release(&buf);
367
return 0;
368
} else
369
#endif
370
{
371
+ struct grep_source gs;
372
int hit;
466
- size_t sz;
467
- void *data = load_file(filename, &sz);
468
- if (!data)
469
- hit = 0;
470
- else
471
- hit = grep_buffer(opt, name, data, sz);
373
473
- free(data);
474
- free(name);
374
+ grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename);
375
+ strbuf_release(&buf);
376
+ hit = grep_source(opt, &gs);
377
+
378
+ grep_source_clear(&gs);
379
return hit;
380
}
381
}