Thread: Easy mmap question

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    Easy mmap question

    So I have this write function that writes a byte. The fact that it is JNI is not important:

    Code:
    JNIEXPORT jint JNICALL Java_com_nextbus_signcontroller_mvc_TechnologicHAL_mmapWriteByte
      (JNIEnv *env, jclass obj, jint address, jbyte value)
    {
        /* Use mmap() to map memory, then write byte to address */
        off_t page;
        volatile unsigned char *start;
        int ret;
        int fd;
    
        fd = open("/dev/mem", O_RDWR);
        if (fd == -1) return -1;
        page = address & PAGE_MASK;
        start = (unsigned char*)
            mmap(0,getpagesize(),PROT_READ|PROT_WRITE,MAP_SHARED,fd,page);
        if (start == MAP_FAILED) {
            close(fd);
            return -1;
        }
        *(start + (ADDR_MASK & address)) = value;
        ret = munmap((void*)start,getpagesize());
        close(fd);
        return ret;
    }
    I want this to become a 32-bit write operation

    My question is this: is changing this to a 32-bit write operation as easy as changing the 'value' parameter from a jbyte to a jint? How would this work if it were not JNI?
    Thanks for any info!
    Alex

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> is changing this to a 32-bit write operation as easy as changing the 'value' parameter from a jbyte to a jint?
    Yes, assuming a "jint" is a 32bit type. However, you must also ensure that the write address is properly aligned for a true 32bit write operation. Otherwise, just call memcpy().

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM