诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

490 lines
12 KiB

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef CURLDEBUG
  24. #include <curl/curl.h>
  25. #define _MPRINTF_REPLACE
  26. #include <curl/mprintf.h>
  27. #include "urldata.h"
  28. #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
  29. #include "curl_memory.h"
  30. #include "memdebug.h"
  31. #ifndef HAVE_ASSERT_H
  32. # define assert(x) Curl_nop_stmt
  33. #endif
  34. /*
  35. * Until 2011-08-17 libcurl's Memory Tracking feature also performed
  36. * automatic malloc and free filling operations using 0xA5 and 0x13
  37. * values. Our own preinitialization of dynamically allocated memory
  38. * might be useful when not using third party memory debuggers, but
  39. * on the other hand this would fool memory debuggers into thinking
  40. * that all dynamically allocated memory is properly initialized.
  41. *
  42. * As a default setting, libcurl's Memory Tracking feature no longer
  43. * performs preinitialization of dynamically allocated memory on its
  44. * own. If you know what you are doing, and really want to retain old
  45. * behavior, you can achieve this compiling with preprocessor symbols
  46. * CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate
  47. * values.
  48. */
  49. #ifdef CURL_MT_MALLOC_FILL
  50. # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff)
  51. # error "invalid CURL_MT_MALLOC_FILL or out of range"
  52. # endif
  53. #endif
  54. #ifdef CURL_MT_FREE_FILL
  55. # if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff)
  56. # error "invalid CURL_MT_FREE_FILL or out of range"
  57. # endif
  58. #endif
  59. #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL)
  60. # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL)
  61. # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL"
  62. # endif
  63. #endif
  64. #ifdef CURL_MT_MALLOC_FILL
  65. # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len))
  66. #else
  67. # define mt_malloc_fill(buf,len) Curl_nop_stmt
  68. #endif
  69. #ifdef CURL_MT_FREE_FILL
  70. # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len))
  71. #else
  72. # define mt_free_fill(buf,len) Curl_nop_stmt
  73. #endif
  74. struct memdebug {
  75. size_t size;
  76. union {
  77. curl_off_t o;
  78. double d;
  79. void * p;
  80. } mem[1];
  81. /* I'm hoping this is the thing with the strictest alignment
  82. * requirements. That also means we waste some space :-( */
  83. };
  84. /*
  85. * Note that these debug functions are very simple and they are meant to
  86. * remain so. For advanced analysis, record a log file and write perl scripts
  87. * to analyze them!
  88. *
  89. * Don't use these with multithreaded test programs!
  90. */
  91. #define logfile curl_debuglogfile
  92. FILE *curl_debuglogfile = NULL;
  93. static bool memlimit = FALSE; /* enable memory limit */
  94. static long memsize = 0; /* set number of mallocs allowed */
  95. /* this sets the log file name */
  96. void curl_memdebug(const char *logname)
  97. {
  98. if(!logfile) {
  99. if(logname && *logname)
  100. logfile = fopen(logname, "w");
  101. else
  102. logfile = stderr;
  103. #ifdef MEMDEBUG_LOG_SYNC
  104. /* Flush the log file after every line so the log isn't lost in a crash */
  105. setvbuf(logfile, (char *)NULL, _IOLBF, 0);
  106. #endif
  107. }
  108. }
  109. /* This function sets the number of malloc() calls that should return
  110. successfully! */
  111. void curl_memlimit(long limit)
  112. {
  113. if(!memlimit) {
  114. memlimit = TRUE;
  115. memsize = limit;
  116. }
  117. }
  118. /* returns TRUE if this isn't allowed! */
  119. static bool countcheck(const char *func, int line, const char *source)
  120. {
  121. /* if source is NULL, then the call is made internally and this check
  122. should not be made */
  123. if(memlimit && source) {
  124. if(!memsize) {
  125. if(source) {
  126. /* log to file */
  127. curl_memlog("LIMIT %s:%d %s reached memlimit\n",
  128. source, line, func);
  129. /* log to stderr also */
  130. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  131. source, line, func);
  132. }
  133. SET_ERRNO(ENOMEM);
  134. return TRUE; /* RETURN ERROR! */
  135. }
  136. else
  137. memsize--; /* countdown */
  138. /* log the countdown */
  139. if(source)
  140. curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
  141. source, line, memsize);
  142. }
  143. return FALSE; /* allow this */
  144. }
  145. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  146. {
  147. struct memdebug *mem;
  148. size_t size;
  149. assert(wantedsize != 0);
  150. if(countcheck("malloc", line, source))
  151. return NULL;
  152. /* alloc at least 64 bytes */
  153. size = sizeof(struct memdebug)+wantedsize;
  154. mem = (Curl_cmalloc)(size);
  155. if(mem) {
  156. /* fill memory with junk */
  157. mt_malloc_fill(mem->mem, wantedsize);
  158. mem->size = wantedsize;
  159. }
  160. if(source)
  161. curl_memlog("MEM %s:%d malloc(%zu) = %p\n",
  162. source, line, wantedsize,
  163. mem ? (void *)mem->mem : (void *)0);
  164. return (mem ? mem->mem : NULL);
  165. }
  166. void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
  167. int line, const char *source)
  168. {
  169. struct memdebug *mem;
  170. size_t size, user_size;
  171. assert(wanted_elements != 0);
  172. assert(wanted_size != 0);
  173. if(countcheck("calloc", line, source))
  174. return NULL;
  175. /* alloc at least 64 bytes */
  176. user_size = wanted_size * wanted_elements;
  177. size = sizeof(struct memdebug) + user_size;
  178. mem = (Curl_ccalloc)(1, size);
  179. if(mem)
  180. mem->size = user_size;
  181. if(source)
  182. curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
  183. source, line, wanted_elements, wanted_size,
  184. mem ? (void *)mem->mem : (void *)0);
  185. return (mem ? mem->mem : NULL);
  186. }
  187. char *curl_dostrdup(const char *str, int line, const char *source)
  188. {
  189. char *mem;
  190. size_t len;
  191. assert(str != NULL);
  192. if(countcheck("strdup", line, source))
  193. return NULL;
  194. len=strlen(str)+1;
  195. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  196. if(mem)
  197. memcpy(mem, str, len);
  198. if(source)
  199. curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
  200. source, line, (void *)str, len, (void *)mem);
  201. return mem;
  202. }
  203. #if defined(WIN32) && defined(UNICODE)
  204. wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
  205. {
  206. wchar_t *mem;
  207. size_t wsiz, bsiz;
  208. assert(str != NULL);
  209. if(countcheck("wcsdup", line, source))
  210. return NULL;
  211. wsiz = wcslen(str) + 1;
  212. bsiz = wsiz * sizeof(wchar_t);
  213. mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
  214. if(mem)
  215. memcpy(mem, str, bsiz);
  216. if(source)
  217. curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
  218. source, line, (void *)str, bsiz, (void *)mem);
  219. return mem;
  220. }
  221. #endif
  222. /* We provide a realloc() that accepts a NULL as pointer, which then
  223. performs a malloc(). In order to work with ares. */
  224. void *curl_dorealloc(void *ptr, size_t wantedsize,
  225. int line, const char *source)
  226. {
  227. struct memdebug *mem=NULL;
  228. size_t size = sizeof(struct memdebug)+wantedsize;
  229. assert(wantedsize != 0);
  230. if(countcheck("realloc", line, source))
  231. return NULL;
  232. #ifdef __INTEL_COMPILER
  233. # pragma warning(push)
  234. # pragma warning(disable:1684)
  235. /* 1684: conversion from pointer to same-sized integral type */
  236. #endif
  237. if(ptr)
  238. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  239. #ifdef __INTEL_COMPILER
  240. # pragma warning(pop)
  241. #endif
  242. mem = (Curl_crealloc)(mem, size);
  243. if(source)
  244. curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
  245. source, line, (void *)ptr, wantedsize,
  246. mem ? (void *)mem->mem : (void *)0);
  247. if(mem) {
  248. mem->size = wantedsize;
  249. return mem->mem;
  250. }
  251. return NULL;
  252. }
  253. void curl_dofree(void *ptr, int line, const char *source)
  254. {
  255. struct memdebug *mem;
  256. assert(ptr != NULL);
  257. #ifdef __INTEL_COMPILER
  258. # pragma warning(push)
  259. # pragma warning(disable:1684)
  260. /* 1684: conversion from pointer to same-sized integral type */
  261. #endif
  262. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  263. #ifdef __INTEL_COMPILER
  264. # pragma warning(pop)
  265. #endif
  266. /* destroy */
  267. mt_free_fill(mem->mem, mem->size);
  268. /* free for real */
  269. (Curl_cfree)(mem);
  270. if(source)
  271. curl_memlog("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
  272. }
  273. curl_socket_t curl_socket(int domain, int type, int protocol,
  274. int line, const char *source)
  275. {
  276. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  277. "FD %s:%d socket() = %d\n" :
  278. (sizeof(curl_socket_t) == sizeof(long)) ?
  279. "FD %s:%d socket() = %ld\n" :
  280. "FD %s:%d socket() = %zd\n" ;
  281. curl_socket_t sockfd = socket(domain, type, protocol);
  282. if(source && (sockfd != CURL_SOCKET_BAD))
  283. curl_memlog(fmt, source, line, sockfd);
  284. return sockfd;
  285. }
  286. #ifdef HAVE_SOCKETPAIR
  287. int curl_socketpair(int domain, int type, int protocol,
  288. curl_socket_t socket_vector[2],
  289. int line, const char *source)
  290. {
  291. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  292. "FD %s:%d socketpair() = %d %d\n" :
  293. (sizeof(curl_socket_t) == sizeof(long)) ?
  294. "FD %s:%d socketpair() = %ld %ld\n" :
  295. "FD %s:%d socketpair() = %zd %zd\n" ;
  296. int res = socketpair(domain, type, protocol, socket_vector);
  297. if(source && (0 == res))
  298. curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
  299. return res;
  300. }
  301. #endif
  302. curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
  303. int line, const char *source)
  304. {
  305. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  306. "FD %s:%d accept() = %d\n" :
  307. (sizeof(curl_socket_t) == sizeof(long)) ?
  308. "FD %s:%d accept() = %ld\n" :
  309. "FD %s:%d accept() = %zd\n" ;
  310. struct sockaddr *addr = (struct sockaddr *)saddr;
  311. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  312. curl_socket_t sockfd = accept(s, addr, addrlen);
  313. if(source && (sockfd != CURL_SOCKET_BAD))
  314. curl_memlog(fmt, source, line, sockfd);
  315. return sockfd;
  316. }
  317. /* separate function to allow libcurl to mark a "faked" close */
  318. void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  319. {
  320. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  321. "FD %s:%d sclose(%d)\n" :
  322. (sizeof(curl_socket_t) == sizeof(long)) ?
  323. "FD %s:%d sclose(%ld)\n" :
  324. "FD %s:%d sclose(%zd)\n" ;
  325. if(source)
  326. curl_memlog(fmt, source, line, sockfd);
  327. }
  328. /* this is our own defined way to close sockets on *ALL* platforms */
  329. int curl_sclose(curl_socket_t sockfd, int line, const char *source)
  330. {
  331. int res=sclose(sockfd);
  332. curl_mark_sclose(sockfd, line, source);
  333. return res;
  334. }
  335. FILE *curl_fopen(const char *file, const char *mode,
  336. int line, const char *source)
  337. {
  338. FILE *res=fopen(file, mode);
  339. if(source)
  340. curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  341. source, line, file, mode, (void *)res);
  342. return res;
  343. }
  344. #ifdef HAVE_FDOPEN
  345. FILE *curl_fdopen(int filedes, const char *mode,
  346. int line, const char *source)
  347. {
  348. FILE *res=fdopen(filedes, mode);
  349. if(source)
  350. curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  351. source, line, filedes, mode, (void *)res);
  352. return res;
  353. }
  354. #endif
  355. int curl_fclose(FILE *file, int line, const char *source)
  356. {
  357. int res;
  358. assert(file != NULL);
  359. res=fclose(file);
  360. if(source)
  361. curl_memlog("FILE %s:%d fclose(%p)\n",
  362. source, line, (void *)file);
  363. return res;
  364. }
  365. #define LOGLINE_BUFSIZE 1024
  366. /* this does the writting to the memory tracking log file */
  367. void curl_memlog(const char *format, ...)
  368. {
  369. char *buf;
  370. int nchars;
  371. va_list ap;
  372. if(!logfile)
  373. return;
  374. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  375. if(!buf)
  376. return;
  377. va_start(ap, format);
  378. nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  379. va_end(ap);
  380. if(nchars > LOGLINE_BUFSIZE - 1)
  381. nchars = LOGLINE_BUFSIZE - 1;
  382. if(nchars > 0)
  383. fwrite(buf, 1, nchars, logfile);
  384. (Curl_cfree)(buf);
  385. }
  386. #endif /* CURLDEBUG */