eo-runtime/src/main/eo/org/eolang/malloc.eo
# The MIT License (MIT)
#
# Copyright (c) 2016-2024 Objectionary.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
+architect yegor256@gmail.com
+home https://github.com/objectionary/eo
+package org.eolang
+rt jvm org.eolang:eo-runtime:0.0.0
+version 0.0.0
# The `malloc` object is an abstraction of a storage of data in heap
# memory. The implementation of `malloc` is platform dependent. It may
# use either OS-level or VM-level memory management mechanism.
#
# There are two ways of usage `malloc`:
#
# First, when the size of required memory block is known upfront:
#
# ```
# malloc.of
# 8
# [m]
# m.put 10 > @
# ```
#
# Here, the first argument is a size of allocated block in memory, the second argument
# is the scope where memory block is available for reading and writing. When `malloc.of` is
# dataized it dataizes the scope, take the data from the block in memory, clears the block and
# returns the data. So there's no need for end-user to care about clearing memory after allocation.
#
# Second, when the size is not known upfront, but there exists
# an object ready to be dataized and placed into the memory block
# (the size of the block will be equal to the amount of bytes
# produced by the dataization of the object):
#
# ```
# malloc.for
# "Hello world!"
# [m]
# m.put "Hello, Jeff!" > @
# ```
#
# Here, the first argument is an object which will be dataized, then a block in memory of given data
# size is allocated and the data is written to the block. The second argument is the same scope as
# in the p.1.
#
# The void attribute in the scope object is memory-block object which provides API to write and read
# data to the memory.
#
# ```
# malloc.of
# 8 # allocate 8 bytes length block in memory
# [m]
# seq > @
# *
# m.write 2 "Hello" # write object "Hello" with offset 2
# m.read 3 4 # read 4 bytes from offset 3 -> "ello"
# m.put 42 # write object 42 with offset 0
# m.get # just get all the data from the memory block -> 42
# m.size # get size of the block
# m.id # get identifier of the block
# m.@ # the same as m.get
# ```
[] > malloc
# Allocates block in memory for given `object`. After allocation the provided object is dataized
# and the data are written into memory.
[object scope] > for
(dataized object).as-bytes > bts
malloc.of > @
bts.size
[m] >>
seq > @
*
m.write 0 ^.bts
^.scope m
# Allocates block in memory of given `size`. After allocation the `size` zero bytes bytes are
# written into memory.
[size scope] > of
[] > @ /bytes
# Allocated block in memory that provides an API for writing and reading.
[id] > allocated
^.size > size
get > @
# Read `length` bytes with `offset` from the allocated block in memory.
[offset length] > read /bytes
# Write `data` with `offset` to the allocated block in memory.
[offset data] > write /true
# Just get all the data from the allocated block in memory.
^.read 0 ^.size > [] > get
# Put `object` into the allocated block in memory. The `object` is supposed to be dataizable.
[object] > put
seq > @
*
^.write 0 object
^.get