disk_cache: fix a memory leak
[mesa.git] / src / util / string_buffer.h
1 /*
2 * Copyright © 2017 Thomas Helland
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24 #ifndef _STRING_BUFFER_H
25 #define _STRING_BUFFER_H
26
27 #include "ralloc.h"
28 #include "u_string.h"
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 struct _mesa_string_buffer {
39 char *buf;
40 uint32_t length;
41 uint32_t capacity;
42 };
43
44 struct _mesa_string_buffer *
45 _mesa_string_buffer_create(void *mem_ctx, uint32_t initial_capacity);
46
47 static inline void
48 _mesa_string_buffer_destroy(struct _mesa_string_buffer *str)
49 {
50 ralloc_free(str);
51 }
52
53 bool
54 _mesa_string_buffer_append_all(struct _mesa_string_buffer *str,
55 uint32_t num_args, ...);
56 bool
57 _mesa_string_buffer_append_len(struct _mesa_string_buffer *str,
58 const char *c, uint32_t len);
59
60 static inline bool
61 _mesa_string_buffer_append_char(struct _mesa_string_buffer *str, char c)
62 {
63 return _mesa_string_buffer_append_len(str, &c, 1);
64 }
65
66 static inline bool
67 _mesa_string_buffer_append(struct _mesa_string_buffer *str, const char *c)
68 {
69 return _mesa_string_buffer_append_len(str, c, strlen(c));
70 }
71
72 static inline void
73 _mesa_string_buffer_clear(struct _mesa_string_buffer *str)
74 {
75 str->length = 0;
76 str->buf[str->length] = '\0';
77 }
78
79 static inline void
80 _mesa_string_buffer_crimp_to_fit(struct _mesa_string_buffer *str)
81 {
82 char *crimped =
83 (char *) reralloc_array_size(str, str->buf, sizeof(char),
84 str->capacity);
85 if (!crimped)
86 return;
87
88 str->capacity = str->length + 1;
89 str->buf = crimped;
90 }
91
92 bool
93 _mesa_string_buffer_vprintf(struct _mesa_string_buffer *str,
94 const char *format, va_list args);
95
96 bool
97 _mesa_string_buffer_printf(struct _mesa_string_buffer *str,
98 const char *format, ...);
99
100 #ifdef __cplusplus
101 } /* extern "C" */
102 #endif
103
104 #endif /* _STRING_BUFFER_H */