Skip to content

Commit 584d32f

Browse files
committed
Relying on faster coarse monotonic clock as time source, fixing a leak
on the HTTP POST module
1 parent 0b16838 commit 584d32f

4 files changed

Lines changed: 27 additions & 11 deletions

File tree

src/hal/tools.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,12 @@ char *memstr(char *haystack, char *needle, int size, char needlesize) {
336336
}
337337

338338
unsigned int millis() {
339-
struct timeval t;
340-
gettimeofday(&t, NULL);
341-
return t.tv_sec * 1000 + (t.tv_usec + 500) / 1000;
339+
struct timespec ts;
340+
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
341+
uint32_t sec = (uint32_t)ts.tv_sec;
342+
uint32_t sec_ms = (sec << 10) - (sec << 4) - (sec << 3);
343+
uint32_t nsec_ms = ts.tv_nsec / 1000000;
344+
return sec_ms + nsec_ms;
342345
}
343346

344347
void reverse(void *arr, size_t width) {

src/http_post.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int http_post_send(hal_jpegdata *jpeg) {
99
if (sockfd == -1)
1010
HAL_ERROR("http_post", "Socket creation failed!\n");
1111

12-
struct addrinfo *server_addr;
12+
struct addrinfo *server_addr = NULL;
1313
int ret = getaddrinfo(host_addr, "80", NULL, &server_addr);
1414
if (!ret) {
1515
const struct addrinfo *r;
@@ -62,7 +62,8 @@ int http_post_send(hal_jpegdata *jpeg) {
6262

6363
close(sockfd);
6464
}
65-
freeaddrinfo(server_addr);
65+
66+
if (server_addr) freeaddrinfo(server_addr);
6667

6768
return EXIT_SUCCESS;
6869
}
@@ -94,6 +95,13 @@ void *http_post_thread(void) {
9495
continue;
9596
}
9697
}
98+
99+
if (jpeg.data) {
100+
free(jpeg.data);
101+
jpeg.data = NULL;
102+
}
103+
104+
return NULL;
97105
}
98106

99107
void start_http_post_send() {
@@ -114,4 +122,4 @@ void start_http_post_send() {
114122

115123
void stop_http_post_send() {
116124
pthread_join(httpPostPid, NULL);
117-
}
125+
}

src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ int main(int argc, char *argv[]) {
109109
HAL_INFO("rtsp", "Server has closed!\n");
110110
}
111111

112+
if (app_config.http_post_enable)
113+
stop_http_post_send();
114+
112115
if (app_config.osd_enable)
113116
stop_region_handler();
114117

src/rtsp/common.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
fprintf(stderr, "XXX ERROR:%s:%s:%d XXX: ",__FILE__,__FUNCTION__,__LINE__);\
4949
fprintf(stderr, fmt, ## args);} while (0)
5050

51-
#define CHECK
51+
#define CHECK
5252

5353
/* debug assert (completely removed in release version) */
54-
//#define DASSERT(b,action)
54+
//#define DASSERT(b,action)
5555

5656
/* must success (check omitted in release version) */
5757
//#define MUST(b,action) do {if ((b)){}} while (0)
@@ -75,19 +75,21 @@
7575
/* Defining infinite time */
7676
#define FOREVER -1
7777

78-
#define CLEAR(x) memset (&(x), 0, sizeof (x))
7978
#define CLOSE(x) do{if((x)>0){close(x);(x) = 0;}}while(0)
8079
#define FCLOSE(x) do{if((x)!=NULL){fclose(x);(x) = NULL;}}while(0)
8180
#define ALLOC(t) (t = (typeof(t))calloc(1,sizeof(typeof(*(t)))))
8281
#define NEW(t) ((t *)calloc(1,sizeof(t)))
8382

83+
#ifndef CLEAR
84+
#define CLEAR(x) memset (&(x), 0, sizeof (x))
85+
#endif
8486
#define COPY(x,y) memcpy ((x),(y),sizeof (*(y)))
8587
//#define FREE(h) do { if(h){printf("freeing 0x%08x\n",(unsigned int)h);free(h);h=NULL;}else{DBG("freeing NULL pointer\n");}} while (0);
8688
#define FREE(h) do { if(h){free(h);h=NULL;}} while (0);
8789

8890
/* common object creation macro: calloc given pointer. when failed, perform 'action' */
8991
#define TALLOC(h,action) do {h=NULL;if(!(ALLOC(h))){ERR("No memory!\n");action;}} while (0)
90-
92+
9193
#ifndef max
9294
#define max(a,b) ((a)>(b)?(a):(b))
9395
#endif
@@ -162,4 +164,4 @@ gbl_set_quit(struct global_state_t *gbl)
162164
pthread_mutex_unlock(&gbl->mutex);
163165
}
164166

165-
#endif
167+
#endif

0 commit comments

Comments
 (0)