动态内存分配模块完成,并添加了测试程序
This commit is contained in:
+2
-1
@@ -1 +1,2 @@
|
|||||||
.vscode
|
.vscode
|
||||||
|
.cache
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.PHONY: all test clear
|
||||||
|
|
||||||
|
all: test
|
||||||
|
|
||||||
|
test:
|
||||||
|
mkdir -p .cache/shared .cache/test
|
||||||
|
gcc -shared -fPIC -o .cache/shared/libdma.so ./src/dynamic_memory_allocation/main.c -I./
|
||||||
|
gcc -o .cache/test/test_dma ./test/dynamic_memory_allocation.c -L./.cache/shared/ -ldma -I./ -Wl,-rpath='./.cache/shared'
|
||||||
|
./.cache/test/test_dma
|
||||||
|
clear:
|
||||||
|
rm -rf .cache
|
||||||
@@ -13,7 +13,7 @@ struct data* new_data();
|
|||||||
size是写入的长度,单位为字节
|
size是写入的长度,单位为字节
|
||||||
函数返回值为0则成功
|
函数返回值为0则成功
|
||||||
*/
|
*/
|
||||||
int write_data(struct data* _p,void* new_data,int size);
|
int write_data(struct data* _p,const void* new_data,int size);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
追加数据到一个数据段
|
追加数据到一个数据段
|
||||||
@@ -22,7 +22,7 @@ int write_data(struct data* _p,void* new_data,int size);
|
|||||||
size是追加的长度,单位为字节
|
size是追加的长度,单位为字节
|
||||||
函数返回值为0则成功
|
函数返回值为0则成功
|
||||||
*/
|
*/
|
||||||
int add_data(struct data* _p,void* add_data,int size);
|
int add_data(struct data* _p,const void* add_data,int size);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
从一个数据段读取可读数据
|
从一个数据段读取可读数据
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <include/dynamic_memory_allocation/main.h>
|
#include <include/dynamic_memory_allocation/main.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
struct data* new_data(){
|
struct data* new_data(){
|
||||||
struct data* _p = malloc(sizeof(struct data));
|
struct data* _p = malloc(sizeof(struct data));
|
||||||
@@ -9,37 +10,38 @@ struct data* new_data(){
|
|||||||
return _p;
|
return _p;
|
||||||
}
|
}
|
||||||
|
|
||||||
int write_data(struct data* _p,void* new_data,int size){
|
int write_data(struct data* _p,const void* new_data,int size){
|
||||||
if(_p==NULL){
|
if(_p==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:14:8:_p is NULL.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:15:8:_p is NULL.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(new_data==NULL){
|
if(new_data==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:18:8:new_data is NULL.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:19:8:new_data is NULL.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
void* new_p = realloc(_p->pointer,size);
|
void* new_p = realloc(_p->pointer,size);
|
||||||
if(new_p==NULL){
|
if(new_p==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:22:19:realloc() failed.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:23:19:realloc() failed.\n");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
_p->pointer = new_p;
|
_p->pointer = new_p;
|
||||||
memcpy(_p->pointer,new_data,size);
|
memcpy(_p->pointer,new_data,size);
|
||||||
|
_p->size=size;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int add_data(struct data* _p,void* add_data,int size){
|
int add_data(struct data* _p,const void* add_data,int size){
|
||||||
if(_p==NULL){
|
if(_p==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:33:8:_p is NULL.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:35:8:_p is NULL.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(new_data==NULL){
|
if(add_data==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:37:8:new_data is NULL.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:39:8:new_data is NULL.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
void* new_p = realloc(_p->pointer,_p->size+size);
|
void* new_p = realloc(_p->pointer,_p->size+size);
|
||||||
if(new_p==NULL){
|
if(new_p==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:41:19:realloc() failed.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:43:19:realloc() failed.\n");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
_p->pointer = new_p;
|
_p->pointer = new_p;
|
||||||
@@ -50,16 +52,24 @@ int add_data(struct data* _p,void* add_data,int size){
|
|||||||
|
|
||||||
int read_data(struct data _p,void* output,unsigned int array,unsigned int size){
|
int read_data(struct data _p,void* output,unsigned int array,unsigned int size){
|
||||||
if(output==NULL){
|
if(output==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:58:8:output is NULL.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:54:8:output is NULL.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if(array>_p.size){
|
||||||
|
perror("ERROR:src/dynamic_memory_allocation/main.c:58:8:array larger than _p.size.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if(size>_p.size){
|
||||||
|
perror("WARN:src/dynamic_memory_allocation/main.c:62:8:size larger than _p.size.\n");
|
||||||
|
size = _p.size - array;
|
||||||
|
}
|
||||||
memcpy(output,_p.pointer+array,size);
|
memcpy(output,_p.pointer+array,size);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rm_data(struct data* _p){
|
int rm_data(struct data* _p){
|
||||||
if(_p==NULL){
|
if(_p==NULL){
|
||||||
perror("src/dynamic_memory_allocation/main.c:66:8:_p is NULL.\n");
|
perror("ERROR:src/dynamic_memory_allocation/main.c:71:8:_p is NULL.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
free(_p->pointer);
|
free(_p->pointer);
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#include <include/dynamic_memory_allocation/main.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
struct data* _test = new_data();
|
||||||
|
if(_test==NULL){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
const char* _test_char = "Hello World!";
|
||||||
|
if(write_data(_test,_test_char,strlen(_test_char))<0){
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
void* _test_buf = malloc(1024);
|
||||||
|
memset(_test_buf,0,1024);
|
||||||
|
memcpy(_test_buf,_test->pointer,strlen(_test->pointer));
|
||||||
|
if(strcmp(_test_char,_test_buf)!=0){
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
memset(_test_buf,0,1024);
|
||||||
|
if(read_data(*_test,_test_buf,0,(*_test).size)<0){
|
||||||
|
return -4;
|
||||||
|
}
|
||||||
|
const char* _test_add_char = "TEST TEXT";
|
||||||
|
if(add_data(_test,_test_add_char,strlen(_test_add_char))<0){
|
||||||
|
return -5;
|
||||||
|
}
|
||||||
|
memcpy(_test_buf,_test->pointer,strlen(_test->pointer));
|
||||||
|
void* _test_buf2 = malloc(1024);
|
||||||
|
memset(_test_buf2,0,1024);
|
||||||
|
memcpy(_test_buf2,_test_char,strlen(_test_char));
|
||||||
|
strcat(_test_buf2,_test_add_char);
|
||||||
|
if(strcmp(_test_buf2,_test_buf)!=0){
|
||||||
|
return -6;
|
||||||
|
}
|
||||||
|
if(rm_data(_test)<0){
|
||||||
|
return -7;
|
||||||
|
}
|
||||||
|
printf("Success! dynamic_memory_allocation\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user