Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
168 user(s) are online (142 user(s) are browsing Forums)

Members: 1
Guests: 167

hlt, more...

Support us!

Headlines

 
  Register To Post  

(1) 2 »
how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@All

In the old SASC/68k there was attribute __aligned. Also the same attribute present on the old gcc2.x, but start from 3.x if i see well it disappear.

Now, in different part of that old code i have __aligned attribute in use. Before, to just make it builds at all, i do some simple define like this:

Quote:

#if defined(__amigaos3__) || defined(__amigaos4__) || defined(__MORPHOS__)
#undef __aligned
#define __aligned __attribute__((aligned(4)))
#endif


But seems that not good enough to cover and 68k bins, and ppc bins. In some parts of code seems such define is ok, in another is not.

For example, let's take one simple source where it uses 2 times:

Quote:

struct FileInfoBlock __aligned fib;
struct AnchorPath __aligned anchor;


I build 68k version of code with that define, and when i access to the function which use that construction by running binary on os4, i have a window:

A Process called a DOS function with a non longword-aligned Anchor.

Function: "MatchFirst()"

So i need something (better if it will be just a simple define like my one), which will works everywhere and in all the cases.

No only that windows problem, but sometime that "bad" alignment cause crashes when i compile such a code as ppc one. Sometime its all ok, sometime not. But its for sure sucky to be in hope, just need to do everything right.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
Currently for those dos structures itix make a macro like this:

/* Long word alignement (mainly used to get
 * FIB or DISK_INFO as auto variables)
 */
#define D_S(type,name) char a_##name[sizeof(type)+3]; \
                       
