为扇入节点添加处理数据的api,格式是四字节数据长度加数据

sledge_graph
hwwang 9 months ago
parent a056d25ff4
commit 4b2a905e92

@ -111,7 +111,8 @@
"atomic": "c", "atomic": "c",
"condition_variable": "c", "condition_variable": "c",
"ostream": "c", "ostream": "c",
"stop_token": "c" "stop_token": "c",
"dag_data_split.h": "c"
}, },
"files.exclude": { "files.exclude": {
"**/.git": true, "**/.git": true,

@ -0,0 +1,99 @@
#include "dag_data_split.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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
}

@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
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);

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "dag_data_split.h" // 包含数据处理的API
unsigned long int fib(unsigned long int n) { unsigned long int fib(unsigned long int n) {
if (n <= 1) return n; if (n <= 1) return n;
@ -12,41 +13,32 @@ unsigned long int fib(unsigned long int n) {
int main() { int main() {
char buffer[1024]; // Buffer to store input data 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) { if (bytes_read < 0) {
perror("Error reading input"); perror("Error reading input");
return 1; return 1;
} }
buffer[bytes_read] = '\0'; // Null-terminate the string
// Parse the expected data length from the first 4 bytes for the first number DataNode *dataList = splitData(buffer, bytes_read);
if (bytes_read < 4) { if (dataList == NULL) {
fprintf(stderr, "Input is too short to contain valid data length.\n"); fprintf(stderr, "Failed to split data.\n");
return 1; return 1;
} }
// First number length and number // 读取解析出的第一个和第二个数字
uint32_t first_num_length; unsigned long int num1, num2;
memcpy(&first_num_length, buffer, sizeof(uint32_t)); const char *firstdata = getDataNodeByIndex(dataList, 1);
char *first_num_start = buffer + 4; const char *seconddata = getDataNodeByIndex(dataList, 2);
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;
}
// Second number length and number if (firstdata == NULL || seconddata == NULL) {
uint32_t second_num_length; fprintf(stderr, "Not enough data for two numbers.\n");
memcpy(&second_num_length, first_num_start + first_num_length, sizeof(uint32_t)); freeDataNodes(dataList);
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");
return 1; return 1;
} }
// Read and calculate Fibonacci numbers if (sscanf(firstdata, "%lu", &num1) != 1 || sscanf(seconddata, "%lu", &num2) != 1) {
unsigned long int num1, num2;
if (sscanf(first_num_start, "%lu", &num1) != 1 || sscanf(second_num_start, "%lu", &num2) != 1) {
fprintf(stderr, "Failed to parse the numbers correctly.\n"); fprintf(stderr, "Failed to parse the numbers correctly.\n");
freeDataNodes(dataList);
return 1; return 1;
} }
@ -61,5 +53,8 @@ int main() {
// Write to stdout // Write to stdout
write(1, output, len); write(1, output, len);
// 清理内存
freeDataNodes(dataList);
return 0; return 0;
} }

@ -3,7 +3,7 @@ DIST_PREFIX=${CURR_DIR}/dist/
all: clean build all: clean build
build: ck jsmn http-parser hashmap build: ck jsmn http-parser
ck: ck:
mkdir -p ${DIST_PREFIX} mkdir -p ${DIST_PREFIX}
@ -26,4 +26,4 @@ clean:
make -C ck uninstall make -C ck uninstall
rm -rf ${DIST_PREFIX} rm -rf ${DIST_PREFIX}
.PHONY: clean all build ck jsmn http-parser hashmap .PHONY: clean all build ck jsmn http-parser

Loading…
Cancel
Save