Line data Source code
1 : /*
2 : * MIT License
3 : *
4 : * Copyright (c) 2026 - A bunch of nerds
5 : *
6 : * Permission is hereby granted, free of charge, to any person obtaining a copy
7 : * of this software and associated documentation files (the "Software"), to
8 : * deal in the Software without restriction, including without limitation the
9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 : * sell copies of the Software, and to permit persons to whom the Software is
11 : * furnished to do so, subject to the following conditions:
12 : *
13 : * The above copyright notice and this permission notice shall be included in
14 : * all copies or substantial portions of the Software.
15 : *
16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 : * DEALINGS IN THE SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : // INTERNAL ONLY. Allocator-aware string helpers that the modules of this
28 : // library share. No program uses the library through them, so the header is
29 : // never installed and has no visibility block.
30 :
31 : #include <common.h>
32 : #include <string.h>
33 :
34 : // Copies a string through the allocator mp, so a module's copies come from the
35 : // caller's allocator; strdup() through mp. It gives the copy, or NULL when
36 : // input is NULL or the allocation fails. A NULL input is allowed so a caller
37 : // can pass on the result of an earlier failed allocation and check once at the
38 : // end.
39 741806 : static inline __attribute__((always_inline)) char *ccol_strdup(
40 : ccol_memmgmt_procs *mp, const char *input) {
41 13003 : if (!input) return NULL;
42 742760 : size_t len = strlen(input) + 1;
43 742760 : char *result = (char *)ccol__mem_alloc(mp, len * sizeof(char));
44 742759 : if (result) {
45 742636 : memcpy(result, input, len);
46 : }
47 : return result;
48 : }
|