Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Gilloo

  • Full Member
  • ***
  • Join Date: Apr 2006
  • Posts: 124
    • Show all replies
Re: Counting on AllocVec's size memo..
« on: January 05, 2007, 03:18:37 PM »
Quote

Jose wrote:
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.


Yes.
Look at this piece of ugly code: I use it in V34 routines...
APTR MAllocVec( unsigned long byteSize, unsigned long requirements )
{
  if (SysBase->LibNode.lib_Version >= 36)
  {
    return AllocVec(byteSize, requirements) ;
  }
  else
  {
    unsigned long *data = NULL ;

    byteSize += sizeof(unsigned long) ;
    data = (unsigned long *)AllocMem(byteSize, requirements) ;
 
    *data = byteSize ;
    data++ ;
    return data ;
  }
}

void MFreeVec( APTR memoryBlock )
{
  if (SysBase->LibNode.lib_Version >= 36)
  {
    FreeVec(memoryBlock) ;
  }
  else
  {
    unsigned long byteSize ;
    unsigned long *data ;

    data = (unsigned long*)memoryBlock ;
    data-- ;
    byteSize = *data ;
    FreeMem(data, byteSize) ;
  }
}

Quote

Jose wrote:
Probably a bit of a hack couting on it or it's not gonna change in the foresenable future, what do you guys think ?

AllocVec/FreeVec are shorthand implementations to AllocMem/FreeMem functions. Internaly, AllocVec uses AllocMem, and FreeVec... FreeMem.
AllocMem/FreeMem uses Allocate/Deallocate and so on...
You can modify AllocVec/FreeVec (with SetFunction) as you want, and add what you want before the data pointer returned by AllocVec (Process name, hunk, Programm name...) to track memory allocations. This story of size stored before pointer returned by AllocVec relies only on ONE implementation!!