s390: optimize register allocation even more - if the function doesn't
[ajla.git] / str.h
blobe4e37a9097189b8eea7e2aad04e069232cf591bb
1 /*
2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
9 * version.
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
19 #ifndef AJLA_STR_H
20 #define AJLA_STR_H
22 #include "mem_al.h"
24 void * attr_fastcall array_realloc_mayfail(void *p, size_t element_size, size_t old_length, size_t new_length, void **err_ptr, ajla_error_t *mayfail);
25 void * attr_fastcall array_finish_realloc(void *p, size_t length);
27 #define array_init_mayfail(type, ptr, len, mayfail) \
28 ( \
29 *(len) = 0, \
30 *(ptr) = mem_alloc_mayfail(type *, 0, mayfail), \
31 (!((is_constant(mayfail) && !return_ptr(mayfail))) && !*(ptr) ? false : true)\
34 #define array_init(type, ptr, len) \
35 (void)array_init_mayfail(type, ptr, len, NULL)
37 #define array_add_mayfail(type, ptr, len, var, err_ptr, mayfail) \
38 ( \
39 (*(len))++, \
40 *(ptr) = cast_ptr(type *, array_realloc_mayfail(*(ptr), sizeof(type), (*(len) - 1), *(len), err_ptr, mayfail)),\
41 (!((is_constant(mayfail) && !return_ptr(mayfail))) && !*(ptr) ? (*(len))--, false : ((*(ptr))[*(len) - 1] = (var), true))\
44 #define array_add(type, ptr, len, var) \
45 (void)array_add_mayfail(type, ptr, len, var, NULL, NULL)
47 #define array_add_multiple_mayfail(type, ptr, len, var, varlen, err_ptr, mayfail)\
48 ( \
49 (*(len)) += (varlen), \
50 *(ptr) = cast_ptr(type *, array_realloc_mayfail(*(ptr), sizeof(type), (*(len) - (varlen)), *(len), err_ptr, mayfail)),\
51 (!((is_constant(mayfail) && !return_ptr(mayfail))) && !*(ptr) ? (*(len)) -= (varlen), false : (memcpy(&(*(ptr))[*(len) - (varlen)], (var), (varlen) * sizeof(type)), true))\
54 #define array_add_multiple(type, ptr, len, var, varlen) \
55 (void)array_add_multiple_mayfail(type, ptr, len, var, varlen, NULL, NULL)
57 #define array_finish(type, ptr, len) \
58 (*(ptr) = (type *)array_finish_realloc(*(ptr), *(len) * sizeof(type)))
61 #define str_init(ptr, len) \
62 do { \
63 array_init(char, (ptr), (len)); \
64 } while (0)
66 #define str_finish(ptr, len) \
67 do { \
68 str_add_char((ptr), (len), 0); \
69 array_finish(char, (ptr), (len)); \
70 } while (0)
72 void attr_fastcall str_add_bytes(char **, size_t *, const char *, size_t);
73 void attr_fastcall str_add_string(char **, size_t *, const char *);
74 void attr_fastcall str_add_char(char **, size_t *, char);
75 void attr_fastcall str_add_unsigned(char **, size_t *, uintbig_t, int);
76 void attr_fastcall str_add_signed(char **, size_t *, intbig_t, int);
77 void attr_fastcall str_add_hex(char **s, size_t *l, const char *hex);
79 char *str_dup(const char *str, size_t max_len, ajla_error_t *err);
81 static inline char *str_from_unsigned(uintbig_t i, int base)
83 size_t str_l;
84 char *str;
85 str_init(&str, &str_l);
86 str_add_unsigned(&str, &str_l, i, base);
87 str_finish(&str, &str_l);
88 return str;
91 static inline char *str_from_signed(uintbig_t i, int base)
93 size_t str_l;
94 char *str;
95 str_init(&str, &str_l);
96 str_add_signed(&str, &str_l, i, base);
97 str_finish(&str, &str_l);
98 return str;
101 #endif