MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / stack_copy

Function stack_copy

stack/stack.c:54–85  ·  view source on GitHub ↗

* @brief Deep-copy an existing stack. * @return New stack with the same contents, or NULL on failure. */

Source from the content-addressed store, hash-verified

52 * @return New stack with the same contents, or NULL on failure.
53 */
54Stack* stack_copy(const Stack* src) {
55 STACK_LOG("[stack_copy]: src=%p", (const void*)src);
56
57 if (!src || !src->vec) {
58 STACK_LOG("[stack_copy]: NULL source");
59 return NULL;
60 }
61
62 Stack* dst = stack_create(src->vec->itemSize);
63 if (!dst) {
64 return NULL;
65 }
66 vector_set_compare(dst->vec, src->vec->compare);
67
68 size_t n = vector_size(src->vec);
69 if (n > 0 && !vector_reserve(dst->vec, n)) {
70 STACK_LOG("[stack_copy]: reserve(%zu) failed", n);
71 stack_deallocate(dst);
72
73 return NULL;
74 }
75
76 for (size_t i = 0; i < n; ++i) {
77 void* elem = vector_at(src->vec, i);
78 if (!elem || !vector_push_back(dst->vec, elem)) {
79 STACK_LOG("[stack_copy]: push_back failed at index %zu", i);
80 stack_deallocate(dst);
81 return NULL;
82 }
83 }
84
85 return dst;
86}
87
88

Callers

nothing calls this directly

Calls 6

stack_createFunction · 0.85
vector_sizeFunction · 0.85
vector_reserveFunction · 0.85
stack_deallocateFunction · 0.85
vector_atFunction · 0.85
vector_push_backFunction · 0.85

Tested by

no test coverage detected