经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
一种优秀的虚拟机内存架构 - AQ
来源:cnblogs  作者:AQORG  时间:2024/7/22 9:35:07  对本文有异议

源链接:https://www.axa6.com/zh/an-excellent-virtual-machine-memory-architecture

简介

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

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

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

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

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

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

设计思路

内存架构

基础内存架构

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

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

选择寄存器的原因

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

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

寄存器架构的区别

标准的寄存器架构

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

  1. 数据类型 - 寄存器将存储的数据的类型(如int、float、double等)
  2. 数据 - 寄存器将存储的数据的值
  3. (可选)标记 - 寄存器将存储的数据的标记(如变量、函数、类等)
  4. (可选)引用 - 寄存器将存储的数据的引用(如对象的地址等)

尽管不同语言的虚拟机内存架构可能有所不同,但大致都存储了这些信息。

而在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_Registerenum类型的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。内含多个代码文件。

  1. CMakeLists.txt - 该目录下的CMake构建文件
  2. memory.h - 内存的数据结构和相关函数
  3. memory.c - 内存的相关函数的实现
  4. 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_GetType

获取 |memory| 中 |index| 字节处的数据类型。

成功时返回小于 4 位 (0X0F) 的类型。如果内存指针为 NULL,返回 0x11。如果索引指针为 NULL,返回 0x12。如果索引超出内存范围,返回 0x13。

  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. }

AqvmMemory_WriteData

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

成功时返回 0。如果内存指针为 NULL,返回 -1。如果索引指针为 NULL,返回 -2。如果索引超出内存范围,返回 -3。如果数据指针为 NULL,返回 -4。

  1. int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index,
  2. void* data_ptr, size_t size) {
  3. if (memory == NULL) {
  4. AqvmRuntimeDebugger_OutputReport(
  5. "\"ERROR\"", "\"AqvmMemory_WriteData_NullMemoryPointer\"",
  6. "\"The memory pointer is NULL.\"", NULL);
  7. return -1;
  8. }
  9. if (memory->type == NULL) {
  10. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  11. "\"AqvmMemory_WriteData_NullTypePointer\"",
  12. "\"The type pointer is NULL.\"", NULL);
  13. return -2;
  14. }
  15. if (index > memory->size) {
  16. AqvmRuntimeDebugger_OutputReport(
  17. "\"ERROR\"", "\"AqvmMemory_WriteData_OutOfMemoryRange\"",
  18. "\"The index is out of memory range.\"", NULL);
  19. return -3;
  20. }
  21. if (data_ptr == NULL) {
  22. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  23. "\"AqvmMemory_WriteData_NullDataPointer\"",
  24. "\"The data pointer is NULL.\"", NULL);
  25. return -4;
  26. }
  27. // Since void* does not have a specific size, pointer moves need to be
  28. // converted before moving.
  29. memcpy((void*)((uintptr_t)memory->data + index), data_ptr, size);
  30. return 0;
  31. }

memory.h完整代码:

  1. // Copyright 2024 AQ author, 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_MEMORY_H_
  5. #define AQ_AQVM_MEMORY_MEMORY_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "aqvm/memory/types.h"
  9. // The struct stores information about the memory.
  10. // |type| is a pointer to an array that stores the type of each byte in the
  11. // memory. Each byte uses 4 bits to store the type. So a uint8_t variable can
  12. // store 2 types. Each uint8_t variable's first 4 bits are used for the even
  13. // byte's type and the next 4 bits are used for the odd byte's type. The type
  14. // list is in types.h.
  15. // |data| is a pointer of type void* to the memory that stores the data.
  16. // |size| is the size of the memory.
  17. // NOTICE: The struct AqvmMemory_Memory only stores information of the memory.
  18. // The memory is allocated by the bytecode function when storing the bytecode.
  19. // The memory of |memory| and |type| is part of the bytecode memory.
  20. struct AqvmMemory_Memory {
  21. uint8_t* type;
  22. void* data;
  23. size_t size;
  24. };
  25. // Checks the memory conditions in the system.
  26. // Returns the number of warnings.
  27. int AqvmMemory_CheckMemoryConditions();
  28. // Creates the struct AqvmMemory_Memory with |data|, |type|, and |size|.
  29. // The function will allocate a struct AqvmMemory_Memory and copy |data|,
  30. // |type|, and |size| into the struct. Returns a pointer to the struct if
  31. // successful. Returns NULL if creation fails.
  32. struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type,
  33. size_t size);
  34. // Free the memory of the |memory_ptr|. No return.
  35. // NOTICE: The function only free the memory of the struct. The memory pointed
  36. // to by pointers to data and type in struct is not freed. This memory is
  37. // managed by bytecode related functions.
  38. void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr);
  39. // Sets the type of the data at |index| bytes in |memory| to |type|. |type|
  40. // should be less than 4 bits.
  41. // Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2
  42. // if the type pointer is NULL. Returns -3 if the index is out of range. Returns
  43. // -4 if the type is out of range.
  44. int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,
  45. uint8_t type);
  46. // Gets the type of the data at |index| bytes in |memory|.
  47. // Returns the type that is less than 4 bits (0X0F) if successful. Returns 0x11
  48. // if the memory pointer is NULL. Returns 0x12 if the type pointer is NULL.
  49. // Returns 0x13 if the index is out of memory range.
  50. uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index);
  51. // Writes the data that |data_ptr| points to of size |size| to the data of at
  52. // |index| bytes in |memory|.
  53. // Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2
  54. // if the type pointer is NULL. Returns -3 if the index is out of range. Returns
  55. // -4 if the data pointer is NULL.
  56. int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index,
  57. void* data_ptr, size_t size);
  58. #endif

memory.c

memory.c完整代码:

  1. // Copyright 2024 AQ author, All Rights Reserved.
  2. // This program is licensed under the AQ License. You can find the AQ license in
  3. // the root directory.
  4. #include "aqvm/memory/memory.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "aqvm/memory/types.h"
  10. #include "aqvm/runtime/debugger/debugger.h"
  11. int AqvmMemory_CheckMemoryConditions() {
  12. int warning_count = 0;
  13. if (sizeof(aqint) != 4) {
  14. AqvmRuntimeDebugger_OutputReport(
  15. "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_IntLengthWarning\"",
  16. "\"The length requirement for the int type does not conform to the "
  17. "type "
  18. "definition.\"",
  19. NULL);
  20. ++warning_count;
  21. }
  22. if (sizeof(aqlong) != 8) {
  23. AqvmRuntimeDebugger_OutputReport(
  24. "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_LongLengthWarning\"",
  25. "\"The length requirement for the long type does not conform to the "
  26. "type "
  27. "definition.\"",
  28. NULL);
  29. ++warning_count;
  30. }
  31. if (sizeof(aqfloat) != 4) {
  32. AqvmRuntimeDebugger_OutputReport(
  33. "\"WARNING\"",
  34. "\"AqvmMemory_CheckMemoryConditions_FloatLengthWarning\"",
  35. "\"The length requirement for the float type does not conform to the "
  36. "type definition.\"",
  37. NULL);
  38. ++warning_count;
  39. }
  40. if (sizeof(aqdouble) != 4) {
  41. AqvmRuntimeDebugger_OutputReport(
  42. "\"WARNING\"",
  43. "\"AqvmMemory_CheckMemoryConditions_DoubleLengthWarning\"",
  44. "\"The length requirement for the double type does not conform to the "
  45. "type definition.\"",
  46. NULL);
  47. ++warning_count;
  48. }
  49. if (sizeof(aqchar) != 1) {
  50. AqvmRuntimeDebugger_OutputReport(
  51. "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_CharLengthWarning\"",
  52. "\"The length requirement for the char type does not conform to the "
  53. "type "
  54. "definition.\"",
  55. NULL);
  56. ++warning_count;
  57. }
  58. if (sizeof(aqbool) != 1) {
  59. AqvmRuntimeDebugger_OutputReport(
  60. "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_BoolLengthWarning\"",
  61. "The length requirement for the bool type does not conform to the type "
  62. "definition.",
  63. NULL);
  64. ++warning_count;
  65. }
  66. if (warning_count == 0) {
  67. AqvmRuntimeDebugger_OutputReport("\"INFO\"",
  68. "\"AqvmMemory_CheckMemoryConditions_CheckNormal\"",
  69. "\"No memory conditions warning.\"", NULL);
  70. }
  71. return warning_count;
  72. }
  73. struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type,
  74. size_t size) {
  75. struct AqvmMemory_Memory* memory_ptr =
  76. (struct AqvmMemory_Memory*)malloc(sizeof(struct AqvmMemory_Memory));
  77. if (memory_ptr == NULL) {
  78. AqvmRuntimeDebugger_OutputReport(
  79. "\"ERROR\"", "\"AqvmMemory_CreateMemory_MemoryAllocationFailure\"",
  80. "\"Failed to allocate memory.\"", NULL);
  81. return NULL;
  82. }
  83. memory_ptr->data = data;
  84. memory_ptr->type = type;
  85. memory_ptr->size = size;
  86. return memory_ptr;
  87. }
  88. void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr) {
  89. free(memory_ptr);
  90. }
  91. int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,
  92. uint8_t type) {
  93. if (memory == NULL) {
  94. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  95. "\"AqvmMemory_SetType_NullMemoryPointer\"",
  96. "\"The memory pointer is NULL.\"", NULL);
  97. return -1;
  98. }
  99. if (memory->type == NULL) {
  100. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  101. "\"AqvmMemory_SetType_NullTypePointer\"",
  102. "\"The type pointer is NULL.\"", NULL);
  103. return -2;
  104. }
  105. if (index > memory->size) {
  106. AqvmRuntimeDebugger_OutputReport(
  107. "\"ERROR\"", "\"AqvmMemory_SetType_OutOfMemoryRange\"",
  108. "\"The index is out of memory range.\"", NULL);
  109. return -3;
  110. }
  111. if (type > 0x0F) {
  112. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  113. "\"AqvmMemory_SetType_OutOfTypeRange\"",
  114. "\"The type is out of range.\"", NULL);
  115. return -4;
  116. }
  117. // Sets the type of the data at |index| bytes in memory.
  118. // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  119. // each uint8_t type location stores two type data. The storage locations
  120. // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  121. // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  122. // stored in the low bits of (|index| / 2).
  123. if (index % 2 != 0) {
  124. memory->type[index / 2] = (memory->type[index / 2] & 0xF0) | type;
  125. } else {
  126. memory->type[index / 2] = (memory->type[index / 2] & 0x0F) | (type << 4);
  127. }
  128. return 0;
  129. }
  130. uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) {
  131. if (memory == NULL) {
  132. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  133. "\"AqvmMemory_GetType_NullMemoryPointer\"",
  134. "\"The memory pointer is NULL.\"", NULL);
  135. return 0x11;
  136. }
  137. if (memory->type == NULL) {
  138. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  139. "\"AqvmMemory_GetType_NullTypePointer\"",
  140. "\"The type pointer is NULL.\"", NULL);
  141. return 0x12;
  142. }
  143. if (index > memory->size) {
  144. AqvmRuntimeDebugger_OutputReport(
  145. "\"ERROR\"", "\"AqvmMemory_GetType_OutOfMemoryRange\"",
  146. "\"The index is out of memory range.\"", NULL);
  147. return 0x13;
  148. }
  149. // Gets the type of the data at |index| bytes in memory.
  150. // Since Aqvm stores type data occupying 4 bits and uint8_t occupying 8 bits,
  151. // each uint8_t type location stores two type data. The storage locations
  152. // (high 4 bits, low 4 bits) are set according to the parity of |index|. Even
  153. // numbers are stored in the high bits of (|index| / 2) and odd numbers are
  154. // stored in the low bits of (|index| / 2).
  155. if (index % 2 != 0) {
  156. return memory->type[index / 2] & 0x0F;
  157. } else {
  158. return (memory->type[index / 2] & 0xF0) >> 4;
  159. }
  160. }
  161. int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index,
  162. void* data_ptr, size_t size) {
  163. if (memory == NULL) {
  164. AqvmRuntimeDebugger_OutputReport(
  165. "\"ERROR\"", "\"AqvmMemory_WriteData_NullMemoryPointer\"",
  166. "\"The memory pointer is NULL.\"", NULL);
  167. return -1;
  168. }
  169. if (memory->type == NULL) {
  170. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  171. "\"AqvmMemory_WriteData_NullTypePointer\"",
  172. "\"The type pointer is NULL.\"", NULL);
  173. return -2;
  174. }
  175. if (index > memory->size) {
  176. AqvmRuntimeDebugger_OutputReport(
  177. "\"ERROR\"", "\"AqvmMemory_WriteData_OutOfMemoryRange\"",
  178. "\"The index is out of memory range.\"", NULL);
  179. return -3;
  180. }
  181. if (data_ptr == NULL) {
  182. AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
  183. "\"AqvmMemory_WriteData_NullDataPointer\"",
  184. "\"The data pointer is NULL.\"", NULL);
  185. return -4;
  186. }
  187. // Since void* does not have a specific size, pointer moves need to be
  188. // converted before moving.
  189. memcpy((void*)((uintptr_t)memory->data + index), data_ptr, size);
  190. return 0;
  191. }

通过这些代码的配合,共同构成了完整的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进行改编或转载。

原文链接:https://www.cnblogs.com/axa6-com/p/18312972

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号