00001 #ifndef ALREADY_memory
00002 #define ALREADY_memory
00003 void freeplacemon();
00004 template<typename T>
00005 void INIT_ELEM(void * ptr, int size)
00006 {
00007
00008 for(unsigned i=0;i<(size/(sizeof(T)));i++)
00009 new((T*)ptr+i) T;
00010
00011 }
00012 #if 0
00013 template<>
00014 void INIT_ELEM<Z<MON_PREMIER, short int> >(void * ptr, int size)
00015 {
00016
00017 for(unsigned i=0;i<(size/(sizeof(Z<MON_PREMIER,short int>)));i++)
00018 {
00019 new(((Z<MON_PREMIER,short int> *)ptr)+i) Z<MON_PREMIER,short int>;
00020 *((Z<MON_PREMIER,short int> *)ptr+i)=0;
00021 }
00022 }
00023 #endif
00024
00025 template<typename T>
00026 void CLEAR_ELEM(void * ptr, int size)
00027 {
00028 for(unsigned int i=0;i<size/(sizeof(T));i++)
00029 ((T*)ptr+i)->~T();
00030 }
00031
00032
00033
00034
00035
00036
00037
00038 template<typename T>
00039 inline void * MAC_REV_MALLOC(int size)
00040 {
00041
00042 void * res;
00043 res=malloc(size);
00044 INIT_ELEM<T>(res,size);
00045 return res;
00046 }
00047
00048 template<typename T>
00049 inline void * MAC_REV_REALLOC(void * ptr,int size,int newsize)
00050 {
00051 void *res=ptr;
00052 if(size<newsize)
00053 {
00054 res=realloc(ptr,newsize);
00055 INIT_ELEM<T>((char*)res+size,newsize-size);
00056 }
00057 if(size>newsize)
00058 {
00059 CLEAR_ELEM<T>((char*)ptr+newsize,size-newsize);
00060 res=realloc(ptr,newsize);
00061 }
00062 return res;
00063 }
00064 template<typename T>
00065 inline void MAC_REV_FREE(void * ptr,int size)
00066 {
00067 if(size>0)
00068 CLEAR_ELEM<T>(ptr,size);
00069 free(ptr);
00070 }
00071 #if 0
00072 template<>
00073 void INIT_ELEM<Mon>(void *ptr,int size)
00074 {
00075 typedef Mon::coeff_t coeff;
00076 int tmp=size/sizeof(Mon);
00077 for(int i=0;i<tmp;i++)
00078 INIT_ELEM<coeff>(&(((Mon*)ptr+i)->coeff),sizeof(coeff));
00079 }
00080
00081 template<>
00082 void CLEAR_ELEM<mon>(void *ptr,int size)
00083 {
00084 typedef mon::coeff_t coeff;
00085 int tmp=size/sizeof(mon);
00086 for(int i=0;i<tmp;i++)
00087 CLEAR_ELEM<coeff>(&(((mon*)ptr)+i)->coeff,sizeof(coeff));
00088 }
00089 #endif
00090 #if 0
00091 template<typename T>
00092 inline void * MAC_REV_MALLOC(int size)
00093 {
00094 void * res;
00095 res=malloc(size);
00096 return res;
00097 }
00098
00099 template<typename T>
00100 inline void * MAC_REV_REALLOC(void * ptr,int size,int newsize)
00101 {
00102 void * res;
00103 res=realloc(ptr,newsize);
00104 return res;
00105 }
00106 template<typename T>
00107 inline void MAC_REV_FREE(void * ptr,int size)
00108 {
00109 free(ptr);
00110 }
00111 template<>
00112 inline void * MAC_REV_MALLOC<mon>(int size)
00113 {
00114 void * res;
00115 res=malloc(size);
00116 for(int i=0;i<size/sizeof(mon);i++)
00117 new((mon *)res+i) mon;
00118 return res;
00119 }
00120
00121 template<>
00122 inline void * MAC_REV_REALLOC<mon>(void * ptr,int size,int newsize)
00123 {
00124 void *res=ptr;
00125 int tmp=size/sizeof(mon);
00126 if(size<newsize)
00127 {
00128 res=realloc(ptr,newsize);
00129 for(int i=0;i<(newsize-size)/sizeof(mon);i++)
00130 new((mon*)res+tmp+i) mon;
00131 }
00132 if(size>newsize)
00133 {
00134 for(int i=0;i<(size-newsize)/sizeof(mon);i++)
00135 ((mon*)res+tmp+i)->~mon();
00136 res=realloc(ptr,newsize);
00137 }
00138 return res;
00139 }
00140
00141 template<>
00142 inline void MAC_REV_FREE<mon>(void * ptr,int size)
00143 {
00144 for(int i=0;i<(size)/sizeof(mon);i++)
00145 ((mon*)ptr+i)->~mon();
00146 free(ptr);
00147 }
00148
00149 #endif
00150
00151 template<typename typdump, typename Base>
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 void sweep(typdump &dump,Base &b)
00165
00166
00167
00168
00169 {
00170 typedef typename typdump::value_type::pol_t::coeff_t coeff;
00171 typedef typename Base::mon mon;
00172 for(typename typdump::iterator iter=dump.begin()
00173 ;iter!=dump.end();iter++)
00174 {
00175 for(int i=0;i<(iter->size);i++) {
00176 MAC_REV_FREE<coeff>(iter->nf[i].nf,iter->nf[i].size*sizeof(coeff));
00177 #ifdef sparse
00178 MAC_REV_FREE<int>(iter->nf[i].nfind,iter->nf[i].size*sizeof(int));
00179 #endif
00180 }
00181 MAC_REV_FREE<pol<mon,coeff> >(iter->nf,iter->size*sizeof(pol<mon,coeff>));
00182 }
00183 for(typename Base::iterator iter=b.begin()
00184 ;iter!=b.end();iter++)
00185 {
00186 MAC_REV_FREE<mon>(iter->accept,iter->taille1*sizeof(mon));
00187 MAC_REV_FREE<mon>(iter->refuse,iter->taille2*sizeof(mon));
00188 }
00189
00190
00191 }
00192 #endif //ALREADY_memory