1. allocated memory enlargement

Er,, i ever thought i'd be asking this, but if i have a block of memory
allocated
on windows, and i want to enlarge it without changing it contents, can i? 
How?

Kat

new topic     » topic index » view message » categorize

2. Re: allocated memory enlargement

> Er,, i ever thought i'd be asking this, but if i have a block of memory 
> allocated
> on windows, and i want to enlarge it without changing it contents, can i?
> How?
>
> Kat

If you have allocated it with the Euphoria-function allocate, you could do 
it like this:

function resize_memblock(atom old_mem, integer old_size, integer new_size)
	atom new_mem
	if old_size = new_size then return old_mem end if
	new_mem = allocate(new_size)
	if old_size < new_size then
		mem_copy(new_mem, old_mem, old_size)
	else
		mem_copy(new_mem, old_mem, new_size)
	end if
	free(old_mem)
	return new_mem
end function

-- Using the function:

pointer = resize_memblock(pointer, size, size + 1000) -- adds 1000 bytes 
to the memory block

-- 

Tommy Carlier
tommy online: http://users.pandora.be/tommycarlier

new topic     » goto parent     » topic index » view message » categorize

3. Re: allocated memory enlargement

Tommy Carlier wrote:

>kat wrote
>  
>
>>Er,, i ever thought i'd be asking this, but if i have a block of memory 
>>allocated
>>on windows, and i want to enlarge it without changing it contents, can i?
>>How?
>>
>>Kat
>>    
>>
>If you have allocated it with the Euphoria-function allocate, you could do 
>it like this:
>
>function resize_memblock(atom old_mem, integer old_size, integer new_size)
>	atom new_mem
>	if old_size = new_size then return old_mem end if
>	new_mem = allocate(new_size)
>	if old_size < new_size then
>		mem_copy(new_mem, old_mem, old_size)
>	else
>		mem_copy(new_mem, old_mem, new_size)
>	end if
>	free(old_mem)
>	return new_mem
>end function
>
>-- Using the function:
>
>pointer = resize_memblock(pointer, size, size + 1000) -- adds 1000 bytes 
>to the memory block
>  
>
I am very pleased to see this. Not being familiar with mem_copy, I had 
wondered about a subject that Kat raised earlier regarding inserting a 
new new record into a memory buffered file. mem_copy makes it easy to 
make room for the new record. (if you are building the file in a 
specific order, alphabetical, for instance.)
Thanks, Tommy,
Allen

new topic     » goto parent     » topic index » view message » categorize

4. Re: allocated memory enlargement

On 26 Mar 2004, at 18:37, Tommy Carlier wrote:

> 
> http://click.topica.com/caab4CEb1dd66b6816za/ Crazy Aaron Enterprises
> 
> 
> > Er,, i ever thought i'd be asking this, but if i have a block of memory 
> > allocated
> > on windows, and i want to enlarge it without changing it contents, can i?
> > How?
> >
> > Kat
> 
> If you have allocated it with the Euphoria-function allocate, you could do it
> like this:
> 
> function resize_memblock(atom old_mem, integer old_size, integer new_size)
>  atom new_mem
>  if old_size = new_size then return old_mem end if
>  new_mem = allocate(new_size)

That last line makes it NOT a resize.

>  if old_size < new_size then
>   mem_copy(new_mem, old_mem, old_size)
>  else
>   mem_copy(new_mem, old_mem, new_size)
>  end if

And that proves it, you are allocating new space while the old still exists. I 
don't have 512 megs of memory on the computer. I need to *resize*, not 
make more!

Kat

new topic     » goto parent     » topic index » view message » categorize

5. Re: allocated memory enlargement

On 26 Mar 2004, at 18:48, Allen Robnett wrote:

