diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/include/dynamic_memory_allocation/data.h b/include/dynamic_memory_allocation/data.h new file mode 100644 index 0000000..6f614de --- /dev/null +++ b/include/dynamic_memory_allocation/data.h @@ -0,0 +1,9 @@ +#ifndef _DMA_DATA_ +#define _DMA_DATA_ + +struct data{ + void* pointer; + unsigned long size; +}; + +#endif \ No newline at end of file diff --git a/include/dynamic_memory_allocation/initialize.h b/include/dynamic_memory_allocation/initialize.h new file mode 100644 index 0000000..2241c15 --- /dev/null +++ b/include/dynamic_memory_allocation/initialize.h @@ -0,0 +1,40 @@ +#ifndef _DMA_INIT_ +#define _DMA_INIT_ + +#include + +/*创建一个新的数据段*/ +struct data* new_data(); + +/* + 写入数据到一个数据段 + _p是要写入的数据段 + new_data是要写入的数据 + size是写入的长度,单位为字节 + 函数返回值为0则成功 +*/ +int write_data(struct data* _p,void* new_data,int size); + +/* + 追加数据到一个数据段 + _p是要写入的数据段 + add_data是要追加的数据 + size是追加的长度,单位为字节 + 函数返回值为0则成功 +*/ +int add_data(struct data* _p,void* add_data,int size); + +/* + 从一个数据段读取可读数据 + _p是要读取的数据段 + output是输出数据的指针 + array是读取的偏移量,单位为字节 + size是读取的长度,单位为字节 + 函数返回值为0则成功 +*/ +int read_data(struct data _p,void* output,unsigned int array,unsigned int size); + +/*彻底删除一段数据*/ +int rm_data(struct data* _p); + +#endif diff --git a/include/dynamic_memory_allocation/main.h b/include/dynamic_memory_allocation/main.h new file mode 100644 index 0000000..113d07d --- /dev/null +++ b/include/dynamic_memory_allocation/main.h @@ -0,0 +1,7 @@ +#ifndef _DMA_ +#define _DMA_ + +#include +#include + +#endif \ No newline at end of file diff --git a/include/simpledb/simpledb.h b/include/simpledb/simpledb.h new file mode 100644 index 0000000..8612532 --- /dev/null +++ b/include/simpledb/simpledb.h @@ -0,0 +1,22 @@ +#include +#include + +#ifndef _SIMPDB_ +#define _SIMPDB_ + +struct database_info{ + struct data name; + struct data path; + struct data data_type; + struct data data_main; + unsigned long size; + FILE* pointer; +}; + +int new_database(struct database_info* db,struct data name,struct data path,struct data data_type); +/*创建并加载数据库*/ +int new_database_nload(struct data name,struct data path,struct data data_type); +/*创建但不加载数据库*/ +int unload_database(struct database_info* db); +/*卸载数据库*/ +#endif \ No newline at end of file diff --git a/src/dynamic_memory_allocation/main.c b/src/dynamic_memory_allocation/main.c new file mode 100644 index 0000000..0c59f82 --- /dev/null +++ b/src/dynamic_memory_allocation/main.c @@ -0,0 +1,68 @@ +#include +#include +#include + +struct data* new_data(){ + struct data* _p = malloc(sizeof(struct data)); + _p->pointer = malloc(0); + _p->size = 0; + return _p; +} + +int write_data(struct data* _p,void* new_data,int size){ + if(_p==NULL){ + perror("src/dynamic_memory_allocation/main.c:14:8:_p is NULL.\n"); + return -1; + } + if(new_data==NULL){ + perror("src/dynamic_memory_allocation/main.c:18:8:new_data is NULL.\n"); + return -1; + } + void* new_p = realloc(_p->pointer,size); + if(new_p==NULL){ + perror("src/dynamic_memory_allocation/main.c:22:19:realloc() failed.\n"); + return -2; + } + _p->pointer = new_p; + memcpy(_p->pointer,new_data,size); + return 0; +} + +int add_data(struct data* _p,void* add_data,int size){ + if(_p==NULL){ + perror("src/dynamic_memory_allocation/main.c:33:8:_p is NULL.\n"); + return -1; + } + if(new_data==NULL){ + perror("src/dynamic_memory_allocation/main.c:37:8:new_data is NULL.\n"); + return -1; + } + void* new_p = realloc(_p->pointer,_p->size+size); + if(new_p==NULL){ + perror("src/dynamic_memory_allocation/main.c:41:19:realloc() failed.\n"); + return -2; + } + _p->pointer = new_p; + memcpy(_p->pointer+_p->size,add_data,size); + _p->size+=size; + return 0; +} + +int read_data(struct data _p,void* output,unsigned int array,unsigned int size){ + if(output==NULL){ + perror("src/dynamic_memory_allocation/main.c:58:8:output is NULL.\n"); + return -1; + } + memcpy(output,_p.pointer+array,size); + return 0; +} + +int rm_data(struct data* _p){ + if(_p==NULL){ + perror("src/dynamic_memory_allocation/main.c:66:8:_p is NULL.\n"); + return -1; + } + free(_p->pointer); + free(_p); + return 0; +} \ No newline at end of file diff --git a/src/simpledb/simpledb.c b/src/simpledb/simpledb.c new file mode 100644 index 0000000..e69de29