Line | Count | Source |
1 | | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | | * Copyright by The HDF Group. * |
3 | | * All rights reserved. * |
4 | | * * |
5 | | * This file is part of HDF5. The full HDF5 copyright notice, including * |
6 | | * terms governing use, modification, and redistribution, is contained in * |
7 | | * the LICENSE file, which can be found at the root of the source code * |
8 | | * distribution tree, or in https://www.hdfgroup.org/licenses. * |
9 | | * If you do not have access to either file, you may request a copy from * |
10 | | * help@hdfgroup.org. * |
11 | | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
12 | | |
13 | | /* |
14 | | * Reference counted string algorithms. |
15 | | * |
16 | | * These are used for various internal strings which get copied multiple times. |
17 | | * They also efficiently handle dynamically allocations and appends. |
18 | | * |
19 | | */ |
20 | | |
21 | | /****************/ |
22 | | /* Module Setup */ |
23 | | /****************/ |
24 | | |
25 | | #include "H5RSmodule.h" /* This source code file is part of the H5RS module */ |
26 | | |
27 | | /***********/ |
28 | | /* Headers */ |
29 | | /***********/ |
30 | | #include "H5private.h" /* Generic Functions */ |
31 | | #include "H5Eprivate.h" /* Error handling */ |
32 | | #include "H5FLprivate.h" /* Free lists */ |
33 | | #include "H5MMprivate.h" /* Memory management */ |
34 | | #include "H5RSprivate.h" /* Reference-counted strings */ |
35 | | |
36 | | /****************/ |
37 | | /* Local Macros */ |
38 | | /****************/ |
39 | | |
40 | | /* Initial buffer size to allocate */ |
41 | 2.54k | #define H5RS_ALLOC_SIZE 256 |
42 | | |
43 | | /******************/ |
44 | | /* Local Typedefs */ |
45 | | /******************/ |
46 | | |
47 | | /* Private typedefs & structs */ |
48 | | struct H5RS_str_t { |
49 | | char *s; /* String to be reference counted */ |
50 | | char *end; /* Pointer to terminating NUL character at the end of the string */ |
51 | | size_t len; /* Current length of the string */ |
52 | | size_t max; /* Size of allocated buffer */ |
53 | | bool wrapped; /* Indicates that the string to be ref-counted is not copied */ |
54 | | unsigned n; /* Reference count of number of pointers sharing string */ |
55 | | }; |
56 | | |
57 | | /********************/ |
58 | | /* Package Typedefs */ |
59 | | /********************/ |
60 | | |
61 | | /********************/ |
62 | | /* Local Prototypes */ |
63 | | /********************/ |
64 | | static herr_t H5RS__xstrdup(H5RS_str_t *rs, const char *s); |
65 | | static herr_t H5RS__prepare_for_append(H5RS_str_t *rs); |
66 | | static herr_t H5RS__resize_for_append(H5RS_str_t *rs, size_t len); |
67 | | |
68 | | /*********************/ |
69 | | /* Package Variables */ |
70 | | /*********************/ |
71 | | |
72 | | /* Package initialization variable */ |
73 | | bool H5_PKG_INIT_VAR = false; |
74 | | |
75 | | /*****************************/ |
76 | | /* Library Private Variables */ |
77 | | /*****************************/ |
78 | | |
79 | | /*******************/ |
80 | | /* Local Variables */ |
81 | | /*******************/ |
82 | | |
83 | | /* Declare a free list to manage the H5RS_str_t struct */ |
84 | | H5FL_DEFINE_STATIC(H5RS_str_t); |
85 | | |
86 | | /* Declare the PQ free list for the wrapped strings */ |
87 | | H5FL_BLK_DEFINE_STATIC(str_buf); |
88 | | |
89 | | /*-------------------------------------------------------------------------- |
90 | | NAME |
91 | | H5RS__xstrdup |
92 | | PURPOSE |
93 | | Duplicate the string being reference counted |
94 | | USAGE |
95 | | herr_t H5RS__xstrdup(rs, s) |
96 | | H5RS_str_t *rs; IN/OUT: Ref-counted string to hold duplicated string |
97 | | const char *s; IN: String to duplicate |
98 | | |
99 | | RETURNS |
100 | | Non-negative on success/Negative on failure |
101 | | DESCRIPTION |
102 | | Duplicate a string buffer being reference counted. Use this instead of |
103 | | [H5MM_][x]strdup, in order to use the free-list memory routines. |
104 | | GLOBAL VARIABLES |
105 | | COMMENTS, BUGS, ASSUMPTIONS |
106 | | EXAMPLES |
107 | | REVISION LOG |
108 | | --------------------------------------------------------------------------*/ |
109 | | static herr_t |
110 | | H5RS__xstrdup(H5RS_str_t *rs, const char *s) |
111 | 2.54k | { |
112 | 2.54k | herr_t ret_value = SUCCEED; /* Return value */ |
113 | | |
114 | 2.54k | FUNC_ENTER_PACKAGE |
115 | | |
116 | | /* Sanity check */ |
117 | 2.54k | assert(rs); |
118 | | |
119 | 2.54k | if (s) { |
120 | 2.54k | size_t len = strlen(s); |
121 | | |
122 | | /* Determine size of buffer to allocate */ |
123 | 2.54k | rs->max = H5RS_ALLOC_SIZE; |
124 | 2.54k | while ((len + 1) > rs->max) |
125 | 0 | rs->max *= 2; |
126 | | |
127 | | /* Allocate the underlying string */ |
128 | 2.54k | if (NULL == (rs->s = (char *)H5FL_BLK_MALLOC(str_buf, rs->max))) |
129 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTALLOC, FAIL, "memory allocation failed"); |
130 | 2.54k | if (len) |
131 | 2.54k | H5MM_memcpy(rs->s, s, len); |
132 | 2.54k | rs->end = rs->s + len; |
133 | 2.54k | *rs->end = '\0'; |
134 | 2.54k | rs->len = len; |
135 | 2.54k | } /* end if */ |
136 | 0 | else { |
137 | | /* Free previous string, if one */ |
138 | 0 | if (rs->s) { |
139 | 0 | H5FL_BLK_FREE(str_buf, rs->s); |
140 | 0 | rs->s = rs->end = NULL; |
141 | 0 | rs->max = rs->len = 0; |
142 | 0 | } /* end if */ |
143 | 0 | else { |
144 | | /* Sanity checks */ |
145 | 0 | assert(NULL == rs->end); |
146 | 0 | assert(0 == rs->max); |
147 | 0 | assert(0 == rs->len); |
148 | 0 | } /* end else */ |
149 | 0 | } /* end else */ |
150 | | |
151 | 2.54k | done: |
152 | 2.54k | FUNC_LEAVE_NOAPI(ret_value) |
153 | 2.54k | } /* end H5RS__xstrdup() */ |
154 | | |
155 | | /*-------------------------------------------------------------------------- |
156 | | NAME |
157 | | H5RS__prepare_for_append |
158 | | PURPOSE |
159 | | Prepare a ref-counted string for an append |
160 | | USAGE |
161 | | herr_t H5RS__prepare_for_append(rs) |
162 | | H5RS_str_t *rs; IN/OUT: Ref-counted string to hold duplicated string |
163 | | RETURNS |
164 | | Non-negative on success/Negative on failure |
165 | | DESCRIPTION |
166 | | Allocate space for a string, or duplicate a wrapped string, in preparation |
167 | | for appending another string. |
168 | | GLOBAL VARIABLES |
169 | | COMMENTS, BUGS, ASSUMPTIONS |
170 | | EXAMPLES |
171 | | REVISION LOG |
172 | | --------------------------------------------------------------------------*/ |
173 | | static herr_t |
174 | | H5RS__prepare_for_append(H5RS_str_t *rs) |
175 | 1.18k | { |
176 | 1.18k | herr_t ret_value = SUCCEED; /* Return value */ |
177 | | |
178 | 1.18k | FUNC_ENTER_PACKAGE |
179 | | |
180 | | /* Sanity check */ |
181 | 1.18k | assert(rs); |
182 | | |
183 | 1.18k | if (NULL == rs->s) { |
184 | 0 | rs->max = H5RS_ALLOC_SIZE; |
185 | 0 | if (NULL == (rs->s = (char *)H5FL_BLK_MALLOC(str_buf, rs->max))) |
186 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTALLOC, FAIL, "memory allocation failed"); |
187 | 0 | rs->end = rs->s; |
188 | 0 | *rs->s = '\0'; |
189 | 0 | rs->len = 0; |
190 | 0 | } /* end if */ |
191 | 1.18k | else { |
192 | | /* If the ref-counted string started life as a wrapper around an |
193 | | * existing string, duplicate the string now, so we can modify it. |
194 | | */ |
195 | 1.18k | if (rs->wrapped) { |
196 | 0 | if (H5RS__xstrdup(rs, rs->s) < 0) |
197 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTCOPY, FAIL, "can't copy string"); |
198 | 0 | rs->wrapped = false; |
199 | 0 | } /* end if */ |
200 | 1.18k | } /* end else */ |
201 | | |
202 | 1.18k | done: |
203 | 1.18k | FUNC_LEAVE_NOAPI(ret_value) |
204 | 1.18k | } /* end H5RS__prepare_for_append() */ |
205 | | |
206 | | /*-------------------------------------------------------------------------- |
207 | | NAME |
208 | | H5RS__resize_for_append |
209 | | PURPOSE |
210 | | Resize ref-counted string buffer to accommodate appending another string |
211 | | USAGE |
212 | | herr_t H5RS__resize_for_append(rs, len) |
213 | | H5RS_str_t *rs; IN/OUT: Ref-counted string to hold duplicated string |
214 | | size_t len; IN: Additional length to accommodate |
215 | | RETURNS |
216 | | Non-negative on success/Negative on failure |
217 | | DESCRIPTION |
218 | | Resize a ref-counted string buffer to be large enough to accommodate |
219 | | another string of a specified length. |
220 | | GLOBAL VARIABLES |
221 | | COMMENTS, BUGS, ASSUMPTIONS |
222 | | EXAMPLES |
223 | | REVISION LOG |
224 | | --------------------------------------------------------------------------*/ |
225 | | static herr_t |
226 | | H5RS__resize_for_append(H5RS_str_t *rs, size_t len) |
227 | 0 | { |
228 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
229 | |
|
230 | 0 | FUNC_ENTER_PACKAGE |
231 | | |
232 | | /* Sanity check */ |
233 | 0 | assert(rs); |
234 | | |
235 | | /* Check if buffer should be re-allocated */ |
236 | 0 | if (len >= (rs->max - rs->len)) { |
237 | | /* Allocate a large enough buffer */ |
238 | 0 | while (len >= (rs->max - rs->len)) |
239 | 0 | rs->max *= 2; |
240 | 0 | if (NULL == (rs->s = (char *)H5FL_BLK_REALLOC(str_buf, rs->s, rs->max))) |
241 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTALLOC, FAIL, "memory allocation failed"); |
242 | 0 | rs->end = rs->s + rs->len; |
243 | 0 | } /* end if */ |
244 | | |
245 | 0 | done: |
246 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
247 | 0 | } /* end H5RS__resize() */ |
248 | | |
249 | | /*-------------------------------------------------------------------------- |
250 | | NAME |
251 | | H5RS_create |
252 | | PURPOSE |
253 | | Create a reference counted string |
254 | | USAGE |
255 | | H5RS_str_t *H5RS_create(s) |
256 | | const char *s; IN: String to initialize ref-counted string with |
257 | | |
258 | | RETURNS |
259 | | Returns a pointer to a new ref-counted string on success, NULL on failure. |
260 | | DESCRIPTION |
261 | | Create a reference counted string. The string passed in is copied into an |
262 | | internal buffer. |
263 | | GLOBAL VARIABLES |
264 | | COMMENTS, BUGS, ASSUMPTIONS |
265 | | EXAMPLES |
266 | | REVISION LOG |
267 | | --------------------------------------------------------------------------*/ |
268 | | H5RS_str_t * |
269 | | H5RS_create(const char *s) |
270 | 2.54k | { |
271 | 2.54k | H5RS_str_t *ret_value = NULL; /* Return value */ |
272 | | |
273 | 2.54k | FUNC_ENTER_NOAPI(NULL) |
274 | | |
275 | | /* Allocate ref-counted string structure */ |
276 | 2.54k | if (NULL == (ret_value = H5FL_CALLOC(H5RS_str_t))) |
277 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTALLOC, NULL, "memory allocation failed"); |
278 | | |
279 | | /* Set the internal fields */ |
280 | 2.54k | if (s) |
281 | 2.54k | if (H5RS__xstrdup(ret_value, s) < 0) |
282 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTCOPY, NULL, "can't copy string"); |
283 | 2.54k | ret_value->n = 1; |
284 | | |
285 | 2.54k | done: |
286 | 2.54k | FUNC_LEAVE_NOAPI(ret_value) |
287 | 2.54k | } /* end H5RS_create() */ |
288 | | |
289 | | /*-------------------------------------------------------------------------- |
290 | | NAME |
291 | | H5RS_wrap |
292 | | PURPOSE |
293 | | "Wrap" a reference counted string around an existing string |
294 | | USAGE |
295 | | H5RS_str_t *H5RS_wrap(s) |
296 | | const char *s; IN: String to wrap ref-counted string around |
297 | | |
298 | | RETURNS |
299 | | Returns a pointer to a new ref-counted string on success, NULL on failure. |
300 | | DESCRIPTION |
301 | | Wrap a reference counted string around an existing string, which is not |
302 | | duplicated, unless its reference count gets incremented. |
303 | | GLOBAL VARIABLES |
304 | | COMMENTS, BUGS, ASSUMPTIONS |
305 | | EXAMPLES |
306 | | REVISION LOG |
307 | | --------------------------------------------------------------------------*/ |
308 | | H5RS_str_t * |
309 | | H5RS_wrap(const char *s) |
310 | 0 | { |
311 | 0 | H5RS_str_t *ret_value = NULL; /* Return value */ |
312 | |
|
313 | 0 | FUNC_ENTER_NOAPI(NULL) |
314 | | |
315 | | /* Allocate ref-counted string structure */ |
316 | 0 | if (NULL == (ret_value = H5FL_MALLOC(H5RS_str_t))) |
317 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTALLOC, NULL, "memory allocation failed"); |
318 | | |
319 | | /* Set the internal fields |
320 | | * |
321 | | * We ignore warnings about storing a const char pointer in the struct |
322 | | * since we never modify or free the string when the wrapped struct |
323 | | * field is set to true. |
324 | | */ |
325 | 0 | H5_WARN_CAST_AWAY_CONST_OFF |
326 | 0 | ret_value->s = (char *)s; |
327 | 0 | H5_WARN_CAST_AWAY_CONST_ON |
328 | |
|
329 | 0 | ret_value->len = strlen(s); |
330 | 0 | ret_value->end = ret_value->s + ret_value->len; |
331 | |
|
332 | 0 | ret_value->wrapped = true; |
333 | 0 | ret_value->max = 0; /* Wrapped, not allocated */ |
334 | 0 | ret_value->n = 1; |
335 | |
|
336 | 0 | done: |
337 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
338 | 0 | } /* end H5RS_wrap() */ |
339 | | |
340 | | /*------------------------------------------------------------------------- |
341 | | * Function: H5RS_asprintf_cat |
342 | | * |
343 | | * Purpose: This function appends formatted output to a ref-counted string, |
344 | | * allocating the managed string if necessary. The formatting |
345 | | * string is printf() compatible. |
346 | | * |
347 | | * Return: SUCCEED / FAIL |
348 | | * |
349 | | *------------------------------------------------------------------------- |
350 | | */ |
351 | | H5_ATTR_FORMAT(printf, 2, 3) |
352 | | herr_t |
353 | | H5RS_asprintf_cat(H5RS_str_t *rs, const char *fmt, ...) |
354 | 0 | { |
355 | 0 | va_list args1, args2; |
356 | 0 | size_t out_len; |
357 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
358 | |
|
359 | 0 | FUNC_ENTER_NOAPI(FAIL) |
360 | | |
361 | | /* Sanity checks */ |
362 | 0 | assert(rs); |
363 | 0 | assert(fmt); |
364 | | |
365 | | /* Prepare the [possibly wrapped or empty] ref-counted string for an append */ |
366 | 0 | if (H5RS__prepare_for_append(rs) < 0) |
367 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTINIT, FAIL, "can't initialize ref-counted string"); |
368 | | |
369 | | /* Attempt to write formatted output into the managed string */ |
370 | 0 | va_start(args1, fmt); |
371 | 0 | va_copy(args2, args1); |
372 | 0 | while ((out_len = (size_t)vsnprintf(rs->end, (rs->max - rs->len), fmt, args1)) >= (rs->max - rs->len)) { |
373 | | /* Allocate a large enough buffer */ |
374 | 0 | if (H5RS__resize_for_append(rs, out_len) < 0) |
375 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTRESIZE, FAIL, "can't resize ref-counted string buffer"); |
376 | | |
377 | | /* Restart the va_list */ |
378 | 0 | va_end(args1); |
379 | 0 | va_copy(args1, args2); |
380 | 0 | } /* end while */ |
381 | | |
382 | | /* Increment the size & end of the string */ |
383 | 0 | rs->len += out_len; |
384 | 0 | rs->end += out_len; |
385 | | |
386 | | /* Finish access to varargs */ |
387 | 0 | va_end(args1); |
388 | 0 | va_end(args2); |
389 | |
|
390 | 0 | done: |
391 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
392 | 0 | } /* end H5RS_asprintf_cat() */ |
393 | | |
394 | | /*------------------------------------------------------------------------- |
395 | | * Function: H5RS_acat |
396 | | * |
397 | | * Purpose: This function appends a character string to a ref-counted string, |
398 | | * allocating the managed string if necessary. |
399 | | * |
400 | | * Return: SUCCEED / FAIL |
401 | | * |
402 | | *------------------------------------------------------------------------- |
403 | | */ |
404 | | herr_t |
405 | | H5RS_acat(H5RS_str_t *rs, const char *s) |
406 | 1.18k | { |
407 | 1.18k | herr_t ret_value = SUCCEED; /* Return value */ |
408 | | |
409 | 1.18k | FUNC_ENTER_NOAPI(FAIL) |
410 | | |
411 | | /* Sanity checks */ |
412 | 1.18k | assert(rs); |
413 | 1.18k | assert(s); |
414 | | |
415 | | /* Concatenate the provided string on to the managed string */ |
416 | 1.18k | if (*s) { |
417 | 1.18k | size_t len = strlen(s); |
418 | | |
419 | | /* Allocate the underlying string, if necessary */ |
420 | 1.18k | if (H5RS__prepare_for_append(rs) < 0) |
421 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTINIT, FAIL, "can't initialize ref-counted string"); |
422 | | |
423 | | /* Increase the managed string's buffer size if necessary */ |
424 | 1.18k | if ((rs->len + len) >= rs->max) |
425 | 0 | if (H5RS__resize_for_append(rs, len) < 0) |
426 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTRESIZE, FAIL, "can't resize ref-counted string buffer"); |
427 | | |
428 | | /* Append the string */ |
429 | 1.18k | H5MM_memcpy(rs->end, s, len); |
430 | 1.18k | rs->end += len; |
431 | 1.18k | *rs->end = '\0'; |
432 | 1.18k | rs->len += len; |
433 | 1.18k | } /* end if */ |
434 | | |
435 | 1.18k | done: |
436 | 1.18k | FUNC_LEAVE_NOAPI(ret_value) |
437 | 1.18k | } /* end H5RS_acat() */ |
438 | | |
439 | | /*------------------------------------------------------------------------- |
440 | | * Function: H5RS_ancat |
441 | | * |
442 | | * Purpose: This function appends at most 'n' characters from a string |
443 | | * to a ref-counted string, allocating the managed string if |
444 | | * necessary. |
445 | | * |
446 | | * Return: SUCCEED / FAIL |
447 | | * |
448 | | *------------------------------------------------------------------------- |
449 | | */ |
450 | | herr_t |
451 | | H5RS_ancat(H5RS_str_t *rs, const char *s, size_t n) |
452 | 0 | { |
453 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
454 | |
|
455 | 0 | FUNC_ENTER_NOAPI(FAIL) |
456 | | |
457 | | /* Sanity checks */ |
458 | 0 | assert(rs); |
459 | 0 | assert(s); |
460 | | |
461 | | /* Concatenate the provided string on to the managed string */ |
462 | 0 | if (n && *s) { |
463 | 0 | size_t len = strlen(s); |
464 | | |
465 | | /* Limit characters to copy to the minimum of 'n' and 'len' */ |
466 | 0 | n = MIN(len, n); |
467 | | |
468 | | /* Allocate the underlying string, if necessary */ |
469 | 0 | if (H5RS__prepare_for_append(rs) < 0) |
470 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTINIT, FAIL, "can't initialize ref-counted string"); |
471 | | |
472 | | /* Increase the managed string's buffer size if necessary */ |
473 | 0 | if ((rs->len + n) >= rs->max) |
474 | 0 | if (H5RS__resize_for_append(rs, n) < 0) |
475 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTRESIZE, FAIL, "can't resize ref-counted string buffer"); |
476 | | |
477 | | /* Append the string */ |
478 | 0 | H5MM_memcpy(rs->end, s, n); |
479 | 0 | rs->end += n; |
480 | 0 | *rs->end = '\0'; |
481 | 0 | rs->len += n; |
482 | 0 | } /* end if */ |
483 | | |
484 | 0 | done: |
485 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
486 | 0 | } /* end H5RS_ancat() */ |
487 | | |
488 | | /*------------------------------------------------------------------------- |
489 | | * Function: H5RS_aputc |
490 | | * |
491 | | * Purpose: This function appends a character to a ref-counted string, |
492 | | * allocating the managed string if necessary. |
493 | | * |
494 | | * Return: SUCCEED / FAIL |
495 | | * |
496 | | *------------------------------------------------------------------------- |
497 | | */ |
498 | | herr_t |
499 | | H5RS_aputc(H5RS_str_t *rs, int c) |
500 | 0 | { |
501 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
502 | |
|
503 | 0 | FUNC_ENTER_NOAPI(FAIL) |
504 | | |
505 | | /* Sanity checks */ |
506 | 0 | assert(rs); |
507 | 0 | assert(c); |
508 | | |
509 | | /* Allocate the underlying string, if necessary */ |
510 | 0 | if (H5RS__prepare_for_append(rs) < 0) |
511 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTINIT, FAIL, "can't initialize ref-counted string"); |
512 | | |
513 | | /* Increase the managed string's buffer size if necessary */ |
514 | 0 | if ((rs->len + 1) >= rs->max) |
515 | 0 | if (H5RS__resize_for_append(rs, 1) < 0) |
516 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTRESIZE, FAIL, "can't resize ref-counted string buffer"); |
517 | | |
518 | | /* Append the current character */ |
519 | 0 | *rs->end++ = (char)c; |
520 | 0 | rs->len++; |
521 | 0 | *rs->end = '\0'; |
522 | |
|
523 | 0 | done: |
524 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
525 | 0 | } /* end H5RS_aputc() */ |
526 | | |
527 | | /*-------------------------------------------------------------------------- |
528 | | NAME |
529 | | H5RS_decr |
530 | | PURPOSE |
531 | | Decrement the reference count for a ref-counted string |
532 | | USAGE |
533 | | herr_t H5RS_decr(rs) |
534 | | H5RS_str_t *rs; IN/OUT: Ref-counted string to decrement count of |
535 | | |
536 | | RETURNS |
537 | | Non-negative on success/Negative on failure |
538 | | DESCRIPTION |
539 | | Decrement the reference count for a reference counted string. If the |
540 | | reference count drops to zero, the reference counted string is deleted. |
541 | | GLOBAL VARIABLES |
542 | | COMMENTS, BUGS, ASSUMPTIONS |
543 | | EXAMPLES |
544 | | REVISION LOG |
545 | | --------------------------------------------------------------------------*/ |
546 | | herr_t |
547 | | H5RS_decr(H5RS_str_t *rs) |
548 | 3.91k | { |
549 | 3.91k | FUNC_ENTER_NOAPI_NOINIT_NOERR |
550 | | |
551 | | /* Sanity check */ |
552 | 3.91k | assert(rs); |
553 | 3.91k | assert(rs->n > 0); |
554 | | |
555 | | /* Decrement reference count for string */ |
556 | 3.91k | if ((--rs->n) == 0) { |
557 | 2.54k | if (!rs->wrapped) |
558 | 2.54k | rs->s = (char *)H5FL_BLK_FREE(str_buf, rs->s); |
559 | 2.54k | rs = H5FL_FREE(H5RS_str_t, rs); |
560 | 2.54k | } /* end if */ |
561 | | |
562 | 3.91k | FUNC_LEAVE_NOAPI(SUCCEED) |
563 | 3.91k | } /* end H5RS_decr() */ |
564 | | |
565 | | /*-------------------------------------------------------------------------- |
566 | | NAME |
567 | | H5RS_incr |
568 | | PURPOSE |
569 | | Increment the reference count for a ref-counted string |
570 | | USAGE |
571 | | herr_t H5RS_incr(rs) |
572 | | H5RS_str_t *rs; IN/OUT: Ref-counted string to increment count of |
573 | | |
574 | | RETURNS |
575 | | Non-negative on success/Negative on failure |
576 | | DESCRIPTION |
577 | | Increment the reference count for a reference counted string. |
578 | | GLOBAL VARIABLES |
579 | | COMMENTS, BUGS, ASSUMPTIONS |
580 | | EXAMPLES |
581 | | REVISION LOG |
582 | | --------------------------------------------------------------------------*/ |
583 | | herr_t |
584 | | H5RS_incr(H5RS_str_t *rs) |
585 | 0 | { |
586 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
587 | |
|
588 | 0 | FUNC_ENTER_NOAPI(FAIL) |
589 | | |
590 | | /* Sanity check */ |
591 | 0 | assert(rs); |
592 | 0 | assert(rs->n > 0); |
593 | | |
594 | | /* If the ref-counted string started life as a wrapper around an existing |
595 | | * string, duplicate the string now, so that the wrapped string can go out |
596 | | * scope appropriately. |
597 | | */ |
598 | 0 | if (rs->wrapped) { |
599 | 0 | if (H5RS__xstrdup(rs, rs->s) < 0) |
600 | 0 | HGOTO_ERROR(H5E_RS, H5E_CANTCOPY, FAIL, "can't copy string"); |
601 | 0 | rs->wrapped = false; |
602 | 0 | } /* end if */ |
603 | | |
604 | | /* Increment reference count for string */ |
605 | 0 | rs->n++; |
606 | |
|
607 | 0 | done: |
608 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
609 | 0 | } /* end H5RS_incr() */ |
610 | | |
611 | | /*-------------------------------------------------------------------------- |
612 | | NAME |
613 | | H5RS_dup |
614 | | PURPOSE |
615 | | "Duplicate" a ref-counted string |
616 | | USAGE |
617 | | H5RS_str_t H5RS_dup(rs) |
618 | | H5RS_str_t *rs; IN/OUT: Ref-counted string to "duplicate" |
619 | | |
620 | | RETURNS |
621 | | Returns a pointer to ref-counted string on success, NULL on failure. |
622 | | DESCRIPTION |
623 | | Increment the reference count for the reference counted string and return |
624 | | a pointer to it. |
625 | | GLOBAL VARIABLES |
626 | | COMMENTS, BUGS, ASSUMPTIONS |
627 | | EXAMPLES |
628 | | REVISION LOG |
629 | | --------------------------------------------------------------------------*/ |
630 | | H5RS_str_t * |
631 | | H5RS_dup(H5RS_str_t *ret_value) |
632 | 1.36k | { |
633 | 1.36k | FUNC_ENTER_NOAPI_NOINIT_NOERR |
634 | | |
635 | | /* Check for valid reference counted string */ |
636 | 1.36k | if (ret_value != NULL) |
637 | | /* Increment reference count for string */ |
638 | 1.36k | ret_value->n++; |
639 | | |
640 | 1.36k | FUNC_LEAVE_NOAPI(ret_value) |
641 | 1.36k | } /* end H5RS_dup() */ |
642 | | |
643 | | /*-------------------------------------------------------------------------- |
644 | | NAME |
645 | | H5RS_cmp |
646 | | PURPOSE |
647 | | Compare two ref-counted strings |
648 | | USAGE |
649 | | int H5RS_cmp(rs1,rs2) |
650 | | const H5RS_str_t *rs1; IN: First Ref-counted string to compare |
651 | | const H5RS_str_t *rs2; IN: Second Ref-counted string to compare |
652 | | |
653 | | RETURNS |
654 | | Returns positive, negative or 0 for comparison of two r-strings [same as |
655 | | strcmp()] |
656 | | DESCRIPTION |
657 | | Compare two ref-counted strings and return a value indicating their sort |
658 | | order [same as strcmp()] |
659 | | GLOBAL VARIABLES |
660 | | COMMENTS, BUGS, ASSUMPTIONS |
661 | | EXAMPLES |
662 | | REVISION LOG |
663 | | --------------------------------------------------------------------------*/ |
664 | | int |
665 | | H5RS_cmp(const H5RS_str_t *rs1, const H5RS_str_t *rs2) |
666 | 0 | { |
667 | | /* Can't return invalid value from this function */ |
668 | 0 | FUNC_ENTER_NOAPI_NOINIT_NOERR |
669 | | |
670 | | /* Sanity check */ |
671 | 0 | assert(rs1); |
672 | 0 | assert(rs1->s); |
673 | 0 | assert(rs2); |
674 | 0 | assert(rs2->s); |
675 | |
|
676 | 0 | FUNC_LEAVE_NOAPI(strcmp(rs1->s, rs2->s)) |
677 | 0 | } /* end H5RS_cmp() */ |
678 | | |
679 | | /*-------------------------------------------------------------------------- |
680 | | NAME |
681 | | H5RS_len |
682 | | PURPOSE |
683 | | Compute the length of a ref-counted string |
684 | | USAGE |
685 | | ssize_t H5RS_cmp(rs) |
686 | | const H5RS_str_t *rs; IN: Ref-counted string to compute length of |
687 | | |
688 | | RETURNS |
689 | | Returns non-negative value on success, can't fail |
690 | | DESCRIPTION |
691 | | Compute the length of a ref-counted string. [same as strlen()] |
692 | | GLOBAL VARIABLES |
693 | | COMMENTS, BUGS, ASSUMPTIONS |
694 | | EXAMPLES |
695 | | REVISION LOG |
696 | | --------------------------------------------------------------------------*/ |
697 | | size_t |
698 | | H5RS_len(const H5RS_str_t *rs) |
699 | 0 | { |
700 | 0 | FUNC_ENTER_NOAPI_NOINIT_NOERR |
701 | | |
702 | | /* Sanity check */ |
703 | 0 | assert(rs); |
704 | 0 | assert(rs->s); |
705 | |
|
706 | 0 | FUNC_LEAVE_NOAPI(strlen(rs->s)) |
707 | 0 | } /* end H5RS_len() */ |
708 | | |
709 | | /*-------------------------------------------------------------------------- |
710 | | NAME |
711 | | H5RS_get_str |
712 | | PURPOSE |
713 | | Get a pointer to the internal string contained in a ref-counted string |
714 | | USAGE |
715 | | char *H5RS_get_str(rs) |
716 | | const H5RS_str_t *rs; IN: Ref-counted string to get internal string from |
717 | | |
718 | | RETURNS |
719 | | Returns a pointer to the internal string being ref-counted on success, |
720 | | NULL on failure. |
721 | | DESCRIPTION |
722 | | Gets a pointer to the internal string being reference counted. This |
723 | | pointer is volatile and might be invalid is further calls to the H5RS |
724 | | API are made. |
725 | | GLOBAL VARIABLES |
726 | | COMMENTS, BUGS, ASSUMPTIONS |
727 | | EXAMPLES |
728 | | REVISION LOG |
729 | | --------------------------------------------------------------------------*/ |
730 | | char * |
731 | | H5RS_get_str(const H5RS_str_t *rs) |
732 | 1.18k | { |
733 | 1.18k | FUNC_ENTER_NOAPI_NOINIT_NOERR |
734 | | |
735 | | /* Sanity check */ |
736 | 1.18k | assert(rs); |
737 | 1.18k | assert(rs->s); |
738 | | |
739 | 1.18k | FUNC_LEAVE_NOAPI(rs->s) |
740 | 1.18k | } /* end H5RS_get_str() */ |
741 | | |
742 | | /*-------------------------------------------------------------------------- |
743 | | NAME |
744 | | H5RS_get_count |
745 | | PURPOSE |
746 | | Get the reference count for a ref-counted string |
747 | | USAGE |
748 | | unsigned H5RS_get_count(rs) |
749 | | const H5RS_str_t *rs; IN: Ref-counted string to get internal count from |
750 | | |
751 | | RETURNS |
752 | | Returns the number of references to the internal string being ref-counted on success, |
753 | | 0 on failure. |
754 | | DESCRIPTION |
755 | | Gets the count of references to the reference counted string. |
756 | | GLOBAL VARIABLES |
757 | | COMMENTS, BUGS, ASSUMPTIONS |
758 | | EXAMPLES |
759 | | REVISION LOG |
760 | | --------------------------------------------------------------------------*/ |
761 | | unsigned |
762 | | H5RS_get_count(const H5RS_str_t *rs) |
763 | 0 | { |
764 | 0 | FUNC_ENTER_NOAPI_NOINIT_NOERR |
765 | | |
766 | | /* Sanity check */ |
767 | 0 | assert(rs); |
768 | 0 | assert(rs->n > 0); |
769 | |
|
770 | 0 | FUNC_LEAVE_NOAPI(rs->n) |
771 | 0 | } /* end H5RS_get_count() */ |