OSDN Git Service

* 2005/3/25
authorkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Fri, 25 Mar 2005 12:46:45 +0000 (12:46 +0000)
committerkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Fri, 25 Mar 2005 12:46:45 +0000 (12:46 +0000)
  起動の高速化。パーサーの高速化。
  パーサーのメモリ管理部分で時間をロスしていたので、修正。

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/modchxj/mod_chxj/trunk@40 1a406e8e-add9-4483-a2c8-d8cac5b7c224

src/qs_malloc.c
src/qs_malloc.h
src/qs_parse_string.h

index c862816..ebaeb58 100644 (file)
@@ -18,6 +18,9 @@
 #include "qs_log.h"
 #include "qs_malloc.h"
 
+static Pointer_Table* s_get_free_pointer_table(Doc* doc);
+static void s_add_to_allocated_list(Doc* doc, Pointer_Table* pt);
+static void s_add_to_free_list(Doc* doc, Pointer_Table* pt);
 /**
  * Init
  */
@@ -25,17 +28,31 @@ void
 qs_init_malloc(Doc* doc) 
 {
   int ii;
-  doc->alloc_size = 0;
+  doc->alloc_size = 0L;
   doc->pointer_table = (Pointer_Table*)malloc(sizeof(Pointer_Table)* QX_ALLOC_MAX);
   if (doc->pointer_table == NULL)
   {
     QX_LOGGER_FATAL("Out Of Memory");
   }
-  for (ii=0; ii<QX_ALLOC_MAX; ii++) {
+  for (ii=0; ii<QX_ALLOC_MAX; ii++) 
+  {
     doc->pointer_table[ii].address = 0;
-    doc->pointer_table[ii].size    = 0;
+    doc->pointer_table[ii].size    = 0L;
+    doc->pointer_table[ii].next    = NULL;
+    if (ii==0)
+    {
+      doc->free_list_head = &(doc->pointer_table[ii]);
+    }
+
+    if (ii < QX_ALLOC_MAX-1)
+    {
+      doc->pointer_table[ii].next = &(doc->pointer_table[ii+1]);
+    }
   }
+  doc->free_list_tail = &(doc->pointer_table[QX_ALLOC_MAX-1]);
   doc->do_init_flag = 1;
+  doc->allocated_list_head = NULL;
+  doc->allocated_list_tail = NULL;
 }
 
 /**
@@ -44,7 +61,7 @@ qs_init_malloc(Doc* doc)
 void*
 qs_malloc(Doc* doc, int size, const char* fname, int line) 
 {
-  int ii;
+  Pointer_Table* pt;
 
   if (doc->do_init_flag == 0) 
   {
@@ -65,71 +82,141 @@ qs_malloc(Doc* doc, int size, const char* fname, int line)
   sprintf(buffer,"malloc Address:[0x%x]", src);
   qs_log(doc,QX_LOG_DEBUG,fname,line,buffer);
 #endif
-  for (ii=0; ii<QX_ALLOC_MAX; ii++) 
-  {
-    if (doc->pointer_table[ii].address == 0) 
-    {
-      doc->pointer_table[ii].address = (unsigned int)src;
-      doc->pointer_table[ii].size = size;
-      doc->alloc_size += size;
-      QX_LOGGER_DEBUG("use new space");
-      break;
-    }
-  }
-  if (ii == QX_ALLOC_MAX) 
+
+  pt = s_get_free_pointer_table(doc);
+  if (pt == NULL)
   {
     QX_LOGGER_FATAL("Out Of Memory");
   }
+
+  pt->address      = (unsigned int)src;
+  pt->size         = size;
+  doc->alloc_size += size;
+
+  s_add_to_allocated_list(doc, pt);
+
   QX_LOGGER_DEBUG_INT("allocated memory size",doc->alloc_size);
   return src;
 }
 
+
+
+static void
+s_add_to_allocated_list(Doc* doc, Pointer_Table* pt)
+{
+  if (doc->allocated_list_head == NULL)
+  {
+    doc->allocated_list_head = pt;
+    doc->allocated_list_tail = pt;
+  }
+  else
+  {
+    doc->allocated_list_tail->next = pt;
+    doc->allocated_list_tail = pt;
+  }
+  pt->next = NULL;
+}
+
+
+static Pointer_Table*
+s_get_free_pointer_table(Doc* doc)
+{
+  Pointer_Table* pt = NULL;
+
+  if (doc->free_list_head != NULL)
+  {
+    pt = doc->free_list_head;
+    doc->free_list_head = doc->free_list_head->next;
+    pt->next = NULL;
+  }
+  return pt;
+}
+
 /**
  *it is a wrapper of "free()" function. 
  */
 void
 qs_free(Doc* doc, void *s, const char* fname, int line) 
 {
-  int ii;
+  Pointer_Table* pp;
+  Pointer_Table* pp_prev;
 #ifdef MALLOC_TRACE
   char buffer[BUFSIZ];
   sprintf(buffer,"free Address:[0x%x]", s);
   qs_log(doc,QX_LOG_DEBUG,fname,line,buffer);
 #endif
-  for (ii=0; ii<QX_ALLOC_MAX; ii++) 
+
+  pp_prev = NULL;
+  for (pp = doc->allocated_list_head; pp; pp = pp->next)
   {
-    if (doc->pointer_table[ii].address == (unsigned int)s) 
+    if (pp->address == (unsigned int)s)
     {
-      doc->pointer_table[ii].address = 0;
-      doc->alloc_size -= doc->pointer_table[ii].size;
-      doc->pointer_table[ii].size = 0;
+      free((void*)pp->address);
+      doc->alloc_size -= pp->size;
+      pp->size = 0;
+
+      break;
     }
+    pp_prev = pp;
   }
-  free(s);
+  if (pp == NULL)
+  {
+    return;
+  }
+  if (pp_prev == NULL)
+  {
+    doc->allocated_list_head = NULL;
+    doc->allocated_list_tail = NULL;
+  }
+  else 
+  {
+    pp_prev->next = pp->next;
+    pp->next = NULL;
+  }
+  s_add_to_free_list(doc, pp);
+
 }
 
 
+
+static void
+s_add_to_free_list(Doc* doc, Pointer_Table* pt)
+{
+  if (doc->free_list_head == NULL)
+  {
+    doc->free_list_head = pt;
+    doc->free_list_tail = pt;
+  }
+  else
+  {
+    doc->free_list_tail->next = pt;
+    doc->free_list_tail = pt;
+  }
+  pt->next = NULL;
+} 
+
+
 void
 qs_all_free(Doc* doc, const char* fname, int line) 
 {
-  int ii;
+  Pointer_Table* pp;
 
   if (doc->do_init_flag) 
   {
-    for (ii=0; ii < QX_ALLOC_MAX; ii++) 
+    for (pp = doc->allocated_list_head; pp; pp = pp->next)
     {
-      if (doc->pointer_table != NULL 
-      &&  doc->pointer_table[ii].address != 0) 
-      {
-        free((void*)(doc->pointer_table[ii].address));
-        doc->pointer_table[ii].address = 0;
-        doc->alloc_size -= doc->pointer_table[ii].size;
-        doc->pointer_table[ii].size =0;
-      }
+      free((void*)pp->address);
+      pp->address = 0;
+      pp->size =0;
     }
-    free(doc->pointer_table);
-    doc->pointer_table = NULL;
+    free((void*)doc->pointer_table);
+    doc->pointer_table       = NULL;
+    doc->allocated_list_head = NULL;
+    doc->allocated_list_tail = NULL;
+    doc->free_list_head      = NULL;
+    doc->free_list_tail      = NULL;
   }
+  doc->alloc_size = 0L;
 
   doc->do_init_flag = 0;
 }
index 5d8c0e3..5837f1a 100644 (file)
@@ -18,7 +18,7 @@
 #define __QS_MALLOC_H__
 #include "qs_parse_string.h"
 
-#define QX_ALLOC_MAX_SIZE (1024*1024)
+#define QX_ALLOC_MAX_SIZE (1024*1024*5)
 
 void* qs_malloc(Doc* doc, int size, const char* fname, int line);
 void qs_init_malloc(Doc* doc);
index 84008e0..f15c2ba 100644 (file)
@@ -24,7 +24,6 @@
 /**
  * Max of memory allocation times.
  */
-/* #define QX_ALLOC_MAX   (4096) */
 #define QX_ALLOC_MAX   (100*1024)
 
 /**
@@ -117,7 +116,7 @@ typedef struct _node {
 
 typedef struct pointer_table_t {
   unsigned int address;
-  unsigned int size;
+  unsigned long size;
   struct pointer_table_t* next;
 } Pointer_Table;
 
@@ -132,10 +131,13 @@ typedef struct _doc {
   Node*         root_node;
 
   int           do_init_flag;
-  unsigned int  alloc_size;
+  unsigned long alloc_size;
 
   Pointer_Table* pointer_table;
-  Pointer_Table* free_list;
+  Pointer_Table* free_list_head;
+  Pointer_Table* free_list_tail;
+  Pointer_Table* allocated_list_head;
+  Pointer_Table* allocated_list_tail;
 
   ParseMode_t    parse_mode;