Skip to content

Commit

Permalink
add CircularBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanDaYu002 committed Apr 23, 2019
1 parent ab18bd9 commit 6e1d766
Show file tree
Hide file tree
Showing 15 changed files with 689 additions and 0 deletions.
1 change: 1 addition & 0 deletions CircularBuffer_be_modifying
Submodule CircularBuffer_be_modifying added at ffa4aa
69 changes: 69 additions & 0 deletions my_malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

/***************************************************************************
* @file:my_malloc.c
* @author:
* @date: 4,11,2019
* @brief: 对malloc系列函数进行封装,方便查找内存泄露位置
* @attention:
***************************************************************************/
#include <stdlib.h>
#include <string.h>

#include "my_malloc.h"

//用来存储每次 malloc 的相关信息
malloc_mem_info_t *malloc_mem_info = NULL;

int my_malloc_init(void)
{
malloc_mem_info = (malloc_mem_info_t*)calloc(MAX_MALLOC_TIMES*sizeof(malloc_mem_info_t),sizeof(char));
if(NULL == malloc_mem_info)
return -1;

return 0;
}

static int malloc_index = 0;
/*******************************************************************************
*@ Description :
*@ Input :<size> 需求内存大小
<file_line> 文件名和行号,格式 "my_malloc.c:66"
传入 __FILE__:__LINE__ 即可
*@ Output :
*@ Return :成功 :返回地址 ;失败:NULL
*@ attention :
*******************************************************************************/
void * my_malloc(int size,char* file_name)
{
void * ptr = malloc(size);
if(ptr)
{
//记录返回的地址,统一管理
malloc_mem_info[malloc_index].address = ptr;
strncpy(malloc_mem_info[malloc_index].file_line,file_name,sizeof(malloc_mem_info.file_line));
malloc_index++;
return ptr;
}
else
{
return NULL;
}

}

void my_free(void *ptr)
{

}
void * my_calloc(int nmemb, int size)
{

}

void * my_realloc(void *ptr, int size)
{


}


23 changes: 23 additions & 0 deletions my_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

/***************************************************************************
* @file:my_malloc.h
* @author:
* @date: 4,11,2019
* @brief: 对malloc系列函数进行封装,方便查找内存泄露位置
* @attention:
***************************************************************************/

#ifndef _MY_MALLOC_H
#define _MY_MALLOC_H

#define MAX_MALLOC_TIMES 600 //最大的malloc次数(泛指 malloc calloc realloc 操作次数)
// 600 * 36/1024 = 21 kB 大小
typedef struct _malloc_mem_info_t
{
void* address; //malloc空间的地址记录
char file_line[32]; //记录文件名和行号,格式 my_malloc.c:66(代表在my_malloc.c文件66行处的malloc内存没有释放)
}malloc_mem_info_t;



#endif
Binary file added tools_data_struct/select/client
Binary file not shown.
52 changes: 52 additions & 0 deletions tools_data_struct/select/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*********************************************************
select()实现多路复用的客户端程序
1. create socket
2. connect
3. send
4. recv
*********************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUF_SIZE 100

int main()
{
//create socket
int iClient = socket(AF_INET, SOCK_STREAM, 0);//SOCK_STREAM:流式套接字,TCP协议用
if (-1 == iClient)
{
return -1;
}
printf("socket ok\r\n");

//connect
struct sockaddr_in stServer;
memset(&stServer, 0, sizeof(struct sockaddr_in));
stServer.sin_family = AF_INET;
stServer.sin_port = htons(8866);
stServer.sin_addr.s_addr = inet_addr("127.0.0.1");
int iRet = connect(iClient, (struct sockaddr *)&stServer, sizeof(struct sockaddr));
if (-1 == iRet)
{
perror("connect");
return -1;
}
printf("connect ok\r\n");

//send
char buf[BUF_SIZE] = {0};
gets(buf);
send(iClient, buf, BUF_SIZE, 0);
memset(buf, 0, BUF_SIZE);

//recv
iRet = recv(iClient, buf, BUF_SIZE, 0);
printf("%d %s\r\n", iRet, buf);
return 0;
}
Binary file added tools_data_struct/select/select
Binary file not shown.
86 changes: 86 additions & 0 deletions tools_data_struct/select/select.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*********************************************************
select()实现多路复用
1. create socket
2. bind
3. select
*********************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>

#define BUF_SIZE 100

int main()
{
//socket
int iServer = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == iServer)
{
return -1;
}
printf("socket ok\r\n");

//bind
struct sockaddr_in stServer;
memset(&stServer, 0, sizeof(struct sockaddr_in));
stServer.sin_family = AF_INET;
stServer.sin_port = htons(8866);
stServer.sin_addr.s_addr = INADDR_ANY;
int iRet = bind(iServer, (struct sockaddr *)&stServer, sizeof(struct sockaddr_in));
if (-1 == iRet)
{
return -1;
}
printf("bind ok\r\n");

//listen
iRet = listen(iServer, 5);
if (-1 == iRet)
{
return -1;
}
printf("listen ok\r\n");


//不同的部分:
//select()
fd_set stRSet;//fd_set是一个结构体类型
FD_ZERO(&stRSet);//清除stRSet中所有的文件描述
char buf[BUF_SIZE];
while(1)
{
FD_SET(0, &stRSet);//0:标准输入,即将标准输入加入到stRSet
FD_SET(iServer, &stRSet);//将iServer文件描述符加入到stRSet
iRet = select(iServer+1, &stRSet, NULL, NULL, NULL);
if (iRet <= 0)
{
continue;
}
if (FD_ISSET(iServer, &stRSet))//判断iServer是否在stRSet中
{
int iClient = accept(iServer, NULL, NULL);
if (iClient < 0)
{
continue;
}
memset(buf, 0, BUF_SIZE);
recv(iClient, buf, BUF_SIZE, 0);
printf("iserver %s\r\n", buf);
send(iClient, buf, BUF_SIZE, 0);
close(iClient);
}
if (FD_ISSET(0, &stRSet))//判断标准输入是否在stRSet中
{
memset(buf, 0, BUF_SIZE);
fgets(buf, BUF_SIZE, stdin);
printf("stdin %s\r\n", buf);
}
}
return 0;
}
Loading

0 comments on commit 6e1d766

Please sign in to comment.