AllocVecTags() questions

This forum is for general developer support questions.
xenic
Posts: 1185
Joined: Sun Jun 19, 2011 12:06 am

AllocVecTags() questions

Post by xenic »

I'm trying to get rid of obsolete functions in a program and the default type of memory allocated is unclear to me.

The AllocVecTags() autodoc states: "In it's simplest form, AllocVecTagList will give you a private memory block of at least size bytes."

The MEMF_PRIVATE tag description states: "Allocate from the private heap. This memory will not be visible to any other address space."

Those statements seem to indicate that the default allocation (e.g. AllocVecTags(size, TAG_END) ) would be MEMF_PRIVATE type but I'm not sure if "a private memory block" or "the private heap" refer to the same thing. What is the default memory type allocation?
AmigaOne X1000 with 2GB memory - OS4.1 FE
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1479
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: AllocVecTags() questions

Post by tonyw »

AFAIR that is correct: by default, you will get MEMF_PRIVATE memory that will not be visible to any other process, so you can't use it in I/O or library calls.
If you want to pass pointers to it (to other processes), you will have to specify MEMF_SHARED.
cheers
tony
xenic
Posts: 1185
Joined: Sun Jun 19, 2011 12:06 am

Re: AllocVecTags() questions

Post by xenic »

tonyw wrote:AFAIR that is correct: by default, you will get MEMF_PRIVATE memory that will not be visible to any other process, so you can't use it in I/O or library calls.
If you want to pass pointers to it (to other processes), you will have to specify MEMF_SHARED.
If that's the case then there are a lot of bad examples in the Documentation WIKI. For example:

Code: Select all

