Direct memory access

Contents

1. Address arithmetic
2. Memory access

1. Address arithmetic

1.1. Conversion

The predefined class POINTER has 2 built-in features, which allow to manipulate with the memory addresses:

to_integer: INTEGER
	-- The corresponding integer value of the current "POINTER" object

from_integer (value: INTEGER)
	-- Sets the current "POINTER" object to the given integer value

1.2. Relative addresses

The predefined class POINTER provides a function that allows to calculate new address (i.e. object of type POINTER) from the given one:

infix "+" (offset: INTEGER): POINTER
	-- The new pointer value calculated as a sum of the current pointer
	-- value and the given `offset'

2. Memory access

2.1. Low-level access

The class MEMORY contains special routine which allows to perform memory copying from the one area into the other one:

mem_copy (dest, source: POINTER; bytes_number: INTEGER)
	-- The routine copies "bytes_number" bytes from the address
	-- "source" to the address "dest"

Example.

Suppose you have a callback routine written in Eiffel:

my_callback (
	wnd_handle : INTEGER;
	message    : INTEGER;
	wparam     : INTEGER;
	lparam     : INTEGER
) is

One of the parameters might be an address of some area with some data. Let it be parameter "lparam". Then the following code would provide a proper access to the data:

my_callback (
	wnd_handle: INTEGER;
	message: INTEGER;
	wparam: INTEGER;
	lparam: INTEGER
) is
	local
		...
		ptr: POINTER; -- temporary variable
		...
	do
		...
		ptr.from_integer (lparam)
		mem.mem_copy ($data, ptr, data_size)
		-- Here we manipulate with "data" as a usual Eiffel object
		...
		-- Then we should save the changes to the specified memory location
		ptr.from_integer (lparam)
		mem.mem_copy (ptr, $data, data_size)
		...
	end

	data: expanded SOME_STRUCTURE -- the corresponding data structure ...
	data_size: INTEGER is ...     -- ... and its size in bytes

mem: MEMORY is
	once
		!!Result
	end

Note that the example assumes that class "SOME_STRUCTURE" was declared as an "interface" class in the Eiffel System/Library Description file to prevent compiler from disordering fields of the object (see the Visual Eiffel compiler description for more).

2.2. High-level access

Visual Eiffel provides a higher level to access memory. It relies on the RTS_MEMORY_... classes that currently belong to the Kernel cluster.

RTS_MEMORY_... hierarchy

Class Description
RTS_MEMORY * A memory area with some address and of some size
RTS_MEMORY_AREA * A memory area with the user-supplied memory address
RTS_MEMORY_OBJECT * A memory area starting from the address of the given Eiffel object
RTS_MEMORY_RESIZABLE * A memory area which could be resized and changed
RTS_MEMORY_MAP [G] * A view of memory area as an array of objects of the given type
RTS_MEMORY_AREA_ITEM A user-supplied memory area with specified address and size
RTS_MEMORY_MAPPED_OBJECT [G] * An object view of the memory area
RTS_MEMORY_MAPPED_ARRAY [G] * An array view of the memory area

Copyright © Object Tools -- info@object-tools.com
Last updated: 2005/02/02 11:51:34