void *chk_realloc(void *ptr, size_t size)
{
    struct hdr *hdr;

//  log_message("%s: %s\n", __FILE__, __FUNCTION__);

    if (!size) {
        chk_free(ptr);
        return NULL;
    }

    if (!ptr)
        return chk_malloc(size);

    hdr = meta(ptr);

    if (del(hdr) < 0) {
        intptr_t bt[MAX_BACKTRACE_DEPTH];
        int depth;
        depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
        if (hdr->tag == BACKLOG_TAG) {
            log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
                       user(hdr), size, hdr->size);
            log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            print_backtrace(hdr->bt, hdr->bt_depth);
            /* hdr->freed_bt_depth should be nonzero here */
            log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
                       user(hdr), hdr->size);
            print_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
            log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            print_backtrace(bt, depth);

             /* We take the memory out of the backlog and fall through so the
             * reallocation below succeeds.  Since we didn't really free it, we
             * can default to this behavior.
             */
            del_from_backlog(hdr);
        }
        else {
            log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
                       user(hdr), size);
            print_backtrace(bt, depth);
            // just get a whole new allocation and leak the old one
            return dlrealloc(0, size);
            // return dlrealloc(user(hdr), size); // assuming it was allocated externally
        }
    }

    hdr = dlrealloc(hdr, sizeof(struct hdr) + size + sizeof(struct ftr));
    if (hdr) {
        hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
        add(hdr, size);
        return user(hdr);
    }

    return NULL;
}
Beispiel #2
0
void * mbed_urealloc(void * ptr, size_t bytes, UAllocTraits_t traits)
{
    void * caller = (void*) caller_addr();
    void *newptr = NULL;
    if (ptr == NULL) {
        return mbed_ualloc(bytes, traits);
    }
    if(traits.flags & ~UALLOC_TRAITS_BITMASK) {
        // Traits not supported in urealloc yet
        ualloc_debug(UALLOC_DEBUG_WARNING, "ua c:%p fail\n", caller);
        return NULL;
    }
    uintptr_t ptr_tmp = (uintptr_t) ptr;
    if ((ptr_tmp < (uintptr_t) mbed_sbrk_ptr) &&
            (ptr_tmp >= (uintptr_t)&__mbed_sbrk_start)) {
        newptr = dlrealloc(ptr, bytes);
    } else {
        ualloc_debug(UALLOC_DEBUG_LOG, "uf c:%p m:%p non-heap realloc\n", caller, ptr);
    }

    if(newptr == NULL) {
        ualloc_debug(UALLOC_DEBUG_WARNING, "ur c:%p m0:%p fail\n", caller, ptr);
    } else {
        ualloc_debug(UALLOC_DEBUG_LOG, "ur c:%p m0:%p m1:%p\n", caller, ptr, newptr);
    }
    return newptr;
}
Beispiel #3
0
void* realloc(void* ptr, size_t size)
{
	if(IS_OUR_PTR(ptr)) {
		if(size == 0) {
			free(ptr);
			return NULL;
		}

		void* new_memory = malloc(size);
		if(new_memory == NULL) {
			return NULL;
		}

		const int old_size = get_block_from_chunk(ptr)->header.chunk_size;
		const size_t nbytes = size < old_size ? size : old_size;
		memcpy(new_memory, ptr, nbytes);
		free(ptr);
		return new_memory;
	}

	pthread_mutex_lock(&dlmalloc_mutex);
	void* result = dlrealloc(ptr, size);
	pthread_mutex_unlock(&dlmalloc_mutex);
	return result;
}
Beispiel #4
0
void free_memory_from_other_thread(void* ptr)
{
	pthread_mutex_lock(&free_chunks_mutex);
	
	if(nfree_chunks == capacity_free_chunks) {
		capacity_free_chunks *= 2;
		if(capacity_free_chunks < 16) {
			capacity_free_chunks = 16;
		}

		pthread_mutex_lock(&dlmalloc_mutex);
		void** new_free_chunks = (void**)dlrealloc(free_chunks, sizeof(void*)*capacity_free_chunks);
		pthread_mutex_unlock(&dlmalloc_mutex);
		if(!new_free_chunks) {
			pthread_mutex_unlock(&free_chunks_mutex);
			fprintf(stderr, "DLREALLOC FAILED!\n");
			return;
		}

		free_chunks = new_free_chunks;
	}

	free_chunks[nfree_chunks++] = ptr;
	pthread_mutex_unlock(&free_chunks_mutex);
}
Beispiel #5
0
void *
shmem_realloc(void *ptr, size_t size)
{
    void *ret;

    SHMEM_ERR_CHECK_INITIALIZED();

    SHMEM_MUTEX_LOCK(shmem_internal_mutex_alloc);
    ret = dlrealloc(ptr, size);
    SHMEM_MUTEX_UNLOCK(shmem_internal_mutex_alloc);

    shmem_internal_barrier_all();

    return ret;
}
Beispiel #6
0
void
FC_SHPCLMOVE(void **addr, fortran_integer_t *length, fortran_integer_t *errcode, fortran_integer_t *want_abort)
{
    void *ret;

    SHMEM_ERR_CHECK_INITIALIZED();
    SHMEM_ERR_CHECK_SYMMETRIC_HEAP(*addr);

    if (*length <= 0) {
        if (0 == *want_abort) {
            fprintf(stderr, "[%03d] shpclmove failure (invalid length)\n",
                    shmem_internal_my_pe);
            RAISE_ERROR(-1);
        } else {
            *errcode = -1;
            return;
        }
    }

    shmem_internal_barrier_all();

    SHMEM_MUTEX_LOCK(shmem_internal_mutex_alloc);
    ret = dlrealloc(*addr, *length * 4); /* length is number of 32 bit words */
    SHMEM_MUTEX_UNLOCK(shmem_internal_mutex_alloc);

    if (*addr != NULL) {
        if (*addr == ret) {
            *errcode = 0;
        } else {
            *errcode = 1;
        }
        *addr = ret;
    } else {
        if (0 == *want_abort) {
            fprintf(stderr, "[%03d] shpclmove failure\n",
                    shmem_internal_my_pe);
            RAISE_ERROR(-2);
        } else {
            *errcode = -2;
            return;
        }
    }

    shmem_internal_barrier_all();
}
Beispiel #7
0
void *
shmem_realloc(void *ptr, size_t size)
{
    void *ret;

    SHMEM_ERR_CHECK_INITIALIZED();
    if (ptr != NULL) {
      SHMEM_ERR_CHECK_SYMMETRIC_HEAP(ptr);
    }

    shmem_internal_barrier_all();

    SHMEM_MUTEX_LOCK(shmem_internal_mutex_alloc);
    ret = dlrealloc(ptr, size);
    SHMEM_MUTEX_UNLOCK(shmem_internal_mutex_alloc);

    shmem_internal_barrier_all();

    return ret;
}
Beispiel #8
0
void* _realloc_r( struct _reent* r, void* ptr, size_t size )
{
  return dlrealloc( ptr, size );
}
extern "C" void* chk_realloc(void* ptr, size_t size) {
//  log_message("%s: %s\n", __FILE__, __FUNCTION__);

    if (!ptr) {
        return chk_malloc(size);
    }

#ifdef REALLOC_ZERO_BYTES_FREE
    if (!size) {
        chk_free(ptr);
        return NULL;
    }
#endif

    hdr_t* hdr = meta(ptr);

    if (del(hdr) < 0) {
        uintptr_t bt[MAX_BACKTRACE_DEPTH];
        int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
        if (hdr->tag == BACKLOG_TAG) {
            log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
                       user(hdr), size, hdr->size);
            log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            log_backtrace(hdr->bt, hdr->bt_depth);
            /* hdr->freed_bt_depth should be nonzero here */
            log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
                       user(hdr), hdr->size);
            log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
            log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            log_backtrace(bt, depth);

             /* We take the memory out of the backlog and fall through so the
             * reallocation below succeeds.  Since we didn't really free it, we
             * can default to this behavior.
             */
            del_from_backlog(hdr);
        } else {
            log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
                       user(hdr), size);
            log_backtrace(bt, depth);
            // just get a whole new allocation and leak the old one
            return dlrealloc(0, size);
            // return dlrealloc(user(hdr), size); // assuming it was allocated externally
        }
    }

    if (hdr->base != hdr) {
        // An allocation from memalign, so create another allocation and
        // copy the data out.
        void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
        if (newMem) {
            memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
            dlfree(hdr->base);
            hdr = static_cast<hdr_t*>(newMem);
        } else {
            dlfree(hdr->base);
            hdr = NULL;
        }
    } else {
        hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
    }
    if (hdr) {
        hdr->base = hdr;
        hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
        add(hdr, size);
        return user(hdr);
    }

    return NULL;
}
Beispiel #10
0
static void *lua_alloc_dl	(void *ud, void *ptr, size_t osize, size_t nsize) {
  (void)ud;
  (void)osize;
  if (nsize == 0)	{	dlfree			(ptr);	 return	NULL;  }
  else				return dlrealloc	(ptr, nsize);
}
Beispiel #11
0
/*!
  Changes the size of the memory block pointed to by \a ptr to \a size bytes.
  The contents will be unchanged to the minimum of the old and new sizes; newly
  allocated memory will be uninitialized.  If \a ptr is 0, the call is
  equivalent to malloc(size); if size is equal to zero, the  call is equivalent
  to free(ptr).  Unless ptr is 0, it must have been returned by an earlier call
  to malloc(),  calloc()  or  realloc().  If the area pointed to was moved, a
  free(ptr) is done.
 */
void *QMallocPool::realloc(void *ptr, size_t size)
{
    Q_ASSERT(d && "Cannot operate on a null malloc pool");
    QMallocPtr p(d);
    return dlrealloc(ptr, size);
}
Beispiel #12
0
void* realloc(void* oldMem, size_t bytes) {
    return dlrealloc(oldMem, bytes);
}