Julius 4.2
libsent/src/util/mybmalloc.c
説明を見る。
00001 
00031 /*
00032  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00033  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00034  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00035  * All rights reserved
00036  */
00037 
00038 #undef DEBUG                    /* output debug message */
00039 
00040 #include <sent/stddefs.h>
00041 
00042 static boolean mybmalloc_initialized = FALSE; 
00043 static unsigned int pagesize;           
00044 static unsigned int blocksize;  
00045 static int align;               
00046 static unsigned int align_mask; 
00047 
00052 static void
00053 mybmalloc_set_param()
00054 {
00055   unsigned int blockpagenum;
00056 
00057   /* block size should be rounded up by page size */
00058   pagesize = getpagesize();
00059   blockpagenum = (MYBMALLOC_BLOCK_SIZE + (pagesize - 1)) / pagesize;
00060   blocksize = pagesize * blockpagenum;
00061 
00062   /* alignment by a word (= pointer size?) */
00063 #ifdef NO_ALIGN_DOUBLE
00064   align = sizeof(void *);
00065 #else
00066   /* better for floating points */
00067   align = sizeof(double);
00068 #endif
00069   align_mask = ~(align - 1);    /* assume power or 2 */
00070   //jlog("Stat: mybmalloc: pagesize=%d blocksize=%d align=%d (bytes)\n", (int)pagesize, blocksize, align);
00071   
00072   mybmalloc_initialized = TRUE;
00073 }
00074 
00083 void *
00084 mybmalloc2(unsigned int size, BMALLOC_BASE **list)
00085 {
00086   void *allocated;
00087   BMALLOC_BASE *new;
00088 
00089   if (!mybmalloc_initialized) mybmalloc_set_param();  /* initialize if not yet */
00090   /* malloc segment should be aligned to a word boundary */
00091   size = (size + align - 1) & align_mask;
00092   if (*list == NULL || (*list)->now + size >= (*list)->end) {
00093     new = (BMALLOC_BASE *)mymalloc(sizeof(BMALLOC_BASE));
00094     if (size > blocksize) {
00095       /* large block, allocate a whole block */
00096       new->base = mymalloc(size);
00097       new->end = (char *)new->base + size;
00098     } else {
00099       /* allocate per blocksize */
00100       new->base = mymalloc(blocksize);
00101       new->end = (char *)new->base + blocksize;
00102     }
00103     new->now = (char *)new->base;
00104     new->next = (*list);
00105     *list = new;
00106   }
00107   /* return current pointer */
00108   allocated = (*list)->now;
00109   (*list)->now += size;
00110   return(allocated);
00111 }
00112 
00121 char *
00122 mybstrdup2(char *s, BMALLOC_BASE **list)
00123 {
00124   char *allocated;
00125   int size = strlen(s) + 1;
00126 
00127   allocated = mybmalloc2(size, list);
00128   memcpy(allocated, s, size);
00129   return(allocated);
00130 }
00131 
00137 void
00138 mybfree2(BMALLOC_BASE **list)
00139 {
00140   BMALLOC_BASE *b, *btmp;
00141   b = *list;
00142   while (b) {
00143     btmp = b->next;
00144     free(b->base);
00145     free(b);
00146     b = btmp;
00147   }
00148   *list = NULL;
00149 }