type *name = (type *)((LONG)(a_##name+3) & ~3);


So i just in code do:

// Build theme list
Att_List *theme_build_list(char *path)
{
    
Att_List *list;

    
// Create list
    
if (list=Att_NewList(LISTF_POOL))
    {
        
long err;
        
char buf[280];
        
D_S(struct AnchorPath,anchor// was with __alignment: struct AnchorPath __aligned anchor;

        
anchor->ap_BreakBits=0// was with __alignment: anchor.ap_BreakBits=0;
        
anchor->ap_Flags=APF_DOWILD// was with __alignment: anchor.ap_Flags=APF_DOWILD;
        
anchor->ap_Strlen=0//was with __alignment: anchor.ap_Strlen=0;
        
strcpy(buf,path);
        
AddPart(buf,"#?.theme",280);
        
err=MatchFirst(buf,anchor); // was with __alignment: err=MatchFirst(buf,&anchor);
        
while (!err)
        {
            
char *ptr;
            if (
anchor->ap_Info.fib_DirEntryType>0// was with __alignment: if (anchor.ap_Info.fib_DirEntryType>0)
            
{
                
anchor->ap_Flags&=~APF_DODIR//was with __alignment: anchor.ap_Flags&=~APF_DODIR;
                
continue;
            }
            
anchor->ap_Info.fib_FileName[strlen(anchor->ap_Info.fib_FileName)-6]=0// was with __alignment: anchor.ap_Info.fib_FileName[strlen(anchor.ap_Info.fib_FileName)-6]=0;
            
for (ptr=anchor->ap_Info.fib_FileName;*ptr;ptr++) // was with alignment: for (ptr=anchor.ap_Info.fib_FileName;*ptr;ptr++)
                
if (*ptr=='_') *ptr=' ';
            
Att_NewNode(list,anchor->ap_Info.fib_FileName,0,ADDNODE_SORT); // was with alignment: Att_NewNode(list,anchor.ap_Info.fib_FileName,0,ADDNODE_SORT);
            
err=MatchNext(anchor); // was with __ailgnment: err=MatchNext(&anchor);
        
}
        
MatchEnd(anchor); // was with __alignment: MatchEnd(&anchor);
    
}

    return list;
}


That deals with alignment in that case, but if for example i have:

char __aligned oldname[34]; 

and 
then it used like:

// Get old filename
strcpy(oldname+1,FilePart(name));
oldname[0]=strlen(oldname+1);

MKBADDR(oldname),



or when in some struct i have:

Quote:

unsigned char __aligned areabuf[AREAVERTEX*5];


and then use it from code like:

Quote:

InitArea(&data->areainfo,data->areabuf,AREAVERTEX);



or when i have it like:

Quote:

__aligned char buffer[NODENAMELEN + 2 + 2 + 1];


and then in code use it like:

lsprintfbuffer"%4ld\a\x5%s\a\xa%s"ft->type.priorityft->type.idft->type.name );

finder_Att_NewNodedata->listview_listbufferft->type.priorityinstalled ADDNODE_INSTALLED )


or when it:

USHORT __aligned
    oldicon_imagedata
[1]={0xffff};

struct Image
   oldicon_image
={0,0,1,1,1,oldicon_imagedata,1,0,0};


(seems collect all possible places where it uses)

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Not too shy to talk
Not too shy to talk


See User information

FileInfoBlock and AnchorPath should be allocated with AllocDosObject. Declaring them as auto variables is deprecated already since OS 1.2.

An example for correct usage of MatchFirst / MatchNext is in the autodocs.

Using __aligned with a char or UBYTE is nonsense IMHO. Bytes are always aligned to byte boundary. Only multi-byte datatypes need special alignment.


Go to top
Re: how to deal with old __aligned attribute ?
Just can't stay away
Just can't stay away


See User information
Quote:

Using __aligned with a char or UBYTE is nonsense IMHO. Bytes are always aligned to byte boundary. Only multi-byte datatypes need special alignment.


If you look at the code it is used for a BSTR so it needs to be 32-bit aligned. BCPL pointers always have to be 32-bit aligned no matter what they are pointing to.

That being said a better solution would probably be to just use AllocMem() or AllocVec() to allocate the buffer.

Go to top
Re: how to deal with old __aligned attribute ?
Just can't stay away
Just can't stay away


See User information
Quote:

USHORT __aligned
oldicon_imagedata[1]={0xffff};

struct Image
oldicon_image={0,0,1,1,1,oldicon_imagedata,1,0,0};


I don't see why the __aligned keyword is used here. The only alignment needed for the imagedata should be 16-bit alignment and the USHORT type should ensure this.

What OTOH could be needed is something like __chip (supported in vbcc, don't know about gcc) to ensure that it's placed in chip memory on Classic machines. If there's no such attribute for gcc you may have to use AllocMem() with MEMF_CHIP.

Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@all
what to do if that __aligned crap in the structure, and i want to use that macro ? For example i have:

struct locals
{
struct FileInfoBlock __aligned fib;

struct DiskObject    *obj;
ULONG            iconflags;
short            opus_xopus_y;
int            putset;
int            isicon;
BPTR            lock;
char            newpath[256];
int            newxoffnewyoff;
ULONG            flags;
};


I just put our macro inside of structure like this:

struct locals
{

D_S(struct FileInfoBlock,fib)

struct DiskObject    *obj;
ULONG            iconflags;
short            opus_xopus_y;
int            putset;
int            isicon;
BPTR            lock;
char            newpath[256];
int            newxoffnewyoff;
ULONG            flags;
};


And it fails with words "error: expected ':', ',', ';', '}' or '__attribute__' before '=' token"

@Salas00
Quote:

I don't see why the __aligned keyword is used here.

Maybe on SASC USHORT with/without __aligned can be different ? But if on gcc is not, then i will just remove it from there.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Just can't stay away
Just can't stay away


See User information
@kas1e

Quote:

Maybe on SASC USHORT with/without __aligned can be different ? But if on gcc is not, then i will just remove it from there.


If it odd-aligns USHORT then it will create broken code on 68000 CPUs (the 68000 will crash if you attempt to access a word or long word at an odd memory address) and slower code on 68020+ CPUs.

Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
Try applying the macro to the entire struct not just the emebedded fib.

A proper solution would be to change the structure to a pointer to the FileInfoBlock and allocate it with AllocDosObject(), that will require a lot of rewriting though, depending on how much this is used.

Perhaps we start to see why previous attempts to port DOpus got bogged down, if this kind of hackery is used a lot in the source.




Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@salas00
Quote:

If it odd-aligns USHORT then it will create broken code on 68000 CPUs (the 68000 will crash if you attempt to access a word or long word at an odd memory address) and slower code on 68020+ CPUs.


So on what should i change that to have all be all right:

USHORT __aligned 
oldicon_imagedata
[1]={0xffff}; 

struct Image 
oldicon_image
={0,0,1,1,1,oldicon_imagedata,1,0,0};


?

@Andy
Quote:

Perhaps we start to see why previous attempts to port DOpus got bogged down, if this kind of hackery is used a lot in the source.

There is not much places where __aligned is used. Mostly that what i put there is from "modules" (some of them done without). In the library and main binary, __aligned used only for FIB/Anchor and InfoData dos variables, where is Itix's D_S macro help well. Through, there is one moment in the Library which i for now don't touch and which need fix. In the library/iff.c:

if ((parent=ParentDir(handle->iff_SafeFile)))
                {
                    
long key;
                    
char __aligned oldname[34];
                    
short a;

                    
// Rename file
                    
key=(long)FindTask(0)+(long)parent;

                    
// Get old filename
                    
strcpy(oldname+1,FilePart(name));
                    
oldname[0]=strlen(oldname+1); 

....


It is only one place in library itself where __aligned used like this, and with which i do not know how to deal properly without rewiring code. I just want to make it as much as possible with less changes for first release to avoid our own bugs. I.e. priority#1 : no changes as possible, no rewriting as possible.

There is whole iff.c if you in interest to fix it properly:
https://sourceforge.net/p/dopus5allami ... s/branch_v1/Library/iff.c

So for library only one place like this.

In the main program binary, __aligned is used only for dos variables, so there is no strange places which can't be covered by macro. There is those changes if you in interest to see: https://sourceforge.net/p/dopus5allamigas/code/266/

Only modules in some places use it somehow strange. I.e. not much of hackery, just some suckery. And imho previous attempt to port dopus5 fail just because one person can't deal with it without much motivation and without asking help. I assume they do something, but then stuck on some part, and instead of asking help publicly, just start to jerky on yourselves skills and its all die because of that.

@All
Btw, i assume that:

Quote:

struct FileInfoBlock __aligned fib={0};


And that:

Quote:

struct FileInfoBlock __aligned fib;


Are the same 1:1, except that in case with {0} its clear with NULL, and in another is not ? I mean __aligned there didn't start to be different because of that ?


Edited by kas1e on 2013/5/27 6:23:20
Edited by kas1e on 2013/5/27 6:23:59
Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Just can't stay away
Just can't stay away


See User information
@kas1e

Quote:

So on what should i change that to have all be all right:
USHORT __aligned  
oldicon_imagedata
[1]={0xffff};  

struct Image  
oldicon_image
={0,0,1,1,1,oldicon_imagedata,1,0,0};



Just remove the __aligned keyword. Also you should rather use UWORD than USHORT as they are the same except that USHORT is an obsolete typedef kept only for compatibility with old code.

UWORD  
oldicon_imagedata
[1]={0xffff};  

struct Image  
oldicon_image
={0,0,1,1,1,oldicon_imagedata,1,0,0};

Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@salas00
Thanks ! icon.module now fixed too. There is commit about (i just note in commit that its you fix it to raise your motivation to help us more to make it works right:) ): https://sourceforge.net/p/dopus5allamigas/code/428/

Now, what worry me pretty much is filetype.module, where __alignment uses not only for autovariables, but also for char bufs, like:

Quote:

__aligned char buffer[NODENAMELEN + 2 + 2 + 1];
__aligned char buf[4 * BYTECOUNT + 1];
__aligned char buffer[256 + 1];


there is full source of that module (1 single file):
https://sourceforge.net/p/dopus5allami ... iletype_module/filetype.c

@all
Common, you see i am suck at it, let's finish it all together and dopus5 will be over our hands faster.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Just can't stay away
Just can't stay away


See User information
@kas1e

TBH I don't see why those char buffers need to be aligned except for the id_to_str() function which you may want to change if you want to remain 68000 compatible to:
char *id_to_strLONG idchar *buf )
{
buf[0] = (id >> 24) & 0xff;
buf[1] = (id >> 16) & 0xff;
buf[2] = (id >> 8) & 0xff;
buf[3] = id 0xff;
buf[4] = 0;

return 
buf;
}

Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
I don't know about alignment issue but I can see a huge bug in this fragment

if ((parent=ParentDir(handle->iff_SafeFile))) 
                { 
                    
long key
                    
char __aligned oldname[34]; 
                    
short a

                    
// Rename file 
                    
key=(long)FindTask(0)+(long)parent

                    
// Get old filename 
                    
strcpy(oldname+1,FilePart(name)); 
                    
oldname[0]=strlen(oldname+1);


As soon as that hits a modern file name with a name longer 34 it will down with a bump!

something like this will be more robust ...

if ((parent=ParentDir(handle->iff_SafeFile))) 
                { 
                    
long key
                    
char __aligned oldname[255]; 
                    
short a

                    
// Rename file 
                    
key=(long)FindTask(0)+(long)parent

                    
// Get old filename 
                    
strncpyoldname+1,FilePart(name),sizeof(oldname) -1); 
                    
oldname[0]=strlen(oldname+1);


Note: I made the sizeof the oldname 255 bytes and changed the strcpy() for a strncpy()


Go to top
Re: how to deal with old __aligned attribute ?
Just popping in
Just popping in


See User information
Don't use strncpy(), it doesn't guarantee nul-termination,
you want to use strlcpy() instead.



Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@Andy

Ikka just go another way:

/* Long word alignement (mainly used to get
* FIB or DISK_INFO as auto variables)
*/
#define D_S(type,name) char a_##name[sizeof(type)+3]; \
                       
type *name = (type *)((LONG)(a_##name+3) & ~3);


D_S(charoldname// char __aligned oldname[34];


And no more changes. I assume by this it all should works fine as it's just char now, and needs no more code changes ?

As for more characters, as its in iff.c i assume its just for name of toolbar images mostly, so we can deal with it later and extend it if there will be any needs.

@Salas00
Done, commit #430:
https://sourceforge.net/p/dopus5allamigas/code/430/

I also fix there these dos variables via ikka's macro, so all should be ok now.

@all
I fix for now all latest __alignment issues in the Library too, commit #432: https://sourceforge.net/p/dopus5allamigas/code/432/

Now we didn't have them not in Program, not in Library, and most of them fixed in the modules too, but that what only left now:

1. diskinfo.module:

typedef struct
{
    
NewConfigWindow        new_win;
    
struct Window        *window;
    
ObjectList        *objlist;

    
unsigned short        pens[4];
    
short            pen_alloc[4];

    
struct InfoData        info;

    
char            buffer[1024];
    
char            path[256];
    
char            decimal_sep;
    
char            pad;
    
char            title[128];
    
char            volume[40];

    
struct Library        *maths;
    
struct Library        *maths1;

    
struct TmpRas        tmpras;
    
PLANEPTR        rasbuf;
    
struct AreaInfo        areainfo;

    
unsigned char        __aligned areabuf[AREAVERTEX*5];
diskinfo_data;


So it is struct, and in struct i can't use D_S macro. But all what can i say, is that diskinfo.module works pretty well when i just align it to "4", via : #define __aligned __attribute__((aligned(4))) . Ikka suggest just replace that string on that:

Quote:

unsigned char areabuf[AREAVERTEX*5] __attribute__((aligned (4)));


I assume that one should be ok ?

2. fixicons.module:

There is just usual fib variable used inside of structure, i.e.:

struct locals
{
struct FileInfoBlock __aligned fib;

struct DiskObject    *obj;
ULONG            iconflags;
short            opus_xopus_y;
int            putset;
int            isicon;
BPTR            lock;
char            newpath[256];
int            newxoffnewyoff;
ULONG            flags;
};


So i can't use D_S macro inside as well. Andy suggest to align whole structure, but do not know if it right and how should i do that.

3. In few parts there is lines:

struct FileInfoBlock __aligned fib={0};

in another ones:

struct FileInfoBlock __aligned fib;

I assume thats no differences at all, and i can remove {0} and use the same D_S macro on them ? I end i can do for {0} ones just:

D_S(struct FileInfoBlock, fib)
fib={0};

But not sure if that's need it, as when it initialized its already clean.


Edited by kas1e on 2013/5/28 11:29:11
Edited by kas1e on 2013/5/28 11:38:06
Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@all
Just to make you know we fix it like this:

Quote:

unsigned char __aligned areabuf[AREAVERTEX*5];


on

Quote:

unsigned char areabuf[AREAVERTEX*5] __attribute__((aligned (4)));



In structure:

struct locals
{
struct FileInfoBlock fib __attribute__((aligned (4)));

struct DiskObject    *obj;
ULONG                iconflags;
short                opus_xopus_y;
int                putset;
int                isicon;
BPTR                lock;
char                newpath[256];
int                newxoffnewyoff;
ULONG                flags;
};


And with {0} just clear via memset:

Quote:

D_S(struct FileInfoBlock,fib)
memset(fib, 0, sizeof(*fib));

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
Quote:

Ikka just go another way:

Ikka just fixed the alignment, not the buffer overflow bug I pointed out. You need that larger buffer and to use strlcpy() (as Colin pointed out my usage of strncpy()was wrong), else it will quickly crash on long file names.


I was about to podt that fib = {0} will not compile but I see you got there allready.


Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@Andy
Quote:

Ikka just fixed the alignment, not the buffer overflow bug I pointed out. You need that larger buffer and to use strlcpy() (as Colin pointed out my usage of strncpy()was wrong), else it will quickly crash on long file names.


Yep, he wrote on our forum about:

Quote:

Quote:

Don't use strncpy(), it doesn't guarantee nul-termination,
you want to use strlcpy() instead.

In this case it will be always NULL terminated. strlcpy() and stccpy() are always safer and faster, though.
The buffer could be extended to hold longer filenames up to 108 characters.


Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: how to deal with old __aligned attribute ?
Just popping in
Just popping in


See User information
Actually you need to suppoprt filenames up to 255 bytes.
And DOS paths up to 1000 bytes long.

Please read the first chapter of the latest dos.doc autodoc.


Go to top
Re: how to deal with old __aligned attribute ?
Home away from home
Home away from home


See User information
@colinw
Quote:

Actually you need to suppoprt filenames up to 255 bytes.

Current dopus5 limitation is 108 characters for filenames, so if i will change it only there it will be just strange. Its something for todo like "make files names more than 108 characters works", because it will needs changes in more than one place.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top

  Register To Post
(1) 2 »

 




Currently Active Users Viewing This Thread: 4 ( 0 members and 4 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project