void * mbed_ualloc(size_t bytes, UAllocTraits_t traits) { void * ptr = NULL; void * caller = (void*) caller_addr(); if (UALLOC_TEST_TRAITS(traits.flags, UALLOC_TRAITS_NEVER_FREE)) { ptr = mbed_krbs(bytes); // krbs uses the same semantics as sbrk, so translate a -1 to NULL. if (ptr == (void*)-1) { ptr = NULL; } if ((ptr != NULL) && UALLOC_TEST_TRAITS(traits.flags, UALLOC_TRAITS_ZERO_FILL)) { memset(ptr, 0, bytes); } } else if (UALLOC_TEST_TRAITS(traits.flags, UALLOC_TRAITS_ZERO_FILL)) { ptr = dlcalloc(1, bytes); } else if (!(traits.flags & ~UALLOC_TRAITS_BITMASK)) { ptr = dlmalloc(bytes); } else if (traits.flags & UALLOC_RESERVED_MASK) { ualloc_debug(UALLOC_DEBUG_ERROR, "ua c:%p reserved: %lx\n", caller, traits.flags & UALLOC_RESERVED_MASK); } if(ptr == NULL) { ualloc_debug(UALLOC_DEBUG_WARNING, "ua c:%p fail\n", caller); } else { ualloc_debug(UALLOC_DEBUG_LOG, "ua c:%p m:%p\n", caller, ptr); } return ptr; }
void* calloc(size_t count, size_t size) { pthread_mutex_lock(&dlmalloc_mutex); void* result = dlcalloc(count, size); pthread_mutex_unlock(&dlmalloc_mutex); return result; }
extern "C" void *chk_calloc(int nmemb, size_t size) { // log_message("%s: %s\n", __FILE__, __FUNCTION__); size_t total_size = nmemb * size; hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t))); if (hdr) { hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); add(hdr, total_size); return user(hdr); } return NULL; }
void *chk_calloc(int nmemb, size_t size) { // log_message("%s: %s\n", __FILE__, __FUNCTION__); struct hdr *hdr; size_t total_size = nmemb * size; hdr = dlcalloc(1, sizeof(struct hdr) + total_size + sizeof(struct ftr)); if (hdr) { hdr->bt_depth = get_backtrace( hdr->bt, MAX_BACKTRACE_DEPTH); add(hdr, total_size); return user(hdr); } return NULL; }
void* _calloc_r( struct _reent* r, size_t nelem, size_t elem_size ) { return dlcalloc( nelem, elem_size ); }
/* This routine serves as entry point for 'calloc'. * This routine behaves similarly to qemu_instrumented_malloc. */ void* qemu_instrumented_calloc(size_t n_elements, size_t elem_size) { if (n_elements == 0 || elem_size == 0) { // Just let go zero bytes allocation. qemu_info_log("::: <libc_pid=%03u, pid=%03u>: Zero calloc redir to malloc", malloc_pid, getpid()); return qemu_instrumented_malloc(0); } /* Fail on overflow - just to be safe even though this code runs only * within the debugging C library, not the production one */ if (n_elements && MAX_SIZE_T / n_elements < elem_size) { return NULL; } MallocDesc desc; /* Calculating prefix size. The trick here is to make sure that * first element (returned to the caller) is properly aligned. */ if (DEFAULT_PREFIX_SIZE >= elem_size) { /* If default alignment is bigger than element size, we will * set our prefix size to the default alignment size. */ desc.prefix_size = DEFAULT_PREFIX_SIZE; /* For the suffix we will use whatever bytes remain from the prefix * allocation size, aligned to the size of an element, plus the usual * default suffix size. */ desc.suffix_size = (DEFAULT_PREFIX_SIZE % elem_size) + DEFAULT_SUFFIX_SIZE; } else { /* Make sure that prefix, and suffix sizes is at least elem_size, * and first element returned to the caller is properly aligned. */ desc.prefix_size = elem_size + DEFAULT_PREFIX_SIZE - 1; desc.prefix_size &= ~(malloc_alignment - 1); desc.suffix_size = DEFAULT_SUFFIX_SIZE; } desc.requested_bytes = n_elements * elem_size; size_t total_size = desc.requested_bytes + desc.prefix_size + desc.suffix_size; size_t total_elements = total_size / elem_size; total_size %= elem_size; if (total_size != 0) { // Add extra to the suffix area. total_elements++; desc.suffix_size += (elem_size - total_size); } desc.ptr = dlcalloc(total_elements, elem_size); if (desc.ptr == NULL) { error_log("<libc_pid=%03u, pid=%03u> calloc: dlcalloc(%u(%u), %u) (prx=%u, sfx=%u) failed.", malloc_pid, getpid(), n_elements, total_elements, elem_size, desc.prefix_size, desc.suffix_size); return NULL; } if (notify_qemu_malloc(&desc)) { log_mdesc(error, &desc, "<libc_pid=%03u, pid=%03u>: calloc(%u(%u), %u): notify_malloc failed for ", malloc_pid, getpid(), n_elements, total_elements, elem_size); dlfree(desc.ptr); return NULL; } else { #if TEST_ACCESS_VIOLATIONS test_access_violation(&desc); #endif // TEST_ACCESS_VIOLATIONS log_mdesc(info, &desc, "### <libc_pid=%03u, pid=%03u> calloc(%u(%u), %u) -> ", malloc_pid, getpid(), n_elements, total_elements, elem_size); return mallocdesc_user_ptr(&desc); } }
/*! Allocates memory for an array of \a nmemb elements of \a size each and returns a pointer to the allocated memory. The memory is set to zero. Returns 0 if the memory could not be allocated. */ void *QMallocPool::calloc(size_t nmemb, size_t size) { Q_ASSERT(d && "Cannot operate on a null malloc pool"); QMallocPtr p(d); return dlcalloc(nmemb, size); }
void* calloc(size_t n_elements, size_t elem_size) { return dlcalloc(n_elements, elem_size); }