Skip to search.
forth200x

Group Information

  • Members: 67
  • Category: Forth
  • Founded: Nov 21, 2004
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Messages

  Messages Help
Advanced
SIZE RfD   Message List  
Reply Message #448 of 899 |
Here is a very simple RfD --- I'm not pushing for quotations or any other such thing that realistically will never be accepted in Forth-200x. This one just fixes one of the more glaring failures of ANS-Forth. For the most part, I have lost interest in Forth-200x. This RfD will fix a problem that is causing my code to be horribly cumbersome and hard-to-read though, and it is a super-simple solution, so I'll post it.

Author
-------

Hugh Aguilar


Problem
---------

The program, given an address returned by ALLOCATE or RESIZE, has no way of finding out how big that chunk of memory is.

Current Practice
-----------------

When the programmer calls ALLOCATE or RESIZE, he keeps track of the size somewhere in his own program.

As an example, In my LIST.4TH software (http://www.forth.org/novice.html), each node has a .NODE-SIZE field that keeps track of the node's size. This is necessary because the lists often contain a variety of data types, all derived from LIST, but all different sizes.

My .NODE-SIZE field is a waste of memory. This information is held internally (FREE and RESIZE are able to obtain it), so there is no need for me to hold onto it as well.

The worst problem though, is that constructors are more complicated. Each constructor calls the parent constructor first, and then does its own initialization of fields. The problem though, is that the .NODE-SIZE field ends up getting initialized repeatedly in the chain of constructors, and smaller every time --- so it is wrong. The solution is to call INIT-LIST with the correct size after having called the parent constructor. This is fixes the .NODE-SIZE field. It is a waste of time to do this though. This is also confusing because the programmers don't do this in other languages such as C++. It seems unnecessary and confusing to them. Also, if they forget, they don't find out about the problem until and if they use CLONE-NODE at some time in the future. There is a huge possibility here for a bug that most programmers would find difficult to track down. I've actually been bitten by this bug so many times now that I'm pretty alert to it, but most programmers would not expect this and would be confused.

Solution
--------

The solution is to provide a word SIZE that returns the size of a heap allocation, given the address of that heap allocation (the address returned by ALLOCATE or FREE).

Proposal
---------

Introduce a new word:  SIZE ( adr -- size )

This would be in the optional Memory-Allocation word set (chapter 14).




Fri Jan 29, 2010 10:41 pm

hughaguilar96
Offline Offline
Send Email Send Email

Message #448 of 899 |
Expand Messages Author Sort by Date

Here is a very simple RfD --- I'm not pushing for quotations or any other such thing that realistically will never be accepted in Forth-200x. This one just...
Hugh Aguilar
hughaguilar96 Offline Send Email
Jan 29, 2010
10:41 pm

On Friday 29 January 2010, Hugh Aguilar wrote: I wouldn't call it SIZE. It's such a common word, I bet it is taken on many Forths. (Not mine BTW). HB -- ...
Hans Bezemer
hansoft@... Send Email
Jan 29, 2010
11:21 pm

That is a good point. Here is an alternative suggestion: ALLOCATION I just thought of that off the top of my head, but I think it might be an improvement as it...
Hugh Aguilar
hughaguilar96 Offline Send Email
Jan 29, 2010
11:45 pm

... Hm, not in mine either, and not in VFX - some classes in MINOS have a SIZE method, but that's embedded in the class scope, and MINOS class words certainly...
Bernd Paysan
berndpaysan Offline Send Email
Jan 30, 2010
4:42 pm

... useful, but the trick is to specify it correctly. I.e. SIZE or whatever you call it will give you the *actual* size of the memory block, while e.g....
BruceMcF
brucemcf... Offline Send Email
Jan 30, 2010
5:39 pm

... ALLOCATED is fine. I don't like hyphenated words. Hans -- ================ "First make it work, then improve it." Visit our website! http://come.to/hansoft...
Hans Bezemer
hansoft@... Send Email
Jan 30, 2010
5:53 pm

... I'm fine with ALLOCATED, too, and it leaves SIZE open for all other kinds of sizes the program wants to keep. -- Bernd Paysan "If you want it done right,...
Bernd Paysan
berndpaysan Offline Send Email
Jan 30, 2010
6:28 pm

... A specification of what the word does would be a good idea. As Bernd mentions, the actual allocated size is often rounded up from the requested size, and...
Anton Ertl
anton@... Send Email
Jan 30, 2010
5:21 pm

________________________________ From: Anton Ertl <anton@...> To: forth200x@yahoogroups.com Sent: Sat, January 30, 2010 10:21:40 AM ...
Hugh Aguilar
hughaguilar96 Offline Send Email
Jan 30, 2010
8:05 pm

... Hugh, libc is just a library. Even VFX Forth uses the C library for some things like ALLOCATE, despite it's otherwise entirely written in Forth. -- Bernd...
Bernd Paysan
berndpaysan Offline Send Email
Jan 30, 2010
8:21 pm

... only so long as the method that ALLOCATE and RESIZE use for rounding is clearly documented (most likely, it is just ALIGNED). The reason this is necessary...
BruceMcF
brucemcf... Offline Send Email
Jan 31, 2010
12:02 am

ALLOCATE and RESIZE can round up to 16-byte paragraphs if they want to; this doesn't matter so long as it is documented. I recommend the addition of another...
Hugh Aguilar
hughaguilar96 Offline Send Email
Jan 31, 2010
1:38 am

... Please quote properly, i.e., a ">" per line, not just per paragraph. And again, please keep to line lengths of around 70 characters for the text you write....
Anton Ertl
anton@... Send Email
Jan 31, 2010
11:50 am

Anton Ertl <anton@...>, Sun, Jan 31, 2010 6:50 am, Re: [forth200x] SIZE RfD ... Testable assumptions *may* allow for more portable code,...
BruceMcF
brucemcf... Offline Send Email
Feb 1, 2010
1:31 am

... Just to clarify, were you looking for something like the following behavior? (Ignore the names and implementation optimization, it is just example code for...
Doug Hoffman
t0004dh Offline Send Email
Jan 31, 2010
11:37 am

Hugh said ... At first thought, I was attracted to this proposal when ALLOCATED replaces SIZE. In our embedded heap, implementing ALLOCATED is trivial and...
Stephen Pelc
sfprem Offline Send Email
Feb 1, 2010
1:15 am

... Note that the size returned by ALLOCATED might be larger than the data you store, so if you want the len for, e.g., TYPE, you won't get it from ALLOCATED. ...
Anton Ertl
anton@... Send Email
Feb 1, 2010
9:30 pm

Of course it suffers from "lack of common practice" --- there is no way to write this in ANS-Forth. If you have the code for the heap, then it is trivial to...
Hugh Aguilar
hughaguilar96 Offline Send Email
Feb 2, 2010
1:02 am

Actually, there is some common practice, and it is possible to do this in ANS Forth. You simply need to redefine ALLOCATE to reserve 1 extra cell and save the...
Dennis Ruffer
DaRuffer Offline Send Email
Feb 2, 2010
2:39 am

Hugh Aguilar <hughaguilar96@...>, Mon, Feb 1, 2010 8:02 pm ... way to ... As already noted, if not available, its straightforward to store the ...
BruceMcF
brucemcf... Offline Send Email
Feb 2, 2010
6:04 am

... Well, yes, but not as straightforward as you put it. Most modern platforms will normally return pointers aligned to whatever the SIMD coprocessor feels...
Jorge Acereda
jacereda@... Send Email
Feb 3, 2010
8:20 pm

Jorge Acereda <jacereda@...>, Wed, Feb 3, 2010 3:20 pm ... If using the address returned by that modification applied to ALLOCATE / RESIZE / FREE causes...
BruceMcF
brucemcf... Offline Send Email
Feb 3, 2010
10:00 pm

... I'm not sure I understood your mail. If you are using a platform where malloc() returns addresses aligned to 16 bytes boundary and your ALLOCATE puts the...
Jorge Acereda
jacereda@... Send Email
Feb 4, 2010
1:07 am

Jorge Acereda <jacereda@...>, Wed, Feb 3, 2010 8:07 pm ... when ... Yes, if there is code that relies on a specific feature of a specific ALLOCATE beyond...
BruceMcF
brucemcf... Offline Send Email
Feb 4, 2010
5:14 am

I don't support this feature and would likely not implement it. Other than the nice discussion of what to name it, I see no compelling reason for its actual...
Leon Wagner
leon_wagner Offline Send Email
Feb 2, 2010
4:03 am

... Not - as official interface - with the traditional malloc and free, but it should be possible to obtain the size of an object allocated with new or delete...
Bernd Paysan
berndpaysan Offline Send Email
Feb 2, 2010
9:03 am

After the recent flurry, it seems that we need two additional words: ALLOCATION (what was requested) ALLOCATED (what you got) For example, if I request 17...
Stephen Pelc
sfprem Offline Send Email
Feb 2, 2010
8:21 pm

________________________________ From: Stephen Pelc <stephen@...> To: forth200x@yahoogroups.com Sent: Tue, February 2, 2010 12:21:44 PM Subject:...
Hugh Aguilar
hughaguilar96 Offline Send Email
Feb 2, 2010
9:10 pm

... Yes, that's where I'm using my version of ALLOCATED - in code that clones memory blocks (it's actually only used in the 3D turtle, and I probably can work...
Bernd Paysan
berndpaysan Offline Send Email
Feb 3, 2010
9:00 am
First  | < Prev  |  Last 
Advanced

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help