2015-03-24 01:16:37 +08:00
|
|
|
/****************************************************************************
|
|
|
|
** GeomIO Library
|
|
|
|
** Copyright FougSys (2 Mar. 2015)
|
|
|
|
** contact@fougsys.fr
|
|
|
|
**
|
|
|
|
** This software is a reusable library whose purpose is to provide complete
|
|
|
|
** I/O support for various CAD file formats (eg. STL)
|
|
|
|
**
|
|
|
|
** This software is governed by the CeCILL-B license under French law and
|
|
|
|
** abiding by the rules of distribution of free software. You can use,
|
|
|
|
** modify and/ or redistribute the software under the terms of the CeCILL-B
|
|
|
|
** license as circulated by CEA, CNRS and INRIA at the following URL
|
2015-03-30 15:05:25 +08:00
|
|
|
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
|
2015-03-24 01:16:37 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
|
2015-03-30 22:37:47 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#if defined(GMIO_HAVE_BSD_ALLOCA_FUNC)
|
|
|
|
# include <alloca.h>
|
2015-04-01 21:43:03 +08:00
|
|
|
#elif defined(GMIO_HAVE_WIN__ALLOCA_FUNC)
|
2015-03-30 22:37:47 +08:00
|
|
|
# include "error.h"
|
|
|
|
# include <windows.h>
|
|
|
|
# include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2015-03-31 16:10:26 +08:00
|
|
|
GMIO_INLINE gmio_buffer_t gmio_buffer_null()
|
2015-03-30 22:37:47 +08:00
|
|
|
{
|
|
|
|
gmio_buffer_t buff = { 0 };
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
2015-03-30 23:25:23 +08:00
|
|
|
gmio_buffer_t gmio_buffer(
|
|
|
|
void* ptr, size_t size, void (*deallocate_func)(void*))
|
2015-03-24 01:16:37 +08:00
|
|
|
{
|
|
|
|
gmio_buffer_t buff;
|
|
|
|
buff.ptr = ptr;
|
2015-03-30 22:48:05 +08:00
|
|
|
buff.size = ptr != NULL ? size : 0;
|
2015-03-30 23:25:23 +08:00
|
|
|
buff.deallocate_func = deallocate_func;
|
2015-03-30 22:37:47 +08:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
gmio_buffer_t gmio_buffer_malloc(size_t size)
|
|
|
|
{
|
2015-03-30 23:25:23 +08:00
|
|
|
return gmio_buffer(malloc(size), size, &free);
|
2015-03-24 01:16:37 +08:00
|
|
|
}
|
2015-03-30 22:37:47 +08:00
|
|
|
|
|
|
|
gmio_buffer_t gmio_buffer_calloc(size_t num, size_t size)
|
|
|
|
{
|
2015-03-30 23:25:23 +08:00
|
|
|
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);
|
2015-03-30 22:37:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gmio_buffer_t gmio_buffer_alloca(size_t size)
|
|
|
|
{
|
|
|
|
#if defined(GMIO_HAVE_BSD_ALLOCA_FUNC)
|
2015-03-30 23:25:23 +08:00
|
|
|
return gmio_buffer(alloca(size), size, NULL);
|
2015-04-01 21:43:03 +08:00
|
|
|
#elif defined(GMIO_HAVE_WIN__ALLOCA_FUNC)
|
2015-03-30 23:25:23 +08:00
|
|
|
# ifdef _MSC_VER
|
2015-03-30 22:37:47 +08:00
|
|
|
__try {
|
2015-03-30 23:25:23 +08:00
|
|
|
return gmio_buffer(_alloca(size), size, NULL);
|
2015-03-30 22:37:47 +08:00
|
|
|
}
|
|
|
|
__except(GetExceptionCode() == STATUS_STACK_OVERFLOW) {
|
|
|
|
/* The stack overflowed */
|
|
|
|
if (_resetstkoflw() == 0)
|
2015-04-02 16:04:24 +08:00
|
|
|
exit(GMIO_ERROR_UNKNOWN);
|
2015-03-30 22:37:47 +08:00
|
|
|
return gmio_buffer_null();
|
|
|
|
}
|
2015-03-30 23:25:23 +08:00
|
|
|
# else
|
|
|
|
return gmio_buffer(_alloca(size), size, NULL);
|
|
|
|
# endif /* _MSC_VER */
|
2015-03-30 22:37:47 +08:00
|
|
|
#else
|
2015-04-01 21:39:53 +08:00
|
|
|
GMIO_UNUSED(size);
|
2015-03-30 22:37:47 +08:00
|
|
|
return gmio_buffer_null();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void gmio_buffer_deallocate(gmio_buffer_t *buffer)
|
|
|
|
{
|
|
|
|
if (buffer != NULL && buffer->deallocate_func != NULL)
|
|
|
|
buffer->deallocate_func(buffer->ptr);
|
|
|
|
}
|