[OPEN-ILS-DEV] C nits: utils.c (Part 2)

David J. Fiander djfiander at fastmail.fm
Fri Mar 23 17:20:25 EDT 2007


How about all the variable declarations at the beginning? :(

I think I need to not read your C code, Bill.

- David

On 23-Mar-07, at 15:42 , Bill Erickson wrote:

>
> Below is some code I'm currently testing.  I had to make one  
> concession with an addition call to memset (still not sure why),  
> but, other than that, it's been revamped more or less according to  
> Scott's suggestions.
>
> Unfortunately, I probaly won't have time to update/test all of the  
> C code with such detail, and, as you guys pointed out, fussiness,  
> but this chunk of code is used /a lot/, so it's worth it.
>
> Thanks, Scott and David for the comments.
>
>
>
> ---------------------------------------------------------------
>
> size_t buffer_add(growing_buffer* gb, const char* data) {
>
>     if(!(gb && data)) return 0;
>
>     size_t data_len = strlen( data );
>
>     if(data_len == 0) return 0;
>
>     size_t space_needed = data_len + gb->n_used + 1; /* add 1 for  
> '\0' */
>
>     if( space_needed > gb->size ) {
>
>         while( space_needed > gb->size )
>             gb->size *= 2;
>
>         if( gb->size > BUFFER_MAX_SIZE ) {
>             fprintf(stderr, "Buffer reached MAX_SIZE of %lu",  
> BUFFER_MAX_SIZE );
>             return 0;
>         }
>
>         if( (gb->buf = realloc(gb->buf, gb->size)) == NULL ) {
>             perror("realloc(): Out of Memory" );
>             exit(99);
>         }
>
>         /* clear the new memory segment.  in theory, this is
>          * unnecessary, but something is depending on this  
> somewhere */
>         memset(gb->buf + space_needed - 1, 0, gb->size -  
> space_needed + 1);
>     }
>
>     /* since we already know the size, use memcpy */
>     memcpy(gb->buf + gb->n_used, data, data_len);
>
>     /* don't count the '\0' in num used */
>     gb->n_used = space_needed - 1;
>
>     //gb->buf[gb->n_used] = '\0'; /* not needed with memset after  
> realloc */
>
>     return gb->n_used;
> }
>
> -------------------------------------------------
> BUFFER_MAX_SIZE is now 10485760L.
>
> Suggestions appreciated.
>
>
> -bill
>
>
>
> -- 
> Bill Erickson
> PINES Systems Developer
> Georgia Public Library Service
> billserickson at gmail.com
> http://open-ils.org

-- 
David J. Fiander
Digital Services Librarian





More information about the Open-ils-dev mailing list