Welcome, Guest. Please login or register.

Author Topic: Counting on AllocVec's size memo..  (Read 5280 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Counting on AllocVec's size memo..
« on: December 22, 2006, 01:58:08 PM »
I was thinking on ways to keep track of a structs size and reminded that AllocVec keeps the size of allocated data in the pointer it returns - 1 IIRC. Probably a bit of a hack couting on it or it's not gonna change in the foresenable future, what do you guys think ?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show only replies by koaftder
    • http://koft.net
Re: Counting on AllocVec's size memo..
« Reply #1 on: December 22, 2006, 02:12:11 PM »
OS4 broke it.

AmigaOS 2.0 to 3.9 and MorphOS have the size at -4.

[EDIT] Oh dear, this really is me, Piru.

Wayne: What the heck is going on? :-) [/EDIT]
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Counting on AllocVec's size memo..
« Reply #2 on: December 22, 2006, 02:15:31 PM »
- 4 LONGs or BYTES ?

Where is the OS4 one ? Does it have some function to get the size (2 maintain future compatibility) ?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show only replies by koaftder
    • http://koft.net
Re: Counting on AllocVec's size memo..
« Reply #3 on: December 22, 2006, 02:24:17 PM »
Dang it, too bad Piru noticed his session was screwed up, I almost got to look like I know what i'm talking about for once (:
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show only replies by koaftder
    • http://koft.net
Re: Counting on AllocVec's size memo..
« Reply #4 on: December 22, 2006, 02:26:50 PM »
bytes.

(and again this was Piru)
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Counting on AllocVec's size memo..
« Reply #5 on: December 22, 2006, 02:27:27 PM »
@Piru
Screw it, post as Koafder anyway and put a comment in the beggining if you want or something...

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Counting on AllocVec's size memo..
« Reply #6 on: December 22, 2006, 02:28:03 PM »
Just for now of course...
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline tboeckel

  • Newbie
  • *
  • Join Date: Jun 2006
  • Posts: 9
    • Show only replies by tboeckel
Re: Counting on AllocVec's size memo..
« Reply #7 on: December 22, 2006, 02:50:11 PM »
OS4 didn't break it!!

Please, *NEVER* rely on such "facts" that hold for OS2/3 or MOS just by pure luck.

It was never documented that AllocVec() stores the size of the allocation at a specific position or at all.

OS4's memory system doesn't need to store the allocation size at all, because its internal structures already cover the size information. Hence it is illegal to peek at ptr-4.

If your application *really* needs to know the size of an arbitrary allocation, then write your own wrapper functions to AllocMem()/FreeMem() and store the allocation size yourself. That will save you *very* much trouble in the future.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Counting on AllocVec's size memo..
« Reply #8 on: December 22, 2006, 03:13:17 PM »
OS4 developers choose to change this with no good justification. There is no urgent need to REMOVE the size from the ptr-4, it could well have been kept there for backwards compatibility alone. OS4 developers did not do this, and as a result some existing applications ceased to work.

I call that breaking.

Naturally applications relying on this are broken aswell, but there was no need to reduce the compatiblity in this way.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Counting on AllocVec's size memo..
« Reply #9 on: December 22, 2006, 03:19:32 PM »
Hi. It seems that I won't be using this as allocated size is obviously different from object size a whole bunch of times, as pointed by Rogue (I asked if there was some OS4 way of knowing the size in AW.net).

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Counting on AllocVec's size memo..
« Reply #10 on: December 22, 2006, 04:02:46 PM »
The only thing one should ever depend on is that the memory allocation system is a black box about which you should never assume anything other than what is officially documented.

This is true for every operating system. Making assumptions of this kind is bad development. When you allocate storage for x bytes, you are only guarenteed that you have at least that much; it could be more.
int p; // A
 

Offline tboeckel

  • Newbie
  • *
  • Join Date: Jun 2006
  • Posts: 9
    • Show only replies by tboeckel
Re: Counting on AllocVec's size memo..
« Reply #11 on: December 22, 2006, 04:36:46 PM »
How can you break something that was never documented? Peeking unallocated memory region was very common practice on 68k system, but will surely crash with OS4. So, does OS4 break this, too?

Ok, the location of AllocVec()'s size storage has become something like a "fact". But that's the point: it never really was a fact.

I think the Frieden brother told more than once, that the size at ptr-4 was removed for various good reasons:
1. it was never documented to be there
2. it was no longer necessary with the new memory system
3. PPC cache lines have different alignment than 68k cache lines and an additional offset of 4 would slow things down
4. so far it did only break one major application (GoldED) and that has been fixed already
5. OS4 tries to enforce certain programming rules and punishes hacking.

