gmio_core: let gmio_buffer() initialized the deallocate_func field

This commit is contained in:
Hugues Delorme 2015-03-30 17:25:23 +02:00
parent ca43839585
commit 55a985d3ee
2 changed files with 27 additions and 16 deletions

View File

@ -31,36 +31,39 @@ GMIO_INLINE static gmio_buffer_t gmio_buffer_null()
return buff;
}
gmio_buffer_t gmio_buffer(void* ptr, size_t size)
gmio_buffer_t gmio_buffer(
void* ptr, size_t size, void (*deallocate_func)(void*))
{
gmio_buffer_t buff;
buff.ptr = ptr;
buff.size = ptr != NULL ? size : 0;
buff.deallocate_func = NULL;
buff.deallocate_func = deallocate_func;
return buff;
}
gmio_buffer_t gmio_buffer_malloc(size_t size)
{
gmio_buffer_t buff = gmio_buffer(malloc(size), size);
buff.deallocate_func = &free;
return buff;
return gmio_buffer(malloc(size), size, &free);
}
gmio_buffer_t gmio_buffer_calloc(size_t num, size_t size)
{
gmio_buffer_t buff = gmio_buffer(calloc(num, size), num * size);
buff.deallocate_func = &free;
return buff;
return gmio_buffer(calloc(num, size), num * size, &free);
}
gmio_buffer_t gmio_buffer_realloc(void* ptr, size_t size)
{
return gmio_buffer(realloc(ptr, size), size, &free);
}
gmio_buffer_t gmio_buffer_alloca(size_t size)
{
#if defined(GMIO_HAVE_BSD_ALLOCA_FUNC)
return gmio_buffer(alloca(size), size);
return gmio_buffer(alloca(size), size, NULL);
#elif defined(GMIO_HAVE_WIN_ALLOCA_FUNC)
# ifdef _MSC_VER
__try {
return gmio_buffer(_alloca(size), size);
return gmio_buffer(_alloca(size), size, NULL);
}
__except(GetExceptionCode() == STATUS_STACK_OVERFLOW) {
/* The stack overflowed */
@ -68,6 +71,9 @@ gmio_buffer_t gmio_buffer_alloca(size_t size)
exit(GMIO_UNKNOWN_ERROR);
return gmio_buffer_null();
}
# else
return gmio_buffer(_alloca(size), size, NULL);
# endif /* _MSC_VER */
#else
return gmio_buffer_null();
#endif

View File

@ -14,7 +14,7 @@
****************************************************************************/
/*! \file buffer.h
* Declaration of gmio_buffer
* Declaration of gmio_buffer and utility functions
*/
#ifndef GMIO_BUFFER_H
@ -33,7 +33,8 @@ struct gmio_buffer
/*! Size (in bytes) of the memory buffer */
size_t size;
/*! Optional pointer on function that deallocates the memory block \p ptr */
/*! Optional pointer on a function that deallocates the memory block
* beginning at \p ptr */
void (*deallocate_func)(void* ptr);
};
typedef struct gmio_buffer gmio_buffer_t;
@ -44,15 +45,19 @@ GMIO_C_LINKAGE_BEGIN
*
* If \p ptr is NULL then gmio_buffer::size is forced to \c 0
*/
GMIO_LIB_EXPORT gmio_buffer_t gmio_buffer(void* ptr, size_t size);
GMIO_LIB_EXPORT gmio_buffer_t gmio_buffer(
void* ptr, size_t size, void (*deallocate_func)(void*));
/*! Returns a gmio_buffer object allocated with standard malloc() */
/*! Returns a gmio_buffer object allocated with standard \c malloc() */
GMIO_LIB_EXPORT gmio_buffer_t gmio_buffer_malloc(size_t size);
/*! Returns a gmio_buffer object allocated with standard calloc() */
/*! Returns a gmio_buffer object allocated with standard \c calloc() */
GMIO_LIB_EXPORT gmio_buffer_t gmio_buffer_calloc(size_t num, size_t size);
/*! Returns a gmio_buffer object allocated with OS-specific alloca() */
/*! Returns a gmio_buffer object allocated with standard \c realloc() */
GMIO_LIB_EXPORT gmio_buffer_t gmio_buffer_realloc(void* ptr, size_t size);
/*! Returns a gmio_buffer object allocated with OS-specific \c alloca() */
GMIO_LIB_EXPORT gmio_buffer_t gmio_buffer_alloca(size_t size);
/*! Safe and convenient call to gmio_buffer::deallocate_func() */