diff --git a/.gitignore b/.gitignore index 600d2d3..c701e61 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode \ No newline at end of file +.vscode +.cache \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e6da0da --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/include/dynamic_memory_allocation/initialize.h b/include/dynamic_memory_allocation/initialize.h index 2241c15..ea64c87 100644 --- a/include/dynamic_memory_allocation/initialize.h +++ b/include/dynamic_memory_allocation/initialize.h @@ -13,7 +13,7 @@ struct data* new_data(); size是写入的长度,单位为字节 函数返回值为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是追加的长度,单位为字节 函数返回值为0则成功 */ -int add_data(struct data* _p,void* add_data,int size); +int add_data(struct data* _p,const void* add_data,int size); /* 从一个数据段读取可读数据 diff --git a/src/dynamic_memory_allocation/main.c b/src/dynamic_memory_allocation/main.c index 0c59f82..8a03ea2 100644 --- a/src/dynamic_memory_allocation/main.c +++ b/src/dynamic_memory_allocation/main.c @@ -1,6 +1,7 @@ #include #include #include +#include struct data* new_data(){ struct data* _p = malloc(sizeof(struct data)); @@ -9,37 +10,38 @@ struct data* new_data(){ 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){ - 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; } 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; } void* new_p = realloc(_p->pointer,size); 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; } _p->pointer = new_p; memcpy(_p->pointer,new_data,size); + _p->size=size; 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){ - 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; } - if(new_data==NULL){ - perror("src/dynamic_memory_allocation/main.c:37:8:new_data is NULL.\n"); + if(add_data==NULL){ + perror("ERROR:src/dynamic_memory_allocation/main.c:39: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"); + perror("ERROR:src/dynamic_memory_allocation/main.c:43:19:realloc() failed.\n"); return -2; } _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){ 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; } + 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); return 0; } int rm_data(struct data* _p){ 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; } free(_p->pointer); diff --git a/test/dynamic_memory_allocation.c b/test/dynamic_memory_allocation.c new file mode 100644 index 0000000..1833f8d --- /dev/null +++ b/test/dynamic_memory_allocation.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +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; +} \ No newline at end of file