• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file op_growable_buffer.c
3  * a growable buffer implementation
4  *
5  * @remark Copyright 2007 OProfile authors
6  * @remark Read the file COPYING
7  *
8  * @author Philippe Elie
9  */
10 
11 #include "op_growable_buffer.h"
12 #include "op_libiberty.h"
13 
14 #include <string.h>
15 #include <stdlib.h>
16 
init_buffer(struct growable_buffer * b)17 void init_buffer(struct growable_buffer * b)
18 {
19 	b->max_size = 0;
20 	b->size = 0;
21 	b->p = NULL;
22 }
23 
24 
free_buffer(struct growable_buffer * b)25 void free_buffer(struct growable_buffer * b)
26 {
27 	free(b->p);
28 }
29 
30 
grow_buffer(struct growable_buffer * b)31 static void grow_buffer(struct growable_buffer * b)
32 {
33 	size_t new_size = (b->max_size + b->size) * 2;
34 	b->p = xrealloc(b->p, new_size);
35 	b->max_size = new_size;
36 }
37 
38 
add_data(struct growable_buffer * b,void const * data,size_t len)39 void add_data(struct growable_buffer * b, void const * data, size_t len)
40 {
41 	size_t old_size = b->size;
42 	b->size += len;
43 	if (b->size > b->max_size)
44 		grow_buffer(b);
45 	memcpy(b->p + old_size, data, len);
46 }
47