CLDC 8: add java.lang.Runtime

This commit is contained in:
Zack Buhman 2025-01-13 23:19:55 -06:00
parent 27584e8aa2
commit bb128578f0
9 changed files with 115 additions and 2 deletions

View File

@ -142,6 +142,21 @@ void memory_iterate_allocated(memory_iterate_func_t func)
}
}
int32_t memory_count_free_memory()
{
int sum = 0;
for (int i = 0; i < free_list_length; i++) {
if (free_list[i] == 0)
sum += 1;
}
return sum * block_size;
}
int32_t memory_count_total_memory()
{
return (sizeof (memory));
}
#if 0
int main()
{

View File

@ -8,3 +8,5 @@ void memory_free(void * p);
bool memory_is_allocated(void * p);
typedef void (* memory_iterate_func_t)(void * address);
void memory_iterate_allocated(memory_iterate_func_t func);
int32_t memory_count_free_memory();
int32_t memory_count_total_memory();

View File

@ -9,6 +9,7 @@
#include "native/memory.h"
#include "native/object.h"
#include "native/printstream.h"
#include "native/runtime.h"
typedef void (* native_func_t)(struct vm * vm, uint32_t * args);
@ -134,6 +135,24 @@ const static struct native_method native_method[] = {
.method_descriptor = "()I",
.func = native_java_lang_object_hashcode_1,
},
{
.class_name = "java/lang/Runtime",
.method_name = "_freeMemory",
.method_descriptor = "()I",
.func = native_java_lang_runtime_freememory_0,
},
{
.class_name = "java/lang/Runtime",
.method_name = "_gc",
.method_descriptor = "()V",
.func = native_java_lang_runtime_gc_0,
},
{
.class_name = "java/lang/Runtime",
.method_name = "_totalMemory",
.method_descriptor = "()I",
.func = native_java_lang_runtime_totalmemory_0,
},
};
struct hash_table_entry * native_init_hash_table(int * hash_table_length)

20
c/native/runtime.c Normal file
View File

@ -0,0 +1,20 @@
#include "runtime.h"
#include "memory_allocator.h"
#include "gc.h"
void native_java_lang_runtime_freememory_0(struct vm * vm, uint32_t * args)
{
int32_t count = memory_count_free_memory();
operand_stack_push_u32(vm->current_frame, count);
}
void native_java_lang_runtime_gc_0(struct vm * vm, uint32_t * args)
{
gc_run(vm);
}
void native_java_lang_runtime_totalmemory_0(struct vm * vm, uint32_t * args)
{
int32_t count = memory_count_total_memory();
operand_stack_push_u32(vm->current_frame, count);
}

9
c/native/runtime.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <stdint.h>
#include "frame.h"
void native_java_lang_runtime_freememory_0(struct vm * vm, uint32_t * args);
void native_java_lang_runtime_gc_0(struct vm * vm, uint32_t * args);
void native_java_lang_runtime_totalmemory_0(struct vm * vm, uint32_t * args);

View File

@ -2,7 +2,6 @@ package java.lang;
import java.lang.DecimalDigits;
public final class Integer
extends Number
implements Comparable<Integer> {

View File

@ -2,7 +2,17 @@ package java.lang;
import java.lang.DecimalDigits;
public class Long {
public final class Long
// extends Number
// implements Comparable<Long> {
{
public static final long MAX_VALUE = 9223372036854775807L;
public static final long MIN_VALUE = -9223372036854775808L;
public static final int SIZE = 64;
public static int stringSize(long n) {
int sign_digits = 0;
if (n < 0) {

View File

@ -0,0 +1,38 @@
package java.lang;
public class Runtime {
private static final Runtime runtime;
private Runtime() {
}
static {
runtime = new Runtime();
}
private static native int _freeMemory();
public long freeMemory() {
return _freeMemory();
}
private static native void _gc();
public void gc() {
_gc();
}
public static Runtime getRuntime() {
return runtime;
}
public long maxMemory() {
return Long.MAX_VALUE;
}
private static native int _totalMemory();
public long totalMemory() {
return _totalMemory();
}
}

View File

@ -27,6 +27,7 @@ OBJ = \
c/native/memory.o \
c/native/object.o \
c/native/printstream.o \
c/native/runtime.o \
c/parse.o \
c/parse_type.o \
c/printf.o \