Hello,
I don't know what realloc() does, the code looks like this
while (size > 0) { interface->altsetting = realloc(interface->altsetting, sizeof(struct usb_interface_descriptor) * (interface->num_altsetting + 1)); if (!interface->altsetting) { if (usb_debug >= 1) fprintf(stderr, "couldn't malloc interface->altsetting\n"); return -1; } .... }
So I was wonder is its extend or just allocating a new block of memory?
Kind regards Rene W. Olsen
Hi,
Rene W. Olsen wrote:
I don't know what realloc() does, the code looks like this
while (size > 0) { interface->altsetting = realloc(interface->altsetting, sizeof(struct usb_interface_descriptor) * (interface->num_altsetting + 1)); if (!interface->altsetting) { if (usb_debug >= 1) fprintf(stderr, "couldn't malloc interface->altsetting\n"); return -1; } .... }
So I was wonder is its extend or just allocating a new block of memory?
Both.
If the original pointer is NULL, it is equivalent to a malloc. If it's not, it will re-allocate the block, and the contents will remain the same (up to min(oldsize, newsize)). If size is zero, it's equivalent to a free.
Note that it does not necessarily operate in-place. It might also allocate at a new address, and copy the contents.
Here's a link to the manpage: http://www.rt.com/man/realloc.3.html
Regards,
On 2005/05/31, Thomas Frieden wrote:
So I was wonder is its extend or just allocating a new block of memory?
Both.
If the original pointer is NULL, it is equivalent to a malloc. If it's not, it will re-allocate the block, and the contents will remain the same (up to min(oldsize, newsize)). If size is zero, it's equivalent to a free.
Note that it does not necessarily operate in-place. It might also allocate at a new address, and copy the contents.
Here's a link to the manpage: http://www.rt.com/man/realloc.3.html
okey thanks
Regards Rene W. Olsen
Hi,
Rene W. Olsen wrote:
But I not really sure this is ok, what happens if reallocvec() need to alloc a new block and move the memory? how do I get the new mem pointer?
Exec's ReallocVec does not move memory, it will resize in place if there is enough room, otherwise it will fail.
Regards,
Hi,
Rene W. Olsen wrote:
Ohh okey, but isent it a really big chance it fails?
Yes. The thing is, a "normal" realloc (i.e. allocate a new block and copy) is simple to do. Hoewever, resizing the block isn't.
if yes, I have to make my own realloc that moves it every time.
I'd recommend that, anyway. In the current system ReallocVec is still a bit broken.
Regards,
On 2005/05/31, Thomas Frieden wrote:
So I was wonder is its extend or just allocating a new block of memory?
Both.
If the original pointer is NULL, it is equivalent to a malloc. If it's not, it will re-allocate the block, and the contents will remain the same (up to min(oldsize, newsize)). If size is zero, it's equivalent to a free.
Note that it does not necessarily operate in-place. It might also allocate at a new address, and copy the contents.
Here's a link to the manpage: http://www.rt.com/man/realloc.3.html
I have been trying to use exec's realloc and my resoult looks like this
APTR my_realloc( struct LibusbBase *libBase, APTR mem, uint32 size ) { struct LibusbBase *libBase; APTR retval;
libBase = (struct LibusbBase *)Self->Data.LibBase;
if ( mem ) { retval = libBase->lib_IExec->AllocVec( size, MEMF_CLEAR|MEMF_PUBLIC ); } else { libBase->lib_IExec->ReallocVec( mem, size, MEMF_CLEAR|MEMF_PUBLIC ); retval = mem; }
return( retval ); }
But I not really sure this is ok, what happens if reallocvec() need to alloc a new block and move the memory? how do I get the new mem pointer?
Regards Rene W. Olsen
On 2005/06/25, Thomas Frieden wrote:
Hi,
Rene W. Olsen wrote:
But I not really sure this is ok, what happens if reallocvec() need to alloc a new block and move the memory? how do I get the new mem pointer?
Exec's ReallocVec does not move memory, it will resize in place if there is enough room, otherwise it will fail.
Ohh okey, but isent it a really big chance it fails?
if yes, I have to make my own realloc that moves it every time.
Regards