00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "cmipool.h"
00015
00016 CpvStaticDeclare(char **, bins);
00017 CpvStaticDeclare(int *, binLengths);
00018 CpvStaticDeclare(int, maxBin);
00019 CpvStaticDeclare(int, numKallocs);
00020 CpvStaticDeclare(int, numMallocs);
00021 CpvStaticDeclare(int, numOallocs);
00022 CpvStaticDeclare(int, numFrees);
00023 CpvStaticDeclare(int, numOFrees);
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 void *malloc_nomigrate(size_t size);
00038 void free_nomigrate(void *mem);
00039
00040 void CmiPoolAllocInit(int numBins)
00041 {
00042 int i;
00043 if (CpvInitialized(bins)) return;
00044 CpvInitialize(char **, bins);
00045 CpvInitialize(int *, binLengths);
00046 CpvInitialize(int, maxBin);
00047 CpvInitialize(int, numKallocs);
00048 CpvInitialize(int, numMallocs);
00049 CpvInitialize(int, numOFrees);
00050 CpvInitialize(int, numFrees);
00051
00052 CpvAccess(bins) = (char **) malloc_nomigrate( numBins*sizeof(char *));
00053 CpvAccess(binLengths) = (int *) malloc_nomigrate( numBins*sizeof(int));
00054 CpvAccess(maxBin) = numBins -1;
00055 for (i=0; i<numBins; i++) CpvAccess(bins)[i] = NULL;
00056 for (i=0; i<numBins; i++) CpvAccess(binLengths)[i] = 0;
00057
00058 CpvAccess(numKallocs) = CpvAccess(numMallocs) = CpvAccess(numFrees)=CpvAccess(numOFrees) = 0;
00059 }
00060
00061 void * CmiPoolAlloc(unsigned int numBytes)
00062 {
00063 char *p;
00064 int bin=0;
00065 int n=numBytes+CMI_POOL_HEADER_SIZE;
00066 CmiInt8 *header;
00067
00068 numBytes = n;
00069 while (n !=0)
00070 {
00071 n = n >> 1;
00072 bin++;
00073 }
00074
00075 if(bin<CpvAccess(maxBin))
00076 {
00077 CmiAssert(bin>0);
00078 if(CpvAccess(bins)[bin] != NULL)
00079 {
00080
00081 #if CMK_WITH_STATS
00082 CpvAccess(numKallocs)++;
00083 #endif
00084
00085 p = CpvAccess(bins)[bin];
00086
00087
00088
00089
00090
00091 #if CMK_WITH_STATS
00092 if(--CpvAccess(binLengths)[bin])
00093 CpvAccess(bins)[bin] = (char *) *((char **)(p -CMI_POOL_HEADER_SIZE));
00094 else
00095 CpvAccess(bins)[bin] = NULL;
00096 #else
00097 CpvAccess(bins)[bin] = (char *) *((char **)(p -CMI_POOL_HEADER_SIZE));
00098 #endif
00099 }
00100 else
00101 {
00102
00103 #if CMK_WITH_STATS
00104 CpvAccess(numMallocs)++;
00105 #endif
00106
00107 p =(char *) malloc_nomigrate(1 << bin) + CMI_POOL_HEADER_SIZE;
00108 }
00109 }
00110 else
00111 {
00112
00113
00114 #if CMK_WITH_STATS
00115 CpvAccess(numOallocs)++;
00116 #endif
00117 p = (char *) malloc_nomigrate(numBytes) + CMI_POOL_HEADER_SIZE;
00118 bin=0;
00119
00120 }
00121 header = (CmiInt8 *) (p-CMI_POOL_HEADER_SIZE);
00122 CmiAssert(header !=NULL);
00123 *header = bin;
00124 return p;
00125 }
00126
00127 void CmiPoolFree(void * p)
00128 {
00129 char **header = (char **)( (char*)p - CMI_POOL_HEADER_SIZE);
00130 int bin = *(CmiInt8 *)header;
00131
00132 if(bin==0)
00133 {
00134 #if CMK_WITH_STATS
00135 CpvAccess(numOFrees)++;
00136 #endif
00137 free_nomigrate(header);
00138 }
00139 else if(bin<CpvAccess(maxBin))
00140 {
00141 #if CMK_WITH_STATS
00142 CpvAccess(numFrees)++;
00143 #endif
00144
00145 *header = CpvAccess(bins)[bin];
00146 CpvAccess(bins)[bin] = (char *)p;
00147 #if CMK_WITH_STATS
00148 CpvAccess(binLengths)[bin]++;
00149 #endif
00150 }
00151 else
00152 CmiAbort("CmiPoolFree: Invalid Bin");
00153 }
00154
00155 void CmiPoolAllocStats(void)
00156 {
00157 int i;
00158 CmiPrintf("numKallocs: %d\n", CpvAccess(numKallocs));
00159 CmiPrintf("numMallocs: %d\n", CpvAccess(numMallocs));
00160 CmiPrintf("numOallocs: %d\n", CpvAccess(numOallocs));
00161 CmiPrintf("numOFrees: %d\n", CpvAccess(numOFrees));
00162 CmiPrintf("numFrees: %d\n", CpvAccess(numFrees));
00163 CmiPrintf("Bin:");
00164 for (i=0; i<=CpvAccess(maxBin); i++)
00165 if(CpvAccess(binLengths)[i])
00166 CmiPrintf("%d\t", i);
00167 CmiPrintf("\nVal:");
00168 for (i=0; i<=CpvAccess(maxBin); i++)
00169 if(CpvAccess(binLengths)[i])
00170 CmiPrintf("%d\t", CpvAccess(binLengths)[i]);
00171 CmiPrintf("\n");
00172 }
00173
00174 void CmiPoolPrintList(char *p)
00175 {
00176 CmiPrintf("Free list is: -----------\n");
00177 while (p != 0) {
00178 char ** header = (char **) p-CMI_POOL_HEADER_SIZE;
00179 CmiPrintf("next ptr is %p. ", p);
00180 CmiPrintf("header is at: %p, and contains: %p \n", header, *header);
00181 p = *header;
00182 }
00183 CmiPrintf("End of Free list: -----------\n");
00184
00185 }
00186
00187
00188