ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
cstr.c
Go to the documentation of this file.
1//! @file ds/cstr.c
2//! @author Avinash Maddikonda (svasssakavi@gmail.com)
3//! @brief Implementation of the C-string types, related constants and
4//! functions.
5//! @since 0.5.0
6//! @date 2023-07-31
7
8#include <stddef.h>
9
10#include "ds/alloc.h"
11#include "ds/char.h"
12#include "ds/cstr.h"
13#include "ds/helpers.h"
14#include "ds/result.h"
15#include "ds/usize.h"
16
18ds_cstr_allocate (ds_cstr_mut_t *const self, ds_usize_t const size)
19{
21 if (size == DS_USIZE_MIN)
22 {
23 // We need not allocate memory since the requested @p size is `0`. Hence,
24 // we simply point to `NULL` to indicate the skipped allocation.
25 *self = NULL;
26 }
27 else
28 {
29 *self
31 if (ds_helpers_is_null (*self))
32 {
34 }
35 }
36
37 return DS_RESULT_OK;
38}
39
41ds_cstr_reallocate (ds_cstr_mut_t *const src_cstr_ptr,
42 ds_usize_t const src_size,
43 ds_cstr_mut_t *const dst_cstr_ptr,
44 ds_usize_t const dst_size)
45{
46 DS_RESULT_PROPAGATE_IF_NULL (src_cstr_ptr);
47 DS_RESULT_PROPAGATE_IF_NULL (dst_cstr_ptr);
48
49 ds_cstr_mut_t const src_str = *src_cstr_ptr;
50
51 // The source is `NULL`, so freshly allocate the destination string instead.
52 if (ds_helpers_is_null (src_str))
53 {
54 return ds_cstr_allocate (dst_cstr_ptr, dst_size);
55 }
56
57 // Move the string from the source to the destination in case reallocation is
58 // not necessary or fails.
59 *src_cstr_ptr = NULL;
60 *dst_cstr_ptr = src_str;
61
62 // We need not reallocate the string since the current size and the required
63 // size are the same. So we leave the moved string as is and return Ok.
64 if (dst_size == src_size)
65 {
66 return DS_RESULT_OK;
67 }
68
70 src_str, DS_CHAR_BYTES * dst_size);
71
72 // Reallocation failed, leave the moved string as is and return the error.
73 if (ds_helpers_is_null (dst_str))
74 {
76 }
77
78 // Reallocation was successful, point to the new destination string instead.
79 *dst_cstr_ptr = dst_str;
80 return DS_RESULT_OK;
81}
82
85{
88
89 // Point to `NULL` to avoid memory leaks or other critical issues caused when
90 // deallocating a pointer twice without any allocation in between, as `NULL`
91 // pointers are simply ignored by deallocating functions.
92 *self = NULL;
93 return DS_RESULT_OK;
94}
Declaration of the Allocator type, related constants and functions.
ds_allocator_t ds_allocator_global
The global memory allocator.
Definition: alloc.c:12
Declaration of the char type, related constants and functions.
#define DS_CHAR_BYTES
The size of char in bytes.
Definition: char.h:560
ds_result_t ds_cstr_deallocate(ds_cstr_mut_t *const self)
Deallocates the C-string referenced by self.
Definition: cstr.c:84
ds_result_t ds_cstr_reallocate(ds_cstr_mut_t *const src_cstr_ptr, ds_usize_t const src_size, ds_cstr_mut_t *const dst_cstr_ptr, ds_usize_t const dst_size)
Attempts to reallocate (extend, shrink) the C-string.
Definition: cstr.c:41
ds_result_t ds_cstr_allocate(ds_cstr_mut_t *const self, ds_usize_t const size)
Attempts to allocate a C-string.
Definition: cstr.c:18
Declaration of the C-string types, related constants and functions.
ds_char_t * ds_cstr_mut_t
A mutable C-string type.
Definition: cstr.h:37
Declaration of helper constants and functions.
bool ds_helpers_is_null(void const *ptr)
Returns true if ptr is NULL.
Definition: helpers.c:13
Declaration of the Result enumeration type, related constants and functions.
ds_result_t
Result is a type that represents either success (Ok) or failure (a DS_RESULT_ERR_* variant).
Definition: result.h:26
@ DS_RESULT_OK
Represents success.
Definition: result.h:28
@ DS_RESULT_ERR_MEM_ALLOC_FAILED
Represents a memory allocation failure (usually due to lack of sufficient available memory).
Definition: result.h:35
#define DS_RESULT_PROPAGATE_IF_NULL(ptr)
Propagates (instantly, possibly early, returns) DS_RESULT_ERR_PTR_IS_NULL if and only if ptr is NULL.
Definition: result.h:162
void *(* reallocate)(void *ptr, ds_usize_t new_size)
Attempts to reallocate (extend, shrink) the memory block.
Definition: alloc.h:66
void(* deallocate)(void *ptr)
Deallocates the memory referenced by ptr.
Definition: alloc.h:75
void *(* allocate)(ds_usize_t size)
Attempts to allocate a block of memory.
Definition: alloc.h:38
Declaration of the usize type, related constants and functions.
#define DS_USIZE_MIN
The smallest value that can be represented by usize.
Definition: usize.h:49
size_t ds_usize_t
The pointer-sized unsigned integer type.
Definition: usize.h:26