save released blocks

This commit is contained in:
Alexander 2017-09-15 18:00:15 +03:00
parent 0a3c852e1b
commit f584f8399a

View File

@ -265,7 +265,7 @@ ImagingDelete(Imaging im)
/* Allocate image as an array of line buffers. */ /* Allocate image as an array of line buffers. */
#define MEMORY_BLOCK_SIZE (1024*1024) #define MEMORY_BLOCK_SIZE (1024*1024)
#define MEMORY_BLOCKS_COUNT 128 #define MEMORY_MAX_BLOCKS 128
void **_blocks = NULL; void **_blocks = NULL;
int _blocks_free = 0; int _blocks_free = 0;
@ -275,7 +275,7 @@ _get_block(int dirty)
{ {
void *block; void *block;
if ( ! _blocks) { if ( ! _blocks) {
_blocks = calloc(sizeof(void*), MEMORY_BLOCKS_COUNT); _blocks = calloc(sizeof(void*), MEMORY_MAX_BLOCKS);
} }
if (_blocks_free > 0) { if (_blocks_free > 0) {
_blocks_free -= 1; _blocks_free -= 1;
@ -296,7 +296,12 @@ _get_block(int dirty)
void void
_return_block(void *block) _return_block(void *block)
{ {
free(block); if (_blocks_free < MEMORY_MAX_BLOCKS) {
_blocks[_blocks_free] = block;
_blocks_free += 1;
} else {
free(block);
}
} }
@ -308,7 +313,7 @@ ImagingDestroyArray(Imaging im)
if (im->blocks) { if (im->blocks) {
while (im->blocks[y]) { while (im->blocks[y]) {
_return_block(im->blocks[y]); _return_block(im->blocks[y]);
y ++; y += 1;
} }
free(im->blocks); free(im->blocks);
} }