Mercurial > repos > youngkim > ezbamqc
comparison ezBAMQC/src/htslib/cram/string_alloc.c @ 0:dfa3745e5fd8
Uploaded
author | youngkim |
---|---|
date | Thu, 24 Mar 2016 17:12:52 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:dfa3745e5fd8 |
---|---|
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 <string.h> | |
40 #include <stdlib.h> | |
41 #include <stdio.h> | |
42 | |
43 #include "string_alloc.h" | |
44 | |
45 #define MIN_STR_SIZE 1024 | |
46 | |
47 | |
48 /* creates the string pool. max_length is the initial size | |
49 a single string can be. Tha max_length can grow as | |
50 needed */ | |
51 | |
52 string_alloc_t *string_pool_create(size_t max_length) { | |
53 string_alloc_t *a_str; | |
54 | |
55 if (NULL == (a_str = (string_alloc_t *)malloc(sizeof(*a_str)))) { | |
56 return NULL; | |
57 } | |
58 | |
59 if (max_length < MIN_STR_SIZE) max_length = MIN_STR_SIZE; | |
60 | |
61 a_str->nstrings = 0; | |
62 a_str->max_length = max_length; | |
63 a_str->strings = NULL; | |
64 | |
65 return a_str; | |
66 } | |
67 | |
68 | |
69 /* internal function to do the actual memory allocation */ | |
70 | |
71 static string_t *new_string_pool(string_alloc_t *a_str) { | |
72 string_t *str; | |
73 | |
74 str = realloc(a_str->strings, (a_str->nstrings + 1) * sizeof(*a_str->strings)); | |
75 | |
76 if (NULL == str) return NULL; | |
77 | |
78 a_str->strings = str; | |
79 str = &a_str->strings[a_str->nstrings]; | |
80 | |
81 str->str = malloc(a_str->max_length);; | |
82 | |
83 if (NULL == str->str) return NULL; | |
84 | |
85 str->used = 0; | |
86 a_str->nstrings++; | |
87 | |
88 return str; | |
89 } | |
90 | |
91 | |
92 /* free allocated memory */ | |
93 | |
94 void string_pool_destroy(string_alloc_t *a_str) { | |
95 size_t i; | |
96 | |
97 for (i = 0; i < a_str->nstrings; i++) { | |
98 free(a_str->strings[i].str); | |
99 } | |
100 | |
101 free(a_str->strings); | |
102 free(a_str); | |
103 } | |
104 | |
105 | |
106 /* allocate space for a string */ | |
107 | |
108 char *string_alloc(string_alloc_t *a_str, size_t length) { | |
109 string_t *str; | |
110 char *ret; | |
111 | |
112 if (length <= 0) return NULL; | |
113 | |
114 // add to last string pool if we have space | |
115 if (a_str->nstrings) { | |
116 str = &a_str->strings[a_str->nstrings - 1]; | |
117 | |
118 if (str->used + length < a_str->max_length) { | |
119 ret = str->str + str->used; | |
120 str->used += length; | |
121 return ret; | |
122 } | |
123 } | |
124 | |
125 // increase the max length if needs be | |
126 if (length > a_str->max_length) a_str->max_length = length; | |
127 | |
128 // need a new string pool | |
129 str = new_string_pool(a_str); | |
130 | |
131 if (NULL == str) return NULL; | |
132 | |
133 str->used = length; | |
134 return str->str; | |
135 } | |
136 | |
137 | |
138 /* equivalent to strdup */ | |
139 | |
140 char *string_dup(string_alloc_t *a_str, char *instr) { | |
141 return string_ndup(a_str, instr, strlen(instr)); | |
142 } | |
143 | |
144 char *string_ndup(string_alloc_t *a_str, char *instr, size_t len) { | |
145 char *str = string_alloc(a_str, len + 1); | |
146 | |
147 if (NULL == str) return NULL; | |
148 | |
149 strncpy(str, instr, len); | |
150 str[len] = 0; | |
151 | |
152 return str; | |
153 } |