00001
00011 #include <GKlib.h>
00012
00013
00014
00017
00018 gk_mcore_t *gk_mcoreCreate(size_t coresize)
00019 {
00020 gk_mcore_t *mcore;
00021
00022 mcore = (gk_mcore_t *)gk_malloc(sizeof(gk_mcore_t), "gk_mcoreCreate: mcore");
00023 memset(mcore, 0, sizeof(gk_mcore_t));
00024
00025 mcore->coresize = coresize;
00026 mcore->corecpos = 0;
00027
00028 mcore->core = (coresize == 0 ? NULL : gk_malloc(mcore->coresize, "gk_mcoreCreate: core"));
00029
00030
00031 mcore->nmops = 2048;
00032 mcore->cmop = 0;
00033 mcore->mops = (gk_mop_t *)gk_malloc(mcore->nmops*sizeof(gk_mop_t), "gk_mcoreCreate: mcore->mops");
00034
00035 return mcore;
00036 }
00037
00038
00039
00042
00043 gk_mcore_t *gk_gkmcoreCreate()
00044 {
00045 gk_mcore_t *mcore;
00046
00047 if ((mcore = (gk_mcore_t *)malloc(sizeof(gk_mcore_t))) == NULL)
00048 return NULL;
00049 memset(mcore, 0, sizeof(gk_mcore_t));
00050
00051
00052 mcore->nmops = 2048;
00053 mcore->cmop = 0;
00054 if ((mcore->mops = (gk_mop_t *)malloc(mcore->nmops*sizeof(gk_mop_t))) == NULL) {
00055 free(mcore);
00056 return NULL;
00057 }
00058
00059 return mcore;
00060 }
00061
00062
00063
00066
00067 void gk_mcoreDestroy(gk_mcore_t **r_mcore, int showstats)
00068 {
00069 gk_mcore_t *mcore = *r_mcore;
00070
00071 if (mcore == NULL)
00072 return;
00073
00074 if (showstats)
00075 printf("\n gk_mcore statistics\n"
00076 " coresize: %12zu nmops: %12zu cmop: %6zu\n"
00077 " num_callocs: %12zu num_hallocs: %12zu\n"
00078 " size_callocs: %12zu size_hallocs: %12zu\n"
00079 " cur_callocs: %12zu cur_hallocs: %12zu\n"
00080 " max_callocs: %12zu max_hallocs: %12zu\n",
00081 mcore->coresize, mcore->nmops, mcore->cmop,
00082 mcore->num_callocs, mcore->num_hallocs,
00083 mcore->size_callocs, mcore->size_hallocs,
00084 mcore->cur_callocs, mcore->cur_hallocs,
00085 mcore->max_callocs, mcore->max_hallocs);
00086
00087 if (mcore->cur_callocs != 0 || mcore->cur_hallocs != 0 || mcore->cmop != 0) {
00088 printf("***Warning: mcore memory was not fully freed when destroyed.\n"
00089 " cur_callocs: %6zu cur_hallocs: %6zu cmop: %6zu\n",
00090 mcore->cur_callocs, mcore->cur_hallocs, mcore->cmop);
00091 }
00092
00093 gk_free((void **)&mcore->core, &mcore->mops, &mcore, LTERM);
00094
00095 *r_mcore = NULL;
00096 }
00097
00098
00099
00102
00103 void gk_gkmcoreDestroy(gk_mcore_t **r_mcore, int showstats)
00104 {
00105 gk_mcore_t *mcore = *r_mcore;
00106
00107 if (mcore == NULL)
00108 return;
00109
00110 if (showstats)
00111 printf("\n gk_mcore statistics\n"
00112 " nmops: %12zu cmop: %6zu\n"
00113 " num_hallocs: %12zu\n"
00114 " size_hallocs: %12zu\n"
00115 " cur_hallocs: %12zu\n"
00116 " max_hallocs: %12zu\n",
00117 mcore->nmops, mcore->cmop,
00118 mcore->num_hallocs,
00119 mcore->size_hallocs,
00120 mcore->cur_hallocs,
00121 mcore->max_hallocs);
00122
00123 if (mcore->cur_hallocs != 0 || mcore->cmop != 0) {
00124 printf("***Warning: mcore memory was not fully freed when destroyed.\n"
00125 " cur_hallocs: %6zu cmop: %6zu\n",
00126 mcore->cur_hallocs, mcore->cmop);
00127 }
00128
00129 free(mcore->mops);
00130 free(mcore);
00131
00132 *r_mcore = NULL;
00133 }
00134
00135
00136
00139
00140 void *gk_mcoreMalloc(gk_mcore_t *mcore, size_t nbytes)
00141 {
00142 void *ptr;
00143
00144
00145 nbytes += (nbytes%8 == 0 ? 0 : 8 - nbytes%8);
00146
00147 if (mcore->corecpos + nbytes < mcore->coresize) {
00148
00149 ptr = ((char *)mcore->core)+mcore->corecpos;
00150 mcore->corecpos += nbytes;
00151
00152 gk_mcoreAdd(mcore, GK_MOPT_CORE, nbytes, ptr);
00153 }
00154 else {
00155
00156 ptr = gk_malloc(nbytes, "gk_mcoremalloc: ptr");
00157
00158 gk_mcoreAdd(mcore, GK_MOPT_HEAP, nbytes, ptr);
00159 }
00160
00161
00162
00163
00164
00165
00166 return ptr;
00167 }
00168
00169
00170
00174
00175 void gk_mcorePush(gk_mcore_t *mcore)
00176 {
00177 gk_mcoreAdd(mcore, GK_MOPT_MARK, 0, NULL);
00178
00179 }
00180
00181
00182
00186
00187 void gk_gkmcorePush(gk_mcore_t *mcore)
00188 {
00189 gk_gkmcoreAdd(mcore, GK_MOPT_MARK, 0, NULL);
00190
00191 }
00192
00193
00194
00197
00198 void gk_mcorePop(gk_mcore_t *mcore)
00199 {
00200 while (mcore->cmop > 0) {
00201 mcore->cmop--;
00202 switch (mcore->mops[mcore->cmop].type) {
00203 case GK_MOPT_MARK:
00204 goto DONE;
00205 break;
00206
00207 case GK_MOPT_CORE:
00208 if (mcore->corecpos < mcore->mops[mcore->cmop].nbytes)
00209 errexit("Internal Error: wspace's core is about to be over-freed [%zu, %zu, %zd]\n",
00210 mcore->coresize, mcore->corecpos, mcore->mops[mcore->cmop].nbytes);
00211
00212 mcore->corecpos -= mcore->mops[mcore->cmop].nbytes;
00213 mcore->cur_callocs -= mcore->mops[mcore->cmop].nbytes;
00214 break;
00215
00216 case GK_MOPT_HEAP:
00217 gk_free((void **)&mcore->mops[mcore->cmop].ptr, LTERM);
00218 mcore->cur_hallocs -= mcore->mops[mcore->cmop].nbytes;
00219 break;
00220
00221 default:
00222 gk_errexit(SIGMEM, "Unknown mop type of %d\n", mcore->mops[mcore->cmop].type);
00223 }
00224 }
00225
00226 DONE:
00227 ;
00228
00229 }
00230
00231
00232
00236
00237 void gk_gkmcorePop(gk_mcore_t *mcore)
00238 {
00239 while (mcore->cmop > 0) {
00240 mcore->cmop--;
00241 switch (mcore->mops[mcore->cmop].type) {
00242 case GK_MOPT_MARK:
00243 goto DONE;
00244 break;
00245
00246 case GK_MOPT_HEAP:
00247 free(mcore->mops[mcore->cmop].ptr);
00248 mcore->cur_hallocs -= mcore->mops[mcore->cmop].nbytes;
00249 break;
00250
00251 default:
00252 gk_errexit(SIGMEM, "Unknown mop type of %d\n", mcore->mops[mcore->cmop].type);
00253 }
00254 }
00255
00256 DONE:
00257 ;
00258 }
00259
00260
00261
00264
00265 void gk_mcoreAdd(gk_mcore_t *mcore, int type, size_t nbytes, void *ptr)
00266 {
00267 if (mcore->cmop == mcore->nmops) {
00268 mcore->nmops *= 2;
00269 mcore->mops = realloc(mcore->mops, mcore->nmops*sizeof(gk_mop_t));
00270 if (mcore->mops == NULL)
00271 gk_errexit(SIGMEM, "***Memory allocation for gkmcore failed.\n");
00272 }
00273
00274 mcore->mops[mcore->cmop].type = type;
00275 mcore->mops[mcore->cmop].nbytes = nbytes;
00276 mcore->mops[mcore->cmop].ptr = ptr;
00277 mcore->cmop++;
00278
00279 switch (type) {
00280 case GK_MOPT_MARK:
00281 break;
00282
00283 case GK_MOPT_CORE:
00284 mcore->num_callocs++;
00285 mcore->size_callocs += nbytes;
00286 mcore->cur_callocs += nbytes;
00287 if (mcore->max_callocs < mcore->cur_callocs)
00288 mcore->max_callocs = mcore->cur_callocs;
00289 break;
00290
00291 case GK_MOPT_HEAP:
00292 mcore->num_hallocs++;
00293 mcore->size_hallocs += nbytes;
00294 mcore->cur_hallocs += nbytes;
00295 if (mcore->max_hallocs < mcore->cur_hallocs)
00296 mcore->max_hallocs = mcore->cur_hallocs;
00297 break;
00298 default:
00299 gk_errexit(SIGMEM, "Incorrect mcore type operation.\n");
00300 }
00301 }
00302
00303
00304
00308
00309 void gk_gkmcoreAdd(gk_mcore_t *mcore, int type, size_t nbytes, void *ptr)
00310 {
00311 if (mcore->cmop == mcore->nmops) {
00312 mcore->nmops *= 2;
00313 mcore->mops = realloc(mcore->mops, mcore->nmops*sizeof(gk_mop_t));
00314 if (mcore->mops == NULL)
00315 gk_errexit(SIGMEM, "***Memory allocation for gkmcore failed.\n");
00316 }
00317
00318 mcore->mops[mcore->cmop].type = type;
00319 mcore->mops[mcore->cmop].nbytes = nbytes;
00320 mcore->mops[mcore->cmop].ptr = ptr;
00321 mcore->cmop++;
00322
00323 switch (type) {
00324 case GK_MOPT_MARK:
00325 break;
00326
00327 case GK_MOPT_HEAP:
00328 mcore->num_hallocs++;
00329 mcore->size_hallocs += nbytes;
00330 mcore->cur_hallocs += nbytes;
00331 if (mcore->max_hallocs < mcore->cur_hallocs)
00332 mcore->max_hallocs = mcore->cur_hallocs;
00333 break;
00334 default:
00335 gk_errexit(SIGMEM, "Incorrect mcore type operation.\n");
00336 }
00337 }
00338
00339
00340
00344
00345 void gk_mcoreDel(gk_mcore_t *mcore, void *ptr)
00346 {
00347 int i;
00348
00349 for (i=mcore->cmop-1; i>=0; i--) {
00350 if (mcore->mops[i].type == GK_MOPT_MARK)
00351 gk_errexit(SIGMEM, "Could not find pointer %p in mcore\n", ptr);
00352
00353 if (mcore->mops[i].ptr == ptr) {
00354 if (mcore->mops[i].type != GK_MOPT_HEAP)
00355 gk_errexit(SIGMEM, "Trying to delete a non-HEAP mop.\n");
00356
00357 mcore->cur_hallocs -= mcore->mops[i].nbytes;
00358 mcore->mops[i] = mcore->mops[--mcore->cmop];
00359 return;
00360 }
00361 }
00362
00363 gk_errexit(SIGMEM, "mcoreDel should never have been here!\n");
00364 }
00365
00366
00367
00372
00373 void gk_gkmcoreDel(gk_mcore_t *mcore, void *ptr)
00374 {
00375 int i;
00376
00377 for (i=mcore->cmop-1; i>=0; i--) {
00378 if (mcore->mops[i].type == GK_MOPT_MARK)
00379 gk_errexit(SIGMEM, "Could not find pointer %p in mcore\n", ptr);
00380
00381 if (mcore->mops[i].ptr == ptr) {
00382 if (mcore->mops[i].type != GK_MOPT_HEAP)
00383 gk_errexit(SIGMEM, "Trying to delete a non-HEAP mop.\n");
00384
00385 mcore->cur_hallocs -= mcore->mops[i].nbytes;
00386 mcore->mops[i] = mcore->mops[--mcore->cmop];
00387 return;
00388 }
00389 }
00390
00391 gk_errexit(SIGMEM, "gkmcoreDel should never have been here!\n");
00392 }
00393