00001
00002
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #ifndef WIN32
00048 # include <unistd.h>
00049 #endif
00050 #include "converse.h"
00051
00052 void * memory_stack_top;
00053 int cpdInSystem=1;
00054
00055
00056 #if CMK_MEMORY_BUILD_DEFAULT
00057
00058 # if CMK_MALLOC_USE_OS_BUILTIN
00059
00060 # define CMK_MEMORY_BUILD_OS 1
00061
00062 # elif CMK_MALLOC_USE_GNUOLD_MALLOC
00063 # define CMK_MEMORY_BUILD_GNUOLD 1
00064 # else
00065
00066 # define CMK_MEMORY_BUILD_GNU 1
00067 # endif
00068
00069 #endif
00070
00071 #if CMK_MEMORY_BUILD_OS_WRAPPED
00072 #define CMK_MEMORY_BUILD_OS 1
00073 #endif
00074 #if CMK_MEMORY_BUILD_GNU_HOOKS
00075
00076
00077 #define CMK_MEMORY_BUILD_OS 1
00078 #endif
00079
00080 #if CMK_MEMORY_BUILD_OS
00081 #if CMK_MEMORY_BUILD_OS_WRAPPED
00082
00083 void initialize_memory_wrapper();
00084 void * initialize_memory_wrapper_calloc(size_t nelem, size_t size);
00085 void * initialize_memory_wrapper_malloc(size_t size);
00086 void * initialize_memory_wrapper_realloc(void *ptr, size_t size);
00087 void * initialize_memory_wrapper_memalign(size_t align, size_t size);
00088 void * initialize_memory_wrapper_valloc(size_t size);
00089 void initialize_memory_wrapper_free(void *ptr);
00090 void initialize_memory_wrapper_cfree(void *ptr);
00091
00092 void * (*mm_malloc)(size_t) = initialize_memory_wrapper_malloc;
00093 void * (*mm_calloc)(size_t,size_t) = initialize_memory_wrapper_calloc;
00094 void * (*mm_realloc)(void*,size_t) = initialize_memory_wrapper_realloc;
00095 void * (*mm_memalign)(size_t,size_t) = initialize_memory_wrapper_memalign;
00096 void * (*mm_valloc)(size_t) = initialize_memory_wrapper_valloc;
00097 void (*mm_free)(void*) = initialize_memory_wrapper_free;
00098 void (*mm_cfree)(void*) = initialize_memory_wrapper_cfree;
00099
00100 void * initialize_memory_wrapper_calloc(size_t nelem, size_t size) {
00101 static int calloc_wrapper = 0;
00102 if (calloc_wrapper) return NULL;
00103 calloc_wrapper = 1;
00104 initialize_memory_wrapper();
00105 return (*mm_calloc)(nelem,size);
00106 }
00107
00108 void * initialize_memory_wrapper_malloc(size_t size) {
00109 static int malloc_wrapper = 0;
00110 if (malloc_wrapper) return NULL;
00111 malloc_wrapper = 1;
00112 initialize_memory_wrapper();
00113 return (*mm_malloc)(size);
00114 }
00115
00116 void * initialize_memory_wrapper_realloc(void *ptr, size_t size) {
00117 initialize_memory_wrapper();
00118 return (*mm_realloc)(ptr,size);
00119 }
00120
00121 void * initialize_memory_wrapper_memalign(size_t align, size_t size) {
00122 initialize_memory_wrapper();
00123 return (*mm_memalign)(align,size);
00124 }
00125
00126 void * initialize_memory_wrapper_valloc(size_t size) {
00127 initialize_memory_wrapper();
00128 return (*mm_valloc)(size);
00129 }
00130
00131 void initialize_memory_wrapper_free(void *ptr) {
00132 initialize_memory_wrapper();
00133 (*mm_free)(ptr);
00134 }
00135
00136 void initialize_memory_wrapper_cfree(void *ptr) {
00137 initialize_memory_wrapper();
00138 (*mm_cfree)(ptr);
00139 }
00140
00141 #define mm_malloc (*mm_malloc)
00142 #define mm_free (*mm_free)
00143 #define mm_calloc (*mm_calloc)
00144 #define mm_cfree (*mm_cfree)
00145 #define mm_realloc (*mm_realloc)
00146 #define mm_memalign (*mm_memalign)
00147 #define mm_valloc (*mm_valloc)
00148
00149 #else
00150 #define mm_malloc malloc
00151 #define mm_calloc calloc
00152 #define mm_memalign memalign
00153 #define mm_free free
00154 #endif
00155 #endif
00156
00157 CMK_TYPEDEF_UINT8 _memory_allocated = 0;
00158 CMK_TYPEDEF_UINT8 _memory_allocated_max = 0;
00159 CMK_TYPEDEF_UINT8 _memory_allocated_min = 0;
00160
00161
00162
00163
00164 static int rank_holding_CmiMemLock=-1;
00165
00166
00167 static int CmiMemoryIs_flag=0;
00168
00169 int CmiMemoryIs(int flag)
00170 {
00171 return (CmiMemoryIs_flag&flag)==flag;
00172 }
00173
00179 static char *memory_lifeRaft=NULL;
00180
00181 void CmiOutOfMemoryInit(void);
00182
00183 void CmiOutOfMemory(int nBytes)
00184 {
00185 char errMsg[200];
00186 if (memory_lifeRaft) free(memory_lifeRaft);
00187 if (nBytes>0) sprintf(errMsg,"Could not malloc() %d bytes--are we out of memory? (used :%.3fMB)",nBytes,CmiMemoryUsage()/1000000.0);
00188 else sprintf(errMsg,"Could not malloc()--are we out of memory? (used: %.3fMB)", CmiMemoryUsage()/1000000.0);
00189 CmiAbort(errMsg);
00190 }
00191
00192
00193 int memory_status_info=0;
00194 int memory_chare_id=0;
00195
00196 #if CMK_MEMORY_BUILD_OS
00197
00198
00199
00200
00201 #if CMK_MEMORY_BUILD_OS_WRAPPED || CMK_MEMORY_BUILD_GNU_HOOKS
00202
00203 #if CMK_MEMORY_BUILD_GNU_HOOKS
00204
00205 static void *meta_malloc(size_t);
00206 static void *meta_realloc(void*,size_t);
00207 static void *meta_memalign(size_t,size_t);
00208 static void meta_free(void*);
00209 static void *meta_malloc_hook(size_t s, const void* c) {return meta_malloc(s);}
00210 static void *meta_realloc_hook(void* p,size_t s, const void* c) {return meta_realloc(p,s);}
00211 static void *meta_memalign_hook(size_t s1,size_t s2, const void* c) {return meta_memalign(s1,s2);}
00212 static void meta_free_hook(void* p, const void* c) {meta_free(p);}
00213
00214 #define BEFORE_MALLOC_CALL \
00215 __malloc_hook = old_malloc_hook; \
00216 __realloc_hook = old_realloc_hook; \
00217 __memalign_hook = old_memalign_hook; \
00218 __free_hook = old_free_hook;
00219 #define AFTER_MALLOC_CALL \
00220 old_malloc_hook = __malloc_hook; \
00221 old_realloc_hook = __realloc_hook; \
00222 old_memalign_hook = __memalign_hook; \
00223 old_free_hook = __free_hook; \
00224 __malloc_hook = meta_malloc_hook; \
00225 __realloc_hook = meta_realloc_hook; \
00226 __memalign_hook = meta_memalign_hook; \
00227 __free_hook = meta_free_hook;
00228
00229 #if CMK_HAS_MALLOC_H
00230 #include <malloc.h>
00231 #endif
00232 static void *(*old_malloc_hook) (size_t, const void*);
00233 static void *(*old_realloc_hook) (void*,size_t, const void*);
00234 static void *(*old_memalign_hook) (size_t,size_t, const void*);
00235 static void (*old_free_hook) (void*, const void*);
00236
00237 #else
00238 #define BEFORE_MALLOC_CALL
00239 #define AFTER_MALLOC_CALL
00240 #endif
00241
00242 #if CMK_MEMORY_BUILD_VERBOSE
00243 #include "memory-verbose.c"
00244 #endif
00245
00246 #if CMK_MEMORY_BUILD_PARANOID
00247 #include "memory-paranoid.c"
00248 #endif
00249
00250 #if CMK_MEMORY_BUILD_LEAK
00251 #include "memory-leak.c"
00252 #endif
00253
00254 #if CMK_MEMORY_BUILD_CACHE
00255 #include "memory-cache.c"
00256 #endif
00257
00258 #if CMK_MEMORY_BUILD_ISOMALLOC
00259 #include "memory-isomalloc.c"
00260 #endif
00261
00262 #if CMK_MEMORY_BUILD_LOCK
00263 #include "memory-lock.c"
00264 #endif
00265
00266 #if CMK_MEMORY_BUILD_CHARMDEBUG
00267 #include "memory-charmdebug.c"
00268 #endif
00269
00270 #if CMK_MEMORY_BUILD_GNU_HOOKS
00271
00272 static void
00273 my_init_hook (void)
00274 {
00275 old_malloc_hook = __malloc_hook;
00276 old_realloc_hook = __realloc_hook;
00277 old_memalign_hook = __memalign_hook;
00278 old_free_hook = __free_hook;
00279 __malloc_hook = meta_malloc_hook;
00280 __realloc_hook = meta_realloc_hook;
00281 __memalign_hook = meta_memalign_hook;
00282 __free_hook = meta_free_hook;
00283 }
00284
00285 void (*__malloc_initialize_hook) (void) = my_init_hook;
00286 #else
00287 void *malloc(size_t size) { return meta_malloc(size); }
00288 void free(void *ptr) { meta_free(ptr); }
00289 void *calloc(size_t nelem, size_t size) { return meta_calloc(nelem,size); }
00290 void cfree(void *ptr) { meta_cfree(ptr); }
00291 void *realloc(void *ptr, size_t size) { return meta_realloc(ptr,size); }
00292 void *memalign(size_t align, size_t size) { return meta_memalign(align,size); }
00293 void *valloc(size_t size) { return meta_valloc(size); }
00294 #endif
00295
00296 #endif
00297
00298 void CmiMemoryInit(argv)
00299 char **argv;
00300 {
00301 CmiMemoryIs_flag |= CMI_MEMORY_IS_OS;
00302 #if CMK_MEMORY_BUILD_OS_WRAPPED || CMK_MEMORY_BUILD_GNU_HOOKS
00303 CmiArgGroup("Converse","Memory module");
00304 meta_init(argv);
00305 #endif
00306 CmiOutOfMemoryInit();
00307 }
00308 void *malloc_reentrant(size_t size) { return malloc(size); }
00309 void free_reentrant(void *mem) { free(mem); }
00310
00311
00312
00313
00314 #if ! CMK_HAS_SBRK
00315 int sbrk(int s) { return 0; }
00316 #endif
00317
00318 #if CMK_HAS_MSTATS
00319 #include <malloc/malloc.h>
00320 #if CMK_C_INLINE
00321 inline
00322 #endif
00323 static CMK_TYPEDEF_UINT8 MemusageMstats(){
00324 struct mstats ms = mstats();
00325 CMK_TYPEDEF_UINT8 memtotal = ms.bytes_used;
00326 return memtotal;
00327 }
00328 #else
00329 #if CMK_C_INLINE
00330 inline
00331 #endif
00332 static CMK_TYPEDEF_UINT8 MemusageMstats() { return 0; }
00333 #endif
00334
00335 static int MemusageInited = 0;
00336 static CMK_TYPEDEF_UINT8 MemusageInitSbrkval = 0;
00337 #if CMK_C_INLINE
00338 inline
00339 #endif
00340 static CMK_TYPEDEF_UINT8 MemusageSbrk(){
00341 CMK_TYPEDEF_UINT8 newval;
00342 if(MemusageInited==0){
00343 MemusageInitSbrkval = (CMK_TYPEDEF_UINT8)sbrk(0);
00344 MemusageInited = 1;
00345 }
00346 newval = (CMK_TYPEDEF_UINT8)sbrk(0);
00347 return (newval - MemusageInitSbrkval);
00348 }
00349
00350 #if CMK_C_INLINE
00351 inline
00352 #endif
00353 static CMK_TYPEDEF_UINT8 MemusageProcSelfStat(){
00354 FILE *f;
00355 int i, ret;
00356 static int failed_once = 0;
00357 CMK_TYPEDEF_UINT8 vsz = 0;
00358
00359 if(failed_once) return 0;
00360
00361 f = fopen("/proc/self/stat", "r");
00362 if(!f) { failed_once = 1; return 0; }
00363 for(i=0; i<22; i++) ret = fscanf(f, "%*s");
00364 ret = fscanf(f, "%lu", &vsz);
00365 fclose(f);
00366 if(!vsz) failed_once=1;
00367 return vsz;
00368 }
00369
00370 #if ! CMK_HAS_MALLINFO || defined(CMK_MALLINFO_IS_BROKEN)
00371 #if CMK_C_INLINE
00372 inline
00373 #endif
00374 static CMK_TYPEDEF_UINT8 MemusageMallinfo(){ return 0;}
00375 #else
00376 #if CMK_HAS_MALLOC_H
00377 #include <malloc.h>
00378 #endif
00379 #if CMK_C_INLINE
00380 inline
00381 #endif
00382 static CMK_TYPEDEF_UINT8 MemusageMallinfo(){
00383
00384 struct mallinfo mi = mallinfo();
00385 CMK_TYPEDEF_UINT8 memtotal = (CMK_TYPEDEF_UINT8) mi.uordblks;
00386 CMK_TYPEDEF_UINT8 memtotal2 = (CMK_TYPEDEF_UINT8) mi.usmblks;
00387 memtotal2 += (CMK_TYPEDEF_UINT8) mi.hblkhd;
00388
00389 #if ! CMK_CRAYXT && ! CMK_CRAYXE
00390 if(memtotal2 > memtotal) memtotal = memtotal2;
00391 #endif
00392 return memtotal;
00393 }
00394 #endif
00395
00396 #if CMK_C_INLINE
00397 inline
00398 #endif
00399 static CMK_TYPEDEF_UINT8 MemusagePS(){
00400 #if ! CMK_HAS_POPEN
00401 return 0;
00402 #else
00403 char pscmd[100];
00404 CMK_TYPEDEF_UINT8 vsz=0;
00405 FILE *p;
00406 int ret;
00407 sprintf(pscmd, "/bin/ps -o vsz= -p %d", getpid());
00408 p = popen(pscmd, "r");
00409 if(p){
00410 ret = fscanf(p, "%ld", &vsz);
00411 pclose(p);
00412 }
00413 return (vsz * (CMK_TYPEDEF_UINT8)1024);
00414 #endif
00415 }
00416
00417 #if defined(_WIN32) && ! defined(__CYGWIN__)
00418 #include <windows.h>
00419 #include <psapi.h>
00420
00421 #if CMK_C_INLINE
00422 inline
00423 #endif
00424 static CMK_TYPEDEF_UINT8 MemusageWindows(){
00425 PROCESS_MEMORY_COUNTERS pmc;
00426 if ( GetProcessMemoryInfo( GetCurrentProcess(), &pmc, sizeof(pmc)) )
00427 {
00428
00429 return pmc.PagefileUsage;
00430 }
00431 return 0;
00432 }
00433 #else
00434 static CMK_TYPEDEF_UINT8 MemusageWindows(){
00435 return 0;
00436 }
00437 #endif
00438
00439 #if CMK_BLUEGENEP
00440
00441
00442
00443 #include <malloc.h>
00444 #if CMK_C_INLINE
00445 inline
00446 #endif
00447 static CMK_TYPEDEF_UINT8 MemusageBGP(){
00448 struct mallinfo m = mallinfo();
00449 return m.hblkhd + m.uordblks;
00450 }
00451 #endif
00452
00453 CMK_TYPEDEF_UINT8 CmiMemoryUsage(){
00454 #if CMK_BLUEGENEP
00455 return MemusageBGP();
00456 #else
00457 CMK_TYPEDEF_UINT8 memtotal = 0;
00458 #ifdef _WIN32
00459 if(!memtotal) memtotal = MemusageWindows();
00460 #endif
00461 if(!memtotal) memtotal = MemusageMstats();
00462 if(!memtotal) memtotal = MemusageMallinfo();
00463 if(!memtotal) memtotal = MemusageProcSelfStat();
00464 if(!memtotal) memtotal = MemusageSbrk();
00465 if(!memtotal) memtotal = MemusagePS();
00466 return memtotal;
00467 #endif
00468 }
00469
00470
00471
00472 CMK_TYPEDEF_UINT8 CmiMaxMemoryUsage() { return 0; }
00473 void CmiResetMaxMemory() {}
00474 CMK_TYPEDEF_UINT8 CmiMinMemoryUsage() { return 0; }
00475 void CmiResetMinMemory() {}
00476
00477 #define MEM_LOCK_AROUND(code) code
00478
00479 #else
00480
00481
00482
00483
00484
00485 #if CMK_MEMORY_BUILD_GNU || CMK_MEMORY_BUILD_GNUOLD
00486 #define meta_malloc mm_malloc
00487 #define meta_free mm_free
00488 #define meta_calloc mm_calloc
00489 #define meta_cfree mm_cfree
00490 #define meta_realloc mm_realloc
00491 #define meta_memalign mm_memalign
00492 #define meta_valloc mm_valloc
00493
00494 #if CMK_MEMORY_BUILD_GNU
00495 # include "memory-gnu.c"
00496 static void meta_init(char **argv) {
00497 CmiMemoryIs_flag |= CMI_MEMORY_IS_GNU;
00498 }
00499 #else
00500 # include "memory-gnuold.c"
00501 static void meta_init(char **argv) {
00502 CmiMemoryIs_flag |= CMI_MEMORY_IS_GNUOLD;
00503 }
00504 #endif
00505
00506 #endif
00507
00508 #define BEFORE_MALLOC_CALL
00509 #define AFTER_MALLOC_CALL
00510
00511 #if CMK_MEMORY_BUILD_VERBOSE
00512 #include "memory-verbose.c"
00513 #endif
00514
00515 #if CMK_MEMORY_BUILD_PARANOID
00516 #include "memory-paranoid.c"
00517 #endif
00518
00519 #if CMK_MEMORY_BUILD_LEAK
00520 #include "memory-leak.c"
00521 #endif
00522
00523 #if CMK_MEMORY_BUILD_CACHE
00524 #include "memory-cache.c"
00525 #endif
00526
00527 #if CMK_MEMORY_BUILD_ISOMALLOC
00528 #include "memory-isomalloc.c"
00529 #endif
00530
00531 #if CMK_MEMORY_BUILD_CHARMDEBUG
00532 #include "memory-charmdebug.c"
00533 #endif
00534
00535
00536 #if 0
00537
00538 #include "memory-gnu.c"
00539 static void meta_init(char **argv)
00540 {
00541
00542 }
00543 static void *meta_malloc(size_t size)
00544 {
00545 return mm_malloc(size);
00546 }
00547 static void meta_free(void *mem)
00548 {
00549 mm_free(mem);
00550 }
00551 static void *meta_calloc(size_t nelem, size_t size)
00552 {
00553 return mm_calloc(nelem,size);
00554 }
00555 static void meta_cfree(void *mem)
00556 {
00557 mm_cfree(m);
00558 }
00559 static void *meta_realloc(void *mem, size_t size)
00560 {
00561 return mm_realloc(mem,size);
00562 }
00563 static void *meta_memalign(size_t align, size_t size)
00564 {
00565 return mm_memalign(align,size);
00566 }
00567 static void *meta_valloc(size_t size)
00568 {
00569 return mm_valloc(size);
00570 }
00571 #endif
00572
00573
00574
00575
00576
00577
00578 void CmiMemoryInit(char **argv)
00579 {
00580 CmiArgGroup("Converse","Memory module");
00581 meta_init(argv);
00582 CmiOutOfMemoryInit();
00583 }
00584
00585
00586 #define MEM_LOCK_AROUND(code) \
00587 CmiMemLock(); \
00588 code; \
00589 CmiMemUnlock();
00590
00591
00592 #define REENTRANT_MEM_LOCK_AROUND(code) \
00593 int myRank=CmiMyRank(); \
00594 if (myRank!=rank_holding_CmiMemLock) { \
00595 CmiMemLock(); \
00596 rank_holding_CmiMemLock=myRank; \
00597 code; \
00598 rank_holding_CmiMemLock=-1; \
00599 CmiMemUnlock(); \
00600 } \
00601 else { \
00602 code; \
00603 }
00604
00605 void *malloc(size_t size)
00606 {
00607 void *result;
00608 MEM_LOCK_AROUND( result = meta_malloc(size); )
00609 if (result==NULL) CmiOutOfMemory(size);
00610 return result;
00611 }
00612
00613 void free(void *mem)
00614 {
00615 MEM_LOCK_AROUND( meta_free(mem); )
00616 }
00617
00618 void *calloc(size_t nelem, size_t size)
00619 {
00620 void *result;
00621 MEM_LOCK_AROUND( result = meta_calloc(nelem, size); )
00622 if (result==NULL) CmiOutOfMemory(size);
00623 return result;
00624 }
00625
00626 void cfree(void *mem)
00627 {
00628 MEM_LOCK_AROUND( meta_cfree(mem); )
00629 }
00630
00631 void *realloc(void *mem, size_t size)
00632 {
00633 void *result;
00634 MEM_LOCK_AROUND( result = meta_realloc(mem, size); )
00635 return result;
00636 }
00637
00638 void *memalign(size_t align, size_t size)
00639 {
00640 void *result;
00641 MEM_LOCK_AROUND( result = meta_memalign(align, size); )
00642 if (result==NULL) CmiOutOfMemory(align*size);
00643 return result;
00644 }
00645
00646 void *valloc(size_t size)
00647 {
00648 void *result;
00649 MEM_LOCK_AROUND( result = meta_valloc(size); )
00650 if (result==NULL) CmiOutOfMemory(size);
00651 return result;
00652 }
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662 void *malloc_reentrant(size_t size) {
00663 void *result;
00664 REENTRANT_MEM_LOCK_AROUND( result = meta_malloc(size); )
00665 return result;
00666 }
00667
00668 void free_reentrant(void *mem)
00669 {
00670 REENTRANT_MEM_LOCK_AROUND( meta_free(mem); )
00671 }
00672
00674 CMK_TYPEDEF_UINT8 CmiMemoryUsage()
00675 {
00676 return _memory_allocated;
00677 }
00678
00680 CMK_TYPEDEF_UINT8 CmiMaxMemoryUsage()
00681 {
00682 return _memory_allocated_max;
00683 }
00684
00686 void CmiResetMaxMemory() {
00687 _memory_allocated_max=_memory_allocated;
00688 }
00689
00690 CMK_TYPEDEF_UINT8 CmiMinMemoryUsage()
00691 {
00692 return _memory_allocated_min;
00693 }
00694
00695 void CmiResetMinMemory() {
00696 _memory_allocated_min=_memory_allocated;
00697 }
00698
00699 #endif
00700
00701 #ifndef CMK_MEMORY_HAS_NOMIGRATE
00702
00703 void *malloc_nomigrate(size_t size) { return malloc(size); }
00704 void free_nomigrate(void *mem) { free(mem); }
00705 #endif
00706
00707 #ifndef CMK_MEMORY_HAS_ISOMALLOC
00708 #include "memory-isomalloc.h"
00709
00710 CmiIsomallocBlockList *CmiIsomallocBlockListActivate(CmiIsomallocBlockList *l)
00711 {return l;}
00712 CmiIsomallocBlockList *CmiIsomallocBlockListCurrent(){
00713 return NULL;
00714 }
00715 void CmiEnableIsomalloc() {}
00716 void CmiDisableIsomalloc() {}
00717 #endif
00718
00719 #ifndef CMI_MEMORY_ROUTINES
00720 void CmiMemoryMark(void) {}
00721 void CmiMemoryMarkBlock(void *blk) {}
00722 void CmiMemorySweep(const char *where) {}
00723 void CmiMemoryCheck(void) {}
00724 #endif
00725
00726 void memory_preallocate_hack()
00727 {
00728 #if CMK_MEMORY_PREALLOCATE_HACK
00729
00730
00731
00732
00733
00734 #define MEMORY_PREALLOCATE_MAX 4096
00735 void *ptrs[MEMORY_PREALLOCATE_MAX];
00736 int i,len=0;
00737 for (i=0;i<MEMORY_PREALLOCATE_MAX;i++) {
00738 ptrs[i] = mm_malloc(1024*1024);
00739 if (ptrs[i]==NULL) break;
00740 else len=i+1;
00741 }
00742
00743
00744 for (i=len-2;i>=0;i--) {
00745 mm_free(ptrs[i]);
00746 }
00747 #endif
00748 }
00749
00750 void CmiOutOfMemoryInit(void) {
00751 if (CmiMyRank() == 0) {
00752 #if CMK_MEMORY_PREALLOCATE_HACK
00753 memory_preallocate_hack();
00754 #endif
00755 MEM_LOCK_AROUND( memory_lifeRaft=(char *)mm_malloc(65536/2); )
00756 }
00757 }
00758
00759 #ifndef CMK_MEMORY_BUILD_CHARMDEBUG
00760
00761 void CpdSetInitializeMemory(int v) { }
00762 size_t cpd_memory_length(void *lenParam) { return 0; }
00763 void cpd_memory_pup(void *itemParam,pup_er p,CpdListItemsRequest *req) { }
00764 void cpd_memory_leak(void *itemParam,pup_er p,CpdListItemsRequest *req) { }
00765 void check_memory_leaks(LeakSearchInfo* i) { }
00766 size_t cpd_memory_getLength(void *lenParam) { return 0; }
00767 void cpd_memory_get(void *itemParam,pup_er p,CpdListItemsRequest *req) { }
00768 void CpdMemoryMarkClean(char *msg) { }
00769
00770
00771 void setProtection(char *mem, char *ptr, int len, int flag) { }
00772
00773 #ifdef setMemoryTypeChare
00774 #undef setMemoryTypeChare
00775 #endif
00776 void setMemoryTypeChare(void *ptr) { }
00777 #ifdef setMemoryTypeMessage
00778 #undef setMemoryTypeMessage
00779 #endif
00780 void setMemoryTypeMessage(void *ptr) { }
00781 void CpdSystemEnter() { }
00782 void CpdSystemExit() { }
00783
00784 void CpdResetMemory() { }
00785 void CpdCheckMemory() { }
00786
00787 int get_memory_allocated_user_total() { return 0; }
00788 void * MemoryToSlot(void *ptr) { return NULL; }
00789 int Slot_ChareOwner(void *s) { return 0; }
00790 int Slot_AllocatedSize(void *s) { return 0; }
00791 int Slot_StackTrace(void *s, void ***stack) { return 0; }
00792 #ifdef setMemoryChareIDFromPtr
00793 #undef setMemoryChareIDFromPtr
00794 #endif
00795 int setMemoryChareIDFromPtr(void *ptr) { return 0; }
00796 #ifdef setMemoryChareID
00797 #undef setMemoryChareID
00798 #endif
00799 void setMemoryChareID(int id) { }
00800 #ifdef setMemoryOwnedBy
00801 #undef setMemoryOwnedBy
00802 #endif
00803 void setMemoryOwnedBy(void *ptr, int id) { }
00804
00805 #endif
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815 void* CmiMallocAligned(const size_t size, const unsigned int alignment) {
00816
00817 void* rtn = NULL;
00818 int tailPadding;
00819 unsigned short offset = 0;
00820
00821
00822 if (size <= 0 || alignment <= 0) return NULL;
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832 tailPadding = alignment - (size % alignment);
00833 if (tailPadding == alignment)
00834 tailPadding = 0;
00835
00836
00837 rtn = malloc(size + alignment + tailPadding);
00838
00839
00840 offset = (char)(((size_t)rtn) % alignment);
00841 offset = alignment - offset;
00842 if (offset == 0) offset = alignment;
00843
00844
00845 *((char*)rtn + offset - 1) = offset;
00846
00847
00848 return (void*)((char*)rtn + offset);
00849 }
00850
00851 void CmiFreeAligned(void* ptr) {
00852
00853 char offset;
00854
00855
00856 if (ptr == NULL) return;
00857
00858
00859 offset = *((char*)ptr - 1);
00860
00861
00862 free ((void*)((char*)ptr - offset));
00863 }
00864
00865
00866