完成动态内存分配
This commit is contained in:
@@ -0,0 +1 @@
|
||||
.vscode
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef _DMA_DATA_
|
||||
#define _DMA_DATA_
|
||||
|
||||
struct data{
|
||||
void* pointer;
|
||||
unsigned long size;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef _DMA_INIT_
|
||||
#define _DMA_INIT_
|
||||
|
||||
#include <include/dynamic_memory_allocation/data.h>
|
||||
|
||||
/*创建一个新的数据段*/
|
||||
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
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef _DMA_
|
||||
#define _DMA_
|
||||
|
||||
#include <include/dynamic_memory_allocation/data.h>
|
||||
#include <include/dynamic_memory_allocation/initialize.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <include/dynamic_memory_allocation/main.h>
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <include/dynamic_memory_allocation/main.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user