| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief Functions dealing with creating and allocating mcores |
| 4 |
|
| 5 |
\date Started 5/30/11 |
| 6 |
\author George |
| 7 |
\author Copyright 1997-2011, Regents of the University of Minnesota |
| 8 |
\version $Id: mcore.c 13953 2013-03-30 16:20:07Z karypis $ |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "GKlib.h" |
| 12 |
|
| 13 |
|
| 14 |
/*************************************************************************/ |
| 15 |
/*! This function creates an mcore |
| 16 |
*/ |
| 17 |
/*************************************************************************/ |
| 18 |
gk_mcore_t *gk_mcoreCreate(size_t coresize) |
| 19 |
{ |
| 20 |
gk_mcore_t *mcore; |
| 21 |
|
| 22 |
mcore = (gk_mcore_t *)malloc(sizeof(gk_mcore_t)); |
| 23 |
memset(mcore, 0, sizeof(gk_mcore_t)); |
| 24 |
|
| 25 |
mcore->coresize = coresize; |
| 26 |
mcore->corecpos = 0; |
| 27 |
|
| 28 |
mcore->core = (coresize == 0 ? NULL : malloc(mcore->coresize)); |
| 29 |
|
| 30 |
/* allocate the memory for keeping track of malloc ops */ |
| 31 |
mcore->nmops = 2048; |
| 32 |
mcore->cmop = 0; |
| 33 |
mcore->mops = (gk_mop_t *)malloc(mcore->nmops*sizeof(gk_mop_t)); |
| 34 |
|
| 35 |
return mcore; |
| 36 |
} |
| 37 |
|
| 38 |
/*************************************************************************/ |
| 39 |
/*! This function destroys an mcore. |
| 40 |
*/ |
| 41 |
/*************************************************************************/ |
| 42 |
void gk_mcoreDestroy(gk_mcore_t **r_mcore, int showstats) |
| 43 |
{ |
| 44 |
gk_mcore_t *mcore = *r_mcore; |
| 45 |
|
| 46 |
if (mcore == NULL) |
| 47 |
return; |
| 48 |
|
| 49 |
gk_free((void **)&mcore->core); |
| 50 |
gk_free((void**)&mcore->mops); |
| 51 |
gk_free((void**)&mcore); |
| 52 |
|
| 53 |
*r_mcore = NULL; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/*************************************************************************/ |
| 58 |
/*! This function allocate space from the core/heap |
| 59 |
*/ |
| 60 |
/*************************************************************************/ |
| 61 |
void *gk_mcoreMalloc(gk_mcore_t *mcore, size_t nbytes) |
| 62 |
{ |
| 63 |
void *ptr; |
| 64 |
|
| 65 |
/* pad to make pointers 8-byte aligned */ |
| 66 |
nbytes += (nbytes%8 == 0 ? 0 : 8 - nbytes%8); |
| 67 |
|
| 68 |
if (mcore->corecpos + nbytes < mcore->coresize) { |
| 69 |
/* service this request from the core */ |
| 70 |
ptr = ((char *)mcore->core)+mcore->corecpos; |
| 71 |
mcore->corecpos += nbytes; |
| 72 |
|
| 73 |
gk_mcoreAdd(mcore, GK_MOPT_CORE, nbytes, ptr); |
| 74 |
} |
| 75 |
else { |
| 76 |
/* service this request from the heap */ |
| 77 |
ptr = malloc(nbytes); |
| 78 |
|
| 79 |
gk_mcoreAdd(mcore, GK_MOPT_HEAP, nbytes, ptr); |
| 80 |
} |
| 81 |
|
| 82 |
return ptr; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/*************************************************************************/ |
| 87 |
/*! This function sets a marker in the stack of malloc ops to be used |
| 88 |
subsequently for freeing purposes |
| 89 |
*/ |
| 90 |
/*************************************************************************/ |
| 91 |
void gk_mcorePush(gk_mcore_t *mcore) |
| 92 |
{ |
| 93 |
gk_mcoreAdd(mcore, GK_MOPT_MARK, 0, NULL); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/*************************************************************************/ |
| 98 |
/*! This function frees all mops since the last push |
| 99 |
*/ |
| 100 |
/*************************************************************************/ |
| 101 |
void gk_mcorePop(gk_mcore_t *mcore) |
| 102 |
{ |
| 103 |
while (mcore->cmop > 0) { |
| 104 |
mcore->cmop--; |
| 105 |
switch (mcore->mops[mcore->cmop].type) { |
| 106 |
case GK_MOPT_MARK: /* push marker */ |
| 107 |
goto DONE; |
| 108 |
break; |
| 109 |
|
| 110 |
case GK_MOPT_CORE: /* core free */ |
| 111 |
if (mcore->corecpos < mcore->mops[mcore->cmop].nbytes) |
| 112 |
GK_ERREXIT("Internal Error: wspace's core is about to be over-freed [%zu, %zu, %zd]\n", |
| 113 |
mcore->coresize, mcore->corecpos, mcore->mops[mcore->cmop].nbytes); |
| 114 |
|
| 115 |
mcore->corecpos -= mcore->mops[mcore->cmop].nbytes; |
| 116 |
break; |
| 117 |
|
| 118 |
case GK_MOPT_HEAP: /* heap free */ |
| 119 |
gk_free((void **)&mcore->mops[mcore->cmop].ptr); |
| 120 |
break; |
| 121 |
|
| 122 |
default: |
| 123 |
GK_ERREXIT("Unknown mop type of %d\n", mcore->mops[mcore->cmop].type); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
DONE: |
| 128 |
; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/*************************************************************************/ |
| 133 |
/*! Adds a memory allocation at the end of the list. |
| 134 |
*/ |
| 135 |
/*************************************************************************/ |
| 136 |
void gk_mcoreAdd(gk_mcore_t *mcore, int type, size_t nbytes, void *ptr) |
| 137 |
{ |
| 138 |
if (mcore->cmop == mcore->nmops) { |
| 139 |
mcore->nmops *= 2; |
| 140 |
mcore->mops = realloc(mcore->mops, mcore->nmops*sizeof(gk_mop_t)); |
| 141 |
if (mcore->mops == NULL) |
| 142 |
GK_ERREXIT("***Memory allocation for gkmcore failed.\n"); |
| 143 |
} |
| 144 |
|
| 145 |
mcore->mops[mcore->cmop].type = type; |
| 146 |
mcore->mops[mcore->cmop].nbytes = nbytes; |
| 147 |
mcore->mops[mcore->cmop].ptr = ptr; |
| 148 |
mcore->cmop++; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
/*************************************************************************/ |
| 153 |
/*! This function deletes the mop associated with the supplied pointer. |
| 154 |
The mop has to be a heap allocation, otherwise it fails violently. |
| 155 |
*/ |
| 156 |
/*************************************************************************/ |
| 157 |
void gk_mcoreDel(gk_mcore_t *mcore, void *ptr) |
| 158 |
{ |
| 159 |
int i; |
| 160 |
|
| 161 |
for (i=mcore->cmop-1; i>=0; i--) { |
| 162 |
if (mcore->mops[i].type == GK_MOPT_MARK) |
| 163 |
GK_ERREXIT("Could not find pointer %p in mcore\n", ptr); |
| 164 |
|
| 165 |
if (mcore->mops[i].ptr == ptr) { |
| 166 |
if (mcore->mops[i].type != GK_MOPT_HEAP) |
| 167 |
GK_ERREXIT("Trying to delete a non-HEAP mop.\n"); |
| 168 |
|
| 169 |
mcore->mops[i] = mcore->mops[--mcore->cmop]; |
| 170 |
return; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
GK_ERREXIT("mcoreDel should never have been here!\n"); |
| 175 |
} |
| 176 |
|