Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
Malloc Debugger       Share
2008-11-13 |  MalathiVanaraj  | Viewed: 264  |    0

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/user.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <signal.h>

void *mymalloc(size_t len)
{
    void *buf;
    size_t pages = (len & PAGE_MASK) + 2;
    size_t offset = PAGE_SIZE - (len & ~PAGE_MASK);
    if(offset < sizeof(size_t))
    {
        pages++;
        offset += PAGE_SIZE;
    }
   
    if((buf = mmap(NULL, pages << PAGE_SHIFT, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANONYMOUS, 0, 0)) == -1)
    {
        perror("mymalloc/mmap");
        exit(1);
    }
 
    *(size_t *)buf = len;
    *(size_t *)(buf+offset+len) = len;
  
    if(mprotect(buf+offset+len, PAGE_SIZE, PROT_NONE) == -1)
    {
        perror("mymalloc/mprotect");
        exit(1);
    }

    return buf+offset;
}


void myfree(void *buf)
{
    void *start = (long)buf & PAGE_MASK;
    size_t offset = (long)buf & ~PAGE_MASK;
    /* Will segfault here on double-free */
    size_t len = *(size_t *)start;

    /* Check to see if we had to tack on another page in mymalloc */
    if(buf - start < sizeof(size_t))
    {
        start -= PAGE_SIZE;
        len = *(size_t *)start;
    }

    /* Unprotect our memory */
    if(mprotect(buf+len, PAGE_SIZE, PROT_READ) == -1)
    {
        perror("myfree/mprotect");
        raise(SIGSEGV);
    }
               
    /* Check to make sure lengths are consistant. If not, advise user to try
       using the (not yet written) underflow version of this library */
    if(*(size_t *)(buf + len) != *(size_t *)start)
    {
        fprintf(stderr, "Error: heap corruption. Inconsistant allocation "
                "sizes. Try using the underflow option to pinpoint source "
                "of error\n"); 
        raise(SIGSEGV);
    }
  
    if(munmap(start, len+offset+PAGE_SIZE) == -1)
    {
        perror("myfree/munmap");
        raise(SIGSEGV);
    }
  
    if(mmap(start, len+offset+PAGE_SIZE, PROT_NONE,
                MAP_ANON | MAP_FIXED | MAP_PRIVATE, 0, 0) == -1)
    {
        perror("myfree/mremap");
    }
}
               


Latest topics
C - pointers to pointers  Viewed: 263
C - switch statement  Viewed: 264
C - binary tree  Viewed: 264
Malloc Debugger  Viewed: 264
Is a prime number or not  Viewed: 264
C - Do while  Viewed: 265
C - Global variables , static variables  Viewed: 265
C - binsearch  Viewed: 265
C Assign content to a function pointer  Viewed: 265
C - Structure example  Viewed: 266

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
C - Data Structure,Rev Linked list
C - Data Structure,linked list
C - time_t strftime example
C - time_t gmtime example
C - enum example
C - strtok example, Splitting string into tokens
C - libxml2 parsing example libxml
C - if else example
C - Structure example
C - pointers and arrays example strlen
C - Pointers to structures
C - gtk scrolled window example
C - Binary Search Tree example
C TCP socket example Server and Client program
C UDP socket example Server and Client program

Related References
c - basics of structure
c - functions and program structure, programmed pass-by-reference via pointers, pass-by-reference parameters
c - structures and functions
c - arrays of structures
c - pointers to structures
explain function pointers with examples?

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us