@@ -29,25 +29,12 @@ static int use_threads = 1;
2929#define THREADS 8
3030static pthread_t threads [THREADS ];
3131
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-
3832/* We use one producer thread and THREADS consumer
3933 * threads. The producer adds struct work_items to 'todo' and the
4034 * consumers pick work items from the same array.
4135 */
4236struct 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 ;
5138 char done ;
5239 struct strbuf out ;
5340};
@@ -98,17 +85,16 @@ static pthread_cond_t cond_result;
9885
9986static int skip_first_line ;
10087
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 )
10290{
10391 grep_lock ();
10492
10593 while ((todo_end + 1 ) % ARRAY_SIZE (todo ) == todo_done ) {
10694 pthread_cond_wait (& cond_write , & grep_mutex );
10795 }
10896
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 );
11298 todo [todo_end ].done = 0 ;
11399 strbuf_reset (& todo [todo_end ].out );
114100 todo_end = (todo_end + 1 ) % ARRAY_SIZE (todo );
@@ -136,21 +122,6 @@ static struct work_item *get_work(void)
136122 return ret ;
137123}
138124
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-
154125static void work_done (struct work_item * w )
155126{
156127 int old_done ;
@@ -177,8 +148,7 @@ static void work_done(struct work_item *w)
177148
178149 write_or_die (1 , p , len );
179150 }
180- free (w -> name );
181- free (w -> identifier );
151+ grep_source_clear (& w -> source );
182152 }
183153
184154 if (old_done != todo_done )
@@ -201,25 +171,8 @@ static void *run(void *arg)
201171 break ;
202172
203173 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 );
223176 work_done (w );
224177 }
225178 free_grep_patterns (arg );
@@ -365,23 +318,10 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
365318 return data ;
366319}
367320
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-
380321static int grep_sha1 (struct grep_opt * opt , const unsigned char * sha1 ,
381322 const char * filename , int tree_name_len )
382323{
383324 struct strbuf pathbuf = STRBUF_INIT ;
384- char * name ;
385325
386326 if (opt -> relative && opt -> prefix_length ) {
387327 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,
391331 strbuf_addstr (& pathbuf , filename );
392332 }
393333
394- name = strbuf_detach (& pathbuf , NULL );
395-
396334#ifndef NO_PTHREADS
397335 if (use_threads ) {
398- grep_sha1_async (opt , name , sha1 );
336+ add_work (GREP_SOURCE_SHA1 , pathbuf .buf , sha1 );
337+ strbuf_release (& pathbuf );
399338 return 0 ;
400339 } else
401340#endif
402341 {
342+ struct grep_source gs ;
403343 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 );
410344
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 );
416348
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 ;
441351 }
442- close (i );
443- data [* sz ] = 0 ;
444- return data ;
445352}
446353
447354static int grep_file (struct grep_opt * opt , const char * filename )
448355{
449356 struct strbuf buf = STRBUF_INIT ;
450- char * name ;
451357
452358 if (opt -> relative && opt -> prefix_length )
453359 quote_path_relative (filename , -1 , & buf , opt -> prefix );
454360 else
455361 strbuf_addstr (& buf , filename );
456- name = strbuf_detach (& buf , NULL );
457362
458363#ifndef NO_PTHREADS
459364 if (use_threads ) {
460- grep_file_async (opt , name , filename );
365+ add_work (GREP_SOURCE_FILE , buf .buf , filename );
366+ strbuf_release (& buf );
461367 return 0 ;
462368 } else
463369#endif
464370 {
371+ struct grep_source gs ;
465372 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 );
472373
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 );
475379 return hit ;
476380 }
477381}
0 commit comments