@@ -29,25 +29,12 @@ static int use_threads = 1;
29
29
#define THREADS 8
30
30
static pthread_t threads [THREADS ];
31
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
-
38
32
/* We use one producer thread and THREADS consumer
39
33
* threads. The producer adds struct work_items to 'todo' and the
40
34
* consumers pick work items from the same array.
41
35
*/
42
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 ;
51
38
char done ;
52
39
struct strbuf out ;
53
40
};
@@ -98,17 +85,16 @@ static pthread_cond_t cond_result;
98
85
99
86
static int skip_first_line ;
100
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 )
102
90
{
103
91
grep_lock ();
104
92
105
93
while ((todo_end + 1 ) % ARRAY_SIZE (todo ) == todo_done ) {
106
94
pthread_cond_wait (& cond_write , & grep_mutex );
107
95
}
108
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 );
112
98
todo [todo_end ].done = 0 ;
113
99
strbuf_reset (& todo [todo_end ].out );
114
100
todo_end = (todo_end + 1 ) % ARRAY_SIZE (todo );
@@ -136,21 +122,6 @@ static struct work_item *get_work(void)
136
122
return ret ;
137
123
}
138
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
-
154
125
static void work_done (struct work_item * w )
155
126
{
156
127
int old_done ;
@@ -177,8 +148,7 @@ static void work_done(struct work_item *w)
177
148
178
149
write_or_die (1 , p , len );
179
150
}
180
- free (w -> name );
181
- free (w -> identifier );
151
+ grep_source_clear (& w -> source );
182
152
}
183
153
184
154
if (old_done != todo_done )
@@ -201,25 +171,8 @@ static void *run(void *arg)
201
171
break ;
202
172
203
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 );
223
176
work_done (w );
224
177
}
225
178
free_grep_patterns (arg );
@@ -365,23 +318,10 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
365
318
return data ;
366
319
}
367
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
-
380
321
static int grep_sha1 (struct grep_opt * opt , const unsigned char * sha1 ,
381
322
const char * filename , int tree_name_len )
382
323
{
383
324
struct strbuf pathbuf = STRBUF_INIT ;
384
- char * name ;
385
325
386
326
if (opt -> relative && opt -> prefix_length ) {
387
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,
391
331
strbuf_addstr (& pathbuf , filename );
392
332
}
393
333
394
- name = strbuf_detach (& pathbuf , NULL );
395
-
396
334
#ifndef NO_PTHREADS
397
335
if (use_threads ) {
398
- grep_sha1_async (opt , name , sha1 );
336
+ add_work (GREP_SOURCE_SHA1 , pathbuf .buf , sha1 );
337
+ strbuf_release (& pathbuf );
399
338
return 0 ;
400
339
} else
401
340
#endif
402
341
{
342
+ struct grep_source gs ;
403
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 );
410
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 );
416
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 ;
441
351
}
442
- close (i );
443
- data [* sz ] = 0 ;
444
- return data ;
445
352
}
446
353
447
354
static int grep_file (struct grep_opt * opt , const char * filename )
448
355
{
449
356
struct strbuf buf = STRBUF_INIT ;
450
- char * name ;
451
357
452
358
if (opt -> relative && opt -> prefix_length )
453
359
quote_path_relative (filename , -1 , & buf , opt -> prefix );
454
360
else
455
361
strbuf_addstr (& buf , filename );
456
- name = strbuf_detach (& buf , NULL );
457
362
458
363
#ifndef NO_PTHREADS
459
364
if (use_threads ) {
460
- grep_file_async (opt , name , filename );
365
+ add_work (GREP_SOURCE_FILE , buf .buf , filename );
366
+ strbuf_release (& buf );
461
367
return 0 ;
462
368
} else
463
369
#endif
464
370
{
371
+ struct grep_source gs ;
465
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 );
472
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 );
475
379
return hit ;
476
380
}
477
381
}
0 commit comments