forked from starnakin/IronGOLEM
add: realloc
This commit is contained in:
parent
c6608ad4eb
commit
f9fa2a5a12
21
src/realloc.🗿
Normal file
21
src/realloc.🗿
Normal file
@ -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;
|
||||
}
|
11
tests/reallocarray.🗿
Normal file
11
tests/reallocarray.🗿
Normal file
@ -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", "");
|
||||
}
|
Loading…
Reference in New Issue
Block a user