void *calloc(size_t nmemb, size_t size) { if (!mm_initialized) { if (mm_initializing) return mm_fake_calloc(nmemb, size); mm_legacy_constructor(); } if (!__malloc_use_mmalloc) { return mm_real_calloc(nmemb, size); } xbt_mheap_t mdp = GET_HEAP(); if (!mdp) return NULL; LOCK(mdp); void *ret = mmalloc(mdp, nmemb*size); UNLOCK(mdp); // This was already done in the callee: if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) { memset(ret, 0, nmemb * size); } return ret; }
void set_string(vm_state *vm, wchar_t *source, value *val){ int len=wcslen(source); wchar_t *dest = (wchar_t *)pbstg_alc(vm, (len+1)*2, GET_HEAP(vm)); wcscpy(dest, source); if (!(val->flags&IS_NULL)) ot_free_val_ptr(vm, val); val->value=(DWORD)dest; val->flags=0x0d00; val->type=pbvalue_string; }
void free(void *p) { if (!mm_initialized) { if (mm_initializing) return mm_fake_free(p); mm_legacy_constructor(); } if (!__malloc_use_mmalloc) { mm_real_free(p); return; } if (!p) return; xbt_mheap_t mdp = GET_HEAP(); LOCK(mdp); mfree(mdp, p); UNLOCK(mdp); }
void *realloc(void *p, size_t s) { if (!mm_initialized) { if (mm_initializing) return mm_fake_realloc(p, s); mm_legacy_constructor(); } if (!__malloc_use_mmalloc) { return mm_real_realloc(p, s); } xbt_mheap_t mdp = GET_HEAP(); if (!mdp) return NULL; LOCK(mdp); void* ret = mrealloc(mdp, p, s); UNLOCK(mdp); return ret; }
void* malloc_no_memset(size_t n) { if (!mm_initialized) { if (mm_initializing) return mm_fake_malloc(n); mm_legacy_constructor(); } if (!__malloc_use_mmalloc) { return mm_real_malloc(n); } xbt_mheap_t mdp = GET_HEAP(); if (!mdp) return NULL; LOCK(mdp); void *ret = mmalloc_no_memset(mdp, n); UNLOCK(mdp); return ret; }
void F77_NAME(aces_malloc,ACES_MALLOC) (f_int * fiInts, f_int * lCore, f_int * l0) { const int iIntLn = sizeof(f_int); #ifdef _PTRS_ARE_WORDS const int iPtrInt = 1; #else const int iPtrInt = iIntLn; #endif size_t Bytes; void * pHeap; if (*fiInts <= 0) { /* define lCore itself (leave *fiInts) */ *lCore = 0; *l0 = 1; return; } Bytes = (size_t)(*fiInts*iIntLn + CACHELINE); if (Bytes <= 0) { printf("@ACES_MALLOC: size_t overflow, reduce memory request\n"); exit(1); } pHeap = GET_HEAP(Bytes); if (pHeap) { const int iTmp = CACHELINE*iPtrInt/iIntLn; size_t zPtr = (size_t)pHeap+(iTmp-1); zPtr /= iTmp; /* the number of lines beneath the heap's address */ zPtr *= iTmp; /* the address of the first whole line in the heap */ *lCore = (f_int)pHeap; /* assign the heap address for freeing */ *l0 = 1+((f_int *)zPtr-lCore); /* Fortran indexing */ { size_t zMemSize = Bytes/iIntLn; zMemSize -= (zPtr-(size_t)pHeap+iPtrInt-1)/iPtrInt; #ifdef _VERBOSE printf("@ACES_MALLOC: allocated >= %li MB of core memory\n", (zMemSize*iIntLn)>>20); #endif *fiInts = zMemSize; /* the number of f_ints at lCore(l0) */ } /* printf("@ACES_MALLOC: %li bytes at %p\n",Bytes,pHeap); */ /* make sure an f_int can index the first and last integer in the heap */ { size_t first = 1+(zPtr-(size_t)lCore)/iPtrInt; size_t last = first+(size_t)*fiInts-1; f_int fi0 = first; f_int fi1 = last; if ((first != (size_t)fi0) || (last != (size_t)fi1)) { printf("\n@ACES_MALLOC: " "The default integer cannot address the heap.\n" "lCore = %p (%li)\n" "&heap = %p (%li)\n\n", (void *)*lCore,(long)*lCore, pHeap,(long) pHeap ); free(pHeap); exit(1); } } if ((void *)*lCore != pHeap) { printf("\n@ACES_MALLOC: " "WARNING - the lCore address was overflowed.\n" " This memory cannot be freed.\n" "lCore = %p (%li)\n" "&heap = %p (%li)\n\n", (void *)*lCore,(long)*lCore, pHeap,(long) pHeap ); } } else {