EGCPool: early realloc failure is not a hard fail

We do proactive reallocations of the EGCPool once we get
to 90% capacity, to avoid expensive exhaustive searches. When
we're over half the maximum size, though, this will fail. We
shouldn't treat that as a hard failure, but instead proceed on
to the search. This lets us hit the full 32MB EGCPool size #486.
This commit is contained in:
nick black 2020-04-15 16:33:22 -04:00
parent 08e769f989
commit 3ddb1d3166
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

View File

@ -43,8 +43,7 @@ egcpool_grow(egcpool* pool, size_t len){
while(len > newsize - pool->poolsize){ // ensure we make enough space while(len > newsize - pool->poolsize){ // ensure we make enough space
newsize *= 2; newsize *= 2;
} }
// offsets only have 25 bits available... if(newsize > 1u << 25u){
if(newsize >= 1u << 25u){
return -1; return -1;
} }
// nasty cast here because c++ source might include this header :/ // nasty cast here because c++ source might include this header :/
@ -129,7 +128,7 @@ egcpool_stash(egcpool* pool, const char* egc, size_t ulen){
if(!duplicated){ if(!duplicated){
duplicated = strdup(egc); duplicated = strdup(egc);
} }
if(egcpool_grow(pool, len)){ if(egcpool_grow(pool, len) && searched){
free(duplicated); free(duplicated);
return -1; return -1;
} }
@ -140,6 +139,7 @@ egcpool_stash(egcpool* pool, const char* egc, size_t ulen){
// memory. if we find it, write it out, and update used count. if we come // memory. if we find it, write it out, and update used count. if we come
// back to where we started, force a growth and try again. // back to where we started, force a growth and try again.
int curpos = pool->poolwrite; int curpos = pool->poolwrite;
//fprintf(stderr, "Stashing [%s] %d starting at %d\n", egc, len, curpos);
do{ do{
if(curpos == pool->poolsize){ if(curpos == pool->poolsize){
curpos = 0; curpos = 0;
@ -167,6 +167,7 @@ egcpool_stash(egcpool* pool, const char* egc, size_t ulen){
pool->poolwrite = curpos + len; pool->poolwrite = curpos + len;
pool->poolused += len; pool->poolused += len;
free(duplicated); free(duplicated);
//fprintf(stderr, "Stashing AT %d\n", curpos);
return curpos; return curpos;
} }
if(pool->poolwrite > curpos && pool->poolwrite - (len - need) < curpos){ if(pool->poolwrite > curpos && pool->poolwrite - (len - need) < curpos){