8.41 Pseudo Memory

One use is to emulate PBR, such as Euphoria's map and stack types.

8.41.0.1 ram_space

include std/eumem.e
namespace eumem
export sequence ram_space

The (pseudo) RAM heap space. Use malloc to gain ownership to a heap location and free to release it back to the system.

8.41.0.2 malloc

include std/eumem.e
namespace eumem
export function malloc(object mem_struct_p = 1, integer cleanup_p = 1)

Allocate a block of (pseudo) memory

Parameters:
  1. mem_struct_p : The initial structure (sequence) to occupy the allocated block. If this is an integer, a sequence of zero this long is used. The default is the number 1, meaning that the default initial structure is {0}
  2. cleanup_p : Identifies whether the memory should be released automatically when the reference count for the handle for the allocated block drops to zero, or when passed to delete(). If 0, then the block must be freed using the free procedure.
Returns:

A handle, to the acquired block. Once you acquire this, you can use it as you need to. Note that if cleanup_p is 1, then the variable holding the handle must be capable of storing an atom as a double floating point value (i.e., not an integer).

Example 1:
my_spot = malloc()
 ram_space[my_spot] = my_data

8.41.0.3 free

include std/eumem.e
namespace eumem
export procedure free(atom mem_p)

Deallocate a block of (pseudo) memory

Parameters:
  1. mem_p : The handle to a previously acquired ram_space location.
Comments:

This allows the location to be used by other parts of your application. You should no longer access this location again because it could be acquired by some other process in your application. This routine should only be called if you passed 0 as cleanup_p to malloc.

Example 1:
my_spot = malloc(1,0)
 ram_space[my_spot] = my_data
     -- . . . do some processing  . . . 
 free(my_spot)

8.41.0.4 valid

include std/eumem.e
namespace eumem
export function valid(object mem_p, object mem_struct_p = 1)

Validates a block of (pseudo) memory

Parameters:
  1. mem_p : The handle to a previously acquired ram_space location.
  2. mem_struct_p : If an integer, this is the length of the sequence that should be occupying the ram_space location pointed to by mem_p.
Returns:

An integer,
0 if either the mem_p is invalid or if the sequence at that location is the wrong length.
1 if the handle and contents is okay.

Comments:

This can only check the length of the contents at the location. Nothing else is checked at that location.

Example 1:
my_spot = malloc()
 ram_space[my_spot] = my_data
 . . . do some processing  . . 
 if valid(my_spot, length(my_data)) then
     free(my_spot)
 end if