> 
> http://click.topica.com/caab4CEb1dd66b6816za/ Crazy Aaron Enterprises
> 
> 
> Tommy Carlier wrote:
> 
> >kat wrote
> >  
> >
> >>Er,, i ever thought i'd be asking this, but if i have a block of memory 
> >>allocated
> >>on windows, and i want to enlarge it without changing it contents, can i?
> >>How?
> >>
> >>Kat
> >>    
> >>
> >If you have allocated it with the Euphoria-function allocate, you could do it
> >like this:
> >
> >function resize_memblock(atom old_mem, integer old_size, integer new_size)
> >	atom new_mem 	if old_size = new_size then return old_mem end if 	new_mem =
> >allocate(new_size) 	if old_size < new_size then 		mem_copy(new_mem, old_mem,
> >old_size) 	else 		mem_copy(new_mem, old_mem, new_size) 	end if 	free(old_mem)
> >	return new_mem end function
> >
> >-- Using the function:
> >
> >pointer = resize_memblock(pointer, size, size + 1000) -- adds 1000 bytes 
> >to the memory block
> >  
> >
> I am very pleased to see this. Not being familiar with mem_copy, I had 
> wondered about a subject that Kat raised earlier regarding inserting a 
> new new record into a memory buffered file. mem_copy makes it easy to 
> make room for the new record. (if you are building the file in a 
> specific order, alphabetical, for instance.)

But the code above is not *expand* file, it's *making another new* file! 
Freeing the space the file occupies is definitely changing the contents of the 
existing space.

Try to do that on a 385 megabyte system when you need to do a few 
hundred inserts real fast in a 200 meg ramdisk. The OS will page the files to 
disk (if you are lucky, but dos won't page to virtual ram), and your speed will 
be slower than if you used a disk-based system in the first place. Nix may 
be faster than Windows in this regard.

Kat

new topic     » goto parent     » topic index » view message » categorize

6. Re: allocated memory enlargement

From MSDN:
_expand returns a void pointer to the reallocated memory block. _expand, 
unlike realloc, cannot move a block to change its size. Thus, if there 
is sufficient memory available to expand the block without moving it, 
the memblock parameter to _expand is the same as the return value.
If there is insufficient memory available to expand the block to the 
given size without moving it. The item pointed to by memblock is 
expanded as much as possible in its current location.

_expand(void *, size) is found in libc.lib.  Header file malloc.h

I don't know if this helps, but here it is for what it's worth.

CoJaBo wrote:
> 
> http://click.topica.com/caab4CEb1dd66b5hhUsa/ Crazy Aaron Enterprises
> 
> 
> Kat wrote:
> 
>>
>>On 26 Mar 2004, at 18:48, Allen Robnett wrote:
>>
>>
>>>http://click.topica.com/caab4CEb1dd66b6816za/ Crazy Aaron Enterprises
>>>
>>>
>>>Tommy Carlier wrote:
>>>
>>>
>>>>kat wrote
>>>> 
>>>>
>>>>>Er,, i ever thought i'd be asking this, but if i have a block of memory 
>>>>>allocated
>>>>>on windows, and i want to enlarge it without changing it contents, can 
>>>>>i? How?
>>>>>
>>>>>Kat
>>>>>   
>>>>>
>>>>If you have allocated it with the Euphoria-function allocate, you could 
>>>>do it
>>>>like this:
>>>>
>>>>function resize_memblock(atom old_mem, integer old_size, integer 
>>>>new_size)
>>>>	atom new_mem 	if old_size = new_size then return old_mem end if  
>>>>new_mem =
>>>>allocate(new_size) 	if old_size < new_size then 		mem_copy(new_mem, 
>>>>old_mem,
>>>>old_size) 	else 		mem_copy(new_mem, old_mem, new_size) 	end if  
>>>>free(old_mem)
>>>>	return new_mem end function
>>>>
>>>>-- Using the function:
>>>>
>>>>pointer = resize_memblock(pointer, size, size + 1000) -- adds 1000 bytes 
>>>>
>>>>to the memory block
>>>> 
>>>>
>>>I am very pleased to see this. Not being familiar with mem_copy, I had 
>>>wondered about a subject that Kat raised earlier regarding inserting a 
>>>new new record into a memory buffered file. mem_copy makes it easy to 
>>>make room for the new record. (if you are building the file in a 
>>>specific order, alphabetical, for instance.)
>>
>>But the code above is not *expand* file, it's *making another new* file! 
>>
>>Freeing the space the file occupies is definitely changing the contents 
>>of the 
>>existing space.
>>
>>Try to do that on a 385 megabyte system when you need to do a few 
>>hundred inserts real fast in a 200 meg ramdisk. The OS will page the 
>>files to 
>>disk (if you are lucky, but dos won't page to virtual ram), and your 
>>speed will 
>>be slower than if you used a disk-based system in the first place. Nix 
>>may 
>>be faster than Windows in this regard.
>>
>>Kat
>>
> 
> Euphoria can't resize memory directly, there is probably a .DLL somewere 
> that can do that(Maybe in the Win32 API). Even if there isn't you could 
> probably make a C .DLL.
> 
> 
> 
> 
> For Topica's complete suite of email marketing solutions visit:
> http://www.topica.com/?p=TEXFOOTER
> 
>

new topic     » goto parent     » topic index » view message » categorize

7. Re: allocated memory enlargement

>Kat wrote:
>> But the code above is not *expand* file, it's *making another new* file! 
Kat, it doesn't seem you can do this with Eu's allocate, but if you
grab the memory yourself using kernel32's GlobalAlloc, HeapAlloc, or
LocalAlloc then you should be able to later call GlobalReAlloc, 
HeapReAlloc, or LocalReAlloc respectively.

Regards,
Pete

new topic     » goto parent     » topic index » view message » categorize

8. Re: allocated memory enlargement

On 27 Mar 2004, at 12:13, Pete Lomax wrote:

> 
> http://click.topica.com/caab5n1b1dd66b6816za/ Ivo Interactive
> 
> 
> >Kat wrote:
> >> But the code above is not *expand* file, it's *making another new* file! 
> Kat, it doesn't seem you can do this with Eu's allocate, but if you
> grab the memory yourself using kernel32's GlobalAlloc, HeapAlloc, or
> LocalAlloc then you should be able to later call GlobalReAlloc, 
> HeapReAlloc, or LocalReAlloc respectively.

Thanks, Pete. A couple questions:

1) do flags like LMEM_MOVEABLE copy the memory over again, or do they 
move the memory in blocks, reallocating the new and deallocating the old as 
it goes? Is this fast?

2) how do i know the value of LMEM_MOVEABLE so i can pass it to a 
winapi function?