This is on the Documentation WIKI "Clipboard Device" page:

    if (buf->mem = IExec->AllocVecTags(length+1, TAG_END))
        {
        buf->size = length+1L;

        ior->io_Command = CMD_READ;
        ior->io_Data    = (STRPTR)buf->mem;

This is on the Documentation WIKI "Console Device" page:

    if (keymap = (struct KeyMap *)
      AllocVecTags(sizeof(struct KeyMap), AVT_ClearWithValue, 0, TAG_END))
    {
        /* device opened, send CD_ASKKEYMAP command to it */
        ConsoleIO->io_Length  = sizeof(struct KeyMap);
        ConsoleIO->io_Data    = (APTR)keymap;      /* where to put it */
        ConsoleIO->io_Command = CD_ASKKEYMAP;
        IExec->DoIO((struct IORequest *)ConsoleIO))

This is on the Documentation WIKI "Timer Device" page:

    struct TimeRequest *TimerIO = IExec->AllocVecTags(sizeof(struct TimeRequest),
        AVT_ClearWithValue, 0,
        TAG_END);
I didn't even bother to check library calls. Amiga programs are so full of device I/O and library calls that it would make sense to me to make all AllocVecTags() memory allocations with MEMF_PUBLIC.
AmigaOne X1000 with 2GB memory - OS4.1 FE
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1479
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: AllocVecTags() questions

Post by tonyw »

There has been so much confusion and misunderstanding about this point over the years that some time ago, Thomas produced a definitive description of what you'll get and what are the defaults and requirements. I can't remember where it is or was, but it was a public release - possibly in the Hyperion blog?

Personally, I try not to depend on defaults. I like to spell it all out in grisly detail.
cheers
tony
chris
Posts: 562
Joined: Sat Jun 18, 2011 11:05 am
Contact:

Re: AllocVecTags() questions

Post by chris »

xenic wrote: I didn't even bother to check library calls. Amiga programs are so full of device I/O and library calls that it would make sense to me to make all AllocVecTags() memory allocations with MEMF_PUBLIC.
I thought the default was MEMF_ANY (MEMF_SHARED) and if you wanted MEMF_PRIVATE you had to ask for it. But that was AllocVec; it could be different with AllocVecTags.

As there's no memory protection it's impossible to tell if you've asked for the wrong type of memory.
User avatar
broadblues
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 600
Joined: Sat Jun 18, 2011 2:40 am
Location: Portsmouth, UK
Contact:

Re: AllocVecTags() questions

Post by broadblues »

it would make sense to me to make all AllocVecTags() memory allocations with MEMF_PUBLIC.
Really, don't do that, whilst memory protection isn't activated, then an eroneuos MEMF_PRIVATE verses MEMF_SHARED may not make much odds (now at least) , MEMF_PUBLIC is locked so it can't be swapped, so is suitable for interupts etc but not main usage.

Libraries in general run on the callers context so don't need to be MEMF_SHARED, those that are special probably ought to have an allocator function, such as AlloSysObject() and AllocDosObject()
xenic
Posts: 1185
Joined: Sun Jun 19, 2011 12:06 am

Re: AllocVecTags() questions

Post by xenic »

tonyw wrote:There has been so much confusion and misunderstanding about this point over the years that some time ago, Thomas produced a definitive description of what you'll get and what are the defaults and requirements. I can't remember where it is or was, but it was a public release - possibly in the Hyperion blog?

Personally, I try not to depend on defaults. I like to spell it all out in grisly detail.
I couldn't locate Thomas's description but a 3rd reread of the "Exec Memory Allocation" page in the Documentation WIKI answered my question about the default type of memory allocated with something like IExec->AllocVecTags(length+1, TAG_END):
The following examples show how to allocate memory.

APTR apointer = IExec->AllocVecTags(100, TAG_END);
 
if (apointer == NULL)
{ /* COULDN'T GET MEMORY, EXIT */ }
AllocVecTags() returns the address of the first byte of a memory block that is at least 100 bytes in size or NULL if there is not that much free memory. Because there are no tags specified, private memory is assumed so this memory cannot be shared with any other tasks.
Apparently the default memory allocated by AllocVecTags is MEMF_PRIVATE. That would mean that some of the allocations in the WIKI device examples are incorrect and need to be fixed.
AmigaOne X1000 with 2GB memory - OS4.1 FE
xenic
Posts: 1185
Joined: Sun Jun 19, 2011 12:06 am

Re: AllocVecTags() questions

Post by xenic »

broadblues wrote:
it would make sense to me to make all AllocVecTags() memory allocations with MEMF_PUBLIC.
Really, don't do that, whilst memory protection isn't activated, then an eroneuos MEMF_PRIVATE verses MEMF_SHARED may not make much odds (now at least) , MEMF_PUBLIC is locked so it can't be swapped, so is suitable for interupts etc but not main usage.

Libraries in general run on the callers context so don't need to be MEMF_SHARED, those that are special probably ought to have an allocator function, such as AlloSysObject() and AllocDosObject()
There is a large page in the WIKI explaining how the memory system works but average guys like me need a list of specific instances when MEMF_PUBLIC should be used. For example, are system hooks executed under the processes context or the context of the process of where the hook is added? I'm assuming that buffers or structures that will be passed in Exec messages should be allocated with MEMF_SHARED but I don't know for sure. Are the variables and constants declared in a program loaded into MEMF_EXECUTABLE memory along with the executable? If so, can a pointer to that data be passed in an Exec message to another process?

Right now it seems to me that misuse of MEMF_PRIVATE is more dangerous if memory protection is activated than the misuse of MEMF_SHARED. MEMF_SHARED will always need to work as long as we have OS3 emulation and older OS4 applictions.
Last edited by xenic on Thu Mar 10, 2016 11:54 pm, edited 2 times in total.
AmigaOne X1000 with 2GB memory - OS4.1 FE
whose
Posts: 92
Joined: Sun Sep 04, 2011 3:11 pm

Re: AllocVecTags() questions

Post by whose »

I´m not 100% sure about this topic, but AFAIR, MEMF_PUBLIC misuse will fill up (and block) precious memory and prevents swap, which is not a good thing at all. Further, AFAIR still, if you plan to share data off the process´ variable space, you should copy it using memory allocated with MEMF_SHARED tag before you share them between tasks/processes. Hooks are different beasts, because the executable code resides in memory tagged with MEMF_EXECUTABLE (and is protected against write operations).

Still you´re right that more in-deep information about this topic is missing... correct examples with a heap more comments would be a good thing.
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: AllocVecTags() questions

Post by salass00 »

There are only three values that are allowed for use with AllocVecTags() AVT_Type tag: MEMF_PRIVATE, MEMF_SHARED and MEMF_EXECUTABLE.

Out of these MEMF_SHARED and MEMF_EXECUTABLE default to being locked in memory (not swappable) while MEMF_PRIVATE is not locked (swappable) by default. This default can be overridden by using the AVT_Lock tag.

MEMF_PUBLIC is a legacy memory flag which should not be used with the new AllocVecTags() API.
Post Reply