Compare commits

...

2 Commits

Author SHA1 Message Date
5dcace0cbe remove get_line, replaced by readline 2023-07-24 02:57:44 +02:00
85aeaebc7a add: realloc and remove reallocarray 2023-07-24 02:57:44 +02:00
4 changed files with 41 additions and 67 deletions

View File

@ -1,32 +0,0 @@
define GET_LINE_BUFFER=100;
get_line()
{
local tmp;
local out;
local c;
local i;
out = NULL;
i = 0;
loop
{
if (i % GET_LINE_BUFFER == 0)
{
tmp = reallocarray(out, i, i + GET_LINE_BUFFER + 1);
if (tmp == NULL)
return NULL;
}
red &c;
if (c == 0xffff)
return NULL;
[tmp + i] = c;
i++;
if (c == '\n')
break;
}
[tmp + i] = 0;
out = strdup(tmp);
free(tmp);
return out;
}

View File

@ -1,21 +1,26 @@
reallocarray(ptr, nmemb, size) realloc(ptr, new_size)
{ {
local tmp; local block_ptr;
local start; local new_space;
local i;
start = ptr; new_space = galloc(new_size);
tmp = galloc(size); if (new_space == NULL)
if (tmp == NULL) {
return NULL; free(ptr);
return (NULL);
}
if (ptr == NULL) if (ptr == NULL)
return tmp; return new_space;
block_ptr = ptr - LOCATION_DATA;
i = 0;
loop loop
{ {
if (start - ptr == nmemb) if (i == new_size | i == [block_ptr + LOCATION_SIZE])
break; break;
[tmp + start - ptr] = [start]; [new_space + i] = [ptr + i];
start++; i++;
} }
free(ptr); free(ptr);
return tmp; return new_space;
} }

View File

@ -1,23 +0,0 @@
define GET_LINE=0;
main()
{
local ptr;
name = "get_line";
if (GET_LINE == 0)
return;
putstr("yo\n");
ptr = get_line();
test_str(ptr, "yo\n", "");
free(ptr);
putstr("ENTER\n");
ptr = get_line();
test_str(ptr, "\n", "");
free(ptr);
putstr("ctrl + D\n");
ptr = get_line();
test_num(ptr, NULL, "");
}

24
tests/realloc.🗿 Normal file
View File

@ -0,0 +1,24 @@
main()
{
local ptr1;
local ptr2;
name = "realloc";
ptr1 = strdup("yo");
ptr2 = realloc(ptr1, 6);
test_str("yo", ptr2, "standart: value");
free(ptr2);
test_num(leaks(), 0, "standart: leaks");
ptr2 = realloc(NULL, 6);
test_num(heap + LOCATION_DATA, ptr2, "NULL: value");
free(ptr2);
test_num(leaks(), 0, "NULL: leaks");
ptr1 = strdup("bonjour");
ptr2 = realloc(ptr1, 2);
test_tab_num(ptr2, "bo", 2, "decrement size: value");
free(ptr2);
test_num(leaks(), 0, "decrement size: leaks");
}