The advantages of removing the size information by far outnumbered the disadvantages. And that's why it has been done. Compatibility is a big thing to maintain, but keeping undocumented features, which lead to hacking, is not always very wise.

If MOS could keep up the compatibility to OS3.x's AllocVec() without sacrificing any advantages then its perfect legal to keep this. But relying on undocumented features should never be encouraged.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Counting on AllocVec's size memo..
« Reply #12 on: December 22, 2006, 05:03:17 PM »
Quote
How can you break something that was never documented?

By changing the implementation for no other reason than to crash broken applications. That is not very smart.

Quote
1. it was never documented to be there

True. But the implementation is widely known, and abused in way more applications than just GoldEd.

Quote
2. it was no longer necessary with the new memory system

This is no reason to remove the compatibility.

Quote
3. PPC cache lines have different alignment than 68k cache lines and an additional offset of 4 would slow things down

Slow down? Yeah right, that must be massive slowdown there. *cough*

Quote
4. so far it did only break one major application (GoldED) and that has been fixed already

That they know about. That is the thing with these changes: you never know when some obscure application breaks, and you have absolutely no way to test everything. And if some application suddenly crashes with new system update, how can you tell why it happens? You can't. Thus, it is a bad idea to do such modifications for no good reasons.

Quote
5. OS4 tries to enforce certain programming rules and punishes hacking.

Now we're getting closer to the truth. The change here makes no sense at all, all the above reasons are not good enough to remove the size at offset -4. IMO changes for the sake of the change itself, or trying to enforce new rules while breaking old applications is a very bad idea.

Similar excersise was "cleaning up" some dos.library return codes (-1 vs 1 for "true"). Good intentions turned out badly, as it broke varions programs checking for "traditional" return values.

Quote
The advantages of removing the size information by far outnumbered the disadvantages.

No it doesn't. Removing the size gives no apparent advantages, but instead makes some applications crash. That is NOT good for the end user.

Quote
And that's why it has been done.

I don't even try to understand why it has been done, there doesn't seem to be any sensible reason for it. If those indeed are the reasons for removing it, I fear OS4 won't run many legacy applications in the end. The compatibility will get worse and worse.

Quote
Compatibility is a big thing to maintain, but keeping undocumented features, which lead to hacking, is not always very wise.

So basically you're saying it's better to make old, existing applications to crash to make sure new programmers don't fall into the old traps? Wouldn't be smarter to provide good programming manuals and encourage good programming practices, rather than making the OS itself crash the programs? Remember, this is not helping the end user. Development tools should indeed make such hacks barf, and this is what tools like Mungwall do.

Quote
But relying on undocumented features should never be encouraged.

Agreed. But deliberately breaking compatibility to force the issue isn't the way either. Provide good development tools, and leave the end user out of the loop.
 

Offline tboeckel

  • Newbie
  • *
  • Join Date: Jun 2006
  • Posts: 9
    • Show only replies by tboeckel
Re: Counting on AllocVec's size memo..
« Reply #13 on: December 22, 2006, 05:43:26 PM »
I think you perfectly know how beginners start programming. Take a program with source and see how other people solved certain problems. Sometimes you look into the Autodocs, but most of the time you get inspriration from "outside". At least that's the way I started programming many years ago. And if a beginner learns from such hacking it is sometimes very hard to convince this person from the oposite.

Good programming manuals are always welcome, but the biggest programming manual are available source codes, even if they are not perfect.

I don't say "let all the old applications crash and burn". I say "keep (wrong) compatibility as long as it is bareable, but sacrifice it if that would prevent really necessary improvements". And the new memory system really is a big step forward!

Surely it is not ok to sarcifice compatibility just to make old programs crash, but during all the beta testing of OS4 GoldED really was the *only* bad application. I think the Friedens would have decided the other way if more than this single program had been affected. But that was not the case.
 

Offline itix

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 2380
    • Show only replies by itix
Re: Counting on AllocVec's size memo..
« Reply #14 on: December 22, 2006, 05:44:45 PM »
Quote

5. OS4 tries to enforce certain programming rules and punishes hacking.


Still many OS4 programs depend on undocumented side effects and features ;^) It is nature of programming. Coders always did and will use all means to reach goal with minimal effort. If it is possible to exploit an undocumented feature there is always someone doing so.
My Amigas: A500, Mac Mini and PowerBook