collection.h: dynamic memory allocation

git-svn-id: svn://kolibrios.org@5965 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Kirill Lipatov (Leency) 2015-12-15 16:18:38 +00:00
parent 478ddf2f66
commit 63cc280c5f
3 changed files with 37 additions and 28 deletions

View File

@ -78,7 +78,7 @@ void GetSystemDiscs()
dword temp_file_count, tempbuf; dword temp_file_count, tempbuf;
dword devbuf; dword devbuf;
disk_list.init(4096); disk_list.drop();
devbuf = malloc(10000); devbuf = malloc(10000);
ReadDir(19, devbuf, "/"); ReadDir(19, devbuf, "/");
dev_num = EBX; dev_num = EBX;

View File

@ -1,4 +1,4 @@
#define MEMSIZE 4096*10 #define MEMSIZE 4096*120
#include "../lib/io.h" #include "../lib/io.h"
#include "../lib/collection.h" #include "../lib/collection.h"
@ -6,12 +6,11 @@
void main() void main()
{ {
collection s; collection s;
int i;
io.run("/sys/develop/board", ""); io.run("/sys/develop/board", "");
s.init(4096); s.add("Hello");
s.add("lorem"); s.add("World!");
s.add("ipsum");
s.add("1234566");
debugln(s.get(0)); debugln(s.get(0));
debugln(s.get(1)); debugln(s.get(1));
debugln(s.get(2)); s.drop();
} }

View File

@ -4,42 +4,52 @@
struct collection struct collection
{ {
int count; int realloc_size, count;
dword element_offset[4096]; dword data_start;
dword data_cur_pos;
dword data_size; dword data_size;
dword string_data_start; dword element_offset[4090];
dword string_data_cur_pos; int add();
void add();
dword get(); dword get();
void drop(); void drop();
void init(); void increase_data_size();
}; };
void collection::init(dword size) { void collection::increase_data_size() {
if (data_size) drop(); int filled_size;
data_size = data_size + size; if (realloc_size<4096) realloc_size = 4096;
string_data_cur_pos = string_data_start = malloc(data_size); if (!data_size) {
count = 0; data_size = realloc_size;
data_start = malloc(realloc_size);
}
else {
data_size = data_size + realloc_size;
data_start = realloc(data_start, data_size);
}
} }
void collection::add(dword in) { int collection::add(dword in) {
strcpy(string_data_cur_pos, in); if (count >= 4090) return 0;
element_offset[count] = string_data_cur_pos; if (data_cur_pos+strlen(in)+2 > data_size) {
string_data_cur_pos += strlen(in) + 1; increase_data_size();
add(in);
return;
}
strcpy(data_start+data_cur_pos, in);
element_offset[count] = data_cur_pos;
data_cur_pos += strlen(in) + 1;
count++; count++;
return 1;
} }
dword collection::get(dword pos) { dword collection::get(dword pos) {
return element_offset[pos]; return data_start + element_offset[pos];
} }
void collection::drop() { void collection::drop() {
if (string_data_start) free(string_data_start); if (data_start) free(data_start);
data_size = data_size = data_start = data_cur_pos = count = 0;
string_data_start =
string_data_cur_pos =
count = 0;
} }
#endif #endif