|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2025 Paul Guyot <pguyot@kallisys.net> |
| 5 | +% |
| 6 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +% you may not use this file except in compliance with the License. |
| 8 | +% You may obtain a copy of the License at |
| 9 | +% |
| 10 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +% |
| 12 | +% Unless required by applicable law or agreed to in writing, software |
| 13 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +% See the License for the specific language governing permissions and |
| 16 | +% limitations under the License. |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | + |
| 21 | +-module(jit_stream_flash). |
| 22 | + |
| 23 | +% Stream implementation using flash, suitable for MCUs with programmable flash |
| 24 | + |
| 25 | +-export([ |
| 26 | + new/1, |
| 27 | + offset/1, |
| 28 | + append/2, |
| 29 | + replace/3, |
| 30 | + map/4, |
| 31 | + flush/1 |
| 32 | +]). |
| 33 | + |
| 34 | +%% Additional nif |
| 35 | +-export([ |
| 36 | + read/3 |
| 37 | +]). |
| 38 | + |
| 39 | +-export_type([stream/0]). |
| 40 | + |
| 41 | +-type stream() :: binary(). |
| 42 | + |
| 43 | +%%----------------------------------------------------------------------------- |
| 44 | +%% @returns An empty binary |
| 45 | +%% @param MaxSize maximum size of the stream |
| 46 | +%% @doc Create a new stream, i.e. return an empty binary |
| 47 | +%% @end |
| 48 | +%%----------------------------------------------------------------------------- |
| 49 | +-spec new(MaxSize :: pos_integer()) -> stream(). |
| 50 | +new(_MaxSize) -> |
| 51 | + erlang:nif_error(undefined). |
| 52 | + |
| 53 | +%%----------------------------------------------------------------------------- |
| 54 | +%% @param Stream stream to get the offset from |
| 55 | +%% @returns The current offset |
| 56 | +%% @doc Get the current offset in the stream |
| 57 | +%% @end |
| 58 | +%%----------------------------------------------------------------------------- |
| 59 | +-spec offset(stream()) -> non_neg_integer(). |
| 60 | +offset(_Stream) -> |
| 61 | + erlang:nif_error(undefined). |
| 62 | + |
| 63 | +%%----------------------------------------------------------------------------- |
| 64 | +%% @param Stream stream to append to |
| 65 | +%% @param Binary binary to append to the stream |
| 66 | +%% @returns The updated stream |
| 67 | +%% @doc Append a binary to the stream |
| 68 | +%% @end |
| 69 | +%%----------------------------------------------------------------------------- |
| 70 | +-spec append(stream(), binary()) -> stream(). |
| 71 | +append(_Stream, _Binary) -> |
| 72 | + erlang:nif_error(undefined). |
| 73 | + |
| 74 | +%%----------------------------------------------------------------------------- |
| 75 | +%% @param Stream stream to update |
| 76 | +%% @param Offset offset to update from |
| 77 | +%% @param Replacement binary to write at offset |
| 78 | +%% @returns The updated stream |
| 79 | +%% @doc Replace bytes at a given offset |
| 80 | +%% @end |
| 81 | +%%----------------------------------------------------------------------------- |
| 82 | +-spec replace(stream(), non_neg_integer(), binary()) -> stream(). |
| 83 | +replace(_Stream, _Offset, _Replacement) -> |
| 84 | + erlang:nif_error(undefined). |
| 85 | + |
| 86 | +%%----------------------------------------------------------------------------- |
| 87 | +%% @param Stream stream to update |
| 88 | +%% @param Offset offset to update from |
| 89 | +%% @param Length length of the section to update |
| 90 | +%% @param MapFunction function that updates the binary |
| 91 | +%% @returns The updated stream |
| 92 | +%% @doc Replace bytes at a given offset calling a map function |
| 93 | +%% @end |
| 94 | +%%----------------------------------------------------------------------------- |
| 95 | +-spec map(stream(), non_neg_integer(), pos_integer(), fun((binary()) -> binary())) -> stream(). |
| 96 | +map(Stream, Offset, Length, MapFunction) -> |
| 97 | + Binary = ?MODULE:read(Stream, Offset, Length), |
| 98 | + Mapped = MapFunction(Binary), |
| 99 | + ?MODULE:replace(Stream, Offset, Mapped). |
| 100 | + |
| 101 | +%%----------------------------------------------------------------------------- |
| 102 | +%% @param Stream stream to read from |
| 103 | +%% @param Offset offset to read from |
| 104 | +%% @param Length number of bytes to read |
| 105 | +%% @returns The binary data read from the stream |
| 106 | +%% @doc Read bytes at a given offset |
| 107 | +%% |
| 108 | +%% @end |
| 109 | +%%----------------------------------------------------------------------------- |
| 110 | +-spec read(stream(), non_neg_integer(), pos_integer()) -> binary(). |
| 111 | +read(_Stream, _Offset, _Length) -> |
| 112 | + erlang:nif_error(undefined). |
| 113 | + |
| 114 | +%%----------------------------------------------------------------------------- |
| 115 | +%% @param Stream stream to flush |
| 116 | +%% @returns The stream flushed |
| 117 | +%% @doc Flush the stream. Typically invalidates instruction cache. |
| 118 | +%% |
| 119 | +%% @end |
| 120 | +%%----------------------------------------------------------------------------- |
| 121 | +-spec flush(stream()) -> stream(). |
| 122 | +flush(_Stream) -> |
| 123 | + erlang:nif_error(undefined). |
0 commit comments