memory.h - ledit - Text editor (WIP)
HTML git clone git://lumidify.org/ledit.git (fast, but not encrypted)
HTML git clone https://lumidify.org/git/ledit.git (encrypted, but very slow)
HTML git clone git://4kcetb7mo7hj6grozzybxtotsub5bempzo4lirzc3437amof2c2impyd.onion/ledit.git (over tor)
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
memory.h (2595B)
---
1 #ifndef _MEMORY_H_
2 #define _MEMORY_H_
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdarg.h>
7
8 /*
9 * These functions all wrap the regular functions but exit on error.
10 */
11 char *ledit_strdup(const char *s);
12 char *ledit_strndup(const char *s, size_t n);
13 void *ledit_malloc(size_t size);
14 void *ledit_calloc(size_t nmemb, size_t size);
15 void *ledit_realloc(void *ptr, size_t size);
16
17 /*
18 * This is different than the normal strcat - it allocates memory for
19 * a new string to hold the concatenation of str1 and str2.
20 */
21 char *ledit_strcat(const char *str1, const char *str2);
22
23 /* This acts like snprintf but automatically allocates
24 a string of the appropriate size.
25 Like the other functions here, it exits on error. */
26 char *print_fmt(char *fmt, ...);
27
28 /*
29 * This is like OpenBSD's reallocarray but exits on error.
30 */
31 void *ledit_reallocarray(void *optr, size_t nmemb, size_t size);
32
33 /*
34 * Move the gap of a gap buffer with elements of size 'elem_size' to 'index'.
35 * 'index' is also written to 'new_gap_ret' if it is not NULL. This is just
36 * for convenience because it usually has to be set anyways.
37 */
38 void move_gap(
39 void *array, size_t elem_size, size_t index,
40 size_t gap, size_t cap, size_t len,
41 size_t *new_gap_ret
42 );
43
44 /*
45 * Resize a generic gap buffer with elements of size 'elem_size' to hold at least
46 * 'min_size' elements and move the gap to element position 'index'.
47 * The array size may be increased or decreased.
48 * 'old_gap' is the old index of the gap buffer, 'old_cap' is the total length of the
49 * array (number of elements, not bytes), and 'len' is the number of valid elements.
50 * 'index' is also written to 'new_gap_ret' if it is not NULL. This is just
51 * for convenience because it usually has to be set anyways.
52 * The new total length of the array is also written to 'new_cap_ret', if it is not
53 * NULL. If it is NULL, you're doing something wrong.
54 * Returns the final array (might be same as the old one).
55 */
56 void *resize_and_move_gap(
57 void *array, size_t elem_size,
58 size_t old_gap, size_t old_cap, size_t len,
59 size_t min_size, size_t index,
60 size_t *new_gap_ret, size_t *new_cap_ret
61 );
62
63 /* FIXME: not sure if this really belongs here */
64 void err_overflow_impl(const char *file, int line, const char *func);
65 #define err_overflow() err_overflow_impl(__FILE__, __LINE__, __func__)
66
67 /*
68 * Return the ideal new size for an array of size 'old' when resizing it
69 * so it fits at least 'needed' elements. The return value may be smaller
70 * than 'old' if 'needed' is smaller.
71 */
72 size_t ideal_array_size(size_t old, size_t needed);
73
74 #endif