1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
|
/* Manage sets of pixel buffers on an image.
*
* 30/10/06
* - from window.c
* 2/2/07
* - speed up the search, use our own lock (thanks Christian)
* 5/2/07
* - split to many buffer lists per image
* 11/2/07
* - split to a buffer hash per thread
* - reuse buffer mallocs when we can
* 20/2/07
* - add VipsBufferCacheList and we can avoid some hash ops on
* done/undone
* 5/3/10
* - move invalid stuff to region
* - move link maintenance to im_demand_hint
* 21/9/11
* - switch to vips_tracked_malloc()
* 18/12/13
* - keep a few buffers in reserve per image, stops malloc/free
* cycling when sharing is repeatedly discovered
* 6/6/16
* - free buffers on image close as well as thread exit, so main thread
* buffers don't clog up the system
* 13/10/16
* - better solution: don't keep a buffercache for non-workers
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG_VERBOSE
#define DEBUG_CREATE
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <stdlib.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/thread.h>
#ifdef DEBUG
/* Track all buffers here for debugging.
*/
static GSList *vips__buffer_all = NULL;
#endif /*DEBUG*/
#ifdef DEBUG_CREATE
static GSList *vips__buffer_cache_all = NULL;
#endif /*DEBUG_CREATE*/
/* The maximum numbers of buffers we hold in reserve per image.
*/
static const int buffer_cache_max_reserve = 2;
/* Workers have a BufferThread (and BufferCache) in a GPrivate they have
* exclusive access to.
*/
static GPrivate *buffer_thread_key = NULL;
void
vips_buffer_print( VipsBuffer *buffer )
{
printf( "VipsBuffer: %p ref_count = %d, ", buffer, buffer->ref_count );
printf( "im = %p, ", buffer->im );
printf( "area.left = %d, ", buffer->area.left );
printf( "area.top = %d, ", buffer->area.top );
printf( "area.width = %d, ", buffer->area.width );
printf( "area.height = %d, ", buffer->area.height );
printf( "done = %d, ", buffer->done );
printf( "cache = %p, ", buffer->cache );
printf( "buf = %p, ", buffer->buf );
printf( "bsize = %zd\n", buffer->bsize );
}
#ifdef DEBUG
static void *
vips_buffer_dump( VipsBuffer *buffer, size_t *reserve, size_t *alive )
{
vips_buffer_print( buffer );
g_assert( buffer->im );
g_assert( buffer->buf );
if( !buffer->cache &&
!buffer->done ) {
/* Global buffer, not linked to any cache.
*/
printf( "global buffer %p, %.3g MB\n",
buffer, buffer->bsize / (1024 * 1024.0) );
*alive += buffer->bsize;
}
else if( buffer->cache &&
buffer->done &&
!vips_rect_isempty( &buffer->area ) &&
g_slist_find( buffer->cache->buffers, buffer ) ) {
/* Published on a thread.
*/
printf( "thread buffer %p, %.3g MB\n",
buffer, buffer->bsize / (1024 * 1024.0) );
*alive += buffer->bsize;
}
else if( buffer->ref_count == 0 &&
buffer->cache &&
!buffer->done &&
vips_rect_isempty( &buffer->area ) &&
g_slist_find( buffer->cache->reserve, buffer ) )
/* Held in reserve.
*/
*reserve += buffer->bsize;
else
printf( "buffer craziness!\n" );
return( NULL );
}
#endif /*DEBUG*/
#ifdef DEBUG_CREATE
static void *
vips_buffer_cache_dump( VipsBufferCache *cache, void *a, void *b )
{
printf( "VipsBufferCache: %p\n", cache );
printf( "\t%d buffers\n", g_slist_length( cache->buffers ) );
printf( "\tthread %p\n", cache->thread );
printf( "\timage %p\n", cache->im );
printf( "\tbuffer_thread %p\n", cache->buffer_thread );
printf( "\t%d in reserve\n", g_slist_length( cache->reserve ) );
return( NULL );
}
#endif /*DEBUG_CREATE*/
void
vips_buffer_dump_all( void )
{
#ifdef DEBUG
if( vips__buffer_all ) {
size_t reserve;
size_t alive;
printf( "buffers:\n" );
reserve = 0;
alive = 0;
vips_slist_map2( vips__buffer_all,
(VipsSListMap2Fn) vips_buffer_dump, &reserve, &alive );
printf( "%.3g MB alive\n", alive / (1024 * 1024.0) );
printf( "%.3g MB in reserve\n", reserve / (1024 * 1024.0) );
}
#ifdef DEBUG_CREATE
if( vips__buffer_cache_all ) {
printf( "buffers: %d buffer cache still alive\n",
g_slist_length( vips__buffer_cache_all ) );
vips_slist_map2( vips__buffer_cache_all,
(VipsSListMap2Fn) vips_buffer_cache_dump, NULL, NULL );
printf( "g_thread_self() == %p\n", g_thread_self() );
}
#endif /*DEBUG_CREATE*/
#endif /*DEBUG*/
}
static void
vips_buffer_free( VipsBuffer *buffer )
{
VIPS_FREEF( vips_tracked_free, buffer->buf );
buffer->bsize = 0;
g_free( buffer );
#ifdef DEBUG
g_mutex_lock( vips__global_lock );
g_assert( g_slist_find( vips__buffer_all, buffer ) );
vips__buffer_all = g_slist_remove( vips__buffer_all, buffer );
g_mutex_unlock( vips__global_lock );
#endif /*DEBUG*/
#ifdef DEBUG_VERBOSE
printf( "vips_buffer_free: freeing buffer %p\n", buffer );
#endif /*DEBUG_VERBOSE*/
}
static void
buffer_thread_free( VipsBufferThread *buffer_thread )
{
VIPS_FREEF( g_hash_table_destroy, buffer_thread->hash );
VIPS_FREE( buffer_thread );
}
/* Run for GDestroyNotify on the VipsBufferThread hash.
*/
static void
buffer_cache_free( VipsBufferCache *cache )
{
GSList *p;
#ifdef DEBUG_CREATE
g_mutex_lock( vips__global_lock );
vips__buffer_cache_all =
g_slist_remove( vips__buffer_cache_all, cache );
g_mutex_unlock( vips__global_lock );
printf( "buffer_cache_free: freeing cache %p on thread %p\n",
cache, g_thread_self() );
printf( "\t(%d caches left)\n",
g_slist_length( vips__buffer_cache_all ) );
#endif /*DEBUG_CREATE*/
/* Need to mark undone so we don't try and take them off this cache on
* unref.
*/
for( p = cache->buffers; p; p = p->next ) {
VipsBuffer *buffer = (VipsBuffer *) p->data;
g_assert( buffer->done );
g_assert( buffer->cache == cache );
buffer->done = FALSE;
buffer->cache = NULL;
}
VIPS_FREEF( g_slist_free, cache->buffers );
for( p = cache->reserve; p; p = p->next ) {
VipsBuffer *buffer = (VipsBuffer *) p->data;
vips_buffer_free( buffer );
}
VIPS_FREEF( g_slist_free, cache->reserve );
g_free( cache );
}
static VipsBufferCache *
buffer_cache_new( VipsBufferThread *buffer_thread, VipsImage *im )
{
VipsBufferCache *cache;
cache = g_new( VipsBufferCache, 1 );
cache->buffers = NULL;
cache->thread = g_thread_self();
cache->im = im;
cache->buffer_thread = buffer_thread;
cache->reserve = NULL;
cache->n_reserve = 0;
#ifdef DEBUG_CREATE
g_mutex_lock( vips__global_lock );
vips__buffer_cache_all =
g_slist_prepend( vips__buffer_cache_all, cache );
g_mutex_unlock( vips__global_lock );
printf( "buffer_cache_new: new cache %p for thread %p on image %p\n",
cache, g_thread_self(), im );
printf( "\t(%d caches now)\n",
g_slist_length( vips__buffer_cache_all ) );
#endif /*DEBUG_CREATE*/
return( cache );
}
static VipsBufferThread *
buffer_thread_new( void )
{
VipsBufferThread *buffer_thread;
buffer_thread = g_new( VipsBufferThread, 1 );
buffer_thread->hash = g_hash_table_new_full(
g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify) buffer_cache_free );
buffer_thread->thread = g_thread_self();
return( buffer_thread );
}
/* Get our private VipsBufferThread. NULL for non-worker threads.
*/
static VipsBufferThread *
buffer_thread_get( void )
{
VipsBufferThread *buffer_thread;
if( vips_thread_isvips() ) {
/* Our threads get a set of private buffers, since we know we
* will be calling vips_thread_shutdown() on thread
* termination.
*/
if( !(buffer_thread = g_private_get( buffer_thread_key )) ) {
buffer_thread = buffer_thread_new();
g_private_set( buffer_thread_key, buffer_thread );
}
g_assert( buffer_thread->thread == g_thread_self() );
}
else
/* Non-vips threads don't have one.
*/
buffer_thread = NULL;
return( buffer_thread );
}
/* Get the VipsBufferCache for this image, or NULL for a non-worker.
*/
static VipsBufferCache *
buffer_cache_get( VipsImage *im )
{
VipsBufferThread *buffer_thread;
VipsBufferCache *cache;
if( (buffer_thread = buffer_thread_get()) ) {
if( !(cache = (VipsBufferCache *)
g_hash_table_lookup( buffer_thread->hash, im )) ) {
cache = buffer_cache_new( buffer_thread, im );
g_hash_table_insert( buffer_thread->hash, im, cache );
}
g_assert( cache->thread == g_thread_self() );
}
else
cache = NULL;
return( cache );
}
/* Pixels have been calculated: publish for other parts of this thread to see.
*/
void
vips_buffer_done( VipsBuffer *buffer )
{
VipsImage *im = buffer->im;
VipsBufferCache *cache;
if( !buffer->done &&
(cache = buffer_cache_get( im )) ) {
g_assert( !g_slist_find( cache->buffers, buffer ) );
g_assert( !buffer->cache );
buffer->done = TRUE;
buffer->cache = cache;
cache->buffers = g_slist_prepend( cache->buffers, buffer );
#ifdef DEBUG_VERBOSE
printf( "vips_buffer_done: "
"thread %p adding buffer %p to cache %p\n",
g_thread_self(), buffer, cache );
vips_buffer_print( buffer );
#endif /*DEBUG_VERBOSE*/
}
}
/* Take off the public 'done' list. Make sure it has no calculated pixels in.
*/
void
vips_buffer_undone( VipsBuffer *buffer )
{
if( buffer->done ) {
VipsBufferCache *cache = buffer->cache;
#ifdef DEBUG_VERBOSE
printf( "vips_buffer_undone: thread %p removing "
"buffer %p from cache %p\n",
g_thread_self(), buffer, cache );
#endif /*DEBUG_VERBOSE*/
g_assert( cache->thread == g_thread_self() );
g_assert( cache->buffer_thread->thread == cache->thread );
g_assert( g_slist_find( cache->buffers, buffer ) );
g_assert( buffer_thread_get() );
g_assert( cache->buffer_thread == buffer_thread_get() );
cache->buffers = g_slist_remove( cache->buffers, buffer );
buffer->done = FALSE;
#ifdef DEBUG_VERBOSE
printf( "vips_buffer_undone: %d buffers left\n",
g_slist_length( cache->buffers ) );
#endif /*DEBUG_VERBOSE*/
}
buffer->cache = NULL;
buffer->area.width = 0;
buffer->area.height = 0;
}
void
vips_buffer_unref( VipsBuffer *buffer )
{
#ifdef DEBUG_VERBOSE
printf( "** vips_buffer_unref: left = %d, top = %d, "
"width = %d, height = %d (%p)\n",
buffer->area.left, buffer->area.top,
buffer->area.width, buffer->area.height,
buffer );
#endif /*DEBUG_VERBOSE*/
g_assert( buffer->ref_count > 0 );
buffer->ref_count -= 1;
if( buffer->ref_count == 0 ) {
VipsBufferCache *cache;
#ifdef DEBUG_VERBOSE
if( !buffer->done )
printf( "vips_buffer_unref: buffer was not done\n" );
#endif /*DEBUG_VERBOSE*/
vips_buffer_undone( buffer );
/* Place on this thread's reserve list for reuse.
*/
if( (cache = buffer_cache_get( buffer->im )) &&
cache->n_reserve < buffer_cache_max_reserve ) {
g_assert( !buffer->cache );
cache->reserve =
g_slist_prepend( cache->reserve, buffer );
cache->n_reserve += 1;
buffer->cache = cache;
buffer->area.width = 0;
buffer->area.height = 0;
}
else
vips_buffer_free( buffer );
}
}
static int
buffer_move( VipsBuffer *buffer, VipsRect *area )
{
VipsImage *im = buffer->im;
size_t new_bsize;
g_assert( buffer->ref_count == 1 );
vips_buffer_undone( buffer );
g_assert( !buffer->done );
buffer->area = *area;
new_bsize = (size_t) VIPS_IMAGE_SIZEOF_PEL( im ) *
area->width * area->height;
if( buffer->bsize < new_bsize ||
!buffer->buf ) {
buffer->bsize = new_bsize;
VIPS_FREEF( vips_tracked_free, buffer->buf );
if( !(buffer->buf = vips_tracked_malloc( buffer->bsize )) )
return( -1 );
}
return( 0 );
}
/* Make a new buffer.
*/
VipsBuffer *
vips_buffer_new( VipsImage *im, VipsRect *area )
{
VipsBufferCache *cache;
VipsBuffer *buffer;
if( (cache = buffer_cache_get( im )) &&
cache->reserve ) {
buffer = (VipsBuffer *) cache->reserve->data;
cache->reserve = g_slist_remove( cache->reserve, buffer );
cache->n_reserve -= 1;
g_assert( buffer->im == im );
g_assert( buffer->done == FALSE );
g_assert( buffer->cache );
buffer->ref_count = 1;
buffer->done = FALSE;
buffer->cache = NULL;
}
else {
buffer = g_new0( VipsBuffer, 1 );
buffer->ref_count = 1;
buffer->im = im;
buffer->done = FALSE;
buffer->cache = NULL;
buffer->buf = NULL;
buffer->bsize = 0;
#ifdef DEBUG
g_mutex_lock( vips__global_lock );
vips__buffer_all =
g_slist_prepend( vips__buffer_all, buffer );
g_mutex_unlock( vips__global_lock );
#endif /*DEBUG*/
}
if( buffer_move( buffer, area ) ) {
vips_buffer_free( buffer );
return( NULL );
}
return( buffer );
}
/* Find an existing buffer that encloses area and return a ref. Or NULL for no
* existing buffer.
*/
static VipsBuffer *
buffer_find( VipsImage *im, VipsRect *r )
{
VipsBufferCache *cache;
VipsBuffer *buffer;
GSList *p;
VipsRect *area;
if( !(cache = buffer_cache_get( im )) )
return( NULL );
/* This needs to be quick :-( don't use
* vips_slist_map2()/vips_rect_includesrect(), do the search
* inline.
*
* FIXME we return the first enclosing buffer, perhaps we should
* search for the largest?
*/
for( p = cache->buffers; p; p = p->next ) {
buffer = (VipsBuffer *) p->data;
area = &buffer->area;
if( area->left <= r->left &&
area->top <= r->top &&
area->left + area->width >= r->left + r->width &&
area->top + area->height >= r->top + r->height ) {
buffer->ref_count += 1;
#ifdef DEBUG_VERBOSE
printf( "buffer_find: left = %d, top = %d, "
"width = %d, height = %d, count = %d (%p)\n",
buffer->area.left, buffer->area.top,
buffer->area.width, buffer->area.height,
buffer->ref_count,
buffer );
#endif /*DEBUG_VERBOSE*/
return( buffer );
}
}
return( NULL );
}
/* Return a ref to a buffer that encloses area. The buffer we return might be
* done.
*/
VipsBuffer *
vips_buffer_ref( VipsImage *im, VipsRect *area )
{
VipsBuffer *buffer;
if( (buffer = buffer_find( im, area )) )
return( buffer );
else
return( vips_buffer_new( im, area ) );
}
/* Unref old, ref new, in a single operation. Reuse stuff if we can. The
* buffer we return might or might not be done.
*/
VipsBuffer *
vips_buffer_unref_ref( VipsBuffer *old_buffer, VipsImage *im, VipsRect *area )
{
VipsBuffer *buffer;
g_assert( !old_buffer ||
old_buffer->im == im );
/* Is the current buffer OK?
*/
if( old_buffer &&
vips_rect_includesrect( &old_buffer->area, area ) )
return( old_buffer );
/* Does the new area already have a buffer?
*/
if( (buffer = buffer_find( im, area )) ) {
VIPS_FREEF( vips_buffer_unref, old_buffer );
return( buffer );
}
/* Is the current buffer unshared? We can just move it.
*/
if( old_buffer &&
old_buffer->ref_count == 1 ) {
if( buffer_move( old_buffer, area ) ) {
vips_buffer_unref( old_buffer );
return( NULL );
}
return( old_buffer );
}
/* Fallback ... unref the old one, make a new one.
*/
VIPS_FREEF( vips_buffer_unref, old_buffer );
if( !(buffer = vips_buffer_new( im, area )) )
return( NULL );
return( buffer );
}
static void
buffer_thread_destroy_notify( VipsBufferThread *buffer_thread )
{
/* We only come here if vips_thread_shutdown() was not called for this
* thread. Do our best to clean up.
*
* GPrivate has stopped working by this point in destruction, be
* careful not to touch that.
*/
buffer_thread_free( buffer_thread );
}
/* Init the buffer cache system. This is called during vips_init.
*/
void
vips__buffer_init( void )
{
static GPrivate private =
G_PRIVATE_INIT( (GDestroyNotify) buffer_thread_destroy_notify );
buffer_thread_key = &private;
if( buffer_cache_max_reserve < 1 )
printf( "vips__buffer_init: buffer reserve disabled\n" );
#ifdef DEBUG
printf( "vips__buffer_init: DEBUG enabled\n" );
#endif /*DEBUG*/
#ifdef DEBUG_CREATE
printf( "vips__buffer_init: DEBUG_CREATE enabled\n" );
#endif /*DEBUG_CREATE*/
}
void
vips__buffer_shutdown( void )
{
VipsBufferThread *buffer_thread;
if( (buffer_thread = g_private_get( buffer_thread_key )) ) {
buffer_thread_free( buffer_thread );
g_private_set( buffer_thread_key, NULL );
}
}
|