my_once.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. /* Not MT-SAFE */
  18. #ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */
  19. #undef SAFEMALLOC
  20. #endif
  21. #include "mysys_priv.h"
  22. #include "my_static.h"
  23. #include "mysys_err.h"
  24. /* alloc for things we don't nead to free */
  25. /* No DBUG_ENTER... here to get smaller dbug-startup */
  26. gptr my_once_alloc(unsigned int Size, myf MyFlags)
  27. {
  28.   uint get_size,max_left;
  29.   gptr point;
  30.   reg1 USED_MEM *next;
  31.   reg2 USED_MEM **prev;
  32.   Size= ALIGN_SIZE(Size);
  33.   prev= &my_once_root_block;
  34.   max_left=0;
  35.   for (next=my_once_root_block ; next && next->left < Size ; next= next->next)
  36.   {
  37.     if (next->left > max_left)
  38.       max_left=next->left;
  39.     prev= &next->next;
  40.   }
  41.   if (! next)
  42.   { /* Time to alloc new block */
  43.     get_size= Size+ALIGN_SIZE(sizeof(USED_MEM));
  44.     if (max_left*4 < my_once_extra && get_size < my_once_extra)
  45.       get_size=my_once_extra; /* Normal alloc */
  46.     if ((next = (USED_MEM*) malloc(get_size)) == 0)
  47.     {
  48.       my_errno=errno;
  49.       if (MyFlags & (MY_FAE+MY_WME))
  50. my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),get_size);
  51.       return((gptr) 0);
  52.     }
  53.     DBUG_PRINT("test",("my_once_malloc %u byte malloced",get_size));
  54.     next->next= 0;
  55.     next->size= get_size;
  56.     next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM));
  57.     *prev=next;
  58.   }
  59.   point= (gptr) ((char*) next+ (next->size-next->left));
  60.   next->left-= Size;
  61.   return(point);
  62. } /* my_once_alloc */
  63. /* deallocate everything used by my_once_alloc */
  64. void my_once_free(void)
  65. {
  66.   reg1 USED_MEM *next,*old;
  67.   DBUG_ENTER("my_once_free");
  68.   for (next=my_once_root_block ; next ; )
  69.   {
  70.     old=next; next= next->next ;
  71.     free((gptr) old);
  72.   }
  73.   my_once_root_block=0;
  74.   DBUG_VOID_RETURN;
  75. } /* my_once_free */