Compare commits
No commits in common. "master" and "master" have entirely different histories.
14
README.md
14
README.md
@ -17,20 +17,8 @@ You can simply add the [latest release](https://git.chauvet.pro/starnakin/IronGO
|
||||
- [aton](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/aton)
|
||||
- [aton_s](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/aton_s)
|
||||
- [bzero](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/bzero)
|
||||
- [aton_s](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/aton_s)
|
||||
- [galloc](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/galloc)
|
||||
- [free](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/free)
|
||||
- [leaks](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/leaks)
|
||||
- [ntoa](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/ntoa)
|
||||
- [ntoa_s](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/ntoa_s)
|
||||
- [geadline](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/geadline)
|
||||
- [geadline2](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/geadline2)
|
||||
- [strcat](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strcat)
|
||||
- [strcpy](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strcpy)
|
||||
- [strncpy](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strncpy)
|
||||
- [strlen](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strlen)
|
||||
- [strncmp](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strncmp)
|
||||
- [strcmp](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strcmp)
|
||||
- [strchr](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strchr)
|
||||
- [strstr](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strstr)
|
||||
- [strndup](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strndup)
|
||||
- [strdup](https://git.chauvet.pro/starnakin/IronGOLEM/wiki/strdup)
|
@ -1,6 +1,3 @@
|
||||
enum MAP_KEY, MAP_VALUE;
|
||||
define MAP_SIZE=2;
|
||||
|
||||
define NUM_MAX = 0xffff;
|
||||
define NUM_MIN = 0x0000;
|
||||
define NUM_S_MIN = 0x8000;
|
||||
|
@ -1,51 +0,0 @@
|
||||
invalid_write(ptr, i)
|
||||
{
|
||||
if (i < GALLOC_PADDING_SIZE & [ptr + GALLOC_HEADER_SIZE + i])
|
||||
return ptr + GALLOC_HEADER_SIZE + i;
|
||||
if (i >= GALLOC_PADDING_SIZE & [ptr + GALLOC_DATA + i - GALLOC_PADDING_SIZE + [ptr + GALLOC_SIZE]])
|
||||
return ptr + GALLOC_DATA + i - GALLOC_PADDING_SIZE + [ptr + GALLOC_SIZE];
|
||||
return 0;
|
||||
}
|
||||
|
||||
galgrind()
|
||||
{
|
||||
local ptr = heap;
|
||||
local i;
|
||||
|
||||
loop
|
||||
{
|
||||
if (ptr == NULL)
|
||||
return 0;
|
||||
if ([ptr + GALLOC_USED] == 1)
|
||||
{
|
||||
putstr("block: ");
|
||||
putnum(ptr - heap);
|
||||
putstr(", size: ");
|
||||
putnum([ptr + GALLOC_SIZE]);
|
||||
putstr("cases");
|
||||
putstr(", start: ");
|
||||
putnum(ptr - heap + GALLOC_DATA);
|
||||
putstr(", end: ");
|
||||
putnum(ptr - heap + GALLOC_DATA + [ptr + GALLOC_SIZE]);
|
||||
putstr(", start galloc block: ");
|
||||
putnum(ptr - heap);
|
||||
putstr(", end galloc block: ");
|
||||
putnum(ptr - heap + GALLOC_DATA + [ptr + GALLOC_SIZE] + GALLOC_PADDING_SIZE);
|
||||
putchar('\n');
|
||||
i = 0;
|
||||
loop
|
||||
{
|
||||
if (i == GALLOC_PADDING_SIZE * 2)
|
||||
break;
|
||||
if (invalid_write(ptr, i))
|
||||
{
|
||||
putnum(invalid_write(ptr, i) - heap);
|
||||
putchar('\n');
|
||||
}
|
||||
i++;
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
ptr = [ptr + GALLOC_NEXT];
|
||||
}
|
||||
}
|
76
src/galloc.🗿
76
src/galloc.🗿
@ -1,8 +1,8 @@
|
||||
define HEAP_SIZE = 0x4000;
|
||||
global heap[HEAP_SIZE] = 0;
|
||||
|
||||
define GALLOC_PADDING_SIZE = 4;
|
||||
define GALLOC_HEADER_SIZE = 5;
|
||||
define PADDING_SIZE = 4;
|
||||
define HEADER_SIZE = 5;
|
||||
|
||||
🗿HEADER REPRESENTATION
|
||||
🗿+-------------+--------+--------+-------------------------------+---------------------------+---------+---------+---------+
|
||||
@ -26,26 +26,26 @@ define GALLOC_HEADER_SIZE = 5;
|
||||
🗿 Is used to check invalid write
|
||||
🗿 If a case doesn't equal to 0 it is an invalid write
|
||||
|
||||
enum GALLOC_INITIALISED, GALLOC_USED, GALLOC_SIZE, GALLOC_PREV, GALLOC_NEXT;
|
||||
define GALLOC_DATA = GALLOC_HEADER_SIZE + GALLOC_PADDING_SIZE;
|
||||
enum LOCATION_INITIALISED, LOCATION_USED, LOCATION_SIZE, LOCATION_PREV, LOCATION_NEXT;
|
||||
define LOCATION_DATA = HEADER_SIZE + PADDING_SIZE;
|
||||
|
||||
galloc_setup_header(ptr, used, size, next_block, prev_block)
|
||||
{
|
||||
local i;
|
||||
|
||||
[ptr + GALLOC_INITIALISED] = 1;
|
||||
[ptr + GALLOC_USED] = used;
|
||||
[ptr + GALLOC_SIZE] = size;
|
||||
[ptr + GALLOC_PREV] = prev_block;
|
||||
[ptr + GALLOC_NEXT] = next_block;
|
||||
[ptr + LOCATION_INITIALISED] = 1;
|
||||
[ptr + LOCATION_USED] = used;
|
||||
[ptr + LOCATION_SIZE] = size;
|
||||
[ptr + LOCATION_PREV] = prev_block;
|
||||
[ptr + LOCATION_NEXT] = next_block;
|
||||
|
||||
i = GALLOC_HEADER_SIZE;
|
||||
i = HEADER_SIZE;
|
||||
loop
|
||||
{
|
||||
if (i == GALLOC_HEADER_SIZE + GALLOC_PADDING_SIZE)
|
||||
if (i == HEADER_SIZE + PADDING_SIZE)
|
||||
break;
|
||||
[ptr + i + GALLOC_HEADER_SIZE] = 0; 🗿 INITIALISE TOP PADDING
|
||||
[ptr + i + GALLOC_PADDING_SIZE + size] = 0; 🗿 INITIALISE BOT PADDING
|
||||
[ptr + i] = 0; 🗿 INITIALISE TOP PADDING
|
||||
[ptr + i + PADDING_SIZE + size] = 0; 🗿 INITIALISE BOT PADDING
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -57,10 +57,10 @@ galloc_find_next_space(size)
|
||||
current = heap;
|
||||
loop
|
||||
{
|
||||
if ([current + GALLOC_USED] == 0
|
||||
& [current + GALLOC_SIZE] >= size)
|
||||
if ([current + LOCATION_USED] == 0
|
||||
& [current + LOCATION_SIZE] >= size)
|
||||
return (current);
|
||||
current = [current + GALLOC_NEXT];
|
||||
current = [current + LOCATION_NEXT];
|
||||
if (current == 0)
|
||||
return (0);
|
||||
}
|
||||
@ -87,18 +87,18 @@ galloc_split_block(ptr, size)
|
||||
local prev;
|
||||
local old_size;
|
||||
|
||||
old_size = [ptr + GALLOC_SIZE];
|
||||
if (size + GALLOC_HEADER_SIZE + GALLOC_PADDING_SIZE * 2 > old_size) 🗿 if the block is to small to be split
|
||||
old_size = [ptr + LOCATION_SIZE];
|
||||
if (size + HEADER_SIZE + PADDING_SIZE * 2 > old_size) 🗿 if the block is to small to be split
|
||||
{
|
||||
[ptr + GALLOC_USED] = 1;
|
||||
[ptr + LOCATION_USED] = 1;
|
||||
return (ptr);
|
||||
}
|
||||
old_next = [ptr + GALLOC_NEXT];
|
||||
next = ptr + size + GALLOC_HEADER_SIZE + GALLOC_PADDING_SIZE * 2;
|
||||
prev = [ptr + GALLOC_PREV];
|
||||
old_next = [ptr + LOCATION_NEXT];
|
||||
next = ptr + size + HEADER_SIZE + PADDING_SIZE * 2;
|
||||
prev = [ptr + LOCATION_PREV];
|
||||
🗿 galloc_setup_header(ptr, used, size, next_block, prev_block);
|
||||
galloc_setup_header(ptr, 1, size, ptr + GALLOC_HEADER_SIZE + GALLOC_PADDING_SIZE * 2 + size, prev);
|
||||
galloc_setup_header(next, 0, old_size - size - GALLOC_HEADER_SIZE - GALLOC_PADDING_SIZE * 2, old_next, ptr);
|
||||
galloc_setup_header(ptr, 1, size, ptr + HEADER_SIZE + PADDING_SIZE * 2 + size, prev);
|
||||
galloc_setup_header(next, 0, old_size - size - HEADER_SIZE - PADDING_SIZE * 2, old_next, ptr);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -107,17 +107,17 @@ galloc(size)
|
||||
local ptr;
|
||||
|
||||
if ([heap] == 0) 🗿 if the heap is not initialised
|
||||
galloc_setup_header(heap, 0, HEAP_SIZE - GALLOC_HEADER_SIZE - GALLOC_PADDING_SIZE * 2, 0, 0); 🗿 initialised all the heap
|
||||
galloc_setup_header(heap, 0, HEAP_SIZE - HEADER_SIZE - PADDING_SIZE * 2, 0, 0); 🗿 initialised all the heap
|
||||
ptr = galloc_find_next_space(size);
|
||||
if (ptr == 0)
|
||||
return (0);
|
||||
if ([ptr + GALLOC_SIZE] == size)
|
||||
if ([ptr + LOCATION_SIZE] == size)
|
||||
{
|
||||
[ptr + GALLOC_USED] = 1;
|
||||
return (ptr + GALLOC_DATA);
|
||||
[ptr + LOCATION_USED] = 1;
|
||||
return (ptr + LOCATION_DATA);
|
||||
}
|
||||
galloc_split_block(ptr, size);
|
||||
return (ptr + GALLOC_DATA);
|
||||
return (ptr + LOCATION_DATA);
|
||||
}
|
||||
|
||||
galloc_merge_blocks(first_block, last_block)
|
||||
@ -126,10 +126,10 @@ galloc_merge_blocks(first_block, last_block)
|
||||
|
||||
if (last_block == first_block)
|
||||
{
|
||||
galloc_setup_header(first_block, 0, [first_block + GALLOC_SIZE], [first_block + GALLOC_NEXT], [first_block + GALLOC_PREV]);
|
||||
galloc_setup_header(first_block, 0, [first_block + LOCATION_SIZE], [first_block + LOCATION_NEXT], [first_block + LOCATION_PREV]);
|
||||
}
|
||||
size = last_block - first_block + [last_block + GALLOC_SIZE];
|
||||
galloc_setup_header(first_block, 0, size, [last_block + GALLOC_NEXT], [first_block + GALLOC_PREV]);
|
||||
size = last_block - first_block + [last_block + LOCATION_SIZE];
|
||||
galloc_setup_header(first_block, 0, size, [last_block + LOCATION_NEXT], [first_block + LOCATION_PREV]);
|
||||
}
|
||||
|
||||
free(ptr)
|
||||
@ -145,14 +145,14 @@ free(ptr)
|
||||
putstr("Error: free: invalid ptr\n");
|
||||
return;
|
||||
}
|
||||
block = ptr - GALLOC_DATA;
|
||||
prev_block = [block + GALLOC_PREV];
|
||||
if (prev_block == 0 | [prev_block + GALLOC_USED])
|
||||
block = ptr - LOCATION_DATA;
|
||||
prev_block = [block + LOCATION_PREV];
|
||||
if (prev_block == 0 | [prev_block + LOCATION_USED])
|
||||
first_block = block;
|
||||
else
|
||||
first_block = prev_block;
|
||||
next_block = [block + GALLOC_NEXT];
|
||||
if (next_block == 0 | [next_block + GALLOC_USED])
|
||||
next_block = [block + LOCATION_NEXT];
|
||||
if (next_block == 0 | [next_block + LOCATION_USED])
|
||||
last_block = block;
|
||||
else
|
||||
last_block = next_block;
|
||||
@ -161,5 +161,5 @@ free(ptr)
|
||||
|
||||
leaks()
|
||||
{
|
||||
return ([heap + GALLOC_NEXT] != 0);
|
||||
return ([heap + LOCATION_NEXT] != 0);
|
||||
}
|
||||
|
@ -12,11 +12,11 @@ realloc(ptr, new_size)
|
||||
}
|
||||
if (ptr == NULL)
|
||||
return new_space;
|
||||
block_ptr = ptr - GALLOC_DATA;
|
||||
block_ptr = ptr - LOCATION_DATA;
|
||||
i = 0;
|
||||
loop
|
||||
{
|
||||
if (i == new_size | i == [block_ptr + GALLOC_SIZE])
|
||||
if (i == new_size | i == [block_ptr + LOCATION_SIZE])
|
||||
break;
|
||||
[new_space + i] = [ptr + i];
|
||||
i++;
|
||||
|
2
test.sh
2
test.sh
@ -17,7 +17,7 @@ tester()
|
||||
}
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
tester $(ls tests/*.🗿 | grep -v 'test.🗿' | sed 's/^tests\///; s/\.🗿$//' | tr '\n' ' ' );
|
||||
tester $(ls tests/ | grep -v 'test.🗿' | sed 's/^tests\///; s/\.🗿$//' | tr '\n' ' ' );
|
||||
else
|
||||
tester $@;
|
||||
fi
|
||||
|
@ -7,11 +7,11 @@ main()
|
||||
name = "galloc";
|
||||
|
||||
ptr1 = galloc(1);
|
||||
test_num(ptr1, heap + GALLOC_DATA, "");
|
||||
test_num(ptr1, heap + LOCATION_DATA, "");
|
||||
free(ptr1);
|
||||
|
||||
ptr1 = galloc(1);
|
||||
test_num(ptr1, heap + GALLOC_DATA, "alloc after free");
|
||||
test_num(ptr1, heap + LOCATION_DATA, "alloc after free");
|
||||
free(ptr1);
|
||||
|
||||
ptr2 = galloc(0x9000);
|
||||
|
@ -23,12 +23,4 @@ main()
|
||||
ptr = ntoa_s(0 - 1);
|
||||
test_str(ptr, "-1", "");
|
||||
free(ptr);
|
||||
|
||||
ptr = ntoa_s(0 - 3);
|
||||
test_str(ptr, "-3", "");
|
||||
free(ptr);
|
||||
|
||||
ptr = ntoa_s(3);
|
||||
test_str(ptr, "3", "");
|
||||
free(ptr);
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ main()
|
||||
test_num(leaks(), 0, "standart: leaks");
|
||||
|
||||
ptr2 = realloc(NULL, 6);
|
||||
test_num(heap + GALLOC_DATA, ptr2, "NULL: value");
|
||||
test_num(heap + LOCATION_DATA, ptr2, "NULL: value");
|
||||
free(ptr2);
|
||||
test_num(leaks(), 0, "NULL: leaks");
|
||||
|
||||
|
@ -1,17 +0,0 @@
|
||||
# STRNCPY
|
||||
Strncpy is a function that takes two chars lists and a number as a parameter and write the second in the first but only the n - 1 first char (like strncpy in C)
|
||||
|
||||
## params
|
||||
1. chars list
|
||||
2. chars list
|
||||
3. number
|
||||
|
||||
## example
|
||||
```
|
||||
strncpy("y", "o", 2) "y" => "o"
|
||||
strncpy("y", "", 1) "y" => ""
|
||||
strncpy("", "o", 2) "" => "o"
|
||||
strncpy("hello ", "world!", 7) => "world!"
|
||||
strncpy("hello ", "world!", 14) => "world!"
|
||||
strncpy("hello ", "world!", 0) => "hello "
|
||||
```
|
Loading…
Reference in New Issue
Block a user