Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel: reduce reliance on autoconf defined values (via config.h) in our header files #4678

1 change: 0 additions & 1 deletion GNUmakefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#

# GAP specific
SYS_IS_DARWIN = @SYS_IS_DARWIN@
SYS_IS_CYGWIN32 = @SYS_IS_CYGWIN32@
ABI = @ABI@
ABI_CFLAGS = @ABI_CFLAGS@
Expand Down
17 changes: 2 additions & 15 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ dnl
dnl Test if pthread_getspecific() can be overridden as
dnl __attribute__((pure)).
dnl
AS_IF([test "x$enable_hpcgap" = xyes],[
AC_MSG_CHECKING([[whether pthread_getspecific() can be made pure]])
AC_COMPILE_IFELSE(
[AC_LANG_SOURCE([[
Expand All @@ -406,6 +407,7 @@ void * pthread_getspecific(pthread_key_t key);
], [
AC_MSG_RESULT(no)
])
])

dnl
dnl User setting: Debug mode (off by default)
Expand Down Expand Up @@ -574,11 +576,6 @@ AC_SUBST([GMP_CPPFLAGS])
AC_SUBST([GMP_LDFLAGS])
AC_SUBST([GMP_LIBS])

dnl Some packages check the USE_GMP flag; retain it for now to
dnl allow these packages to work correctly for now. We should
dnl remove this eventually.
AC_DEFINE([USE_GMP], [1], [for backwards compatibility])


dnl Find zlib
AC_ARG_WITH([zlib],
Expand Down Expand Up @@ -881,7 +878,6 @@ esac

case "$host_os" in
*cygwin*)
AC_DEFINE([SYS_IS_CYGWIN32], [1], [define if this is the Cygwin32 port])
CYGWIN=yes

AS_IF([test "x$enable_compat_mode" = xyes],[
Expand All @@ -896,15 +892,9 @@ case "$host_os" in
AC_CONFIG_FILES([bin/gapcmd.bat:cnf/cygwin/gapcmd.bat.in], [chmod +x bin/gapcmd.bat])
])
;;
*darwin*)
AC_DEFINE([SYS_IS_DARWIN], [1], [define if this is the Darwin port])
DARWIN=yes
;;
esac

AC_SUBST([SYS_IS_CYGWIN32], [$CYGWIN])
SYS_IS_DARWIN=$DARWIN
AC_SUBST([SYS_IS_DARWIN])


dnl
Expand Down Expand Up @@ -969,9 +959,6 @@ AC_CHECK_FUNCS([setitimer])
dnl check for functions dealing with virtual memory
AC_CHECK_FUNCS([vm_allocate sbrk madvise sysconf])

dnl check for functions dealing with strings and integers
AC_CHECK_FUNCS([strlcpy strlcmp strlcat])

dnl check for large-file support (for accessing files whose sizes or inodes require 64bits)
AC_SYS_LARGEFILE

Expand Down
26 changes: 26 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,38 @@

#include "debug.h"

// check if we are on a 64 or 32 bit machine; in the former
// case, define SYS_IS_64_BIT.
// also define SIZEOF_VOID_P for backwards compatibility with some
// GAP packages that expect it
#if INTPTR_MAX == INT64_MAX
#define SYS_IS_64_BIT 1
#define SIZEOF_VOID_P 8
#elif INTPTR_MAX == INT32_MAX
#undef SYS_IS_64_BIT
#define SIZEOF_VOID_P 4
#else
#error Unknown pointer size or missing size macros!
#endif

// check that the pointer size detected by configure matches that of the
// current compiler; this helps prevent kernel extensions from being
// compiled with the wrong ABI
GAP_STATIC_ASSERT(sizeof(void *) == SIZEOF_VOID_P, "sizeof(void *) is wrong");


// check for cygwin
#if defined(__CYGWIN__) || defined(__CYGWIN32__)
// for historical reasons, the macro we define is called SYS_IS_CYGWIN32
#define SYS_IS_CYGWIN32 1
#endif


// Some packages check the USE_GMP flag; retain it for now to allow these
// packages to work correctly for now. We should remove this eventually.
#define USE_GMP 1


#ifdef USE_GASMAN
#define GAP_ENABLE_SAVELOAD
#endif
Expand Down
8 changes: 6 additions & 2 deletions src/compiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
// updated to not need it.
#include <stdio.h>

// HACK: most (all?) GAP packages with a kernel extension include compiled.h
// to get all GAP headers. They should ultimately all switch to including
// gap_all.h; however that header has only been available since GAP 4.11.0, so
// it will take some time for packages to make the switch.
#include "gap_all.h"

#ifdef __cplusplus
extern "C" {
#endif

#include "gap_all.h"

extern Obj InfoDecision;


Expand Down
2 changes: 0 additions & 2 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

// Enable various GAP debugging features
#define COUNT_BAGS
#define COUNT_OPERS
#define DEBUG_GMP 1
#else
#define GAP_ASSERT(x)
#endif
Expand Down
22 changes: 11 additions & 11 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,34 +522,34 @@ void RequireArgumentEx(const char * funcname,
Int arg1 = 0;

if (funcname) {
strlcat(msgbuf, funcname, sizeof(msgbuf));
strlcat(msgbuf, ": ", sizeof(msgbuf));
gap_strlcat(msgbuf, funcname, sizeof(msgbuf));
gap_strlcat(msgbuf, ": ", sizeof(msgbuf));
}
if (argname) {
strlcat(msgbuf, argname, sizeof(msgbuf));
strlcat(msgbuf, " ", sizeof(msgbuf));
gap_strlcat(msgbuf, argname, sizeof(msgbuf));
gap_strlcat(msgbuf, " ", sizeof(msgbuf));
}
strlcat(msgbuf, msg, sizeof(msgbuf));
gap_strlcat(msgbuf, msg, sizeof(msgbuf));
if (IS_INTOBJ(op)) {
strlcat(msgbuf, " (not the integer %d)", sizeof(msgbuf));
gap_strlcat(msgbuf, " (not the integer %d)", sizeof(msgbuf));
arg1 = INT_INTOBJ(op);
}
else if (op == True)
strlcat(msgbuf, " (not the value 'true')", sizeof(msgbuf));
gap_strlcat(msgbuf, " (not the value 'true')", sizeof(msgbuf));
else if (op == False)
strlcat(msgbuf, " (not the value 'false')", sizeof(msgbuf));
gap_strlcat(msgbuf, " (not the value 'false')", sizeof(msgbuf));
else if (op == Fail)
strlcat(msgbuf, " (not the value 'fail')", sizeof(msgbuf));
gap_strlcat(msgbuf, " (not the value 'fail')", sizeof(msgbuf));
else {
const char * tnam = TNAM_OBJ(op);
// heuristic to choose between 'a' and 'an': use 'an' before a vowel
// and 'a' otherwise; however, that's not always correct, e.g. it is
// "an FFE", so we add a special case for that as well
if (TNUM_OBJ(op) == T_FFE || tnam[0] == 'a' || tnam[0] == 'e' ||
tnam[0] == 'i' || tnam[0] == 'o' || tnam[0] == 'u')
strlcat(msgbuf, " (not an %s)", sizeof(msgbuf));
gap_strlcat(msgbuf, " (not an %s)", sizeof(msgbuf));
else
strlcat(msgbuf, " (not a %s)", sizeof(msgbuf));
gap_strlcat(msgbuf, " (not a %s)", sizeof(msgbuf));
arg1 = (Int)tnam;
}

Expand Down
8 changes: 4 additions & 4 deletions src/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ static Obj FuncSHELL(Obj self,
if (GET_LEN_STRING(prompt) > 80)
ErrorMayQuit("SHELL: <prompt> must be a string of length at most 80", 0, 0);
promptBuffer[0] = '\0';
strlcat(promptBuffer, CONST_CSTR_STRING(prompt), sizeof(promptBuffer));
gap_strlcat(promptBuffer, CONST_CSTR_STRING(prompt), sizeof(promptBuffer));

if (preCommandHook == False)
preCommandHook = 0;
Expand Down Expand Up @@ -744,7 +744,7 @@ static Obj FuncGASMAN(Obj self, Obj args)
if ( TNAM_TNUM(k) != 0 ) {
Char buf[41];
buf[0] = '\0';
strlcat( buf, TNAM_TNUM(k), sizeof(buf) );
gap_strlcat( buf, TNAM_TNUM(k), sizeof(buf) );
Pr("%40s ", (Int)buf, 0);
Pr("%8d %8d ", (Int)InfoBags[k].nrLive,
(Int)(InfoBags[k].sizeLive/1024));
Expand All @@ -769,7 +769,7 @@ static Obj FuncGASMAN(Obj self, Obj args)
InfoBags[k].sizeAll != 0) ) {
Char buf[41];
buf[0] = '\0';
strlcat( buf, TNAM_TNUM(k), sizeof(buf) );
gap_strlcat( buf, TNAM_TNUM(k), sizeof(buf) );
Pr("%40s ", (Int)buf, 0);
Pr("%8d %8d ", (Int)InfoBags[k].nrLive,
(Int)(InfoBags[k].sizeLive/1024));
Expand Down Expand Up @@ -1502,7 +1502,7 @@ void InitializeGap (
char buf[41];

buf[0] = '\0';
strlcat( buf, TNAM_TNUM(i), sizeof(buf) );
gap_strlcat( buf, TNAM_TNUM(i), sizeof(buf) );
Pr("#W %36s ", (Int)buf, 0);
Pr("%8d %8d ", (Int)InfoBags[i].nrLive,
(Int)(InfoBags[i].sizeLive/1024));
Expand Down
8 changes: 4 additions & 4 deletions src/gaptime.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <sys/resource.h>
#endif

#ifdef SYS_IS_DARWIN
#ifdef __MACH__ // macOS
#include <mach/mach_time.h>
#endif

Expand Down Expand Up @@ -86,7 +86,7 @@ Int8 SyNanosecondsSinceEpoch(void)
{
Int8 res;

#if defined(SYS_IS_DARWIN)
#if defined(__MACH__)
static mach_timebase_info_data_t timeinfo;
if (timeinfo.denom == 0) {
(void)mach_timebase_info(&timeinfo);
Expand Down Expand Up @@ -146,7 +146,7 @@ static Int8 SyNanosecondsSinceEpochResolution(void)
{
Int8 res;

#if defined(SYS_IS_DARWIN)
#if defined(__MACH__)
static mach_timebase_info_data_t timeinfo;
if (timeinfo.denom == 0) {
(void)mach_timebase_info(&timeinfo);
Expand Down Expand Up @@ -254,7 +254,7 @@ static Obj FuncNanosecondsSinceEpochInfo(Obj self)
const char * method = "unsupported";
Int monotonic = 0;

#if defined(SYS_IS_DARWIN)
#if defined(__MACH__)
method = "mach_absolute_time";
monotonic = 1;
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
Expand Down
6 changes: 3 additions & 3 deletions src/gvars.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,8 @@ UInt GVarName (
if (*cns) { /* only if a namespace is set */
len = strlen(name);
if (name[len-1] == NSCHAR) {
strlcpy(gvarbuf, name, 512);
strlcat(gvarbuf, cns, sizeof(gvarbuf));
gap_strlcpy(gvarbuf, name, 512);
gap_strlcat(gvarbuf, cns, sizeof(gvarbuf));
name = gvarbuf;
}
}
Expand Down Expand Up @@ -721,7 +721,7 @@ UInt GVarName (
gvar = INTOBJ_INT(numGVars);
SET_ELM_PLIST( TableGVars, pos, gvar );
if (name != gvarbuf)
strlcpy(gvarbuf, name, sizeof(gvarbuf));
gap_strlcpy(gvarbuf, name, sizeof(gvarbuf));
string = MakeImmString(gvarbuf);

#ifdef USE_GVAR_BUCKETS
Expand Down
4 changes: 3 additions & 1 deletion src/integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ static Obj ObjInt_UIntInv( UInt i );


/* debugging */
#ifndef DEBUG_GMP
#ifdef GAP_KERNEL_DEBUG
#define DEBUG_GMP 1
#else
#define DEBUG_GMP 0
#endif

Expand Down
5 changes: 3 additions & 2 deletions src/intobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ EXPORT_INLINE Obj prod_intobjs(Int l, Int r)
#else

#ifdef SYS_IS_64_BIT
typedef Int4 HalfInt;
#define HalfInt Int4
#else
typedef Int2 HalfInt;
#define HalfInt Int2
#endif

EXPORT_INLINE Obj prod_intobjs(Int l, Int r)
Expand All @@ -256,6 +256,7 @@ EXPORT_INLINE Obj prod_intobjs(Int l, Int r)

return (Obj)0;
}
#undef HalfInt
#endif

#define PROD_INTOBJS(o, l, r) ((o) = prod_intobjs((Int)(l), (Int)(r)))
Expand Down
6 changes: 3 additions & 3 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ UInt OpenInput(TypInputFile * input, const Char * filename)
else
input->echo = FALSE;

strlcpy(input->name, filename, sizeof(input->name));
gap_strlcpy(input->name, filename, sizeof(input->name));
input->gapnameid = 0;

// start with an empty line
Expand Down Expand Up @@ -427,7 +427,7 @@ UInt OpenInputStream(TypInputFile * input, Obj stream, BOOL echo)
input->sline = 0;
}
input->echo = echo;
strlcpy(input->name, "stream", sizeof(input->name));
gap_strlcpy(input->name, "stream", sizeof(input->name));
input->gapnameid = 0;

// start with an empty line
Expand Down Expand Up @@ -951,7 +951,7 @@ void SetPrompt(const char * prompt)
{
if (SyQuiet)
prompt = "";
strlcpy(STATE(Prompt), prompt, sizeof(STATE(Prompt)));
gap_strlcpy(STATE(Prompt), prompt, sizeof(STATE(Prompt)));
}


Expand Down
18 changes: 9 additions & 9 deletions src/opers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ extern "C" {

} // extern "C"

#ifdef GAP_KERNEL_DEBUG
#define COUNT_OPERS
#endif

#ifndef __has_cpp_attribute // For backwards compatibility
#define __has_cpp_attribute(x) 0
Expand Down Expand Up @@ -1166,7 +1169,6 @@ Obj NewAndFilter (

Int str_len;
Obj str;
char* s;

RequireFilter(0, oper1, "<oper1>");
RequireFilter(0, oper2, "<oper2>");
Expand All @@ -1182,14 +1184,12 @@ Obj NewAndFilter (

str_len = GET_LEN_STRING(NAME_FUNC(oper1)) + GET_LEN_STRING(NAME_FUNC(oper2)) + 8;
str = NEW_STRING(str_len);
s = CSTR_STRING(str);
s[0] = '(';
s[1] = 0;
strlcat(s, CONST_CSTR_STRING(NAME_FUNC(oper1)), str_len);
strlcat(s, " and ", str_len);
strlcat(s, CONST_CSTR_STRING(NAME_FUNC(oper2)), str_len);
strlcat(s, ")", str_len);
SET_LEN_STRING(str, str_len - 1);
SET_LEN_STRING(str, 0);
AppendCStr(str, "(", 1);
AppendString(str, NAME_FUNC(oper1));
AppendCStr(str, " and ", 5);
AppendString(str, NAME_FUNC(oper2));
AppendCStr(str, ")", 1);

getter = NewFunctionT( T_FUNCTION, sizeof(OperBag), str, 1,
ArglistObj, (ObjFunc)DoAndFilter );
Expand Down
4 changes: 2 additions & 2 deletions src/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ enableAtStartup(char * filename, Int repeats, TickMethod tickMethod)
Panic("Failed to open '%s' for profiling output.\n", filename);
}

strlcpy(profileState.filename, filename, GAP_PATH_MAX);
gap_strlcpy(profileState.filename, filename, GAP_PATH_MAX);

ActivateHooks(&profileHooks);

Expand Down Expand Up @@ -744,7 +744,7 @@ static Obj FuncACTIVATE_PROFILING(Obj self,

fopenMaybeCompressed(CONST_CSTR_STRING(filename), &profileState);

strlcpy(profileState.filename, CONST_CSTR_STRING(filename), GAP_PATH_MAX);
gap_strlcpy(profileState.filename, CONST_CSTR_STRING(filename), GAP_PATH_MAX);

if(profileState.Stream == 0) {
HashUnlock(&profileState);
Expand Down
2 changes: 1 addition & 1 deletion src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ static LHSRef ReadVar(ReaderState * rs, TypSymbolSet follow)
ref.type = R_GVAR;
// we do not want to call GVarName on this value until after we
// have checked if this is the argument to a lambda function
strlcpy(varname, rs->s.Value, sizeof(varname));
gap_strlcpy(varname, rs->s.Value, sizeof(varname));
}

// match away the identifier, now that we know the variable
Expand Down
Loading