void *mosquitto__realloc(void *ptr, size_t size) { #ifdef REAL_WITH_MEMORY_TRACKING if(mem_limit && memcount + size > mem_limit){ return NULL; } #endif void *mem; #ifdef REAL_WITH_MEMORY_TRACKING if(ptr){ memcount -= malloc_usable_size(ptr); } #endif mem = realloc(ptr, size); #ifdef REAL_WITH_MEMORY_TRACKING if(mem){ memcount += malloc_usable_size(mem); if(memcount > max_memcount){ max_memcount = memcount; } } #endif return mem; }
size_t VMemoryManager_CRT::MemSize(void* ptr) { // Not all memory size functions cope with NULL pointer if(ptr == NULL) return 0; #if defined(WIN32) || defined (_VISION_XENON) #if defined(VBASE_USE_CRT_DEBUG) return _msize_dbg(ptr, _NORMAL_BLOCK); #else return _msize(ptr); #endif #elif defined (_VISION_PS3) return malloc_usable_size(ptr); #elif defined (_VISION_POSIX) return 0; // TODO: IOS #elif defined (_VISION_PSP2) return malloc_usable_size(ptr); #elif defined (_VISION_WIIU) return 0; //XXCK can fake if you really need it #else #error Undefined platform! #endif }
void *my_realloc(void *r, const int size) { void *ret; int oldsiz; #ifdef DEBUG if (!r) return my_malloc(size); if (size == 0) { my_free(r); return 0; } register_atexit(); if (!RSTree_remove(allocs_tree, (int)r)) allocs_fatal("realloc on non-allocated memory"); #endif /* DEBUG */ if (size < 0) allocs_fatal("my_realloc size is < 0"); oldsiz = malloc_usable_size(r); ret = original_realloc(r, size); if (size != 0 && !ret) allocs_fatal("realloc failed"); if (size > oldsiz) bzero(ret + oldsiz, size - oldsiz); #ifdef DEBUG RSTree_put_val(allocs_tree, (int)ret, malloc_usable_size(ret)); memop++; #endif /* DEBUG */ return ret; }
void *tcc_realloc(void *ptr, unsigned long size) { void *newptr; #ifdef MEM_DEBUG mem_cur_size -= malloc_usable_size(ptr); #endif newptr = realloc(ptr, size); #ifdef MEM_DEBUG // NOTE: count not correct if alloc error, but not critical mem_cur_size += malloc_usable_size(newptr); if (mem_cur_size > mem_max_size) cmem_max_size = mem_cur_size; #endif return newptr; }
int main(int argc, char **argv) { assert(argc == 2); const char *action = argv[1]; fprintf(stderr, "%s:\n", action); #if __LP64__ || defined(_WIN64) static const size_t kMaxAllowedMallocSize = 1ULL << 40; static const size_t kChunkHeaderSize = 16; #else static const size_t kMaxAllowedMallocSize = 2UL << 30; static const size_t kChunkHeaderSize = 8; #endif if (!strcmp(action, "malloc")) { void *p = malloc(kMaxAllowedMallocSize); assert(!p); p = malloc(kMaxAllowedMallocSize - kChunkHeaderSize); assert(!p); } else if (!strcmp(action, "calloc")) { // Trigger an overflow in calloc. size_t size = std::numeric_limits<size_t>::max(); void *p = calloc((size / 0x1000) + 1, 0x1000); assert(!p); } else if (!strcmp(action, "new")) { void *p = operator new(kMaxAllowedMallocSize); assert(!p); } else if (!strcmp(action, "new-nothrow")) { void *p = operator new(kMaxAllowedMallocSize, std::nothrow); assert(!p); } else if (!strcmp(action, "usable")) { // Playing with the actual usable size of a chunk. void *p = malloc(1007); assert(p); size_t size = malloc_usable_size(p); assert(size >= 1007); memset(p, 'A', size); p = realloc(p, 2014); assert(p); size = malloc_usable_size(p); assert(size >= 2014); memset(p, 'B', size); free(p); } else { assert(0); } return 0; }
void _mosquitto_free(void *mem) { #ifdef REAL_WITH_MEMORY_TRACKING memcount -= malloc_usable_size(mem); #endif free(mem); }
enum nss_status _nss_nis_setnetgrent (const char *group, struct __netgrent *netgrp) { char *domain; int len; enum nss_status status; status = NSS_STATUS_SUCCESS; if (group == NULL || group[0] == '\0') return NSS_STATUS_UNAVAIL; if (yp_get_default_domain (&domain)) return NSS_STATUS_UNAVAIL; internal_nis_endnetgrent (netgrp); status = yperr2nss (yp_match (domain, "netgroup", group, strlen (group), &netgrp->data, &len)); if (status == NSS_STATUS_SUCCESS) { /* Our implementation of yp_match already allocates a buffer which is one byte larger than the value in LEN specifies and the last byte is filled with NUL. So we can simply use that buffer. */ assert (len > 0); assert (malloc_usable_size (netgrp->data) >= len + 1); assert (netgrp->data[len] == '\0'); netgrp->data_size = len; netgrp->cursor = netgrp->data; } return status; }
void * malloc(size_t size) { void * ptr = NULL; size_t memsize = 0; /* amount of memory actually allocated */ /* This is the first time malloc is being called If so, we need to initialize */ if(real_malloc == NULL) memwatch_init(); /* check if we are beyond the limit. If so, call malloc_fail_hook() */ if(((allocated_mem + size) > maxsize) && (malloc_fail_hook != NULL)) malloc_fail_hook(allocated_mem, size, maxsize); /* actually allocate */ ptr = real_malloc(size); memsize = malloc_usable_size(ptr); /* Add to allocated memory */ allocated_mem += memsize; /* Some debugging */ /*printf("Allocating %ld bytes (Actual: %ld)\n", size, memsize); */ /*printf("Allocated memory now %ld\n", allocated_mem);*/ return ptr; }
void *tanger_stm_realloc(void *ptr, size_t size) { /* TODO to ITM_imize */ void *p; #ifdef EXPLICIT_TX_PARAMETER struct stm_tx * tx = stm_current_tx(); #endif /* EXPLICIT_TX_PARAMETER */ if (ptr == NULL) { /* Equivalent to malloc */ return tanger_stm_malloc(size); } if (size == 0) { /* Equivalent to free */ tanger_stm_free(ptr); return NULL; } /* Allocate new region */ p = tanger_stm_malloc(size); /* Copy old content to new region */ stm_load_bytes(TX_PARAM ptr, p, malloc_usable_size(ptr)); /* Free old region */ tanger_stm_free(ptr); return p; }
static void mp_list_remove_entry (MpNode *node) { if (node->next) { MpNode *link=(MpNode *)(node->next); //if (link->cookie == MP_COOKIE) link->prev = node->prev; //else // fprintf(stderr,"broken next link\n"); } if (node->prev) { MpNode *link=(MpNode *)(node->prev); //if (link->cookie == MP_COOKIE) link->next = node->next; //else // fprintf(stderr,"broken prev link\n"); } else list = node->next; // update sumary asize -= node->size; usize -= malloc_usable_size (node); }
void * malloc(size_t size) { size_t usable; void *ptr, *p; while(real_malloc == NULL){ if(!initializing){ initializing = 1; __init(); initializing = 0; } sched_yield(); } ptr = real_malloc(size + SIZEOF_F_RZ + SIZEOF_SIZE); usable = malloc_usable_size(ptr); p = ptr + size; /* end of user region */ memset(p, MAGIC_BYTE, SIZEOF_RZ(usable, size)); p += SIZEOF_RZ(usable,size); /* end of redzone */ *(size_t *)p = size; OFC_DUMP(ptr, usable, size); return ptr; }
size_t malloc_size(const void* ptr) { /* Docs say that usinng usable_size is considered to be a bad practice. I don't really care, it does the job. */ return malloc_usable_size((void*)ptr); }
int posix_memalign(void **memptr, size_t alignment, size_t size) { size_t usable; void *p; int ret; while(real_posix_memalign == NULL){ if(!initializing){ initializing = 1; __init(); initializing = 0; } sched_yield(); } ret = real_posix_memalign(memptr, alignment, size + SIZEOF_F_RZ + SIZEOF_SIZE); if (ret != 0) return ret; usable = malloc_usable_size(*memptr); p = *memptr + size; /* end of user region */ memset(p, MAGIC_BYTE, SIZEOF_RZ(usable, size)); p += SIZEOF_RZ(usable,size); /* end of redzone */ *(size_t *)p = size; OFC_DUMP(*memptr, usable, size); return 0; }
void * calloc(size_t nmemb, size_t size) { size_t newNmemb,newSize,usable; void *ptr, *p; while(real_calloc == NULL){ if(!initializing){ initializing = 1; __init(); initializing = 0; } sched_yield(); } if ((size * nmemb) == 0){ /* corner cases */ /* When size=0 or nmemb=0, */ /* malloc() maybe return minimum block, */ newSize = SIZEOF_F_RZ + SIZEOF_SIZE; newNmemb = 1; }else{ newSize = size; newNmemb = nmemb + ((SIZEOF_F_RZ + SIZEOF_SIZE - 1) / size + 1); } ptr = real_calloc(newNmemb ,newSize); usable = malloc_usable_size(ptr); p = ((char*)ptr + (size * nmemb)); /* end of user region */ memset(p, MAGIC_BYTE, SIZEOF_RZ(usable, size * nmemb)); p += SIZEOF_RZ(usable,size * nmemb); /* end of redzone */ *(size_t *)p = size * nmemb; OFC_DUMP(ptr, usable, size * nmemb); return ptr; }
void * realloc(void *ptr, size_t size) { size_t usable; void *p; while (real_realloc == NULL){ if (!initializing){ initializing = 1; __init(); initializing = 0; } sched_yield(); } if (size == 0){ /* size=0,ptr is not NULL => free(ptr); */ free(ptr); return NULL; } ptr = real_realloc(ptr, size + SIZEOF_F_RZ + SIZEOF_SIZE); usable = malloc_usable_size(ptr); /* corner cases */ /* ptr is NULL => malloc(size); */ p = ptr + size; /* end of user region */ memset(p, MAGIC_BYTE, SIZEOF_RZ(usable, size)); p += SIZEOF_RZ(usable,size); /* end of redzone */ *(size_t *)p = size; OFC_DUMP(ptr, usable, size); return ptr; }
int main(void) { int mallocSize = 321; printf("Allocating %d bytes with malloc()\n", mallocSize); char *m = malloc(mallocSize); // sizeof() does not work with malloc as you might expect (see // sizeof.c). To get the amount of space that we have malloc()'d, // try using malloc_usable_size() on Linux. // The man page says: /* The value returned by malloc_usable_size() may be greater than the requested size of the allocation because of alignment and minimum size constraints. Although the excess bytes can be overwritten by the application without ill effects, this is not good programming practice: the number of excess bytes in an allocation depends on the underlying implementation. The main use of this function is for debugging and introspection. */ size_t usableSize = malloc_usable_size(m); printf("Usable size: %zu\n", usableSize); }
int main() { mie_init(); printf("malloc=%p, free=%p\n", malloc, free); puts("main"); test_realloc(); char *p = malloc(7); #ifdef USE_DLOPEN puts("dlopen"); void *h = dlopen(soName, RTLD_LAZY); if (h == NULL) { perror("dlopen"); return 1; } puts("dlsym"); void (*f)() = dlsym(h, "sub_free"); if (f == NULL) { perror("dlsym"); return 1; } puts("call"); f(p); puts("dlclose"); dlclose(h); #else sub_free(p); #endif { char *q = malloc(3); printf("malloc size=%zd\n", malloc_usable_size(q)); q[3] = 'a'; free(q); } return 0; }
size_t __cdecl iso_aligned_msize(void *ptr, size_t alignment, size_t offset) { size_t header_size; /* Size of the header block */ size_t footer_size; /* Size of the footer block */ size_t total_size; /* total size of the allocated block */ size_t user_size; /* size of the user block*/ uintptr_t uintptr_offset; /* keep the alignment of the data block */ /* after the sizeof(void*) aligned pointer */ /* to the beginning of the allocated block */ /* HEADER SIZE + FOOTER SIZE = uintptr_offset + ALIGNMENT + SIZE OF A POINTER*/ /* HEADER SIZE + USER SIZE + FOOTER SIZE = TOTAL SIZE */ _ASSERT(ptr != NULL); ALIGN_BLOCK_HEADER *pBlockHdr = NULL; /* points to the beginning of the allocated block*/ pBlockHdr = (ALIGN_BLOCK_HEADER *)((uintptr_t)ptr & ~(sizeof(uintptr_t) - 1)) - 1; #ifdef __linux__ total_size = malloc_usable_size(pBlockHdr->pvAlloc); #else total_size = _msize(pBlockHdr->pvAlloc); #endif header_size = (uintptr_t)ptr - (uintptr_t)(pBlockHdr->pvAlloc); uintptr_offset = (0 - offset) & (sizeof(uintptr_t) - 1); /* The align cannot be smaller than the sizeof(uintptr_t) */ alignment = (alignment > sizeof(uintptr_t) ? alignment : sizeof(uintptr_t)) -1; footer_size = alignment + sizeof(ALIGN_BLOCK_HEADER) + uintptr_offset - header_size; user_size = total_size - header_size - footer_size; return user_size; }
int main(void) { # if !defined(VGO_aix5) && !defined(VGO_darwin) // Because our allocations are in multiples of 8 or 16, 99 will round up // to 104 or 112. int* x = malloc(99); // XXX: would be better to have a HAVE_MALLOC_USABLE_SIZE variable here assert(104 == malloc_usable_size(x) || 112 == malloc_usable_size(x)); assert( 0 == malloc_usable_size(NULL)); assert( 0 == malloc_usable_size((void*)0xdeadbeef)); # endif return 0; }
static void* _memory_reallocate_malloc( void* p, uint64_t size, unsigned int align ) { align = _memory_get_align( align ); #if FOUNDATION_PLATFORM_WINDOWS return _aligned_realloc( p, (size_t)size, align ); #else if( align ) { //No realloc aligned available void* memory = aligned_alloc( align, (size_t)size ); if( !memory ) { log_panicf( ERROR_OUT_OF_MEMORY, "Unable to reallocate memory: %s", system_error_message( 0 ) ); return 0; } if( p ) { size_t prev_size = malloc_usable_size( p ); memcpy( memory, p, ( size < prev_size ) ? size : prev_size ); } return memory; } return realloc( p, (size_t)size ); #endif }
unsigned int nvmed_check_buffer(void* nvmed_buf) { size_t buf_size; unsigned int predict_size, actual_size=0; unsigned int *magic; if(nvmed_buf == NULL) return 0; buf_size = malloc_usable_size(nvmed_buf); if(buf_size < PAGE_SIZE) return 0; predict_size = buf_size / PAGE_SIZE; if(predict_size >= 512) predict_size -= predict_size >> 9; magic = nvmed_buf + (PAGE_SIZE * predict_size); if(*magic == NVMED_BUF_MAGIC) { actual_size = *(++magic); } else { predict_size--; magic = nvmed_buf + (PAGE_SIZE * predict_size); if(*magic == NVMED_BUF_MAGIC) { actual_size = *(++magic); } } return actual_size; }
static void * do_test(void *arg) { int **bufs = malloc(NBUFS * sizeof (void *)); ASSERTne(bufs, NULL); size_t *sizes = malloc(NBUFS * sizeof (size_t)); ASSERTne(sizes, NULL); for (int j = 0; j < NBUFS; j++) { sizes[j] = sizeof (int) + 64 * (rand() % 100); bufs[j] = malloc(sizes[j]); ASSERTne(bufs[j], NULL); } for (int j = 0; j < NBUFS; j++) { ASSERT(malloc_usable_size(bufs[j]) >= sizes[j]); free(bufs[j]); } free(sizes); free(bufs); return NULL; }
extern "C" void* realloc(void* in, size_t size) { size_t oldsize = (D && in) ? malloc_usable_size(in) : 0; REF; void* out = __libc_realloc(in,size); DEREF; if (D) { D->now_usable.fetchAndAddOrdered(malloc_usable_size(out) - oldsize); /* Overhead is affected only if old size was 0 */ if (!oldsize) D->now_overhead.fetchAndAddOrdered(CHUNK_OVERHEAD); D->updatePeak(); } return out; }
void* SparseHeap::resizeBig(void* ptr, size_t new_size, MemoryUsageStats& stats) { auto old = static_cast<HeapObject*>(ptr); auto old_cap = m_bigs.get(old); #ifdef USE_JEMALLOC auto const newNode = static_cast<HeapObject*>( rallocx(ptr, new_size, 0) ); auto new_cap = sallocx(newNode, 0); #else auto const newNode = static_cast<HeapObject*>( safe_realloc(ptr, new_size) ); auto new_cap = malloc_usable_size(newNode); if (new_cap % kSmallSizeAlign != 0) { // adjust to satisfy RadixMap (see justification in allocBig()) new_cap += kSmallSizeAlign - new_cap % kSmallSizeAlign; } #endif if (newNode != old || new_cap != old_cap) { m_bigs.erase(old); m_bigs.insert(newNode, new_cap); } stats.mm_udebt -= new_cap - old_cap; stats.malloc_cap += new_cap - old_cap; return newNode; }
tdata_t tif_ReadRGBData(TIFF* tif) { int* buffer = (int*)tif_Malloc(tif); char* raster = (char*)tif_Malloc(tif); printf("sizeof(raster) = %ld\n", malloc_usable_size(raster)); printf("sizeof(buffer) = %ld\n", malloc_usable_size(buffer)); int rgba; tsize_t result; uint16 c = tif_nChannels(tif); uint32 w = tif_Width(tif); uint32 h = tif_Height(tif); uint16 ic; uint32 iw, ih; if (tif == NULL) return NULL; if (buffer != NULL) { printf("Reading raster rgba: w = %d, h = %d, c = %d, tif = %p, buffer = %p, raster = %p\n", w, h, c, tif, buffer, raster); result = TIFFReadRGBAImage(tif, w, h, (uint32*)buffer, 0); printf("Result = %ld\n", result); if (result == 0) { printf("Read error on input rgba image.\n"); } printf("Read ok: result = %ld\n", result); } if (raster != NULL) { for(ih = 0; ih < h; ih++){ for(iw = 0; iw < w; iw++){ rgba = buffer[(h-ih-1)*w+iw]; ic = 0; raster[macro_RasterPos2dRgb] = TIFFGetR(rgba); ic = 1; if (c >= 2) raster[macro_RasterPos2dRgb] = TIFFGetG(rgba); ic = 2; if (c >= 3) raster[macro_RasterPos2dRgb] = TIFFGetB(rgba); ic = 3; if (c >= 4) raster[macro_RasterPos2dRgb] = TIFFGetA(rgba); } } } return (tdata_t)raster; }
static void deinit( void *ptr ) { if (ptr) { size_t asz = malloc_usable_size( ptr ); memset( ptr, FILL_BYTE_FREED_MEMORY, asz ); } }
int main(void) { // Since our allocations are in multiples of 8, 99 will round up to 104. int* x = malloc(99); # if !defined(_AIX) assert(104 == malloc_usable_size(x)); # endif return 0; }
inline static void* clean_prefix(char* ptr) { size_t size = malloc_usable_size(ptr); uint32_t *p = (uint32_t *)(ptr + size - sizeof(uint32_t)); uint32_t handle; memcpy(&handle, p, sizeof(handle)); update_xmalloc_stat_free(handle, size); return ptr; }
char *floattohexfloat(long double value, char *buffer) { /* Convert an float value in an hexadecimal string stored in the given pointer; ******************************************************************************/ if (buffer == NULL) { /** Given an NULL pointer as argument **/ buffer=calloc(128,sizeof(char)) ; } #ifdef HAVE_MALLOC_USABLE_SIZE if (malloc_usable_size(buffer) < 128) { if ( realloc(buffer,128) == NULL) { errno=EINVAL ; return NULL ; } } #endif long double int_part=0.0 ; long double float_part=0.0 ; _Bool is_negativ = false ; if (value < 0) { value=fabs(value) ; is_negativ=true ; } float_part=modfl(value,&int_part) ; /** modulo float */ /** variables for splitting in integer and float part */ char *int_part_str_hex=malloc(128) ; char *float_part_str_hex=malloc(128) ; memset(int_part_str_hex,'\0',128) ; memset(float_part_str_hex,'\0',128) ; /** Perform splitting in integer and float part */ inttohex((long long) int_part,int_part_str_hex) ; __inttohexfloatpart(float_part,float_part_str_hex,127) ; /** result binar string variable (pointer given as argument) */ memset(buffer,'\0',128) ; if ((is_negativ) && (int_part_str_hex[0] != '-')) { /** Case value to convert in binfloat string is negativ */ strcpy(buffer,"-") ; } /** Assemble final binar string */ strcat(buffer,int_part_str_hex) ; strcat(buffer,".") ; strcat(buffer,float_part_str_hex) ; return buffer ; }
void *_mosquitto_realloc(void *ptr, size_t size) { void *mem; #ifdef REAL_WITH_MEMORY_TRACKING if(ptr){ memcount -= malloc_usable_size(ptr); } #endif mem = realloc(ptr, size); #ifdef REAL_WITH_MEMORY_TRACKING memcount += malloc_usable_size(mem); if(memcount > max_memcount){ max_memcount = memcount; } #endif return mem; }