找回密码
 立即注册
首页 业界区 业界 一种优秀的虚拟机内存架构 - AQ

一种优秀的虚拟机内存架构 - AQ

氛疵 2025-6-5 19:49:31
源链接:https://www.axa6.com/zh/an-excellent-virtual-machine-memory-architecture
简介

虚拟机内存架构直接影响虚拟机的性能和占用。设计一个优秀的架构可以有效提升性能和效率。

本文将介绍AQ虚拟机使用的内存架构,以及AQ虚拟机内存的详细标准。

通过对于虚拟机内存架构的优化,有助于虚拟机的运行效率减少占用。如果可以,应该尽可能地平衡两者,使虚拟机达到最佳状态。

在某些情况下,应该根据虚拟机的特殊需求进行不同的开发。

例如:在单片机等内存受限情况下,需要尽可能地减少占用

而在并行计算等性能敏感情况,则需要侧重于性能优化

设计思路

内存架构

基础内存架构

AQ采取了寄存器的基础内存架构,但与标准的寄存器架构有所不同,对寄存器架构进行了部分改进和优化。

此处的寄存器并非CPU中的寄存器,而是在内存中模拟出的虚拟寄存器。

选择寄存器的原因

相较与JAVA、Python等主流语言虚拟机采取堆栈架构不同,AQ决定采取寄存器架构的原因是性能的优化与字节码的容易理解。

虽然堆栈架构被普遍认为更容易移植和编写,但在实际的性能中会有一些损耗,对于内存的多次访问会减缓其速度,这是不可避免并且难以彻底优化的。因此,为了解决此处的性能损耗,AQ采用了寄存器架构。同时,从字节码的角度上说,寄存器架构的字节码更容易理解,其指令类似于函数的参数方式,而不是直接面对堆栈的众多操作。

寄存器架构的区别

标准的寄存器架构

标准的寄存器架构中,寄存器包含:


  • 数据类型 - 寄存器将存储的数据的类型(如int、float、double等)
  • 数据 - 寄存器将存储的数据的值
  • (可选)标记 - 寄存器将存储的数据的标记(如变量、函数、类等)
  • (可选)引用 - 寄存器将存储的数据的引用(如对象的地址等)
尽管不同语言的虚拟机内存架构可能有所不同,但大致都存储了这些信息。

而在AQ开发过程中曾使用了该架构,但是经过测试,其存在较大的内存占用。

以下是AQ曾使用的register.h代码:
  1. // Copyright 2024 AQ authors, All Rights Reserved.
  2. // This program is licensed under the AQ License. You can find the AQ license in
  3. // the root directory.
  4. #ifndef AQ_AQVM_MEMORY_REGISTER_H_
  5. #define AQ_AQVM_MEMORY_REGISTER_H_
  6. #include <stdbool.h>
  7. enum AqvmMemoryRegister_ValueType {
  8.   // TODO(Register): Waiting for the improvement of the register.
  9.   AqvmMemoryRegisterValueType_INT,
  10.   AqvmMemoryRegisterValueType_CONSTINT,
  11.   AqvmMemoryRegisterValueType_FLOAT,
  12.   AqvmMemoryRegisterValueType_CONSTFLOAT,
  13.   AqvmMemoryRegisterValueType_DOUBLE,
  14.   AqvmMemoryRegisterValueType_CONSTDOUBLE,
  15.   AqvmMemoryRegisterValueType_LONG,
  16.   AqvmMemoryRegisterValueType_CONSTLONG,
  17.   AqvmMemoryRegisterValueType_CHARACTER,
  18.   AqvmMemoryRegisterValueType_CONSTCHARACTER,
  19.   AqvmMemoryRegisterValueType_BOOLEAN,
  20.   AqvmMemoryRegisterValueType_CONSTBOOLEAN
  21. };
  22. union AqvmMemoryRegister_Value {
  23.   // TODO(Register): Waiting for the improvement of the register.
  24.   int int_value;
  25.   const int const_int_value;
  26.   float float_value;
  27.   const float const_float_value;
  28.   double double_value;
  29.   const double const_double_value;
  30.   long long_value;
  31.   const long const_long_value;
  32.   char character_value;
  33.   const char const_character_value;
  34.   bool boolean_value;
  35.   const bool const_boolean_value;
  36. };
  37. struct AqvmMemoryRegister_Register {
  38.   enum AqvmMemoryRegister_ValueType type;
  39.   union AqvmMemoryRegister_Value value;
  40. };
  41. #endif
复制代码
从上述代码可以看出,即使仅保留了必要内容,但由于enum类型的AqvmMemoryRegister_ValueType占用4字节,union类型的AqvmMemoryRegister_Value占用8字节,struct类型本身就会占用12字节内存。

同时,由于C编译器的优化,struct类型的AqvmMemoryRegister_Register中enum类型的type为与union类型的value进行内存对齐,因此加入4字节的填充内存。使struct类型的AqvmMemoryRegister_Register占用16字节。

其中如果使用int等非8字节类型,则会有4字节的填充内存被浪费,从而造成内存损耗。因此在全部的寄存器中会有4-8字节的内存浪费。

AQ的寄存器架构

为了解决传统寄存器架构的占用问题,AQ结合了JVM的栈帧的局部变量表特点,对内存架构进行了优化,使占用问题显著减少。

以下是备选的三种方案:
  1. // plan 1:
  2. struct AqvmMemoryRegister_Register{
  3.   uint8_t type;
  4.   void* value_ptr;
  5. };
  6. void* value;
  7. AqvmMemoryRegister_Register array[];
  8. // plan 2:
  9. void* value;
  10. // value to the memory address of index 0 is int, the index 0 to the index 1 is
  11. // float, etc.
  12. size_t type[];
  13. // plan 3:
  14. struct AqvmMemoryRegister_Register {
  15.   uint32_t* value;
  16.   size_t size;
  17. };
复制代码
由于指针占用4-8字节,数据本身占用1-8字节,加上类型1字节,因此plan 1占用6-17字节,同时可能会存在内存对齐,因此plan 1同样会造成极大的内存损失。

事实上,在要求保留内存类型信息时,内存利用率最高的是plan 2,但plan 2不能保存在同一数据结构(如:结构体)中不同类型数据的连贯性,可能会使部分指针操作失效。因此为了内存安全,不使用plan 2。

在某些情况下(虚拟机指令集包括类型),plan 3也可以满足内存存储的需要,但由于精简指令集的需要,没有在指令中包含类型信息,因此无法满足虚拟机运行需要。

因此我们采取如下设计,保证对于内存的利用率,同时使内存占用问题有了很大改善。

AQ的内存直接使用void*指针存储数据,size_t存储占用内存大小,并且使用uint8_t数组存储类型。由于uint8_t占用8位,为减少占用,每个字节使用4位来存储类型。因此,一个uint8_t变量可以存储2个类型。每个uint8_t变量的前4位用于偶数字节的类型,后4位用于奇数字节的类型。
  1. // The struct stores information about the memory.
  2. // |type| is a pointer to an array that stores the type of each byte in the
  3. // memory. Each byte uses 4 bits to store the type. So a uint8_t variable can
  4. // store 2 types. Each uint8_t variable's first 4 bits are used for the even
  5. // byte's type and the next 4 bits are used for the odd byte's type. The type
  6. // list is in types.h.
  7. // |data| is a pointer of type void* to the memory that stores the data.
  8. // |size| is the size of the memory.
  9. // NOTICE: The struct AqvmMemory_Memory only stores information of the memory.
  10. // The memory is allocated by the bytecode function when storing the bytecode.
  11. // The memory of |memory| and |type| is part of the bytecode memory.
  12. struct AqvmMemory_Memory {
  13.   uint8_t* type;
  14.   void* data;
  15.   size_t size;
  16. };
复制代码
由于内存的原因,对于type的存取需要精确的利用。uint8_t类型需要8位,但是超过了类型的存储需要,因此4位既可以满足对于类型的存储需要,同时又可以减少内存占用。但是需要特殊的函数维持type的存取。
  1. // Sets the type of the data at |index| bytes in |memory| to |type|. |type|
  2. // should be less than 4 bits.
  3. // Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2
  4. // if the type pointer is NULL. Returns -3 if the index is out of range. Returns
  5. // -4 if the type is out of range.
  6. int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,
  7.                        uint8_t type) {
  8.   if (memory == NULL) {
  9.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  10.                                      ""AqvmMemory_SetType_NullMemoryPointer"",
  11.                                      ""The memory pointer is NULL."", NULL);
  12.     return -1;
  13.   }
  14.   if (memory->type == NULL) {
  15.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  16.                                      ""AqvmMemory_SetType_NullTypePointer"",
  17.                                      ""The type pointer is NULL."", NULL);
  18.     return -2;
  19.   }
  20.   if (index > memory->size) {
  21.     AqvmRuntimeDebugger_OutputReport(
  22.         ""ERROR"", ""AqvmMemory_SetType_OutOfMemoryRange"",
  23.         ""The index is out of memory range."", NULL);
  24.     return -3;
  25.   }
  26.   if (type > 0x0F) {
  27.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  28.                                      ""AqvmMemory_SetType_OutOfTypeRange"",
  29.                                      ""The type is out of range."", NULL);
  30.     return -4;
  31.   }
  32.   // Sets the type of the data at |index| bytes in memory.
  33.   // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  34.   // each uint8_t type location stores two type data. The storage locations
  35.   // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  36.   // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  37.   // stored in the low bits of (|index| / 2).
  38.   if (index % 2 != 0) {
  39.     memory->type[index / 2] = (memory->type[index / 2] & 0xF0) | type;
  40.   } else {
  41.     memory->type[index / 2] = (memory->type[index / 2] & 0x0F) | (type << 4);
  42.   }
  43.   return 0;
  44. }
  45. // Gets the type of the data at |index| bytes in |memory|.
  46. // Returns the type that is less than 4 bits (0X0F) if successful. Returns 0x11
  47. // if the memory pointer is NULL. Returns 0x12 if the type pointer is NULL.
  48. // Returns 0x13 if the index is out of memory range.
  49. uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) {
  50.   if (memory == NULL) {
  51.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  52.                                      ""AqvmMemory_GetType_NullMemoryPointer"",
  53.                                      ""The memory pointer is NULL."", NULL);
  54.     return 0x11;
  55.   }
  56.   if (memory->type == NULL) {
  57.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  58.                                      ""AqvmMemory_GetType_NullTypePointer"",
  59.                                      ""The type pointer is NULL."", NULL);
  60.     return 0x12;
  61.   }
  62.   if (index > memory->size) {
  63.     AqvmRuntimeDebugger_OutputReport(
  64.         ""ERROR"", ""AqvmMemory_GetType_OutOfMemoryRange"",
  65.         ""The index is out of memory range."", NULL);
  66.     return 0x13;
  67.   }
  68.   // Gets the type of the data at |index| bytes in memory.
  69.   // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  70.   // each uint8_t type location stores two type data. The storage locations
  71.   // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  72.   // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  73.   // stored in the low bits of (|index| / 2).
  74.   if (index % 2 != 0) {
  75.     return memory->type[index / 2] & 0x0F;
  76.   } else {
  77.     return (memory->type[index / 2] & 0xF0) >> 4;
  78.   }
  79. }
复制代码
但使用该设计对于数据的存储有较高要求,因为数据的长度不固定,因此需要专门的函数配合内存进行操作。
  1. // Writes the data that |data_ptr| points to of size |size| to the data of at
  2. // |index| bytes in |memory|.
  3. // Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2
  4. // if the type pointer is NULL. Returns -3 if the index is out of range. Returns
  5. // -4 if the data pointer is NULL.
  6. int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index,
  7.                          void* data_ptr, size_t size) {
  8.   if (memory == NULL) {
  9.     AqvmRuntimeDebugger_OutputReport(
  10.         ""ERROR"", ""AqvmMemory_WriteData_NullMemoryPointer"",
  11.         ""The memory pointer is NULL."", NULL);
  12.     return -1;
  13.   }
  14.   if (memory->type == NULL) {
  15.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  16.                                      ""AqvmMemory_WriteData_NullTypePointer"",
  17.                                      ""The type pointer is NULL."", NULL);
  18.     return -2;
  19.   }
  20.   if (index > memory->size) {
  21.     AqvmRuntimeDebugger_OutputReport(
  22.         ""ERROR"", ""AqvmMemory_WriteData_OutOfMemoryRange"",
  23.         ""The index is out of memory range."", NULL);
  24.     return -3;
  25.   }
  26.   if (data_ptr == NULL) {
  27.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  28.                                      ""AqvmMemory_WriteData_NullDataPointer"",
  29.                                      ""The data pointer is NULL."", NULL);
  30.     return -4;
  31.   }
  32.   // Since void* does not have a specific size, pointer moves need to be
  33.   // converted before moving.
  34.   memcpy((void*)((uintptr_t)memory->data + index), data_ptr, size);
  35.   return 0;
  36. }
复制代码
除了减少内存使用外,避免内存的二次占用同样重要。因此我们复用字节码的内存,将内存数据和类型存储在字节码的内存部分中,利用字节码文件中预先分配的内存(字节码文件中包含内存的数据和类型),实现对于内存的高效利用。

因为如果单独存储两部分,则需要有两部分重复的内存数据和类型,一份在内存部分,而另一份,字节码部分则不会被使用,因此我们采取了复用的方法,减少了因内存数据和类型而造成的内存浪费。

但因此需要特殊的函数实现,同时需要注意内存数据和类型的内存的分配和释放由字节码的相关函数进行管理。
  1. // Creates the struct AqvmMemory_Memory with |data|, |type|, and |size|.
  2. // The function will allocate a struct AqvmMemory_Memory and copy |data|,
  3. // |type|, and |size| into the struct. Returns a pointer to the struct if
  4. // successful. Returns NULL if creation fails.
  5. struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type,
  6.                                                   size_t size) {
  7.   struct AqvmMemory_Memory* memory_ptr =
  8.       (struct AqvmMemory_Memory*)malloc(sizeof(struct AqvmMemory_Memory));
  9.   if (memory_ptr == NULL) {
  10.     AqvmRuntimeDebugger_OutputReport(
  11.         ""ERROR"", ""AqvmMemory_CreateMemory_MemoryAllocationFailure"",
  12.         ""Failed to allocate memory."", NULL);
  13.     return NULL;
  14.   }
  15.   memory_ptr->data = data;
  16.   memory_ptr->type = type;
  17.   memory_ptr->size = size;
  18.   return memory_ptr;
  19. }
  20. // Free the memory of the |memory_ptr|. No return.
  21. // NOTICE: The function only free the memory of the struct. The memory pointed
  22. // to by pointers to data and type in struct is not freed. This memory is
  23. // managed by bytecode related functions.
  24. void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr) {
  25.   free(memory_ptr);
  26. }
复制代码
除此之外,由于部分系统对于类型的定义与AQ标准有所差异,因此设计了相关函数确保虚拟机符合标准。如果系统与标准存在差异,应当为这些系统进行特殊的设计。
  1. // Checks the memory conditions in the system.
  2. // Returns the number of warnings.
  3. int AqvmMemory_CheckMemoryConditions() {
  4.   int warning_count = 0;
  5.   if (sizeof(aqint) != 4) {
  6.     AqvmRuntimeDebugger_OutputReport(
  7.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_IntLengthWarning"",
  8.         ""The length requirement for the int type does not conform to the "
  9.         "type "
  10.         "definition."",
  11.         NULL);
  12.     ++warning_count;
  13.   }
  14.   if (sizeof(aqlong) != 8) {
  15.     AqvmRuntimeDebugger_OutputReport(
  16.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_LongLengthWarning"",
  17.         ""The length requirement for the long type does not conform to the "
  18.         "type "
  19.         "definition."",
  20.         NULL);
  21.     ++warning_count;
  22.   }
  23.   if (sizeof(aqfloat) != 4) {
  24.     AqvmRuntimeDebugger_OutputReport(
  25.         ""WARNING"",
  26.         ""AqvmMemory_CheckMemoryConditions_FloatLengthWarning"",
  27.         ""The length requirement for the float type does not conform to the "
  28.         "type definition."",
  29.         NULL);
  30.     ++warning_count;
  31.   }
  32.   if (sizeof(aqdouble) != 4) {
  33.     AqvmRuntimeDebugger_OutputReport(
  34.         ""WARNING"",
  35.         ""AqvmMemory_CheckMemoryConditions_DoubleLengthWarning"",
  36.         ""The length requirement for the double type does not conform to the "
  37.         "type definition."",
  38.         NULL);
  39.     ++warning_count;
  40.   }
  41.   if (sizeof(aqchar) != 1) {
  42.     AqvmRuntimeDebugger_OutputReport(
  43.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_CharLengthWarning"",
  44.         ""The length requirement for the char type does not conform to the "
  45.         "type "
  46.         "definition."",
  47.         NULL);
  48.     ++warning_count;
  49.   }
  50.   if (sizeof(aqbool) != 1) {
  51.     AqvmRuntimeDebugger_OutputReport(
  52.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_BoolLengthWarning"",
  53.         "The length requirement for the bool type does not conform to the type "
  54.         "definition.",
  55.         NULL);
  56.     ++warning_count;
  57.   }
  58.   if (warning_count == 0) {
  59.     AqvmRuntimeDebugger_OutputReport(""INFO"",
  60.                                      ""AqvmMemory_CheckMemoryConditions_CheckNormal"",
  61.                                      ""No memory conditions warning."", NULL);
  62.   }
  63.   return warning_count;
  64. }
复制代码
详细标准:

目录结构

memory部分的代码位于/aqvm/memory。内含多个代码文件。


  • CMakeLists.txt - 该目录下的CMake构建文件
  • memory.h - 内存的数据结构和相关函数
  • memory.c - 内存的相关函数的实现
  • types.h - 内存类型的定义
memory.h

AqvmMemory_Memory

该结构体存储有关内存的信息。

|type| 是一个指向数组的指针,该数组存储内存中每个字节的类型。每个字节使用4位来存储类型。因此,一个 uint8_t 变量可以存储2个类型。每个 uint8_t 变量的前4位用于偶数字节的类型,后4位用于奇数字节的类型。类型列表在 types.h 中。

|data| 是一个指向存储数据的内存的 void* 类型的指针。

|size| 是内存的大小。

注意:结构体 AqvmMemory_Memory 仅存储内存的信息。内存由存储字节码时的字节码函数分配。|memory| 和 |type| 的内存是字节码内存的一部分。
  1. struct AqvmMemory_Memory {
  2.   uint8_t* type;
  3.   void* data;
  4.   size_t size;
  5. };
复制代码
AqvmMemory_CheckMemoryConditions

检查系统中的内存条件。

返回警告数量。
  1. int AqvmMemory_CheckMemoryConditions() {
  2.   int warning_count = 0;
  3.   if (sizeof(aqint) != 4) {
  4.     AqvmRuntimeDebugger_OutputReport(
  5.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_IntLengthWarning"",
  6.         ""The length requirement for the int type does not conform to the "
  7.         "type "
  8.         "definition."",
  9.         NULL);
  10.     ++warning_count;
  11.   }
  12.   if (sizeof(aqlong) != 8) {
  13.     AqvmRuntimeDebugger_OutputReport(
  14.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_LongLengthWarning"",
  15.         ""The length requirement for the long type does not conform to the "
  16.         "type "
  17.         "definition."",
  18.         NULL);
  19.     ++warning_count;
  20.   }
  21.   if (sizeof(aqfloat) != 4) {
  22.     AqvmRuntimeDebugger_OutputReport(
  23.         ""WARNING"",
  24.         ""AqvmMemory_CheckMemoryConditions_FloatLengthWarning"",
  25.         ""The length requirement for the float type does not conform to the "
  26.         "type definition."",
  27.         NULL);
  28.     ++warning_count;
  29.   }
  30.   if (sizeof(aqdouble) != 4) {
  31.     AqvmRuntimeDebugger_OutputReport(
  32.         ""WARNING"",
  33.         ""AqvmMemory_CheckMemoryConditions_DoubleLengthWarning"",
  34.         ""The length requirement for the double type does not conform to the "
  35.         "type definition."",
  36.         NULL);
  37.     ++warning_count;
  38.   }
  39.   if (sizeof(aqchar) != 1) {
  40.     AqvmRuntimeDebugger_OutputReport(
  41.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_CharLengthWarning"",
  42.         ""The length requirement for the char type does not conform to the "
  43.         "type "
  44.         "definition."",
  45.         NULL);
  46.     ++warning_count;
  47.   }
  48.   if (sizeof(aqbool) != 1) {
  49.     AqvmRuntimeDebugger_OutputReport(
  50.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_BoolLengthWarning"",
  51.         "The length requirement for the bool type does not conform to the type "
  52.         "definition.",
  53.         NULL);
  54.     ++warning_count;
  55.   }
  56.   if (warning_count == 0) {
  57.     AqvmRuntimeDebugger_OutputReport(""INFO"",
  58.                                      ""AqvmMemory_CheckMemoryConditions_CheckNormal"",
  59.                                      ""No memory conditions warning."", NULL);
  60.   }
  61.   return warning_count;
  62. }
复制代码
AqvmMemory_CreateMemory

创建包含 |data|、|type| 和 |size| 的结构体 AqvmMemory_Memory。

该函数将分配一个 AqvmMemory_Memory 结构体,并将 |data|、|type| 和 |size| 复制到结构体中。返回指向该结构体的指针。如果创建失败则返回NULL。
  1. struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type,
  2.                                                   size_t size) {
  3.   struct AqvmMemory_Memory* memory_ptr =
  4.       (struct AqvmMemory_Memory*)malloc(sizeof(struct AqvmMemory_Memory));
  5.   if (memory_ptr == NULL) {
  6.     AqvmRuntimeDebugger_OutputReport(
  7.         ""ERROR"", ""AqvmMemory_CreateMemory_MemoryAllocationFailure"",
  8.         ""Failed to allocate memory."", NULL);
  9.     return NULL;
  10.   }
  11.   memory_ptr->data = data;
  12.   memory_ptr->type = type;
  13.   memory_ptr->size = size;
  14.   return memory_ptr;
  15. }
复制代码
AqvmMemory_FreeMemory

释放 |memory_ptr| 的内存。无返回值。

注意:该函数仅释放结构体的内存。结构体中指向数据和类型的指针所指向的内存不会被释放。这些内存由字节码相关函数管理。
  1. void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr) {
  2.   free(memory_ptr);
  3. }
复制代码
AqvmMemory_SetType

设置 |memory| 中 |index| 字节处的数据类型为 |type|。|type| 应小于 4 位。

成功时返回 0。如果内存指针为 NULL,返回 -1。如果索引指针为 NULL,返回 -2。如果索引超出范围,返回 -3。如果类型超出范围,返回 -4。
  1. int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,
  2.                        uint8_t type) {
  3.   if (memory == NULL) {
  4.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  5.                                      ""AqvmMemory_SetType_NullMemoryPointer"",
  6.                                      ""The memory pointer is NULL."", NULL);
  7.     return -1;
  8.   }
  9.   if (memory->type == NULL) {
  10.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  11.                                      ""AqvmMemory_SetType_NullTypePointer"",
  12.                                      ""The type pointer is NULL."", NULL);
  13.     return -2;
  14.   }
  15.   if (index > memory->size) {
  16.     AqvmRuntimeDebugger_OutputReport(
  17.         ""ERROR"", ""AqvmMemory_SetType_OutOfMemoryRange"",
  18.         ""The index is out of memory range."", NULL);
  19.     return -3;
  20.   }
  21.   if (type > 0x0F) {
  22.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  23.                                      ""AqvmMemory_SetType_OutOfTypeRange"",
  24.                                      ""The type is out of range."", NULL);
  25.     return -4;
  26.   }
  27.   // Sets the type of the data at |index| bytes in memory.
  28.   // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  29.   // each uint8_t type location stores two type data. The storage locations
  30.   // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  31.   // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  32.   // stored in the low bits of (|index| / 2).
  33.   if (index % 2 != 0) {
  34.     memory->type[index / 2] = (memory->type[index / 2] & 0xF0) | type;
  35.   } else {
  36.     memory->type[index / 2] = (memory->type[index / 2] & 0x0F) | (type << 4);
  37.   }
  38.   return 0;
  39. }
复制代码
AqvmMemory_WriteData

将 |data_ptr| 指向的大小为 |size| 的数据写入 |memory| 中 |index| 字节处的数据。

成功时返回 0。如果内存指针为 NULL,返回 -1。如果索引指针为 NULL,返回 -2。如果索引超出内存范围,返回 -3。如果数据指针为 NULL,返回 -4。
  1. uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) {
  2.   if (memory == NULL) {
  3.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  4.                                      ""AqvmMemory_GetType_NullMemoryPointer"",
  5.                                      ""The memory pointer is NULL."", NULL);
  6.     return 0x11;
  7.   }
  8.   if (memory->type == NULL) {
  9.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  10.                                      ""AqvmMemory_GetType_NullTypePointer"",
  11.                                      ""The type pointer is NULL."", NULL);
  12.     return 0x12;
  13.   }
  14.   if (index > memory->size) {
  15.     AqvmRuntimeDebugger_OutputReport(
  16.         ""ERROR"", ""AqvmMemory_GetType_OutOfMemoryRange"",
  17.         ""The index is out of memory range."", NULL);
  18.     return 0x13;
  19.   }
  20.   // Gets the type of the data at |index| bytes in memory.
  21.   // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  22.   // each uint8_t type location stores two type data. The storage locations
  23.   // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  24.   // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  25.   // stored in the low bits of (|index| / 2).
  26.   if (index % 2 != 0) {
  27.     return memory->type[index / 2] & 0x0F;
  28.   } else {
  29.     return (memory->type[index / 2] & 0xF0) >> 4;
  30.   }
  31. }
复制代码
memory.h完整代码:
  1. // Copyright 2024 AQ author, All Rights Reserved.// This program is licensed under the AQ License. You can find the AQ license in// the root directory.#ifndef AQ_AQVM_MEMORY_MEMORY_H_#define AQ_AQVM_MEMORY_MEMORY_H_#include #include #include "aqvm/memory/types.h"// The struct stores information about the memory.
  2. // |type| is a pointer to an array that stores the type of each byte in the
  3. // memory. Each byte uses 4 bits to store the type. So a uint8_t variable can
  4. // store 2 types. Each uint8_t variable's first 4 bits are used for the even
  5. // byte's type and the next 4 bits are used for the odd byte's type. The type
  6. // list is in types.h.
  7. // |data| is a pointer of type void* to the memory that stores the data.
  8. // |size| is the size of the memory.
  9. // NOTICE: The struct AqvmMemory_Memory only stores information of the memory.
  10. // The memory is allocated by the bytecode function when storing the bytecode.
  11. // The memory of |memory| and |type| is part of the bytecode memory.
  12. struct AqvmMemory_Memory {
  13.   uint8_t* type;
  14.   void* data;
  15.   size_t size;
  16. };// Checks the memory conditions in the system.// Returns the number of warnings.int AqvmMemory_CheckMemoryConditions();// Creates the struct AqvmMemory_Memory with |data|, |type|, and |size|.// The function will allocate a struct AqvmMemory_Memory and copy |data|,// |type|, and |size| into the struct. Returns a pointer to the struct if// successful. Returns NULL if creation fails.struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type,                                                  size_t size);// Free the memory of the |memory_ptr|. No return.// NOTICE: The function only free the memory of the struct. The memory pointed// to by pointers to data and type in struct is not freed. This memory is// managed by bytecode related functions.void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr);// Sets the type of the data at |index| bytes in |memory| to |type|. |type|// should be less than 4 bits.// Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2// if the type pointer is NULL. Returns -3 if the index is out of range. Returns// -4 if the type is out of range.int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,                       uint8_t type);// Gets the type of the data at |index| bytes in |memory|.// Returns the type that is less than 4 bits (0X0F) if successful. Returns 0x11// if the memory pointer is NULL. Returns 0x12 if the type pointer is NULL.// Returns 0x13 if the index is out of memory range.uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index);// Writes the data that |data_ptr| points to of size |size| to the data of at// |index| bytes in |memory|.// Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2// if the type pointer is NULL. Returns -3 if the index is out of range. Returns// -4 if the data pointer is NULL.int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index,                         void* data_ptr, size_t size);#endif
复制代码
memory.c

memory.c完整代码:
  1. // Copyright 2024 AQ author, All Rights Reserved.// This program is licensed under the AQ License. You can find the AQ license in// the root directory.#include "aqvm/memory/memory.h"#include #include #include #include #include "aqvm/memory/types.h"#include "aqvm/runtime/debugger/debugger.h"int AqvmMemory_CheckMemoryConditions() {
  2.   int warning_count = 0;
  3.   if (sizeof(aqint) != 4) {
  4.     AqvmRuntimeDebugger_OutputReport(
  5.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_IntLengthWarning"",
  6.         ""The length requirement for the int type does not conform to the "
  7.         "type "
  8.         "definition."",
  9.         NULL);
  10.     ++warning_count;
  11.   }
  12.   if (sizeof(aqlong) != 8) {
  13.     AqvmRuntimeDebugger_OutputReport(
  14.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_LongLengthWarning"",
  15.         ""The length requirement for the long type does not conform to the "
  16.         "type "
  17.         "definition."",
  18.         NULL);
  19.     ++warning_count;
  20.   }
  21.   if (sizeof(aqfloat) != 4) {
  22.     AqvmRuntimeDebugger_OutputReport(
  23.         ""WARNING"",
  24.         ""AqvmMemory_CheckMemoryConditions_FloatLengthWarning"",
  25.         ""The length requirement for the float type does not conform to the "
  26.         "type definition."",
  27.         NULL);
  28.     ++warning_count;
  29.   }
  30.   if (sizeof(aqdouble) != 4) {
  31.     AqvmRuntimeDebugger_OutputReport(
  32.         ""WARNING"",
  33.         ""AqvmMemory_CheckMemoryConditions_DoubleLengthWarning"",
  34.         ""The length requirement for the double type does not conform to the "
  35.         "type definition."",
  36.         NULL);
  37.     ++warning_count;
  38.   }
  39.   if (sizeof(aqchar) != 1) {
  40.     AqvmRuntimeDebugger_OutputReport(
  41.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_CharLengthWarning"",
  42.         ""The length requirement for the char type does not conform to the "
  43.         "type "
  44.         "definition."",
  45.         NULL);
  46.     ++warning_count;
  47.   }
  48.   if (sizeof(aqbool) != 1) {
  49.     AqvmRuntimeDebugger_OutputReport(
  50.         ""WARNING"", ""AqvmMemory_CheckMemoryConditions_BoolLengthWarning"",
  51.         "The length requirement for the bool type does not conform to the type "
  52.         "definition.",
  53.         NULL);
  54.     ++warning_count;
  55.   }
  56.   if (warning_count == 0) {
  57.     AqvmRuntimeDebugger_OutputReport(""INFO"",
  58.                                      ""AqvmMemory_CheckMemoryConditions_CheckNormal"",
  59.                                      ""No memory conditions warning."", NULL);
  60.   }
  61.   return warning_count;
  62. }struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type,
  63.                                                   size_t size) {
  64.   struct AqvmMemory_Memory* memory_ptr =
  65.       (struct AqvmMemory_Memory*)malloc(sizeof(struct AqvmMemory_Memory));
  66.   if (memory_ptr == NULL) {
  67.     AqvmRuntimeDebugger_OutputReport(
  68.         ""ERROR"", ""AqvmMemory_CreateMemory_MemoryAllocationFailure"",
  69.         ""Failed to allocate memory."", NULL);
  70.     return NULL;
  71.   }
  72.   memory_ptr->data = data;
  73.   memory_ptr->type = type;
  74.   memory_ptr->size = size;
  75.   return memory_ptr;
  76. }void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr) {
  77.   free(memory_ptr);
  78. }int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,
  79.                        uint8_t type) {
  80.   if (memory == NULL) {
  81.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  82.                                      ""AqvmMemory_SetType_NullMemoryPointer"",
  83.                                      ""The memory pointer is NULL."", NULL);
  84.     return -1;
  85.   }
  86.   if (memory->type == NULL) {
  87.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  88.                                      ""AqvmMemory_SetType_NullTypePointer"",
  89.                                      ""The type pointer is NULL."", NULL);
  90.     return -2;
  91.   }
  92.   if (index > memory->size) {
  93.     AqvmRuntimeDebugger_OutputReport(
  94.         ""ERROR"", ""AqvmMemory_SetType_OutOfMemoryRange"",
  95.         ""The index is out of memory range."", NULL);
  96.     return -3;
  97.   }
  98.   if (type > 0x0F) {
  99.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  100.                                      ""AqvmMemory_SetType_OutOfTypeRange"",
  101.                                      ""The type is out of range."", NULL);
  102.     return -4;
  103.   }
  104.   // Sets the type of the data at |index| bytes in memory.
  105.   // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  106.   // each uint8_t type location stores two type data. The storage locations
  107.   // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  108.   // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  109.   // stored in the low bits of (|index| / 2).
  110.   if (index % 2 != 0) {
  111.     memory->type[index / 2] = (memory->type[index / 2] & 0xF0) | type;
  112.   } else {
  113.     memory->type[index / 2] = (memory->type[index / 2] & 0x0F) | (type << 4);
  114.   }
  115.   return 0;
  116. }uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) {
  117.   if (memory == NULL) {
  118.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  119.                                      ""AqvmMemory_GetType_NullMemoryPointer"",
  120.                                      ""The memory pointer is NULL."", NULL);
  121.     return 0x11;
  122.   }
  123.   if (memory->type == NULL) {
  124.     AqvmRuntimeDebugger_OutputReport(""ERROR"",
  125.                                      ""AqvmMemory_GetType_NullTypePointer"",
  126.                                      ""The type pointer is NULL."", NULL);
  127.     return 0x12;
  128.   }
  129.   if (index > memory->size) {
  130.     AqvmRuntimeDebugger_OutputReport(
  131.         ""ERROR"", ""AqvmMemory_GetType_OutOfMemoryRange"",
  132.         ""The index is out of memory range."", NULL);
  133.     return 0x13;
  134.   }
  135.   // Gets the type of the data at |index| bytes in memory.
  136.   // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  137.   // each uint8_t type location stores two type data. The storage locations
  138.   // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  139.   // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  140.   // stored in the low bits of (|index| / 2).
  141.   if (index % 2 != 0) {
  142.     return memory->type[index / 2] & 0x0F;
  143.   } else {
  144.     return (memory->type[index / 2] & 0xF0) >> 4;
  145.   }
  146. }
复制代码
通过这些代码的配合,共同构成了完整的Aqvm的内存架构,有效缓解内存压力的同时,提高了Aqvm的运行效率。
我们正在更加努力地开发AQ虚拟机。如果您想了解更多信息或参与开发工作,请关注我们的官网:https://www.axa6.com 和 Github:https://github.com/aq-org/AQ。

本文章基于AQ License:https://github.com/aq-org/AQ/blob/main/LICENSE 发布,如有需要,请根据AQ License进行改编或转载。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册