This commit is contained in:
Alexander 2017-09-17 20:10:31 +03:00
parent c8a2923d17
commit dc192be83f

View File

@ -268,11 +268,23 @@ ImagingDelete(Imaging im)
#define MEMORY_CACHE_BLOCKS 0 #define MEMORY_CACHE_BLOCKS 0
#define MEMORY_ALIGN_LINES 1 #define MEMORY_ALIGN_LINES 1
void **_blocks = NULL; typedef struct {
int _blocks_free = 0; int alignment = 1;
int block_size = 1024*1024;
int blocks_max = 0;
int blocks_free = 0;
void **blocks = NULL
} *ImagingMemoryArean;
void
memory_set_blocks_max(ImagingMemoryArean arena, int blocks_max)
{
arena->blocks_max = blocks_max;
}
void * void *
_get_block(int requested_size, int dirty) memory_get_block(ImagingMemoryArean arena, int requested_size, int dirty)
{ {
void *block; void *block;
if ( ! _blocks) { if ( ! _blocks) {
@ -306,7 +318,7 @@ _get_block(int requested_size, int dirty)
} }
void void
_return_block(void *block) memory_return_block(ImagingMemoryArean arena, void *block)
{ {
if (_blocks_free < MEMORY_CACHE_BLOCKS) { if (_blocks_free < MEMORY_CACHE_BLOCKS) {
_blocks[_blocks_free] = block; _blocks[_blocks_free] = block;
@ -345,7 +357,7 @@ ImagingAllocateArray(Imaging im, int dirty)
return im; return im;
} }
linesize = (im->linesize + MEMORY_ALIGN_LINES - 1) & -MEMORY_ALIGN_LINES; linesize = (im->linesize + arena->alignment - 1) & -arena->alignment;
lines_per_block = MEMORY_BLOCK_SIZE / linesize; lines_per_block = MEMORY_BLOCK_SIZE / linesize;
if (lines_per_block == 0) if (lines_per_block == 0)
lines_per_block = 1; lines_per_block = 1;
@ -370,7 +382,7 @@ ImagingAllocateArray(Imaging im, int dirty)
if (lines_remained > im->ysize - y) { if (lines_remained > im->ysize - y) {
lines_remained = im->ysize - y; lines_remained = im->ysize - y;
} }
p = (char *)_get_block(lines_remained * linesize, dirty); p = (char *)memory_get_block(lines_remained * linesize, dirty);
if ( ! p) { if ( ! p) {
return (Imaging) ImagingError_MemoryError(); return (Imaging) ImagingError_MemoryError();
} }