Note about test suites.
[rsync.git] / stringarea.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "rsync.h"
22
23 struct string_area {
24         char *base;
25         char *end;
26         char *current;
27         struct string_area *next;
28 };
29
30 struct string_area *string_area_new(int size)
31 {
32         struct string_area *a;
33
34         if (size <= 0)
35                 size = ARENA_SIZE;
36         a = malloc(sizeof(*a));
37         if (!a)
38                 out_of_memory("string_area_new");
39         a->current = a->base = malloc(size);
40         if (!a->current)
41                 out_of_memory("string_area_new buffer");
42         a->end = a->base + size;
43         a->next = NULL;
44
45         stats.string_areas += sizeof(*a) + size;
46
47         return a;
48 }
49
50 void string_area_free(struct string_area *a)
51 {
52         struct string_area *next;
53
54         for (; a; a = next) {
55                 next = a->next;
56                 free(a->base);
57         }
58 }
59
60 char *string_area_malloc(struct string_area **ap, int size)
61 {
62         char *p;
63         struct string_area *a;
64
65         /* does the request fit into the current space? */
66         a = *ap;
67         if (a->current + size >= a->end) {
68                 /* no; get space, move new string_area to front of the list */
69                 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
70                 a->next = *ap;
71                 *ap = a;
72         }
73
74         /* have space; do the "allocation." */
75         p = a->current;
76         a->current += size;
77         return p;
78 }
79
80 char *string_area_strdup(struct string_area **ap, const char *src)
81 {
82         char *dest = string_area_malloc(ap, strlen(src) + 1);
83         return strcpy(dest, src);
84 }
85