⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 error.c

📁 减少内存碎片的malloc分配函数
💻 C
📖 第 1 页 / 共 2 页
字号:
		     dmalloc_logpath);  }    (void)close(outfile_fd);  outfile_fd = -1;  /* we don't call open here, we'll let the next message do it */}#if LOG_PNT_TIMEVAL/* * char *_dmalloc_ptimeval * * DESCRIPTION: * * Print the time into local buffer. * * RETURNS: * * Poiner to the buf argument. * * ARGUMENTS: * * timeval_p -> Pointer to a time value. * * buf -> Internal buffer into which we are writing the time. * * buf_size -> Size of the buffer. * * elapsed_b -> Set to 1 to dump the elapsed instead of global time. */char	*_dmalloc_ptimeval(const TIMEVAL_TYPE *timeval_p, char *buf,			   const int buf_size, const int elapsed_b){  unsigned long	hrs, mins, secs, usecs;    secs = timeval_p->tv_sec;  usecs = timeval_p->tv_usec;    if (elapsed_b) {    if (usecs >= _dmalloc_start.tv_usec) {      usecs -= _dmalloc_start.tv_usec;    }    else {      usecs = _dmalloc_start.tv_usec - usecs;      secs--;    }    secs -= _dmalloc_start.tv_sec;        hrs = secs / SECS_IN_HOUR;    mins = (secs / SECS_IN_MIN) % MINS_IN_HOUR;    secs %= SECS_IN_MIN;        (void)loc_snprintf(buf, buf_size, "%lu:%02lu:%02lu.%06lu",		       hrs, mins, secs, usecs);  }  else {    (void)loc_snprintf(buf, buf_size, "%lu.%06lu",		       secs, usecs);  }    return buf;}#endif/* NOTE: we do the ifdef this way for fillproto */#if LOG_PNT_TIMEVAL == 0 && HAVE_TIME/* * char *_dmalloc_ptime * * DESCRIPTION: * * Print the time into local buffer. * * RETURNS: * * Poiner to the buf argument. * * ARGUMENTS: * * time_p -> Pointer to a time value. * * buf -> Internal buffer into which we are writing the time. * * buf_size -> Size of the buffer. * * elapsed_b -> Set to 1 to dump the elapsed instead of global time. */char	*_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size,			const int elapsed_b){  unsigned long	hrs, mins, secs;    secs = *time_p;    if (elapsed_b) {    secs -= _dmalloc_start;        hrs = secs / SECS_IN_HOUR;    mins = (secs / SECS_IN_MIN) % MINS_IN_HOUR;    secs %= SECS_IN_MIN;        (void)loc_snprintf(buf, buf_size, "%lu:%02lu:%02lu", hrs, mins, secs);  }  else {    (void)loc_snprintf(buf, buf_size, "%lu", secs);  }    return buf;}#endif/* * void _dmalloc_vmessage * * DESCRIPTION: * * Message writer with vprintf like arguments which adds a line to the * dmalloc logfile. * * RETURNS: * * None. * * ARGUMENTS: * * format -> Printf-style format statement. * * args -> Already converted pointer to a stdarg list. */void	_dmalloc_vmessage(const char *format, va_list args){  char	*str_p, *bounds_p;  int	len;    str_p = message_str;  bounds_p = str_p + sizeof(message_str);    /* no logpath and no print then no workie */  if (dmalloc_logpath == NULL      && ! BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) {    return;  }  #if HAVE_GETPID && LOG_REOPEN  if (dmalloc_logpath != NULL) {    char	*log_p;        /*     * This static pid will be checked to make sure it doesn't change.     * We make it long in case it's big and we hope it will promote if     * not.     */    static long		current_pid = -1;    long		new_pid;        new_pid = getpid();    if (new_pid != current_pid) {      /* NOTE: we need to do this _before_ the reopen otherwise we recurse */      current_pid = new_pid;            /* if the new pid doesn't match the old one then reopen it */      if (current_pid >= 0) {		/* this only works if there is a %p in the logpath */	for (log_p = dmalloc_logpath; *log_p != '\0'; log_p++) {	  if (*log_p == '%' && *(log_p + 1) == 'p') {	    _dmalloc_reopen_log();	    break;	  }	}      }    }  }#endif    /* do we need to open the logfile? */  if (dmalloc_logpath != NULL && outfile_fd < 0) {    _dmalloc_open_log();  }  #if HAVE_TIME#if LOG_TIME_NUMBER  {    long	now;    now = time(NULL);    str_p += loc_snprintf(str_p, bounds_p - str_p, "%ld: ", now);  }#endif /* LOG_TIME_NUMBER */#if HAVE_CTIME#if LOG_CTIME_STRING  {    TIME_TYPE	now;    now = time(NULL);    str_p += loc_snprintf(str_p, bounds_p - str_p, "%.24s: ", ctime(&now));  }#endif /* LOG_CTIME_STRING */#endif /* HAVE_CTIME */#endif /* HAVE_TIME */  #if LOG_ITERATION  /* add the iteration number */  str_p += loc_snprintf(str_p, bounds_p - str_p, "%lu: ", _dmalloc_iter_c);#endif#if LOG_PID && HAVE_GETPID  {    /* we make it long in case it's big and we hope it will promote if not */    long	our_pid = getpid();        /* add the pid to the log file */    str_p += loc_snprintf(str_p, bounds_p - str_p, "p%ld: ", our_pid);  }#endif    /*   * NOTE: the following code, as well as the function definition   * above, would need to be altered to conform to non-ANSI-C   * specifications if necessary.   */    /* write the format + info into str */  len = loc_vsnprintf(str_p, bounds_p - str_p, format, args);    /* was it an empty format? */  if (len == 0) {    return;  }  str_p += len;    /* tack on a '\n' if necessary */  if (*(str_p - 1) != '\n') {    *str_p++ = '\n';    *str_p = '\0';  }  len = str_p - message_str;    /* do we need to write the message to the logfile */  if (dmalloc_logpath != NULL) {    (void)write(outfile_fd, message_str, len);  }    /* do we need to print the message? */  if (BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) {    (void)write(STDERR, message_str, len);  }}/* * void _dmalloc_die * * DESCRIPTION: * * Kill the program because of an internal malloc error. * * RETURNS: * * None. * * ARGUMENTS: * * silent_b -> Set to 1 to not drop log entries. */void	_dmalloc_die(const int silent_b){  char	*stop_str;  int	len;    if (! silent_b) {    if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_ABORT)) {      stop_str = "dumping";    }    else {      stop_str = "halting";    }        /* print a message that we are going down */    len = loc_snprintf(error_str, sizeof(error_str),		       "debug-malloc library: %s program, fatal error\r\n",		       stop_str);    (void)write(STDERR, error_str, len);    if (dmalloc_errno != ERROR_NONE) {      len = loc_snprintf(error_str, sizeof(error_str),			 "   Error: %s (err %d)\r\n",			 dmalloc_strerror(dmalloc_errno), dmalloc_errno);      (void)write(STDERR, error_str, len);    }  }    /*   * set this in case the following generates a recursive call for   * some dumb reason   */  _dmalloc_aborting_b = 1;    /* do I need to drop core? */  if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_ABORT)      || BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_DUMP)) {#if USE_ABORT    abort();#else#ifdef KILL_PROCESS    KILL_PROCESS;#endif#endif  }    /*   * NOTE: this should not be exit() because fclose will free, etc   */  _exit(1);}/* * void dmalloc_error * * DESCRIPTION: * * Handler of error codes.  The caller should have set the errno already * * RETURNS: * * None. * * ARGUMENTS: * * func -> Function name for the logs. */void	dmalloc_error(const char *func){  /* do we need to log or print the error? */  if (dmalloc_logpath != NULL      || BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) {        /* default str value */    if (func == NULL) {      func = "_malloc_error";    }        /* print the malloc error message */    dmalloc_message("ERROR: %s: %s (err %d)",		    func, dmalloc_strerror(dmalloc_errno), dmalloc_errno);  }    /* do I need to abort? */  if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_ABORT)) {    _dmalloc_die(0);  }  #if HAVE_FORK  /* how about just drop core? */  if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_DUMP)) {    if (fork() == 0) {      _dmalloc_die(1);    }  }#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -