00001
00018 #include <GKlib.h>
00019
00020
00021 static CMK_THREADLOCAL gk_mcore_t *gkmcore = NULL;
00022
00023
00024
00026
00027 GK_MKALLOC(gk_c, char)
00028 GK_MKALLOC(gk_i, int)
00029 GK_MKALLOC(gk_i32, int32_t)
00030 GK_MKALLOC(gk_i64, int64_t)
00031 GK_MKALLOC(gk_z, ssize_t)
00032 GK_MKALLOC(gk_f, float)
00033 GK_MKALLOC(gk_d, double)
00034 GK_MKALLOC(gk_idx, gk_idx_t)
00035
00036 GK_MKALLOC(gk_ckv, gk_ckv_t)
00037 GK_MKALLOC(gk_ikv, gk_ikv_t)
00038 GK_MKALLOC(gk_i32kv, gk_i32kv_t)
00039 GK_MKALLOC(gk_i64kv, gk_i64kv_t)
00040 GK_MKALLOC(gk_zkv, gk_zkv_t)
00041 GK_MKALLOC(gk_fkv, gk_fkv_t)
00042 GK_MKALLOC(gk_dkv, gk_dkv_t)
00043 GK_MKALLOC(gk_skv, gk_skv_t)
00044 GK_MKALLOC(gk_idxkv, gk_idxkv_t)
00045
00046
00047
00048
00049
00050
00051
00054
00055 void gk_AllocMatrix(void ***r_matrix, size_t elmlen, size_t ndim1, size_t ndim2)
00056 {
00057 gk_idx_t i, j;
00058 void **matrix;
00059
00060 *r_matrix = NULL;
00061
00062 if ((matrix = (void **)gk_malloc(ndim1*sizeof(void *), "gk_AllocMatrix: matrix")) == NULL)
00063 return;
00064
00065 for (i=0; i<ndim1; i++) {
00066 if ((matrix[i] = (void *)gk_malloc(ndim2*elmlen, "gk_AllocMatrix: matrix[i]")) == NULL) {
00067 for (j=0; j<i; j++)
00068 gk_free((void **)&matrix[j], LTERM);
00069 return;
00070 }
00071 }
00072
00073 *r_matrix = matrix;
00074 }
00075
00076
00077
00080
00081 void gk_FreeMatrix(void ***r_matrix, size_t ndim1, size_t ndim2)
00082 {
00083 gk_idx_t i;
00084 void **matrix;
00085
00086 if ((matrix = *r_matrix) == NULL)
00087 return;
00088
00089 for (i=0; i<ndim1; i++)
00090 gk_free((void **)&matrix[i], LTERM);
00091
00092 gk_free((void **)r_matrix, LTERM);
00093
00094 }
00095
00096
00097
00100
00101 int gk_malloc_init()
00102 {
00103 if (gkmcore == NULL)
00104 gkmcore = gk_gkmcoreCreate();
00105
00106 if (gkmcore == NULL)
00107 return 0;
00108
00109 gk_gkmcorePush(gkmcore);
00110
00111 return 1;
00112 }
00113
00114
00115
00119
00120 void gk_malloc_cleanup(int showstats)
00121 {
00122 if (gkmcore != NULL) {
00123 gk_gkmcorePop(gkmcore);
00124 if (gkmcore->cmop == 0) {
00125 gk_gkmcoreDestroy(&gkmcore, showstats);
00126 gkmcore = NULL;
00127 }
00128 }
00129 }
00130
00131
00132
00141
00142 void *gk_malloc(size_t nbytes, char *msg)
00143 {
00144 void *ptr=NULL;
00145
00146 if (nbytes == 0)
00147 nbytes++;
00148
00149 ptr = (void *)malloc(nbytes);
00150
00151 if (ptr == NULL) {
00152 fprintf(stderr, " Current memory used: %10zu bytes\n", gk_GetCurMemoryUsed());
00153 fprintf(stderr, " Maximum memory used: %10zu bytes\n", gk_GetMaxMemoryUsed());
00154 gk_errexit(SIGMEM, "***Memory allocation failed for %s. Requested size: %zu bytes",
00155 msg, nbytes);
00156 return NULL;
00157 }
00158
00159
00160 if (gkmcore != NULL) gk_gkmcoreAdd(gkmcore, GK_MOPT_HEAP, nbytes, ptr);
00161
00162
00163 #ifndef NDEBUG
00164 memset(ptr, 0, nbytes);
00165 #endif
00166
00167 return ptr;
00168 }
00169
00170
00171
00172
00173
00174 void *gk_realloc(void *oldptr, size_t nbytes, char *msg)
00175 {
00176 void *ptr=NULL;
00177
00178 if (nbytes == 0)
00179 nbytes++;
00180
00181
00182 if (gkmcore != NULL && oldptr != NULL) gk_gkmcoreDel(gkmcore, oldptr);
00183
00184 ptr = (void *)realloc(oldptr, nbytes);
00185
00186 if (ptr == NULL) {
00187 fprintf(stderr, " Maximum memory used: %10zu bytes\n", gk_GetMaxMemoryUsed());
00188 fprintf(stderr, " Current memory used: %10zu bytes\n", gk_GetCurMemoryUsed());
00189 gk_errexit(SIGMEM, "***Memory realloc failed for %s. " "Requested size: %zu bytes",
00190 msg, nbytes);
00191 return NULL;
00192 }
00193
00194
00195 if (gkmcore != NULL) gk_gkmcoreAdd(gkmcore, GK_MOPT_HEAP, nbytes, ptr);
00196
00197 return ptr;
00198 }
00199
00200
00201
00202
00203
00204 void gk_free(void **ptr1,...)
00205 {
00206 va_list plist;
00207 void **ptr;
00208
00209 if (*ptr1 != NULL) {
00210 free(*ptr1);
00211
00212
00213 if (gkmcore != NULL) gk_gkmcoreDel(gkmcore, *ptr1);
00214 }
00215 *ptr1 = NULL;
00216
00217 va_start(plist, ptr1);
00218 while ((ptr = va_arg(plist, void **)) != LTERM) {
00219 if (*ptr != NULL) {
00220 free(*ptr);
00221
00222
00223 if (gkmcore != NULL) gk_gkmcoreDel(gkmcore, *ptr);
00224 }
00225 *ptr = NULL;
00226 }
00227 va_end(plist);
00228 }
00229
00230
00231
00232
00233
00234
00235 size_t gk_GetCurMemoryUsed()
00236 {
00237 if (gkmcore == NULL)
00238 return 0;
00239 else
00240 return gkmcore->cur_hallocs;
00241 }
00242
00243
00244
00245
00246
00247
00248 size_t gk_GetMaxMemoryUsed()
00249 {
00250 if (gkmcore == NULL)
00251 return 0;
00252 else
00253 return gkmcore->max_hallocs;
00254 }