Skip to content

Commit 2f7bc6c

Browse files
memory models in CHAIN
1 parent f923b3a commit 2f7bc6c

5 files changed

Lines changed: 64 additions & 13 deletions

File tree

include/basic/context.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
#include "audio.h"
1111
#include "data.h"
1212

13+
typedef enum memory_model_t {
14+
mm_small = 25,
15+
mm_medium = 26,
16+
mm_large = 27,
17+
mm_huge = 28,
18+
} memory_model_t;
19+
1320
/**
1421
* @brief Represents a queued UDP packet.
1522
*
@@ -486,7 +493,7 @@ typedef struct basic_ctx {
486493
* @param error Pointer to store any error messages.
487494
* @return A pointer to the initialized `basic_ctx` context.
488495
*/
489-
struct basic_ctx* basic_init(const char *program, uint32_t pid, const char* file, char** error);
496+
struct basic_ctx* basic_init(const char *program, uint32_t pid, const char* file, char** error, enum memory_model_t model);
490497

491498
/**
492499
* @brief Destroy the BASIC context and free associated resources.

include/basic/tokenizer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ typedef void (*keyword_handler_t)(struct basic_ctx*);
225225
T(MEMMOVE, STMT, memmove_statement) /* 153 */ \
226226
T(RESTRICT, STMT, restrict_statement) /* 154 */ \
227227
T(DERESTRICT, STMT, derestrict_statement) /* 155 */ \
228+
T(MEMORY, STMT, NULL) /* 156 */ \
229+
T(SMALL, STMT, NULL) /* 157 */ \
230+
T(MEDIUM, STMT, NULL) /* 158 */ \
231+
T(LARGE, STMT, NULL) /* 159 */ \
232+
T(HUGE, STMT, NULL) /* 160 */ \
228233

229234
GENERATE_ENUM_LIST(TOKEN, token_t)
230235

include/taskswitch.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <kernel.h>
1010

11+
enum memory_model_t;
12+
1113
/**
1214
* @brief Represents the current state of a process.
1315
*/
@@ -133,7 +135,7 @@ typedef struct idle_timer {
133135
* @param csd Current selected directory
134136
* @return process_t* Pointer to new process details
135137
*/
136-
process_t* proc_load(const char* fullpath, pid_t parent_pid, const char* csd);
138+
process_t* proc_load(const char* fullpath, pid_t parent_pid, const char* csd, enum memory_model_t model);
137139

138140
/**
139141
* @brief Find a process by PID.
@@ -321,4 +323,4 @@ process_t* proc_load_anonymous(const char* source, pid_t parent_pid, const char*
321323
*/
322324
uint32_t proc_cpu_percent(pid_t pid);
323325

324-
bool is_basic(const char* buf, size_t size);
326+
bool is_basic(const char* buf, size_t size);

src/basic/main.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ bool basic_hash_lines(struct basic_ctx *ctx, char **error) {
265265
return true;
266266
}
267267

268-
struct basic_ctx *basic_init(const char *program, uint32_t pid, const char *file, char **error) {
268+
struct basic_ctx *basic_init(const char *program, uint32_t pid, const char *file, char **error, enum memory_model_t model) {
269269
build_keyword_prefix_offsets();
270270
build_builtin_fn_maps();
271271
if (!isdigit(*program)) {
@@ -275,7 +275,7 @@ struct basic_ctx *basic_init(const char *program, uint32_t pid, const char *file
275275
*error = "Out of memory";
276276
return NULL;
277277
}
278-
struct basic_ctx *c = basic_init(numbered, pid, file, error);
278+
struct basic_ctx *c = basic_init(numbered, pid, file, error, model);
279279
kfree_null(&numbered);
280280
return c;
281281
}
@@ -292,7 +292,7 @@ struct basic_ctx *basic_init(const char *program, uint32_t pid, const char *file
292292
*error = "Out of memory";
293293
return NULL;
294294
}
295-
buddy_init(ctx->allocator, 6, 26, 26);
295+
buddy_init(ctx->allocator, 6, (int)model, (int)model);
296296
ctx->maps = hashmap_new_with_allocator(varmap_malloc, varmap_realloc, varmap_free, sizeof(basic_map_handle_entry), 0, SEED0, SEED1, int64_hash, int64_compare, elfree_map_handle_entry, ctx->allocator);
297297
ctx->active_restrictions = NULL;
298298
ctx->child_restrictions = NULL;
@@ -669,13 +669,49 @@ void chain_statement(struct basic_ctx *ctx) {
669669
uint32_t cpu = logical_cpu_id();
670670
process_t *proc = proc_cur(cpu);
671671
accept_or_return(CHAIN, ctx);
672+
672673
const char *pn = str_expr(ctx);
673-
process_t *p = proc_load(pn, proc->pid, proc->csd);
674+
674675
bool background = false;
676+
memory_model_t memory_model = mm_medium;
677+
678+
/* Optional: , TRUE|FALSE */
675679
if (tokenizer_token(ctx) == COMMA) {
676680
tokenizer_next(ctx);
677681
background = expr(ctx);
678682
}
683+
684+
/* Optional: MEMORY SMALL|MEDIUM|LARGE|HUGE */
685+
if (tokenizer_token(ctx) == MEMORY) {
686+
tokenizer_next(ctx);
687+
688+
switch (tokenizer_token(ctx)) {
689+
case SMALL:
690+
memory_model = mm_small;
691+
break;
692+
693+
case MEDIUM:
694+
memory_model = mm_medium;
695+
break;
696+
697+
case LARGE:
698+
memory_model = mm_large;
699+
break;
700+
701+
case HUGE:
702+
memory_model = mm_huge;
703+
break;
704+
705+
default:
706+
tokenizer_error_print(ctx, "Expected SMALL, MEDIUM, LARGE or HUGE");
707+
accept_or_return(NEWLINE, ctx);
708+
return;
709+
}
710+
711+
tokenizer_next(ctx);
712+
}
713+
714+
process_t *p = proc_load(pn, proc->pid, proc->csd, memory_model);
679715
if (p == NULL) {
680716
accept_or_return(NEWLINE, ctx);
681717
return;
@@ -725,6 +761,7 @@ void chain_statement(struct basic_ctx *ctx) {
725761
if (!background) {
726762
proc_wait(proc, p->pid);
727763
}
764+
728765
accept_or_return(NEWLINE, ctx);
729766
}
730767

src/taskswitch.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ bool is_basic(const char* buf, size_t size) {
108108
return false;
109109
}
110110

111-
static process_t* proc_create_common(const char* source, pid_t parent_pid, const char* csd, const char* program_name, const char* directory, size_t size)
111+
static process_t* proc_create_common(const char* source, pid_t parent_pid, const char* csd, const char* program_name, const char* directory, size_t size, enum memory_model_t model)
112112
{
113113
char* error = "Unknown error";
114114

@@ -126,7 +126,7 @@ static process_t* proc_create_common(const char* source, pid_t parent_pid, const
126126
return NULL;
127127
}
128128

129-
newproc->code = basic_init(source, nextid, directory, &error);
129+
newproc->code = basic_init(source, nextid, directory, &error, model);
130130
if (!newproc->code) {
131131
kfree_null(&newproc);
132132
kprintf("Fatal error parsing program: %s\n", error);
@@ -204,7 +204,7 @@ static process_t* proc_create_common(const char* source, pid_t parent_pid, const
204204
return newproc;
205205
}
206206

207-
process_t* proc_load(const char* fullpath, pid_t parent_pid, const char* csd)
207+
process_t* proc_load(const char* fullpath, pid_t parent_pid, const char* csd, enum memory_model_t model)
208208
{
209209
fs_directory_entry_t* fsi = fs_get_file_info(fullpath);
210210
if (fsi == NULL || (fsi->flags & FS_DIRECTORY)) {
@@ -229,7 +229,7 @@ process_t* proc_load(const char* fullpath, pid_t parent_pid, const char* csd)
229229
return NULL;
230230
}
231231

232-
process_t* newproc = proc_create_common((const char*)programtext, parent_pid, csd, fsi->filename, fullpath, fsi->size);
232+
process_t* newproc = proc_create_common((const char*)programtext, parent_pid, csd, fsi->filename, fullpath, fsi->size, model);
233233
kfree_null(&programtext);
234234
return newproc;
235235
}
@@ -239,7 +239,7 @@ process_t* proc_load_anonymous(const char* source, pid_t parent_pid, const char*
239239
const char* name = "<anonymous>";
240240
const char* directory = "<eval>";
241241

242-
return proc_create_common(source, parent_pid, csd, name, directory, strlen(source));
242+
return proc_create_common(source, parent_pid, csd, name, directory, strlen(source), mm_medium);
243243
}
244244

245245
process_t* proc_cur(uint8_t logical_cpu)
@@ -499,7 +499,7 @@ void init_process()
499499
}
500500

501501
dprintf("Spawning " INIT_PROGRAM "\n");
502-
process_t* init = proc_load(INIT_PROGRAM, 0, "/");
502+
process_t* init = proc_load(INIT_PROGRAM, 0, "/", mm_medium);
503503
if (!init) {
504504
preboot_fail(INIT_PROGRAM " missing or invalid!\n");
505505
}

0 commit comments

Comments
 (0)