Skip to content

Commit 427d45a

Browse files
authored
Merge pull request #87 from Electrostat-Lab/unix-res
Unix resources: File Memory Model API
2 parents cb5b10c + 72318c7 commit 427d45a

17 files changed

Lines changed: 848 additions & 43 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="test-fileread" type="ShConfigurationType">
3+
<option name="SCRIPT_TEXT" value="" />
4+
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
5+
<option name="SCRIPT_PATH" value="$PROJECT_DIR$/helper-scripts/ci-cd/test-electrostatic.sh" />
6+
<option name="SCRIPT_OPTIONS" value="&quot;linux/hello_file_read.c&quot; &quot;hello_file_read&quot;" />
7+
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
8+
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
9+
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
10+
<option name="INTERPRETER_PATH" value="/bin/bash" />
11+
<option name="INTERPRETER_OPTIONS" value="" />
12+
<option name="EXECUTE_IN_TERMINAL" value="true" />
13+
<option name="EXECUTE_SCRIPT_FILE" value="true" />
14+
<envs />
15+
<method v="2" />
16+
</configuration>
17+
</component>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="test-filewrite" type="ShConfigurationType">
3+
<option name="SCRIPT_TEXT" value="" />
4+
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
5+
<option name="SCRIPT_PATH" value="$PROJECT_DIR$/helper-scripts/ci-cd/test-electrostatic.sh" />
6+
<option name="SCRIPT_OPTIONS" value="&quot;linux/hello_file_write.c&quot; &quot;hello_file_write&quot;" />
7+
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
8+
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
9+
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
10+
<option name="INTERPRETER_PATH" value="/bin/bash" />
11+
<option name="INTERPRETER_OPTIONS" value="" />
12+
<option name="EXECUTE_IN_TERMINAL" value="true" />
13+
<option name="EXECUTE_SCRIPT_FILE" value="true" />
14+
<envs />
15+
<method v="2" />
16+
</configuration>
17+
</component>

electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/algorithm/arithmos/adt/list.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
extern "C" {
4444
#endif
4545

46-
enum list_type {
47-
CONTIGUOUS_BUFFER,
48-
LINKED_BUFFER
49-
};
50-
5146
struct list_element {
5247
void *data;
5348
void *metadata;

electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/algorithm/arithmos/list/linked_buffer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
extern "C" { // disables the C++ name mangling
1111
#endif
1212

13-
enum linked_buffer_type {
14-
SINGLE_ENDED,
15-
DOUBLE_ENDED,
16-
};
17-
1813
struct linked_buffer {
1914
// no alignment issues
2015
// 64-bit (8 bytes) aligned structure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @brief A completely memory-safe not internally synchronized API that provides a dynamic
3+
* way of reading and writing to files without a [realloc] overhead; by delegating memory
4+
* allocation as a part of the pre-processor states to the caller, and the memory deallocation or further reallocation calls
5+
* as a part of the accepting states to another function of the caller API or Application.
6+
* @note Synchronization (using polling or mutexes) could be introduced through the lifecycle struct
7+
* [file_op_processor] for file operation processor.
8+
* @author pavl_g.
9+
* @date 2025-10
10+
*/
11+
#ifndef _FILE_OPERATIONS_H_
12+
#define _FILE_OPERATIONS_H_
13+
14+
#ifndef _ELECTRO_MIO
15+
16+
#ifdef __ANDROID__
17+
#define __off_t size_t
18+
#endif
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
#include <sys/stat.h>
25+
#include <errno.h>
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include <unistd.h>
29+
#include <stdint.h>
30+
#include <electrostatic/electronetsoft/util/utilities.h>
31+
32+
struct read_op_processor {
33+
void (*on_bytes_processed)(file_mem *, ssize_t, void *); /* Executed on each successful read operation. */
34+
void (*on_eof_reached)(file_mem *); /* Executed when EOF is reached. Could be used to chain calls to memory deallocators. */
35+
void (*on_last_char_sampled)(file_mem *, void *caller);
36+
void (*on_error_encountered)(file_mem *, int, void *caller);
37+
};
38+
39+
struct write_op_processor {
40+
void (*on_bytes_processed)(file_mem *, ssize_t, void *); /* Executed on each successful read operation. */
41+
void (*on_eob_reached)(file_mem *);
42+
void (*on_last_char_sampled)(file_mem *, void *caller);
43+
void (*on_error_encountered)(file_mem *, int, void *caller);
44+
};
45+
46+
struct update_op_processor {
47+
void (*on_update_nbytes)(file_mem *, ssize_t); /* Executed when updating the number of bytes attr (of the buffer for the file mem model) is commenced */
48+
void (*on_update_size)(file_mem *, __off_t); /* Executed when updating size is commenced. */
49+
void (*on_update_pos)(file_mem *, __off_t); /* Executed when updating the position is commenced. */
50+
void (*update_model_preprocessor)(file_mem *, void *caller); /* Executed */
51+
void (*update_model_postprocessor)(file_mem *, void *caller); /* Executed after the file mem model attrs update has finished. Could be used to chain calls to memory allocators. */
52+
};
53+
54+
struct serializer_op_processor {
55+
void (*serializer_init_preprocessor)(pipe_serializer *);
56+
void (*serializer_init_postprocessor)(pipe_serializer *);
57+
};
58+
59+
struct file_mem {
60+
int fd;
61+
char trailing; /* The trailing byte of the buffer.*/
62+
ssize_t n_bytes; /* Total number of bytes to allocate for the buffer in memory
63+
* including the number of read or write bytes
64+
* starting from the current pos based on
65+
* the file size, and the trailing byte. */
66+
ssize_t bytes_processed;
67+
char *buffer;
68+
__off_t file_size; /* Cached file size in bytes */
69+
__off_t file_pos; /* Cached file position */
70+
uint8_t __auto_update_attrs; /* Conditional flag to specify whether auto-update
71+
* in the pre-processing stage is enabled. */
72+
uint8_t __continue_after_eof; /* Conditional flag to specify whether to continue after the EOF. */
73+
};
74+
75+
/**
76+
* @brief A pipe-serializer object creates a pipe that has
77+
* a write end and read end; both ends are mapped to the same
78+
* filesystems, in which the write operations are available to be read
79+
* by the memory model processor. Therefore, both memory models utilize
80+
* a shared memory buffer, but they possess different filesystems and thus
81+
* different file positions.
82+
*/
83+
struct pipe_serializer {
84+
file_mem read_end;
85+
file_mem write_end;
86+
};
87+
88+
89+
/**
90+
* @brief Delegates to allocate a heap buffer and reads a file identified by this file descriptor
91+
* into the heap memory; returning the memory address into the
92+
* [buffer] pointer of the file memory model object.
93+
*
94+
* @param mem a pointer to the file memory model struct.
95+
* @param _processor a pointer to the file read operations processor; that links the API to the user application.
96+
* @param __processor a pointer to the file attrs update processors.
97+
*/
98+
status_code read_into_mem(file_mem *, read_op_processor *, update_op_processor *);
99+
100+
status_code update_file_attrs(file_mem *, update_op_processor *);
101+
102+
/**
103+
* @brief Writes a pre-allocated buffer to a file identified by a file descriptor from
104+
* the file memory model structure; until reaching the trailing character which is excluded
105+
* from the this write operation and marks the accepting state of this machine.
106+
*
107+
* @param mem a pointer to the file memory model struct.
108+
* @param processor a pointer to the file operations processor; that links the API to the user application.
109+
*/
110+
status_code write_from_mem(file_mem *, write_op_processor *, update_op_processor *);
111+
112+
status_code init_serializer(pipe_serializer *, serializer_op_processor *, update_op_processor *);
113+
114+
#ifdef __cplusplus
115+
}
116+
#endif
117+
118+
#endif
119+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef _FILE_ATTRS_H_
2+
#define _FILE_ATTRS_H_
3+
4+
#ifndef _ELECTRO_MIO
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#include <sys/stat.h>
11+
#include <errno.h>
12+
#include <stdio.h>
13+
#include <unistd.h>
14+
#include <stdint.h>
15+
#include <electrostatic/electronetsoft/util/utilities.h>
16+
17+
static inline status_code get_file_bytes(int fd, __off_t *__size) {
18+
if (fd < 0 || NULL == __size) {
19+
return EUNDEFINEDBUFFER;
20+
}
21+
struct stat statbuf;
22+
int __status = fstat(fd, &statbuf);
23+
if (__status != 0) {
24+
return errno;
25+
}
26+
*__size = statbuf.st_size;
27+
28+
return PASS;
29+
}
30+
31+
static inline status_code get_file_pos(int fd, off_t *__offset) {
32+
if (fd < 0 || NULL == __offset) {
33+
return EUNDEFINEDBUFFER;
34+
}
35+
*__offset = lseek(fd, 0, SEEK_CUR);
36+
if (*__offset < 0) {
37+
return errno;
38+
}
39+
return PASS;
40+
}
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
46+
#endif
47+
#endif

electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_verify.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _FILE_VERIFY_H_
22
#define _FILE_VERIFY_H_
33

4+
#ifndef _ELECTRO_MIO
5+
46
#ifdef __cplusplus
57
extern "C" {
68
#endif
@@ -145,4 +147,5 @@ static inline int is_fsymbolic_link(int fd) {
145147
}
146148
#endif
147149

150+
#endif
148151
#endif

electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/types.h

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,31 @@
88
#ifndef __TYPES_H_
99
#define __TYPES_H_
1010

11+
#include <inttypes.h>
12+
1113
#ifdef __cplusplus
1214
extern "C" { // disables C++ Name Mangling
1315
#endif
1416

1517
// Abstract Data Types for Lists
1618
typedef struct list (list);
1719
typedef struct list_info (list_info);
18-
typedef enum list_type (list_type);
20+
21+
typedef enum list_type {
22+
CONTIGUOUS_BUFFER,
23+
LINKED_BUFFER
24+
} list_type;
25+
1926
typedef struct list_element (list_element);
2027
typedef struct list_function_table (list_function_table);
2128

2229
// Concretized types for linked buffers
2330
typedef struct linked_buffer (linked_buffer);
24-
typedef enum linked_buffer_type (linked_buffer_type);
31+
32+
typedef enum linked_buffer_type {
33+
SINGLE_ENDED,
34+
DOUBLE_ENDED,
35+
} linked_buffer_type;
2536

2637
// Abstract types for Mathematical Graph Structures
2738
typedef struct vertex (vertex);
@@ -45,8 +56,23 @@ typedef struct dijkstra_structure (dijkstra_structure);
4556
typedef struct api_lifecycle (api_lifecycle);
4657
typedef struct typed_pointer (typed_pointer);
4758
typedef union pointer (pointer);
48-
typedef enum pointer_type (pointer_type);
49-
typedef enum status_code (status_code);
59+
60+
typedef enum status_code {
61+
PASS = INT32_MAX,
62+
EUNDEFINEDBUFFER = INT32_MIN,
63+
EEMPTYBUFFER = (EUNDEFINEDBUFFER + 1),
64+
EFULLBUFFER = (EEMPTYBUFFER + 1),
65+
EINCOMPATTYPE = (EFULLBUFFER + 1),
66+
ENOELEMENT = (EINCOMPATTYPE + 1),
67+
EBUFFERTURNCATION = (ENOELEMENT + 1),
68+
EBUFFEROVERFLOW = (EBUFFERTURNCATION + 1),
69+
EDLLOPENFAIL = (EBUFFEROVERFLOW + 1),
70+
EDLLSYMFAIL = (EDLLOPENFAIL + 1),
71+
EDLLCONVENTIONCALLRETURN = (EDLLSYMFAIL + 1),
72+
UNEXPECTED_ERROR = (EDLLCONVENTIONCALLRETURN + 1),
73+
ASSERTION_SUCCESS = 1,
74+
ASSERTION_FAILURE = 0
75+
} status_code;
5076

5177
typedef struct memory_partition (memory_partition);
5278

@@ -55,6 +81,14 @@ typedef struct routine_data (routine_data);
5581
typedef struct dll_function_table (dll_function_table);
5682
typedef struct routine_callbacks (routine_callbacks);
5783

84+
typedef struct write_op_processor (write_op_processor);
85+
typedef struct read_op_processor (read_op_processor);
86+
typedef struct update_op_processor (update_op_processor);
87+
88+
typedef struct serializer_op_processor (serializer_op_processor);
89+
typedef struct file_mem (file_mem);
90+
typedef struct pipe_serializer (pipe_serializer);
91+
5892
// Types for Vector Maths Libraries
5993
typedef struct vector2d (vector2d);
6094

0 commit comments

Comments
 (0)