1 /*
2 Copyright (c) 2010 Genome Research Ltd.
3 Author: Andrew Whitwham <aw7@sanger.ac.uk>
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
16 Institute nor the names of its contributors may be used to endorse or promote
17 products derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32 /*
33 A pooled string allocator intended to cut down on the
34 memory overhead of many small string allocations.
35
36 Andrew Whitwham, September 2010.
37 */
38
39 #include <config.h>
40
41 #include <string.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44
45 #include "cram/string_alloc.h"
46
47 #define MIN_STR_SIZE 1024
48
49
50 /* creates the string pool. max_length is the initial size
51 a single string can be. Tha max_length can grow as
52 needed */
53
string_pool_create(size_t max_length)54 string_alloc_t *string_pool_create(size_t max_length) {
55 string_alloc_t *a_str;
56
57 if (NULL == (a_str = (string_alloc_t *)malloc(sizeof(*a_str)))) {
58 return NULL;
59 }
60
61 if (max_length < MIN_STR_SIZE) max_length = MIN_STR_SIZE;
62
63 a_str->nstrings = 0;
64 a_str->max_length = max_length;
65 a_str->strings = NULL;
66
67 return a_str;
68 }
69
70
71 /* internal function to do the actual memory allocation */
72
new_string_pool(string_alloc_t * a_str)73 static string_t *new_string_pool(string_alloc_t *a_str) {
74 string_t *str;
75
76 str = realloc(a_str->strings, (a_str->nstrings + 1) * sizeof(*a_str->strings));
77
78 if (NULL == str) return NULL;
79
80 a_str->strings = str;
81 str = &a_str->strings[a_str->nstrings];
82
83 str->str = malloc(a_str->max_length);;
84
85 if (NULL == str->str) return NULL;
86
87 str->used = 0;
88 a_str->nstrings++;
89
90 return str;
91 }
92
93
94 /* free allocated memory */
95
string_pool_destroy(string_alloc_t * a_str)96 void string_pool_destroy(string_alloc_t *a_str) {
97 size_t i;
98
99 for (i = 0; i < a_str->nstrings; i++) {
100 free(a_str->strings[i].str);
101 }
102
103 free(a_str->strings);
104 free(a_str);
105 }
106
107
108 /* allocate space for a string */
109
string_alloc(string_alloc_t * a_str,size_t length)110 char *string_alloc(string_alloc_t *a_str, size_t length) {
111 string_t *str;
112 char *ret;
113
114 if (length <= 0) return NULL;
115
116 // add to last string pool if we have space
117 if (a_str->nstrings) {
118 str = &a_str->strings[a_str->nstrings - 1];
119
120 if (str->used + length < a_str->max_length) {
121 ret = str->str + str->used;
122 str->used += length;
123 return ret;
124 }
125 }
126
127 // increase the max length if needs be
128 if (length > a_str->max_length) a_str->max_length = length;
129
130 // need a new string pool
131 str = new_string_pool(a_str);
132
133 if (NULL == str) return NULL;
134
135 str->used = length;
136 return str->str;
137 }
138
139
140 /* equivalent to strdup */
141
string_dup(string_alloc_t * a_str,char * instr)142 char *string_dup(string_alloc_t *a_str, char *instr) {
143 return string_ndup(a_str, instr, strlen(instr));
144 }
145
string_ndup(string_alloc_t * a_str,char * instr,size_t len)146 char *string_ndup(string_alloc_t *a_str, char *instr, size_t len) {
147 char *str = string_alloc(a_str, len + 1);
148
149 if (NULL == str) return NULL;
150
151 strncpy(str, instr, len);
152 str[len] = 0;
153
154 return str;
155 }
156