Example #1
0
File: m61.c Project: rogergzou/cs61
void* m61_calloc(size_t nmemb, size_t sz, const char* file, int line) {
    // Your code here (to fix test014).
    void* ptr = m61_malloc(nmemb * sz, file, line);
    if (ptr)
        memset(ptr, 0, nmemb * sz);
    return ptr;
}
Example #2
0
void *m61_calloc(size_t nmemb, size_t sz, const char *file, int line) {
    (void) file, (void) line;	// avoid uninitialized variable warnings
    if (sz>maximumSizeValid()/nmemb){
        ++fail_count;
	fail_size+=(unsigned long long)sz;
        return NULL;
    }
    void *ptr = m61_malloc(sz * nmemb, file, line);
    if (ptr != NULL)
	memset(ptr, 0, sz * nmemb);     // clear memory to 0
    return ptr;
}
Example #3
0
void* m61_calloc(size_t nmemb, size_t sz, const char* file, int line) {
    // Your code here (to fix test014).
    if ((size_t) - 1/nmemb < sz) {
        globe_stats.nfail++;
        globe_stats.fail_size += (unsigned long long) sz * (unsigned long long) nmemb;
        return NULL;
    }
    void *ptr = m61_malloc(nmemb * sz, file, line);
    
    if (ptr)
        memset(ptr, 0, nmemb * sz);
    
    return ptr;

}
Example #4
0
void *m61_realloc(void *ptr, size_t sz, const char *file, int line) {
    (void) file, (void) line;	// avoid uninitialized variable warnings
    void *new_ptr = NULL;
    if (sz != 0)
        new_ptr = m61_malloc(sz,file,line);
    if (ptr != NULL && new_ptr != NULL) {
            metadata *meta_ptr=getMetadata(ptr); 
            size_t old_sz = meta_ptr->sz;
            if (old_sz < sz)
             memcpy(new_ptr, ptr, old_sz);
        else
             memcpy(new_ptr, ptr, sz);
    }
    m61_free(ptr,file, line);
    return new_ptr;
}
Example #5
0
void* m61_realloc(void* ptr, size_t sz, const char* file, int line) {
    void *new_ptr = NULL;
    if (sz)
        new_ptr = m61_malloc(sz, file, line);
    if (ptr && new_ptr) {
        // Copy the data from `ptr` into `new_ptr`.
        // To do that, we must figure out the size of allocation `ptr`.
        // Your code here (to fix test012).
        
        size_t old_sz = ((struct header *) ptr - 1)->alloc_size;
        if (old_sz < sz) 
            memcpy(new_ptr,ptr,old_sz);
        else 
            memcpy(new_ptr,ptr,sz);
    }
    m61_free(ptr, file, line);
    return new_ptr;
}
Example #6
0
File: m61.c Project: rogergzou/cs61
void* m61_realloc(void* ptr, size_t sz, const char* file, int line) {
    //void* new_ptr = NULL;
    m61_metadata* new_ptr = NULL;
    if (sz)
        new_ptr = m61_malloc(sz, file, line);
    if (ptr && new_ptr) {
        // Copy the data from `ptr` into `new_ptr`.
        // To do that, we must figure out the size of allocation `ptr`.
        // Your code here (to fix test012).
        m61_metadata *itismeta = ptr;
        m61_metadata *meta = itismeta - sizeof(m61_metadata);
        m61_metadata new_meta = { (meta->size) };
        new_ptr -= sizeof(m61_metadata);
        memcpy(new_ptr,&new_meta,sizeof(m61_metadata));
        new_ptr += sizeof(m61_metadata);
    }
    m61_free(ptr, file, line);
    return new_ptr;
}