switch buffer allocation to tracked

buffer allocs and image memory allocs go to the tracked pool
This commit is contained in:
John Cupitt 2011-09-21 18:00:03 +01:00
parent 9c84b0dfd9
commit 5462d5d6b2
4 changed files with 22 additions and 35 deletions

View File

@ -69,7 +69,7 @@ char *vips_strdup( VipsObject *object, char *str );
int vips_free( void *buf );
void vips_tracked_free( void *s );
void *vips_tracked_malloc( VipsObject *object, size_t size );
void *vips_tracked_malloc( size_t size );
size_t vips_tracked_get_mem( void );
size_t vips_tracked_get_mem_highwater( void );
int vips_tracked_get_allocs( void );

View File

@ -15,6 +15,8 @@
* 5/3/10
* - move invalid stuff to region
* - move link maintenance to im_demand_hint
* 21/9/11
* - switch to vips_tracked_malloc()
*/
/*
@ -114,7 +116,7 @@ buffer_cache_list_free( VipsBufferCacheList *cache_list )
}
g_slist_free( cache_list->buffers );
vips_free( cache_list );
g_free( cache_list );
}
static VipsBufferCacheList *
@ -122,8 +124,7 @@ buffer_cache_list_new( VipsBufferCache *cache, VipsImage *im )
{
VipsBufferCacheList *cache_list;
if( !(cache_list = VIPS_NEW( NULL, VipsBufferCacheList )) )
return( NULL );
cache_list = g_new( VipsBufferCacheList, 1 );
cache_list->buffers = NULL;
cache_list->thread = g_thread_self();
cache_list->cache = cache;
@ -143,9 +144,7 @@ buffer_cache_new( void )
{
VipsBufferCache *cache;
if( !(cache = VIPS_NEW( NULL, VipsBufferCache )) )
return( NULL );
cache = g_new( VipsBufferCache, 1 );
cache->hash = g_hash_table_new_full( g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify) buffer_cache_list_free );
cache->thread = g_thread_self();
@ -275,9 +274,9 @@ vips_buffer_unref( VipsBuffer *buffer )
vips_buffer_undone( buffer );
buffer->im = NULL;
VIPS_FREE( buffer->buf );
vips_tracked_free( buffer->buf );
buffer->bsize = 0;
vips_free( buffer );
g_free( buffer );
#ifdef DEBUG
g_mutex_lock( vips__global_lock );
@ -297,9 +296,7 @@ vips_buffer_new( VipsImage *im, VipsRect *area )
{
VipsBuffer *buffer;
if( !(buffer = VIPS_NEW( NULL, VipsBuffer )) )
return( NULL );
buffer = g_new( VipsBuffer, 1 );
buffer->ref_count = 1;
buffer->im = im;
buffer->area = *area;
@ -307,7 +304,7 @@ vips_buffer_new( VipsImage *im, VipsRect *area )
buffer->cache = NULL;
buffer->bsize = (size_t) VIPS_IMAGE_SIZEOF_PEL( im ) *
area->width * area->height;
if( !(buffer->buf = vips_malloc( NULL, buffer->bsize )) ) {
if( !(buffer->buf = vips_tracked_malloc( buffer->bsize )) ) {
vips_buffer_unref( buffer );
return( NULL );
}
@ -346,8 +343,8 @@ buffer_move( VipsBuffer *buffer, VipsRect *area )
area->width * area->height;
if( buffer->bsize < new_bsize ) {
buffer->bsize = new_bsize;
VIPS_FREE( buffer->buf );
if( !(buffer->buf = vips_malloc( NULL, buffer->bsize )) )
vips_tracked_free( buffer->buf );
if( !(buffer->buf = vips_tracked_malloc( buffer->bsize )) )
return( -1 );
}

View File

@ -258,7 +258,7 @@ vips_image_finalize( GObject *gobject )
if( image->dtype == VIPS_IMAGE_SETBUF ) {
VIPS_DEBUG_MSG( "vips_image_finalize: "
"freeing buffer\n" );
vips_free( image->data );
vips_tracked_free( image->data );
image->dtype = VIPS_IMAGE_NONE;
}
@ -1859,7 +1859,7 @@ vips__image_write_prepare( VipsImage *image )
/* Allocate memory.
*/
if( !image->data )
if( !(image->data = vips_malloc( NULL,
if( !(image->data = vips_tracked_malloc(
VIPS_IMAGE_SIZEOF_IMAGE( image ))) )
return( -1 );

View File

@ -79,11 +79,13 @@
* g_malloc()/g_free() functions. Memory allocated and freeded using these
* functions is interchangeable with any other glib library.
*
* Second, a pair of functions, vips_tracked_malloc() and vips_tracked_free()
* Second, a pair of functions, vips_tracked_malloc() and vips_tracked_free(),
* which are NOT compatible. If you g_free() memory that has been allocated
* with vips_tracked_malloc() you will see crashes. The tracked functions are
* with vips_tracked_malloc() you will see crashes.
*
* The tracked functions are
* only suitable for large allocations internal to the library, for example
* pixel buffers. libvips tracks the total amount of live tracked memory and
* pixel buffers. libvips watches the total amount of live tracked memory and
* uses this information to decide when to trim caches.
*/
@ -245,20 +247,12 @@ vips_tracked_mutex_new( void *data )
return( g_mutex_new() );
}
static void
vips_tracked_cb( VipsObject *object, char *buf )
{
vips_tracked_free( buf );
}
/**
* vips_tracked_malloc:
* @object: allocate memory local to this #VipsObject, or %NULL
* @size: number of bytes to allocate
*
* Malloc local to @object, that is, the memory will be automatically
* freed for you when the object is closed. If @object is %NULL, you need to
* free the memory explicitly with vips_tracked_free().
* Allocate an area of memory that will be tracked by vips_tracked_get_mem()
* and friends.
*
* If allocation fails, vips_malloc() returns %NULL and
* sets an error message.
@ -270,7 +264,7 @@ vips_tracked_cb( VipsObject *object, char *buf )
* Returns: a pointer to the allocated memory, or %NULL on error.
*/
void *
vips_tracked_malloc( VipsObject *object, size_t size )
vips_tracked_malloc( size_t size )
{
static GOnce vips_tracked_once = G_ONCE_INIT;
@ -312,10 +306,6 @@ vips_tracked_malloc( VipsObject *object, size_t size )
g_mutex_unlock( vips_tracked_mutex );
if( object )
g_signal_connect( object, "postclose",
G_CALLBACK( vips_tracked_cb ), buf );
return( buf );
}