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

📄 local_services.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
       scan != NULL; prev=scan, scan=scan->next)
    if (scan == head)
      break;
  if (scan == NULL)
    local_error("Attempting to free a chunk of memory which was not "
                "allocated using `local_malloc' or which has been "
                "corrupted!");
  assert((head->id > 0) && (head->id < local_next_id) &&
         (head->num_bytes <= stats->current_bytes));
  stats->current_bytes -= head->num_bytes;
  local_current_bytes -= head->num_bytes;
  if (prev != NULL)
    prev->next = head->next;
  else
    {
      stats->head = head->next;
      if (stats->head == NULL)
        assert(stats->current_bytes == 0);
    }
  free(head);
}

/*****************************************************************************/
/* EXTERN                     local_memory_report                            */
/*****************************************************************************/

void
  local_memory_report(void)
{
  local_mem_stats_ptr scan;
  int total_leakage, total_bytes;

  if (local_next_id == 0)
    {
      local_printf(4,76,"\nCannot print memory report!!  Statistics "
                   "collection was not enabled by call to "
                   "`local_memory_collect_stats'!\n\n");
      return;
    }

  local_printf(0,76,"MEMORY USAGE REPORT");
  if (mem_stats == NULL)
    {
      local_printf(4,76,"Nothing allocated!!\n\n");
      return;
    }
  total_leakage = total_bytes = 0;
  for (scan=mem_stats; scan != NULL; scan=scan->next)
    {
      local_printf(4,72,"Key = \"%s\"",scan->key);
      local_printf(50,28,"Peak bytes = %g",(double)(scan->peak_bytes));
      if (scan->current_bytes > 0)
        {
          total_leakage += scan->current_bytes;
          local_printf(8,70,"*** %d bytes not yet deallocated!\nIdentifier "
                       "of first non-deallocated memory chunk is %d;\n"
                       "Supply this to the `-mem' program argument, in order "
                       "to catch the point at which this memory was first "
                       "allocated!",scan->current_bytes,scan->head->id);
        }
      total_bytes += scan->total_bytes;
    }
  local_printf(4,70,"OVERALL MEMORY USAGE");
  local_printf(50,28,"Peak bytes = %g",(double) local_peak_bytes);
  local_printf(0,76,"\n");
  if (total_leakage)
    {
      local_printf(0,76,"WARNING");
      local_printf(4,70,"You have memory leaks!!  Total number of bytes "
                   "allocated but not yet deallocated is %d!\n\n",
                   total_leakage);
    }
}


/* ========================================================================= */
/* ----------------------------- Termination ------------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* EXTERN                         local_exit                                 */
/*****************************************************************************/

void
  local_exit(int exit_code)
{
  exit(exit_code);
}

/* ========================================================================= */
/* ------------------------------- Messages -------------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* EXTERN                        local_vprintf                               */
/*****************************************************************************/

void
  local_vprintf(int indent, int width, char *format, va_list arg_ptr)
{
  char conversion_spec[21], format_buf[81];
  char *line, *phrase, *sp, *dp;
  int i, line_length, remaining_width, phrase_length, alt_src;
  int done;
  FILE *dest;

  dest = stdout;
  assert(width > 0);
  line = (char *) malloc((size_t)(width+indent+2));
  phrase = (char *) malloc((size_t)(width+2));
  sp = format;
  remaining_width = width;
  line_length = phrase_length = 0;
  dp = phrase;
  alt_src = 0;
  done = (*sp == '\0');
  while (!done)
    {
      if ((*sp == '\0') && alt_src)
        {
          sp = format;
          alt_src = 0;
          continue;
        }
      else if ((*sp == '\0') || (*sp == ' ') || (*sp == '\t') || (*sp == '\n') ||
               (phrase_length == width))
        { /* End of phrase. */
          int phrase_offset, newline, linebreak, tmp_phrase_length;

          while ((*sp == ' ') || (*sp == '\t'))
            sp++;
          newline = (*sp == '\n');
          if (*sp == '\n')
            for (sp++; (*sp == ' ') || (*sp == '\t'); sp++);
          done = (*sp == '\0');
          if (done)
            newline = 1;
          if ((phrase_length == 0) && (!newline))
            continue;
          phrase_offset = 0;
          do {
            tmp_phrase_length = phrase_length;
            linebreak = newline;
            if (phrase_length > remaining_width)
              {
                if (line_length == 0)
                  tmp_phrase_length = width;
                else
                  tmp_phrase_length = 0;
                linebreak = 1;
              }
            if ((line_length > 0) && (tmp_phrase_length > 0))
              line[line_length++] = ' ';
            strncpy(line+line_length,phrase+phrase_offset,tmp_phrase_length);
            line_length += tmp_phrase_length;
            phrase_offset += tmp_phrase_length;
            phrase_length -= tmp_phrase_length;
            remaining_width -= tmp_phrase_length + 1;
            if (linebreak)
              {
                line[line_length] = '\0';
                for (i=0; i < indent; i++)
                  putc(' ',dest);
                fprintf(dest,"%s\n",line);
                line_length = 0;
                remaining_width = width;
              }
          } while (phrase_length > 0);
          dp = phrase;
          assert(phrase_length == 0);
        }
      else if ((*sp == '%') && (!alt_src))
        {
          int conversion_spec_length;
          char *scan;

          assert(sp[1] != '\0');
          if (sp[1] == '%')
            { sp+=2; *(dp++) = '%'; phrase_length++; continue; }
          scan = strpbrk(sp,"sdioxXucfeEgGp");
          if (scan == NULL)
            { sp++; continue; }
          format = scan+1; /* Place from which to continue on. */
          if (*scan == 's')
            { /* String printing. */
              alt_src = 1;
              sp = va_arg(arg_ptr,char *);
              continue;
            }
          conversion_spec_length = (scan-sp)+1;
          assert(conversion_spec_length < 20);
          strncpy(conversion_spec,sp,conversion_spec_length);
          conversion_spec[conversion_spec_length] = '\0';
          *format_buf = '\0';
          if (strchr("dioxXucp",(int)(*scan)) != NULL)
            {
              int arg;

              arg = va_arg(arg_ptr,int);
              sprintf(format_buf,conversion_spec,arg);
            }
          else
            {
              double arg;

              arg = va_arg(arg_ptr,double);
              sprintf(format_buf,conversion_spec,arg);
            }
          alt_src = 1;
          sp = format_buf;
        }
      else
        {
          *(dp++) = *(sp++);
          phrase_length++;
        }
    }
  free(line);
  free(phrase);
}

/*****************************************************************************/
/* EXTERN                        local_printf                                */
/*****************************************************************************/

void
  local_printf(int indent, int width, char *format, ...)
{
  va_list arg_ptr;

  va_start(arg_ptr,format);
  local_vprintf(indent,width,format,arg_ptr);
  va_end(arg_ptr);
}

/*****************************************************************************/
/* EXTERN                         local_error                                */
/*****************************************************************************/

void
  local_error(char *format, ...)
{
  va_list arg_ptr;

  va_start(arg_ptr,format);
  local_printf(0,76,"ERROR");
  local_vprintf(6,70,format,arg_ptr);
  va_end(arg_ptr);
  local_exit(-1);
}

⌨️ 快捷键说明

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