Coverage Report

Created: 2025-09-27 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/htslib/cram/string_alloc.c
Line
Count
Source
1
/*
2
Copyright (c) 2010, 2013, 2018-2019 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
#define HTS_BUILDING_LIBRARY // Enables HTSLIB_EXPORT, see htslib/hts_defs.h
40
#include <config.h>
41
42
#include <string.h>
43
#include <stdlib.h>
44
#include <stdio.h>
45
46
#include "string_alloc.h"
47
48
106k
#define MIN_STR_SIZE 1024
49
50
51
/* creates the string pool. max_length is the initial size
52
   a single string can be.  The max_length can grow as
53
   needed */
54
55
106k
string_alloc_t *string_pool_create(size_t max_length) {
56
106k
    string_alloc_t *a_str;
57
58
106k
    if (NULL == (a_str = (string_alloc_t *)malloc(sizeof(*a_str)))) {
59
0
        return NULL;
60
0
    }
61
62
106k
    if (max_length < MIN_STR_SIZE) max_length = MIN_STR_SIZE;
63
64
106k
    a_str->nstrings    = 0;
65
106k
    a_str->max_strings = 0;
66
106k
    a_str->max_length  = max_length;
67
106k
    a_str->strings     = NULL;
68
69
106k
    return a_str;
70
106k
}
71
72
73
/* internal function to do the actual memory allocation */
74
75
60.3k
static string_t *new_string_pool(string_alloc_t *a_str) {
76
60.3k
    string_t *str;
77
78
60.3k
    if (a_str->nstrings == a_str->max_strings) {
79
60.1k
        size_t new_max = (a_str->max_strings | (a_str->max_strings >> 2)) + 1;
80
60.1k
        str = realloc(a_str->strings, new_max * sizeof(*a_str->strings));
81
82
60.1k
        if (NULL == str) return NULL;
83
84
60.1k
        a_str->strings = str;
85
60.1k
        a_str->max_strings = new_max;
86
60.1k
    }
87
88
60.3k
    str = &a_str->strings[a_str->nstrings];
89
90
60.3k
    str->str = malloc(a_str->max_length);
91
92
60.3k
    if (NULL == str->str) return NULL;
93
94
60.3k
    str->used = 0;
95
60.3k
    a_str->nstrings++;
96
97
60.3k
    return str;
98
60.3k
}
99
100
101
/* free allocated memory */
102
103
106k
void string_pool_destroy(string_alloc_t *a_str) {
104
106k
    size_t i;
105
106
166k
    for (i = 0; i < a_str->nstrings; i++) {
107
60.3k
        free(a_str->strings[i].str);
108
60.3k
    }
109
110
106k
    free(a_str->strings);
111
106k
    free(a_str);
112
106k
}
113
114
115
/* allocate space for a string */
116
117
6.20M
char *string_alloc(string_alloc_t *a_str, size_t length) {
118
6.20M
    string_t *str;
119
6.20M
    char *ret;
120
121
6.20M
    if (length <= 0) return NULL;
122
123
    // add to last string pool if we have space
124
6.20M
    if (a_str->nstrings) {
125
6.14M
        str = &a_str->strings[a_str->nstrings - 1];
126
127
6.14M
        if (str->used + length < a_str->max_length) {
128
6.14M
            ret = str->str + str->used;
129
6.14M
            str->used += length;
130
6.14M
            return ret;
131
6.14M
        }
132
6.14M
    }
133
134
    // increase the max length if needs be
135
60.3k
    if (length > a_str->max_length) a_str->max_length = length;
136
137
    // need a new string pool
138
60.3k
    str = new_string_pool(a_str);
139
140
60.3k
    if (NULL == str) return NULL;
141
142
60.3k
    str->used = length;
143
60.3k
    return str->str;
144
60.3k
}
145
146
147
/* equivalent to strdup */
148
149
6.40k
char *string_dup(string_alloc_t *a_str, const char *instr) {
150
6.40k
    return string_ndup(a_str, instr, strlen(instr));
151
6.40k
}
152
153
6.20M
char *string_ndup(string_alloc_t *a_str, const char *instr, size_t len) {
154
6.20M
    char *str = string_alloc(a_str, len + 1);
155
156
6.20M
    if (NULL == str) return NULL;
157
158
6.20M
    memcpy(str, instr, len);
159
6.20M
    str[len] = 0;
160
161
6.20M
    return str;
162
6.20M
}