From f9fa2a5a12d61e4cdc2cf6615dafb1298b6091c4 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 23 Jul 2023 15:35:32 +0200 Subject: [PATCH] add: realloc --- src/realloc.🗿 | 21 +++++++++++++++++++++ tests/reallocarray.🗿 | 11 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/realloc.🗿 create mode 100644 tests/reallocarray.🗿 diff --git a/src/realloc.🗿 b/src/realloc.🗿 new file mode 100644 index 0000000..48ddac4 --- /dev/null +++ b/src/realloc.🗿 @@ -0,0 +1,21 @@ +reallocarray(ptr, nmemb, size) +{ + local tmp; + local start; + + start = ptr; + tmp = galloc(size); + if (tmp == NULL) + return NULL; + if (ptr == NULL) + return tmp; + loop + { + if (start - ptr == nmemb) + break; + [tmp + start - ptr] = [start]; + start++; + } + free(ptr); + return tmp; +} diff --git a/tests/reallocarray.🗿 b/tests/reallocarray.🗿 new file mode 100644 index 0000000..3a14868 --- /dev/null +++ b/tests/reallocarray.🗿 @@ -0,0 +1,11 @@ +main() +{ + local tmp; + name = "reallocarray"; + + tmp = strdup("yo"); + if (tmp == NULL) + return 1; + tmp = reallocarray(tmp, strlen(tmp), 5); + test_str(tmp, "yo", ""); +}