2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gb_common.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __COMMON_C
|
|
|
|
#define __COMMON_CHECK_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
|
|
|
#include "gb_common_check.h"
|
|
|
|
|
|
|
|
#include "gb_common_case_temp.h"
|
|
|
|
#include "gb_common_buffer_temp.h"
|
|
|
|
#include "gb_common_swap_temp.h"
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
sigjmp_buf CHECK_jump;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static sighandler_t _oldsegv;
|
|
|
|
static sighandler_t _oldbus;
|
|
|
|
static int _dummy;
|
|
|
|
static volatile int _got_error;
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void COMMON_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void signal_error()
|
|
|
|
{
|
|
|
|
_got_error = TRUE;
|
|
|
|
siglongjmp(CHECK_jump, 1);
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void CHECK_enter(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
_got_error = FALSE;
|
|
|
|
_oldsegv = signal(SIGSEGV, signal_error);
|
|
|
|
_oldbus = signal(SIGBUS, signal_error);
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void CHECK_leave(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
signal(SIGSEGV, _oldsegv);
|
|
|
|
signal(SIGBUS, _oldbus);
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool CHECK_got_error(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
return _got_error;
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool CHECK_address(void *ptr, size_t len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
offset_t i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
CHECK_enter();
|
|
|
|
if (sigsetjmp(CHECK_jump, TRUE) == 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < len; i += 1024)
|
|
|
|
_dummy = ((int *)ptr)[i];
|
|
|
|
}
|
|
|
|
CHECK_leave();
|
|
|
|
return _got_error;
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool CHECK_strlen(char *ptr, size_t *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
size_t l = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
CHECK_enter();
|
|
|
|
if (sigsetjmp(CHECK_jump, TRUE) == 0)
|
|
|
|
{
|
|
|
|
l = strlen(ptr);
|
|
|
|
*len = l;
|
|
|
|
}
|
|
|
|
CHECK_leave();
|
2008-01-17 22:39:26 +01:00
|
|
|
if (l != (size_t)(int)l)
|
|
|
|
_got_error = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
return _got_error;
|
|
|
|
}
|
|
|
|
|