Explorar o código

迭代优化swapi(c函数)基础库

niujiuru hai 2 días
pai
achega
d361026100
Modificáronse 9 ficheiros con 23 adicións e 40 borrados
  1. 3 2
      swapi/Makefile
  2. 0 16
      swapi/swapi.h
  3. 1 0
      swapi/swdir.c
  4. 1 1
      swapi/swhash.c
  5. 0 3
      swapi/swmem.c
  6. 9 9
      swapi/swrand.c
  7. 2 2
      swapi/swtcp.c
  8. 2 2
      swapi/swthrd.c
  9. 5 5
      swapi/swudp.c

+ 3 - 2
swapi/Makefile

@@ -27,6 +27,7 @@ OBJS := $(SRCS:.c=.o)
 
 # 编译器
 CC := gcc
+AR := ar
 CFLAGS1 := -Wall -fpic -O2
 CFLAGS2 := -shared
 DEFS := -D_GNU_SOURCE -D_DEBUG
@@ -50,9 +51,9 @@ libswapi.so : $(OBJS)
 	$(CC) $(CFLAGS2) $(OBJS) -o $@
 
 swapi_test.out : testLib.c logfromgo.c
-	$(CC) $(DEFS) $(CFLAGS1) testLib.c logfromgo.c $(INCS) -static -L./ -lswapi -pthread -lrt -o $@
+	$(CC) $(DEFS) $(CFLAGS1) $(INCS) testLib.c logfromgo.c -L./ -lswapi -pthread -lrt -static -o $@
 
 .PHONY : clean
 
 clean :
-	rm -rf $(OBJS) ./libswapi.a ./libswapi.so ./testLib.o ./swapi_test.out
+	rm -rf $(OBJS) *.o libswapi.a libswapi.so swapi_test.out

+ 0 - 16
swapi/swapi.h

@@ -146,22 +146,6 @@
 #define MAX_LINE_CHARS 192
 #endif
 
-#ifndef __cplusplus
-
-#ifndef bool
-#define bool unsigned char
-#endif
-
-#ifndef true
-#define true 1
-#endif
-
-#ifndef false
-#define false 0
-#endif
-
-#endif
-
 #ifndef BOOL
 #define BOOL int
 #endif

+ 1 - 0
swapi/swdir.c

@@ -21,6 +21,7 @@ int sw_dir_create(const char *dir)
 	if(!dir) return -1;
 	
 	strncpy(path1, dir, sizeof(path1)-1);
+	path1[sizeof(path1) - 1] = '\0';
 	xstrcharreplace(path1, '\\', '/');
 	ptr1 = path1;
 	while(ptr1 < path1+strlen(path1))

+ 1 - 1
swapi/swhash.c

@@ -308,7 +308,7 @@ static int32_t ch_insert(hash_t htable, const void *key, int32_t keylen, const v
   newelem = (struct elem *)sw_heap_malloc(sizeof(struct elem));
   if(!newelem) { errno = ENOMEM; goto ErrorP; }
 
-  keycopy = (struct elem *)sw_heap_malloc(keylen*sizeof(char));
+  keycopy = (void *)sw_heap_malloc(keylen*sizeof(char));
   if(!keycopy) { errno = ENOMEM; goto ErrorP; }
   else memcpy(keycopy, key, keylen);
 

+ 0 - 3
swapi/swmem.c

@@ -91,9 +91,6 @@ void sw_mem_destroy(void *hMem)
 	if(xm->hMutex)
 	{
 		sw_mutex_destroy(xm->hMutex);
-#ifdef LINUX
-		free(xm->hMutex);
-#endif
 	}
 	
 	memset(xm, 0, sizeof(SMemHeader));

+ 9 - 9
swapi/swrand.c

@@ -12,16 +12,16 @@
 /* 产生一个特定范围内的随机数, 并返回结果 */
 unsigned int xrand32(unsigned int range)
 {
-	struct timespec ts = {0};
-	unsigned int randNum = 0;
+  static __thread uint32_t seed = 0;
 
-	if((int)range < 0) return -1;
+	if(seed == 0) seed = (uint32_t)time(NULL);
+	if(seed == 0) seed = 2463534242u;
 
-	clock_gettime(CLOCK_REALTIME, &ts);
-	srand((unsigned int)(ts.tv_sec + ts.tv_nsec + rand()));
+  seed ^= seed << 13;
+  seed ^= seed >> 17;
+  seed ^= seed << 5;
 
-	if(range == 0) randNum = (unsigned int)((rand() << 16) | rand());
-	else randNum = ((unsigned int)((rand() << 16) | rand())) % range;
-	
-	return randNum;
+  if(range == 0) return seed;
+
+  return seed % range;
 }

+ 2 - 2
swapi/swtcp.c

@@ -55,12 +55,12 @@ int sw_tcp_listen(int skt, int max)
 int sw_tcp_accept(int skt, unsigned int *ip, unsigned short *port)
 {
 	struct sockaddr_in from;
-	unsigned int slen = sizeof(from);
+	socklen_t slen = sizeof(from);
 	
 	skt = accept(skt, (struct sockaddr *)&from, &slen);
 	if(0 <= skt)
 	{
-		if(ip) *ip = from.sin_addr.s_addr;
+		if(ip)   *ip   = from.sin_addr.s_addr;
 		if(port) *port = from.sin_port;
 	}
 	

+ 2 - 2
swapi/swthrd.c

@@ -242,7 +242,7 @@ bool sw_thrd_setPriority(void *hThrd, int priority)
 	
 	if(!pInfo) return false;
 	
-	sparam.__sched_priority = priority;
+	sparam.sched_priority = priority;
 	if(pthread_setschedparam(pInfo->nThrdID, MY_SCHEDPOLICY, &sparam) == 0) return true;
 	else return false;
 }
@@ -256,7 +256,7 @@ int sw_thrd_getPriority(void *hThrd)
 	
 	if(!pInfo) return -1;
 	
-	if(pthread_getschedparam(pInfo->nThrdID, &policy, &sparam) == 0) return sparam.__sched_priority;
+	if(pthread_getschedparam(pInfo->nThrdID, &policy, &sparam) == 0) return sparam.sched_priority;
 	else return -1;
 }
 

+ 5 - 5
swapi/swudp.c

@@ -97,13 +97,13 @@ int sw_udp_send(int skt, unsigned int ip, unsigned short port, char *buf, int se
 int sw_udp_recv(int skt, unsigned int *ip, unsigned short *port, char *buf, int buf_size)
 {
 	struct sockaddr_in from;
-	unsigned int len = sizeof(from);
+	socklen_t len = sizeof(from);
 	
-	len = recvfrom(skt, buf, buf_size, 0, (struct sockaddr *)&from, &len);
-	if(ip) *ip = from.sin_addr.s_addr;
-	if(port) *port = from.sin_port;
+	int ret = recvfrom(skt, buf, buf_size, 0, (struct sockaddr *)&from, &len);
+	if(ret >= 0 && ip)   *ip   = from.sin_addr.s_addr;
+	if(ret >= 0 && port) *port = from.sin_port;
 	
-	return len;
+	return ret;
 }
 
 /* 配置udp socket */