lib/collection.h: fixed memory leak, more safe, optimized

This commit is contained in:
2025-03-06 17:01:00 +02:00
parent c63159c78c
commit 4e5b13ed8d
2 changed files with 34 additions and 37 deletions

View File

@@ -13,7 +13,6 @@ struct collection_int
dword buf; dword buf;
dword buf_size; dword buf_size;
unsigned count; unsigned count;
void alloc();
void add(); void add();
dword get(); dword get();
dword set(); dword set();
@@ -22,34 +21,35 @@ struct collection_int
dword get_last(); dword get_last();
void pop(); void pop();
void drop(); void drop();
#define DWSIZE4 4
}; };
:void collection_int::alloc() {
if (!buf) {
buf_size = 4096;
buf = malloc(4096);
} else {
buf_size += 4096;
buf = realloc(buf, buf_size);
}
}
:void collection_int::add(dword _in) { :void collection_int::add(dword _in) {
if (!buf) || (count * sizeof(dword) >= buf_size) alloc(); unsigned i;
EAX = count * sizeof(dword) + buf; if (!buf) {
ESDWORD[EAX] = _in; if (buf_size) notify("'buf_size on empty buf' -A");
buf_size = 4096 * 5;
buf = malloc(4096 * 5);
if (!buf) notify("'malloc error' -E");
} else if (count + 1 * DWSIZE4 >= buf_size) {
buf_size += 4096 * 5;
buf = realloc(buf, buf_size);
if (!buf) notify("'realloc error' -E");
}
i = count * DWSIZE4 + buf;
ESDWORD[i] = _in;
count++; count++;
} }
:dword collection_int::get(dword pos) { :dword collection_int::get(dword pos) {
if (pos<0) || (pos>=count) return 0; if (!buf) || (pos<0) || (pos>=count) return 0;
return ESDWORD[pos * sizeof(dword) + buf]; return ESDWORD[pos * DWSIZE4 + buf];
} }
:dword collection_int::set(dword pos, _in) { :dword collection_int::set(dword pos, _in) {
while (pos >= count) add(0); while (pos >= count) add(0);
EAX = pos * sizeof(dword) + buf; EAX = pos * DWSIZE4 + buf;
ESDWORD[EAX] = _in; ESDWORD[EAX] = _in;
return ESDWORD[EAX]; return ESDWORD[EAX];
} }
@@ -57,8 +57,8 @@ struct collection_int
:void collection_int::swap(dword pos1, pos2) { :void collection_int::swap(dword pos1, pos2) {
while (pos1 >= count) add(0); while (pos1 >= count) add(0);
while (pos2 >= count) add(0); while (pos2 >= count) add(0);
EAX = pos1 * sizeof(dword) + buf; EAX = pos1 * DWSIZE4 + buf;
EBX = pos2 * sizeof(dword) + buf; EBX = pos2 * DWSIZE4 + buf;
ESDWORD[EAX] >< ESDWORD[EBX]; ESDWORD[EAX] >< ESDWORD[EBX];
} }
@@ -77,6 +77,8 @@ struct collection_int
:void collection_int::drop() { :void collection_int::drop() {
count = 0; count = 0;
if (buf) buf = free(buf);
buf_size = 0;
} }
/*======================================================== /*========================================================
@@ -87,12 +89,11 @@ struct collection_int
struct collection struct collection
{ {
int realloc_size, count; unsigned int realloc_size, count;
dword data_start; dword data_start;
dword data_size; dword data_size;
collection_int offset; collection_int offset;
int add(); dword add();
int addn();
dword get(); //get_name_by_pos dword get(); //get_name_by_pos
dword get_pos_by_name(); dword get_pos_by_name();
void drop(); void drop();
@@ -105,28 +106,22 @@ struct collection
if (realloc_size<4096) realloc_size = 4096; if (realloc_size<4096) realloc_size = 4096;
if (!data_size) { if (!data_size) {
data_size = realloc_size; data_size = realloc_size;
data_start = malloc(realloc_size); data_start = malloc(data_size);
} } else {
else {
data_size = data_size + realloc_size; data_size = data_size + realloc_size;
data_start = realloc(data_start, data_size); data_start = realloc(data_start, data_size);
} }
} }
:int collection::add(dword in) { :dword collection::add(dword in) {
return addn(in, strlen(in)); dword len = strlen(in);
} while (offset.get(count) + len + 4 > data_size) {
:int collection::addn(dword in, len) {
if (offset.get(count)+len+2 > data_size) {
increase_data_size(); increase_data_size();
addn(in, len);
return 1;
} }
strncpy(data_start+offset.get(count), in, len); strncpy(data_start+offset.get(count), in, len);
count++; count++;
offset.set(count, offset.get(count-1) + len + 1); offset.set(count, offset.get(count-1) + len + 1);
return 1; return data_start+offset.get(count-1);
} }
:dword collection::get(dword pos) { :dword collection::get(dword pos) {
@@ -141,7 +136,9 @@ struct collection
:dword collection::get_pos_by_name(dword name) { :dword collection::get_pos_by_name(dword name) {
dword i; dword i;
for (i=0; i<count; i++) { for (i=0; i<count; i++) {
if (strcmp(data_start + offset.get(i), name)==0) return i; if (streq(data_start + offset.get(i), name)) {
return i;
}
} }
return -1; return -1;
} }

View File

@@ -57,8 +57,8 @@ inline fastcall void utf8rutodos( ESI)
while (BL=ESBYTE[ESI]) while (BL=ESBYTE[ESI])
{ {
if (BL == 0xD0) || (BL == 0xD1) EDI--; if (BL == 0xD0) || (BL == 0xD1) EDI--;
else if (BL == 0x81) && (ESBYTE[ESI-1]==0xD0) ESBYTE[EDI] = 0xF0; //<EFBFBD> else if (BL == 0x81) && (ESBYTE[ESI-1]==0xD0) ESBYTE[EDI] = 0xF0; //I urk
else if (BL == 0x91) && (ESBYTE[ESI-1]==0xD1) ESBYTE[EDI] = 0xF1; //<EFBFBD> else if (BL == 0x91) && (ESBYTE[ESI-1]==0xD1) ESBYTE[EDI] = 0xF1; //i urk
//0xE2 0x80 - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //0xE2 0x80 - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else if (BL == 0xE2) && (ESBYTE[ESI+1]==0x80) else if (BL == 0xE2) && (ESBYTE[ESI+1]==0x80)
switch (ESBYTE[ESI+2]) switch (ESBYTE[ESI+2])