[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Memory Allocation in Ruby

Vikrant Singh

2/27/2008 9:24:00 AM

Hello All,

This is the first time I am working on Ruby and I am not sure if I am
asking very basics of the language.


I am working on a tool which is written in Ruby and I need to implement
a new feature in it. Although itâ??s a small update still to work and
understand the code I need to learn the basics of Ruby first.

I am using â??Programming Ruby, 2nd Editionâ? by Dave Thomas as a
reference. Its a good book and I am able to proceed using it.

Today I got stuck in memory handling code

Following is what I am doing and need your comments on the sameâ?¦

1.Working in a function which receives number of bytes as argument.
2.I need to allocate memory (double in the size sent in argument) to a
buffer and also initialize all the bits to zero.

I wrote following code for the last two pointsâ?¦
def FunctionName (numBytes)
bufferSize = numBytes*2
buffer = dl.malloc(bufferSize)
end
I am using this dl.malloc after taking reference from existing code.
Here I searched in the book and found that Ruby presents its own APIs
for memory allocation.
I also found that dl is a way of using win32 APIs instead of Rubyâ??s API.
Which one is better?
Also, after allocation how to make sure that memory is initialized with
all zeros.

3.Set the 10th bit in the buffer
buffer[bufferSize -2] = buffer[bufferSize -2] | 0x4

4.Execute some library calls with this buffer
5.Clear the buffer again

I searched the book and found that clear function is used to clear the
content of an array. I am not sure if the same will work with the
dynamically allocated buffer as well.
Buffer.clear

6.Execute some library calls again in this buffer
7.Copy the first half of the buffer in a new buffer and second half of
the buffer in another.

firstHalf = dl.malloc(numBytes)
secondHalf = dl.malloc(numBytes)
#Copy numBytesâ?¦numBytes/2
memcpy(firstHalf, buffer, numBytes)
#Copy numbytes/2 â?¦ 0
Memcpy(secondHalf, buffer[0..numBytes/2], numbytes)

Please reply with your view on the same. Please correct me if I am wrong
somewhere.
--
Posted via http://www.ruby-....

1 Answer

James Tucker

2/27/2008 12:06:00 PM

0


On 27 Feb 2008, at 09:24, Vikrant Singh wrote:

> Hello All,
>
> This is the first time I am working on Ruby and I am not sure if I am
> asking very basics of the language.
>
>
> I am working on a tool which is written in Ruby and I need to =20
> implement
> a new feature in it. Although it=92s a small update still to work and
> understand the code I need to learn the basics of Ruby first.
>
> I am using =93Programming Ruby, 2nd Edition=94 by Dave Thomas as a
> reference. Its a good book and I am able to proceed using it.
>
> Today I got stuck in memory handling code
>
> Following is what I am doing and need your comments on the same=85
>
> 1.Working in a function which receives number of bytes as argument.
> 2.I need to allocate memory (double in the size sent in argument) to a
> buffer and also initialize all the bits to zero.
>
> I wrote following code for the last two points=85
> def FunctionName (numBytes)
> bufferSize =3D numBytes*2
> buffer =3D dl.malloc(bufferSize)
> end

def FunctionName(numBytes)
bufferSize =3D numBytes * 2
buffer =3D "\0" * bufferSize
end

Most of the external interfaces can use strings as sets of binary =20
data, as this is our best binary representation at present.

If you use a ruby string, instead of a dl.malloc, the only thing you =20
need to ensure is that the data is not GC'd prematurely. This will =20
happen if there are no references left on the ruby side.

I can't comment on the semantics of dl, as I've never used it.

>
> I am using this dl.malloc after taking reference from existing code.
> Here I searched in the book and found that Ruby presents its own APIs
> for memory allocation.
> I also found that dl is a way of using win32 APIs instead of Ruby=92s =20=

> API.
> Which one is better?
> Also, after allocation how to make sure that memory is initialized =20
> with
> all zeros.
>
> 3.Set the 10th bit in the buffer
> buffer[bufferSize -2] =3D buffer[bufferSize -2] | 0x4
>
> 4.Execute some library calls with this buffer
> 5.Clear the buffer again
>
> I searched the book and found that clear function is used to clear the
> content of an array. I am not sure if the same will work with the
> dynamically allocated buffer as well.
> Buffer.clear
>
> 6.Execute some library calls again in this buffer
> 7.Copy the first half of the buffer in a new buffer and second half of
> the buffer in another.
>
> firstHalf =3D dl.malloc(numBytes)
> secondHalf =3D dl.malloc(numBytes)
> #Copy numBytes=85numBytes/2
> memcpy(firstHalf, buffer, numBytes)
> #Copy numbytes/2 =85 0
> Memcpy(secondHalf, buffer[0..numBytes/2], numbytes)

first_half =3D buffer[0..half]
sec_half =3D buffer[half..-1]

You won't need to memcpy if you're treating the data as a regular ruby =20=

string.

>
>
> Please reply with your view on the same. Please correct me if I am =20
> wrong
> somewhere.
> --=20
> Posted via http://www.ruby-....
>