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

Gasman fixes for stable-4.9 #2166

Merged
merged 4 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/gasman.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@

#include <src/gaputils.h>
#include <src/io.h>
#include <src/sysfiles.h>

#include <stdio.h>

/****************************************************************************
**
Expand Down Expand Up @@ -294,7 +296,12 @@ void CHANGED_BAG(Bag bag) {
answer in units of a word (ie sizeof(UInt) bytes), which should
therefore be small enough not to cause problems. */

#define SpaceBetweenPointers(a,b) (((UInt)((UInt)(a) - (UInt)(b)))/sizeof(Bag))
static inline UInt SpaceBetweenPointers(const Bag * a, const Bag * b)
{
GAP_ASSERT(b <= a);
UInt res = (((UInt)((UInt)(a) - (UInt)(b))) / sizeof(Bag));
return res;
}

#define SizeMptrsArea SpaceBetweenPointers(OldBags, MptrBags)
#define SizeOldBagsArea SpaceBetweenPointers(YoungBags, OldBags)
Expand Down Expand Up @@ -1078,7 +1085,7 @@ Bag NewBag (
if ( (FreeMptrBags == 0 || SizeAllocationArea < WORDS_BAG(sizeof(BagHeader)+size))
&& CollectBags( size, 0 ) == 0 )
{
return 0;
SyAbortBags("cannot extend the workspace any more!!!!");
}

#ifdef COUNT_BAGS
Expand Down Expand Up @@ -1282,9 +1289,9 @@ UInt ResizeBag (
else if ( PTR_BAG(bag) + WORDS_BAG(old_size) == AllocBags ) {
CLEAR_CANARY();
// check that enough storage for the new bag is available
if ( EndBags < PTR_BAG(bag)+WORDS_BAG(new_size)
if (SpaceBetweenPointers(EndBags, CONST_PTR_BAG(bag)) < WORDS_BAG(new_size)
&& CollectBags( new_size-old_size, 0 ) == 0 ) {
return 0;
SyAbortBags("cannot extend the workspace any more!!!!!");
}

// update header pointer in case bag moved
Expand Down Expand Up @@ -1312,7 +1319,7 @@ UInt ResizeBag (
/* check that enough storage for the new bag is available */
if ( SizeAllocationArea < WORDS_BAG(sizeof(BagHeader)+new_size)
&& CollectBags( new_size, 0 ) == 0 ) {
return 0;
SyAbortBags("Cannot extend the workspace any more!!!!!!");
}
CLEAR_CANARY();

Expand Down Expand Up @@ -1362,7 +1369,7 @@ UInt ResizeBag (
SET_PTR_BAG(bag, dst);

/* copy the contents of the bag */
memmove((void *)dst, (void *)DATA(header),
SyMemmove((void *)dst, (void *)DATA(header),
sizeof(Obj) * WORDS_BAG(old_size));
}

Expand Down Expand Up @@ -1884,7 +1891,7 @@ UInt CollectBags (

/* Otherwise do the default thing */
else if ( dst != DATA(header) ) {
memmove(dst, DATA(header), (end - DATA(header))*sizeof(Bag));
SyMemmove(dst, DATA(header), (end - DATA(header))*sizeof(Bag));
dst += end - DATA(header);
}
else {
Expand Down Expand Up @@ -1925,6 +1932,11 @@ UInt CollectBags (

/* * * * * * * * * * * * * * * check phase * * * * * * * * * * * * * * */

// Check if this allocation would even fit into memory
if (SIZE_MAX - (size_t)(sizeof(BagHeader) + size) < (size_t)AllocBags) {
return 0;
}

// store in 'stopBags' where this allocation takes us
Bag * stopBags = AllocBags + WORDS_BAG(sizeof(BagHeader)+size);

Expand Down Expand Up @@ -2024,7 +2036,7 @@ UInt CollectBags (
i = SpaceBetweenPointers(EndBags,stopBags)/7 - (SizeMptrsArea-NrLiveBags);

/* move the bags area */
memmove(OldBags+i, OldBags, SizeAllBagsArea*sizeof(*OldBags));
SyMemmove(OldBags+i, OldBags, SizeAllBagsArea*sizeof(*OldBags));

/* update the masterpointers */
for ( p = MptrBags; p < OldBags; p++ ) {
Expand Down Expand Up @@ -2057,7 +2069,7 @@ UInt CollectBags (
/* information after the check phase */
if ( MsgsFuncBags )
(*MsgsFuncBags)( FullBags, 5,
SpaceBetweenPointers(EndBags, stopBags)/(1024/sizeof(Bag)));
(EndBags - stopBags)/(1024/sizeof(Bag)));
if ( MsgsFuncBags )
(*MsgsFuncBags)( FullBags, 6,
SizeWorkspace/(1024/sizeof(Bag)));
Expand Down
7 changes: 4 additions & 3 deletions src/listfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <src/pperm.h>
#include <src/set.h>
#include <src/stringobj.h>
#include <src/sysfiles.h>
#include <src/trans.h>

#ifdef HPCGAP
Expand Down Expand Up @@ -100,7 +101,7 @@ void AddPlist3 (
if (pos <= len) {
GROW_PLIST(list, len+1);
SET_LEN_PLIST(list, len+1);
memmove(ADDR_OBJ(list) + pos+1,
SyMemmove(ADDR_OBJ(list) + pos+1,
CONST_ADDR_OBJ(list) + pos,
(size_t)(sizeof(Obj)*(len - pos + 1)));
}
Expand Down Expand Up @@ -288,7 +289,7 @@ Obj FuncAPPEND_LIST_INTR (
GROW_STRING(list1, len1 + len2);
SET_LEN_STRING(list1, len1 + len2);
CLEAR_FILTS_LIST(list1);
memmove( CHARS_STRING(list1) + len1, CHARS_STRING(list2), len2 + 1);
SyMemmove( CHARS_STRING(list1) + len1, CHARS_STRING(list2), len2 + 1);
/* ensure trailing zero */
*(CHARS_STRING(list1) + len1 + len2) = 0;
return (Obj) 0;
Expand Down Expand Up @@ -1623,7 +1624,7 @@ Obj FuncCOPY_LIST_ENTRIES( Obj self, Obj args )
GROW_PLIST(srclist, srcmax);
if (srcinc == 1 && dstinc == 1)
{
memmove(ADDR_OBJ(dstlist) + dststart,
SyMemmove(ADDR_OBJ(dstlist) + dststart,
CONST_ADDR_OBJ(srclist) + srcstart,
(size_t) number*sizeof(Obj));
}
Expand Down
5 changes: 3 additions & 2 deletions src/opers.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <src/records.h>
#include <src/saveload.h>
#include <src/stringobj.h>
#include <src/sysfiles.h>

#ifdef HPCGAP
#include <src/hpc/aobjects.h>
Expand Down Expand Up @@ -1991,7 +1992,7 @@ static ALWAYS_INLINE Obj GetMethodCached(Obj oper,
Obj buf[cacheEntrySize];
memcpy(buf, cache + i,
sizeof(Obj) * cacheEntrySize);
memmove(cache + target + cacheEntrySize,
SyMemmove(cache + target + cacheEntrySize,
cache + target,
sizeof(Obj) * (i - target));
memcpy(cache + target, buf,
Expand All @@ -2017,7 +2018,7 @@ CacheMethod(Obj oper, UInt n, Int prec, Obj * ids, Obj method)
UInt cacheEntrySize = n + 2;
Bag cacheBag = GET_METHOD_CACHE(oper, n);
Obj * cache = 1 + prec * cacheEntrySize + ADDR_OBJ(cacheBag);
memmove(cache + cacheEntrySize, cache,
SyMemmove(cache + cacheEntrySize, cache,
sizeof(Obj) * (CACHE_SIZE - prec - 1) * cacheEntrySize);
cache[0] = method;
cache[1] = INTOBJ_INT(prec);
Expand Down
5 changes: 3 additions & 2 deletions src/permutat.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <src/range.h>
#include <src/records.h>
#include <src/saveload.h>
#include <src/sysfiles.h>
#include <src/trans.h>

/****************************************************************************
Expand Down Expand Up @@ -4443,12 +4444,12 @@ Obj FuncSCR_SIFT_HELPER(Obj self, Obj S, Obj g, Obj n)
/* Copy g into the buffer */
if (IS_PERM2(g) && useP2) {
UInt2 * ptR = ADDR_PERM2(result);
memmove(ptR, CONST_ADDR_PERM2(g),2*dg);
SyMemmove(ptR, CONST_ADDR_PERM2(g),2*dg);
for ( i = dg; i < nn; i++)
ptR[i] = (UInt2)i;
} else if (IS_PERM4(g) && !useP2) {
UInt4 *ptR = ADDR_PERM4(result);
memmove(ptR,CONST_ADDR_PERM4(g),4*dg);
SyMemmove(ptR,CONST_ADDR_PERM4(g),4*dg);
for ( i = dg; i <nn; i++)
ptR[i] = (UInt4)i;
} else if (IS_PERM2(g) && !useP2) {
Expand Down
3 changes: 2 additions & 1 deletion src/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <src/listfunc.h>
#include <src/lists.h>
#include <src/plist.h>
#include <src/sysfiles.h>


/****************************************************************************
Expand Down Expand Up @@ -449,7 +450,7 @@ Obj FuncADD_SET (
{
Obj *ptr;
ptr = PTR_BAG(set);
memmove(ptr + pos+1, ptr+pos, sizeof(Obj)*(len+1-pos));
SyMemmove(ptr + pos+1, ptr+pos, sizeof(Obj)*(len+1-pos));
}
SET_ELM_PLIST( set, pos, obj );
CHANGED_BAG( set );
Expand Down
61 changes: 61 additions & 0 deletions src/sysfiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -3598,6 +3598,67 @@ Obj SyReadStringFid(Int fid) {
#endif


#ifdef USE_CUSTOM_MEMMOVE
// The memmove in glibc on 32-bit SSE2 systems, contained in
// __memmove_sse2_unaligned, is buggy in at least versions
// 2.21 - 2.26 when crossing the 2GB boundary, so GAP must
// include its own simple memmove implementation.
void * SyMemmove(void * dst, const void * src, UInt size)
{
char * d = dst;
const char * s = src;

if ((d == s) || (size == 0))
return dst;

if (d + size < s || d > s + size) {
memcpy(dst, src, size);
}
else if (d > s) {
d = d + (size - 1);
s = s + (size - 1);

// This is about 4x slower than glibc, but
// is simple and also complicated enough that
// gcc or clang seem unable to "optimise" it back
// into a call to memmove.

// Do size 4 jumps
while (size > 4) {
*d = *s;
*(d - 1) = *(s - 1);
*(d - 2) = *(s - 2);
*(d - 3) = *(s - 3);
d -= 4;
s -= 4;
size -= 4;
}
// Finish
while (size > 0) {
*d-- = *s--;
size--;
}
}
else {
while (size > 4) {
*d = *s;
*(d + 1) = *(s + 1);
*(d + 2) = *(s + 2);
*(d + 3) = *(s + 3);
d += 4;
s += 4;
size -= 4;
}
// Finish
while (size > 0) {
*d++ = *s++;
size--;
}
}
return dst;
}
#endif


/****************************************************************************
**
Expand Down
13 changes: 13 additions & 0 deletions src/sysfiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ extern Char * SyTmpname ( void );
*/
extern Char * SyTmpdir ( const Char * hint );


/****************************************************************************
**
*F void getwindowsize( void ) . probe the OS for the window size and
Expand Down Expand Up @@ -612,6 +613,18 @@ extern Obj SyReadStringFid(Int fid);
extern Obj SyReadStringFile(Int fid);
extern Obj SyReadStringFileGeneric(Int fid);


#if !defined(SYS_IS_64_BIT) && defined(__GNU_LIBRARY__)
#define USE_CUSTOM_MEMMOVE 1
#endif

#ifdef USE_CUSTOM_MEMMOVE
// Internal implementation of memmove, to avoid issues with glibc
void * SyMemmove(void * dst, const void * src, size_t size);
#else
#define SyMemmove memmove
#endif

/****************************************************************************
**
*F * * * * * * * * * * * * * initialize package * * * * * * * * * * * * * * *
Expand Down
2 changes: 1 addition & 1 deletion src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@ void InitSystem (
const UInt pathlen = strlen(SyGapRootPaths[i]);
if (SyGapRootPaths[i][0] == '~' &&
userhomelen + pathlen < sizeof(SyGapRootPaths[i])) {
memmove(SyGapRootPaths[i] + userhomelen,
SyMemmove(SyGapRootPaths[i] + userhomelen,
/* don't copy the ~ but the trailing '\0' */
SyGapRootPaths[i] + 1, pathlen);
memcpy(SyGapRootPaths[i], userhome, userhomelen);
Expand Down