Kat

new topic     » goto parent     » topic index » view message » categorize

9. Re: allocated memory enlargement

On Sat, 27 Mar 2004 06:42:41 -0600, Kat <gertie at visionsix.com> wrote:

>> >Kat wrote:
>> >> But the code above is not *expand* file, it's *making another new* file! 
>> Kat, it doesn't seem you can do this with Eu's allocate, but if you
>> grab the memory yourself using kernel32's GlobalAlloc, HeapAlloc, or
>> LocalAlloc then you should be able to later call GlobalReAlloc, 
>> HeapReAlloc, or LocalReAlloc respectively.
>
>Thanks, Pete. A couple questions:
>
>1) do flags like LMEM_MOVEABLE copy the memory over again, or do they 
>move the memory in blocks, reallocating the new and deallocating the old as 
>it goes? Is this fast?
I would imagine it uses repne movsd, which is a single machine
instruction to move large chunks of data and can't be bettered.
Without the M$ source, I can't say whether it will leave full pages
(or are they called segments) alone or not.
>
>2) how do i know the value of LMEM_MOVEABLE so i can pass it to a 
>winapi function?
It is 2 according to google blink

Regards,
Pete
PS I haven't actually used these myself, I've only glanced at them
with an eye to possibly using them in the near future.

Another way to deal with this might be to allocate say 2MB chunks by
hand and maintain a table of them. That way you never have to realloc,
just add another chunk to the end of the table. Of course it is a lot
more work to cope with the breaks in the data though.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu