diff --git a/.vscode/settings.json b/.vscode/settings.json index ec2c3e6..dbd901d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -111,7 +111,8 @@ "atomic": "c", "condition_variable": "c", "ostream": "c", - "stop_token": "c" + "stop_token": "c", + "dag_data_split.h": "c" }, "files.exclude": { "**/.git": true, @@ -160,7 +161,7 @@ "**/runtime/thirdparty/http-parser/**": true, "**/runtime/thirdparty/jsmn/**": true, "**/runtime/thirdparty/dist/**": true, - "**/runtime/thirdparty/hashmap/**": true, + "**/runtime/thirdparty/hashmap/**": true, "*.o": true, "*.bc": true, "*.wasm": true @@ -224,4 +225,4 @@ "C_Cpp_Runner.msvcSecureNoWarnings": false, "git.detectSubmodules": true, "git.enabled": true -} +} \ No newline at end of file diff --git a/runtime/tests/fibonacciadd/dag_data_split.c b/runtime/tests/fibonacciadd/dag_data_split.c new file mode 100644 index 0000000..c8b0919 --- /dev/null +++ b/runtime/tests/fibonacciadd/dag_data_split.c @@ -0,0 +1,99 @@ +#include "dag_data_split.h" +#include +#include +#include + +struct DataNode { + uint32_t dataLength; + char *data; + struct DataNode *next; +}; + +DataNode* splitData(char *buffer, uint32_t bufferSize) { + DataNode *head = NULL; + DataNode *tail = NULL; + uint32_t offset = 0; + + while (offset < bufferSize) { + if (offset + 4 > bufferSize) { + break; + } + + uint32_t dataLength = *(uint32_t *)(buffer + offset); + offset += 4; + + if (offset + dataLength > bufferSize) { + break; + } + + DataNode *newNode = (DataNode *)malloc(sizeof(DataNode)); + if (newNode == NULL) { + perror("Memory allocation failed"); + freeDataNodes(head); // 释放已分配的节点内存 + return NULL; + } + + newNode->data = (char *)malloc(dataLength); + if (newNode->data == NULL) { + free(newNode); + perror("Memory allocation failed"); + freeDataNodes(head); // 释放已分配的节点内存 + return NULL; + } + memcpy(newNode->data, buffer + offset, dataLength); + newNode->dataLength = dataLength; + newNode->next = NULL; + + if (head == NULL) { + head = newNode; + } else { + tail->next = newNode; + } + tail = newNode; + + offset += dataLength; + } + return head; +} + +void freeDataNodes(DataNode *head) { + while (head != NULL) { + DataNode *next = head->next; + free(head->data); + free(head); + head = next; + } +} + +void printDataList(DataNode *head) { + int index = 0; + DataNode *current = head; + while (current != NULL) { + printf("Data %d: Length = %u\n", index, current->dataLength); + index++; + current = current->next; + } +} + +int getDataNodeCount(DataNode *head) { + int count = 0; + DataNode *current = head; + while (current != NULL) { + count++; + current = current->next; + } + return count; +} + +const char* getDataNodeByIndex(DataNode *head, int index) { + int count = 1; + DataNode *current = head; + while (current != NULL) { + if (count == index) { + return current->data; + } + count++; + current = current->next; + } + return NULL; // 如果索引超出范围,返回NULL +} diff --git a/runtime/tests/fibonacciadd/dag_data_split.h b/runtime/tests/fibonacciadd/dag_data_split.h new file mode 100644 index 0000000..7debb67 --- /dev/null +++ b/runtime/tests/fibonacciadd/dag_data_split.h @@ -0,0 +1,17 @@ +#pragma once +#include + +typedef struct DataNode DataNode; + +DataNode* splitData(char *buffer, uint32_t bufferSize); + +void freeDataNodes(DataNode *head); + +void printDataList(DataNode *head); + +int getDataNodeCount(DataNode *head); + +/** + * @param index is form 1 to n +*/ +const char* getDataNodeByIndex(DataNode *head, int index); diff --git a/runtime/tests/fibonacciadd/main.c b/runtime/tests/fibonacciadd/main.c index 10f4e7f..7e86c0e 100644 --- a/runtime/tests/fibonacciadd/main.c +++ b/runtime/tests/fibonacciadd/main.c @@ -3,6 +3,7 @@ #include #include #include +#include "dag_data_split.h" // 包含数据处理的API unsigned long int fib(unsigned long int n) { if (n <= 1) return n; @@ -12,41 +13,32 @@ unsigned long int fib(unsigned long int n) { int main() { char buffer[1024]; // Buffer to store input data - ssize_t bytes_read = read(0, buffer, sizeof(buffer) - 1); + ssize_t bytes_read = read(0, buffer, sizeof(buffer)); if (bytes_read < 0) { perror("Error reading input"); return 1; } - buffer[bytes_read] = '\0'; // Null-terminate the string - // Parse the expected data length from the first 4 bytes for the first number - if (bytes_read < 4) { - fprintf(stderr, "Input is too short to contain valid data length.\n"); + DataNode *dataList = splitData(buffer, bytes_read); + if (dataList == NULL) { + fprintf(stderr, "Failed to split data.\n"); return 1; } - // First number length and number - uint32_t first_num_length; - memcpy(&first_num_length, buffer, sizeof(uint32_t)); - char *first_num_start = buffer + 4; - if (bytes_read < 4 + first_num_length + 4) { // Check if there is enough data for the first number and the second length - fprintf(stderr, "Input does not contain enough data for the first number and the second length.\n"); - return 1; - } + // 读取解析出的第一个和第二个数字 + unsigned long int num1, num2; + const char *firstdata = getDataNodeByIndex(dataList, 1); + const char *seconddata = getDataNodeByIndex(dataList, 2); - // Second number length and number - uint32_t second_num_length; - memcpy(&second_num_length, first_num_start + first_num_length, sizeof(uint32_t)); - char *second_num_start = first_num_start + first_num_length + 4; - if (bytes_read < 4 + first_num_length + 4 + second_num_length) { // Check if there is enough data for the second number - fprintf(stderr, "Input does not contain enough data for the second number.\n"); + if (firstdata == NULL || seconddata == NULL) { + fprintf(stderr, "Not enough data for two numbers.\n"); + freeDataNodes(dataList); return 1; } - // Read and calculate Fibonacci numbers - unsigned long int num1, num2; - if (sscanf(first_num_start, "%lu", &num1) != 1 || sscanf(second_num_start, "%lu", &num2) != 1) { + if (sscanf(firstdata, "%lu", &num1) != 1 || sscanf(seconddata, "%lu", &num2) != 1) { fprintf(stderr, "Failed to parse the numbers correctly.\n"); + freeDataNodes(dataList); return 1; } @@ -61,5 +53,8 @@ int main() { // Write to stdout write(1, output, len); + // 清理内存 + freeDataNodes(dataList); + return 0; -} +} \ No newline at end of file diff --git a/runtime/thirdparty/Makefile b/runtime/thirdparty/Makefile index c85cb59..5c30788 100644 --- a/runtime/thirdparty/Makefile +++ b/runtime/thirdparty/Makefile @@ -3,7 +3,7 @@ DIST_PREFIX=${CURR_DIR}/dist/ all: clean build -build: ck jsmn http-parser hashmap +build: ck jsmn http-parser ck: mkdir -p ${DIST_PREFIX} @@ -26,4 +26,4 @@ clean: make -C ck uninstall rm -rf ${DIST_PREFIX} -.PHONY: clean all build ck jsmn http-parser hashmap +.PHONY: clean all build ck jsmn http-parser