Indirectly free(3) buffers in fshutdown(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-08-03 21:14:57 +02:00
parent 9fec909970
commit 97e1551c81
3 changed files with 6 additions and 3 deletions

View File

@ -86,6 +86,7 @@ struct FILE
void (*free_func)(void* free_user, struct FILE* fp);
/* Application writers shouldn't use anything beyond this point. */
pthread_mutex_t file_lock;
void (*buffer_free_indirect)(void*);
struct FILE* prev;
struct FILE* next;
int flags;

View File

@ -66,5 +66,7 @@ extern "C" int fsetdefaultbuf_unlocked(FILE* fp)
// The buffer now belongs to the FILE.
fp->flags |= _FILE_BUFFER_OWNED;
fp->buffer_free_indirect = free;
return 0;
}

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
This file is part of the Sortix C Library.
@ -34,8 +34,8 @@ extern "C" int fshutdown(FILE* fp)
exact error value, for instance, as with popen/pclose. */;
}
ret = fp->close_func ? fp->close_func(fp->user) : ret;
if ( fp->flags & _FILE_BUFFER_OWNED )
free(fp->buffer);
if ( fp->flags & _FILE_BUFFER_OWNED && fp->buffer_free_indirect )
fp->buffer_free_indirect(fp->buffer);
// Resetting the FILE here isn't needed in the case where fclose calls us,
// but it's nice to zero it out anyway (avoiding state) data, and it's a
// feature when called by freopen that wishes to reuse the FILE. It also